Some tweaks to the pick from photo library process.

Contributes to IOS-364
This commit is contained in:
shannon 2025-01-28 11:02:03 -05:00
parent 82f844d83a
commit 233e8d4027
3 changed files with 22 additions and 32 deletions

View File

@ -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
}
}

View File

@ -172,9 +172,6 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable
}
} // end Task
self.uploadTask = uploadTask
Task {
await uploadTask.value
}
}
deinit {

View File

@ -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 dont 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: