diff --git a/Mastodon/Scene/Compose/ComposeViewController.swift b/Mastodon/Scene/Compose/ComposeViewController.swift index 91e79c645..dcb2df19c 100644 --- a/Mastodon/Scene/Compose/ComposeViewController.swift +++ b/Mastodon/Scene/Compose/ComposeViewController.swift @@ -516,16 +516,15 @@ extension ComposeViewController { case .publishPost: publishBarButtonItemPressed(publishBarButtonItem) case .mediaBrowse: - guard !isViewControllerIsAlreadyModal(composeContentViewController.documentPickerController) else { return } + guard composeContentViewController.documentPickerController.presentingViewController == nil else { return } present(composeContentViewController.documentPickerController, animated: true, completion: nil) case .mediaPhotoLibrary: - guard !isViewControllerIsAlreadyModal(composeContentViewController.photoLibraryPicker) else { return } - present(composeContentViewController.photoLibraryPicker, animated: true, completion: nil) + composeContentViewController.presentPhotoLibraryPicker() case .mediaCamera: guard UIImagePickerController.isSourceTypeAvailable(.camera) else { return } - guard !isViewControllerIsAlreadyModal(composeContentViewController.imagePickerController) else { return } + guard composeContentViewController.imagePickerController.presentingViewController == nil else { return } present(composeContentViewController.imagePickerController, animated: true, completion: nil) case .togglePoll: composeContentViewModel.isPollActive.toggle() @@ -542,8 +541,4 @@ extension ComposeViewController { } } - private func isViewControllerIsAlreadyModal(_ viewController: UIViewController) -> Bool { - return viewController.presentingViewController != nil - } - } diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift index 1c4ce444d..57d08d7c9 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift @@ -172,9 +172,6 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable } } // end Task self.uploadTask = uploadTask - Task { - await uploadTask.value - } } deinit { diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewController.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewController.swift index b3c912d33..c01d84d48 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewController.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewController.swift @@ -53,11 +53,7 @@ public final class ComposeContentViewController: UIViewController { return configuration } - public private(set) lazy var photoLibraryPicker: PHPickerViewController = { - let imagePicker = PHPickerViewController(configuration: ComposeContentViewController.createPhotoLibraryPickerConfiguration()) - imagePicker.delegate = self - return imagePicker - }() + public private(set) var photoLibraryPicker: PHPickerViewController? public private(set) lazy var imagePickerController: UIImagePickerController = { let imagePickerController = UIImagePickerController() @@ -137,6 +133,7 @@ extension ComposeContentViewController { viewModel.$isEmojiActive, viewModel.$autoCompleteInfo ) + .receive(on: DispatchQueue.main) .sink(receiveValue: { [weak self] keyboardEvents, isEmojiActive, autoCompleteInfo in guard let self = self else { return } @@ -270,15 +267,6 @@ extension ComposeContentViewController { // bind toolbar bindToolbarViewModel() - - // bind attachment picker - viewModel.$attachmentViewModels - .receive(on: DispatchQueue.main) - .sink { [weak self] _ in - guard let self = self else { return } - self.resetImagePicker() - } - .store(in: &disposeBag) } public override func viewDidLayoutSubviews() { @@ -333,10 +321,10 @@ extension ComposeContentViewController { // run on background thread since NLLanguageRecognizer seems to do CPU-bound work // that we don’t want on main .receive(on: DispatchQueue.global(qos: .utility)) - .sink { [unowned self] content in + .sink { [weak self] content in if content.isEmpty { DispatchQueue.main.async { - self.composeContentToolbarViewModel.suggestedLanguages = [] + self?.composeContentToolbarViewModel.suggestedLanguages = [] } return } @@ -345,15 +333,15 @@ extension ComposeContentViewController { let hypotheses = languageRecognizer .languageHypotheses(withMaximum: 3) DispatchQueue.main.async { - self.composeContentToolbarViewModel.suggestedLanguages = hypotheses + self?.composeContentToolbarViewModel.suggestedLanguages = hypotheses .filter { _, probability in probability > 0.1 } .keys .map(\.rawValue) if let bestLanguage = hypotheses.max(by: { $0.value < $1.value }), bestLanguage.value > 0.99 { - self.composeContentToolbarViewModel.highConfidenceSuggestedLanguage = bestLanguage.key.rawValue + self?.composeContentToolbarViewModel.highConfidenceSuggestedLanguage = bestLanguage.key.rawValue } else { - self.composeContentToolbarViewModel.highConfidenceSuggestedLanguage = nil + self?.composeContentToolbarViewModel.highConfidenceSuggestedLanguage = nil } } } @@ -397,7 +385,8 @@ extension ComposeContentViewController { } } - private func resetImagePicker() { + private func resetPhotoPicker() { + photoLibraryPicker?.delegate = nil let selectionLimit = max(1, viewModel.maxMediaAttachmentLimit - viewModel.attachmentViewModels.count) let configuration = ComposeContentViewController.createPhotoLibraryPickerConfiguration(selectionLimit: selectionLimit) photoLibraryPicker = createImagePicker(configuration: configuration) @@ -545,13 +534,22 @@ extension ComposeContentViewController: ComposeContentToolbarViewDelegate { } } + public func presentPhotoLibraryPicker() { + if let photoLibraryPicker { + guard photoLibraryPicker.presentingViewController == nil else { return } + } + resetPhotoPicker() + guard let photoLibraryPicker else { return } + present(photoLibraryPicker, animated: true, completion: nil) + } + func composeContentToolbarView( _ viewModel: ComposeContentToolbarView.ViewModel, attachmentMenuDidPressed action: ComposeContentToolbarView.ViewModel.AttachmentAction ) { switch action { case .photoLibrary: - present(photoLibraryPicker, animated: true, completion: nil) + presentPhotoLibraryPicker() case .camera: present(imagePickerController, animated: true, completion: nil) case .browse: