Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Better Video (IOS-117) #1111

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Mastodon/Scene/Compose/ComposeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,16 @@ extension ComposeViewController {
// Look for images on the clipboard
if UIPasteboard.general.hasImages, let images = UIPasteboard.general.images {
logger.warning("Got image paste event, however attachments are not yet re-implemented.");

let mediaAttachmentSettings = viewModel.authContext.mastodonAuthenticationBox.authenticationRecord.object(in: viewModel.context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments
let attachmentViewModels = images.map { image in
return AttachmentViewModel(
api: viewModel.context.apiService,
authContext: viewModel.authContext,
input: .image(image),
sizeLimit: composeContentViewModel.sizeLimit,
delegate: composeContentViewModel
delegate: composeContentViewModel,
mediaAttachmentSettings: mediaAttachmentSettings
)
}
composeContentViewModel.attachmentViewModels += attachmentViewModels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import AVKit
import MastodonCore
import SessionExporter
import Nuke
import MastodonSDK

extension AttachmentViewModel {
func compressVideo(url: URL) async throws -> URL? {
func compressVideo(url: URL, mediaAttachmentSettings: Mastodon.Entity.Instance.Configuration.MediaAttachments? = nil) async throws -> URL? {
let urlAsset = AVURLAsset(url: url)

guard let track = urlAsset.tracks(withMediaType: .video).first else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable
public let authContext: AuthContext
public let input: Input
public let sizeLimit: SizeLimit
private let mediaAttachmentSettings: Mastodon.Entity.Instance.Configuration.MediaAttachments?
@Published var caption = ""
@Published public private(set) var isCaptionEditable = true

Expand Down Expand Up @@ -79,12 +80,14 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable
authContext: AuthContext,
input: Input,
sizeLimit: SizeLimit,
delegate: AttachmentViewModelDelegate
delegate: AttachmentViewModelDelegate,
mediaAttachmentSettings: Mastodon.Entity.Instance.Configuration.MediaAttachments?
) {
self.api = api
self.authContext = authContext
self.input = input
self.sizeLimit = sizeLimit
self.mediaAttachmentSettings = mediaAttachmentSettings
self.delegate = delegate
super.init()
// end init
Expand Down Expand Up @@ -158,7 +161,7 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable
case .video(let fileURL, let mimeType):
self.output = output
self.update(uploadState: .compressing)
guard let compressedFileURL = try await compressVideo(url: fileURL) else {
guard let compressedFileURL = try await compressVideo(url: fileURL, mediaAttachmentSettings: mediaAttachmentSettings) else {
assertionFailure("Unable to compress video")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Combine
import PhotosUI
import MastodonCore
import NaturalLanguage
import MastodonSDK

public final class ComposeContentViewController: UIViewController {

Expand Down Expand Up @@ -480,14 +481,16 @@ extension ComposeContentViewController: UITableViewDelegate { }
extension ComposeContentViewController: PHPickerViewControllerDelegate {
public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true, completion: nil)
let mediaAttachmentSettings = viewModel.authContext.mastodonAuthenticationBox.authenticationRecord.object(in: viewModel.context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments

let attachmentViewModels: [AttachmentViewModel] = results.map { result in
AttachmentViewModel(
api: viewModel.context.apiService,
authContext: viewModel.authContext,
input: .pickerResult(result),
sizeLimit: viewModel.sizeLimit,
delegate: viewModel
delegate: viewModel,
mediaAttachmentSettings: mediaAttachmentSettings
)
}
viewModel.attachmentViewModels += attachmentViewModels
Expand All @@ -501,12 +504,15 @@ extension ComposeContentViewController: UIImagePickerControllerDelegate & UINavi

guard let image = info[.originalImage] as? UIImage else { return }

let mediaAttachmentSettings = viewModel.authContext.mastodonAuthenticationBox.authenticationRecord.object(in: viewModel.context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments

let attachmentViewModel = AttachmentViewModel(
api: viewModel.context.apiService,
authContext: viewModel.authContext,
input: .image(image),
sizeLimit: viewModel.sizeLimit,
delegate: viewModel
delegate: viewModel,
mediaAttachmentSettings: mediaAttachmentSettings
)
viewModel.attachmentViewModels += [attachmentViewModel]
}
Expand All @@ -522,12 +528,15 @@ extension ComposeContentViewController: UIDocumentPickerDelegate {
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }

let mediaAttachmentSettings = viewModel.authContext.mastodonAuthenticationBox.authenticationRecord.object(in: viewModel.context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments

let attachmentViewModel = AttachmentViewModel(
api: viewModel.context.apiService,
authContext: viewModel.authContext,
input: .url(url),
sizeLimit: viewModel.sizeLimit,
delegate: viewModel
delegate: viewModel,
mediaAttachmentSettings: mediaAttachmentSettings
)
viewModel.attachmentViewModels += [attachmentViewModel]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,15 @@ public final class ComposeContentViewModel: NSObject, ObservableObject {
self.isVisibilityButtonEnabled = false
self.attachmentViewModels = status.attachments.compactMap {
guard let assetURL = $0.assetURL, let url = URL(string: assetURL) else { return nil }
let mediaAttachmentSettings = authContext.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments

let attachmentViewModel = AttachmentViewModel(
api: context.apiService,
authContext: authContext,
input: .mastodonAssetUrl(url, $0.id),
sizeLimit: sizeLimit,
delegate: self
delegate: self,
mediaAttachmentSettings: mediaAttachmentSettings
)
attachmentViewModel.caption = $0.altDescription ?? ""
return attachmentViewModel
Expand All @@ -312,11 +315,6 @@ public final class ComposeContentViewModel: NSObject, ObservableObject {

bind()
}

deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}

}

extension ComposeContentViewModel {
Expand Down
12 changes: 10 additions & 2 deletions ShareActionExtension/Scene/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,30 @@ extension ShareViewController {
}

if let movieProvider = _movieProvider {

let mediaAttachmentSettings = authContext.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments

let attachmentViewModel = AttachmentViewModel(
api: context.apiService,
authContext: authContext,
input: .itemProvider(movieProvider),
sizeLimit: .init(image: nil, video: nil),
delegate: composeContentViewModel
delegate: composeContentViewModel,
mediaAttachmentSettings: mediaAttachmentSettings
)
composeContentViewModel.attachmentViewModels.append(attachmentViewModel)
} else if !imageProviders.isEmpty {

let mediaAttachmentSettings = authContext.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments

let attachmentViewModels = imageProviders.map { provider in
AttachmentViewModel(
api: context.apiService,
authContext: authContext,
input: .itemProvider(provider),
sizeLimit: .init(image: nil, video: nil),
delegate: composeContentViewModel
delegate: composeContentViewModel,
mediaAttachmentSettings: mediaAttachmentSettings
)
}
composeContentViewModel.attachmentViewModels.append(contentsOf: attachmentViewModels)
Expand Down