Browse Source

MediaPicker now holds a weak reference to its parent navigationController

B. Petersen 5 years ago
parent
commit
cbe35425da

+ 1 - 5
deltachat-ios/Controller/ChatViewController.swift

@@ -64,11 +64,7 @@ class ChatViewController: MessagesViewController {
     var previewView: UIView?
 
     private lazy var mediaPicker: MediaPicker? = {
-        if let navigationController = navigationController {
-            return MediaPicker(navigationController: navigationController)
-        } else {
-            return nil
-        }
+        return MediaPicker(navigationController: navigationController)
     }()
 
     var emptyStateView: PaddingLabel = {

+ 1 - 5
deltachat-ios/Controller/EditGroupViewController.swift

@@ -12,11 +12,7 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     var avatarSelectionCell: AvatarSelectionCell
 
     private lazy var mediaPicker: MediaPicker? = {
-        if let navigationController = navigationController {
-            return MediaPicker(navigationController: navigationController)
-        } else {
-            return nil
-        }
+        return MediaPicker(navigationController: navigationController)
     }()
 
     lazy var groupNameCell: TextFieldCell = {

+ 1 - 5
deltachat-ios/Controller/EditSettingsController.swift

@@ -21,11 +21,7 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     private let tagAccountSettingsCell = 1
 
     private lazy var mediaPicker: MediaPicker? = {
-        if let navigationController = self.parent as? UINavigationController {
-            return MediaPicker(navigationController: navigationController)
-        } else {
-            return nil
-        }
+        return MediaPicker(navigationController: navigationController)
     }()
 
     private lazy var statusCell: MultilineTextFieldCell = {

+ 1 - 5
deltachat-ios/Controller/NewGroupController.swift

@@ -26,11 +26,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     private let sectionGroupMembers = 2
 
     private lazy var mediaPicker: MediaPicker? = {
-        if let navigationController = navigationController {
-            return MediaPicker(navigationController: navigationController)
-        } else {
-            return nil
-        }
+        return MediaPicker(navigationController: navigationController)
     }()
 
     lazy var groupNameCell: TextFieldCell = {

+ 1 - 5
deltachat-ios/Controller/ProfileInfoViewController.swift

@@ -7,11 +7,7 @@ class ProfileInfoViewController: UITableViewController {
     private var displayName: String?
 
     private lazy var mediaPicker: MediaPicker? = {
-        if let navigationController = navigationController {
-            return MediaPicker(navigationController: navigationController)
-        } else {
-            return nil
-        }
+        return MediaPicker(navigationController: navigationController)
     }()
 
     private lazy var doneButtonItem: UIBarButtonItem = {

+ 16 - 16
deltachat-ios/Helper/MediaPicker.swift

@@ -28,14 +28,14 @@ extension MediaPickerDelegate {
 
 class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate, AudioRecorderControllerDelegate, UIDocumentPickerDelegate {
 
-    private let navigationController: UINavigationController
+    private weak var navigationController: UINavigationController?
     private weak var delegate: MediaPickerDelegate?
 
-    init(navigationController: UINavigationController) {
+    init(navigationController: UINavigationController?) {
+        // it does not make sense to give nil here, but it makes construction easier
         self.navigationController = navigationController
     }
 
-
     func showVoiceRecorder(delegate: MediaPickerDelegate) {
         self.delegate = delegate
         let audioRecorderController = AudioRecorderController()
@@ -43,8 +43,8 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         //audioRecorderController.maximumRecordDuration = 1200
         let audioRecorderNavController = UINavigationController(rootViewController: audioRecorderController)
 
-        navigationController.present(audioRecorderNavController, animated: true, completion: nil)
- }
+        navigationController?.present(audioRecorderNavController, animated: true, completion: nil)
+    }
 
     func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
         if PHPhotoLibrary.authorizationStatus() != .authorized {
@@ -71,7 +71,7 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         documentPicker.allowsMultipleSelection = false
         documentPicker.modalPresentationStyle = .formSheet
         self.delegate = delegate
-        navigationController.present(documentPicker, animated: true, completion: nil)
+        navigationController?.present(documentPicker, animated: true, completion: nil)
     }
 
     func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
@@ -91,7 +91,7 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
                 kUTTypeImage as String
             ]
             self.delegate = delegate
-            navigationController.present(videoPicker, animated: true, completion: nil)
+            navigationController?.present(videoPicker, animated: true, completion: nil)
         }
     }
 
@@ -109,10 +109,10 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
                 if let image = image {
                     self?.delegate?.onImageSelected(image: image)
                 }
-                self?.navigationController.dismiss(animated: true, completion: nil)})
+                self?.navigationController?.dismiss(animated: true, completion: nil)})
         self.delegate = delegate
         controller.modalPresentationStyle = .fullScreen
-        navigationController.present(controller, animated: true, completion: nil)
+        navigationController?.present(controller, animated: true, completion: nil)
     }
 
     func showCamera(delegate: MediaPickerDelegate, allowCropping: Bool) {
@@ -134,17 +134,17 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
                     if let image = image {
                         self?.delegate?.onImageSelected(image: image)
                     }
-                    self?.navigationController.dismiss(animated: true, completion: nil)}
+                    self?.navigationController?.dismiss(animated: true, completion: nil)}
             )
             self.delegate = delegate
             cameraViewController.modalPresentationStyle = .fullScreen
-            navigationController.present(cameraViewController, animated: true, completion: nil)
+            navigationController?.present(cameraViewController, animated: true, completion: nil)
         } else {
             let alert = UIAlertController(title: String.localized("chat_camera_unavailable"), message: nil, preferredStyle: .alert)
             alert.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel, handler: { _ in
-                self.navigationController.dismiss(animated: true, completion: nil)
+                self.navigationController?.dismiss(animated: true, completion: nil)
             }))
-            navigationController.present(alert, animated: true, completion: nil)
+            navigationController?.present(alert, animated: true, completion: nil)
         }
     }
 
@@ -161,15 +161,15 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
                     logger.error(error.localizedDescription)
                     let alert = UIAlertController(title: String.localized("error"), message: nil, preferredStyle: .alert)
                     alert.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel, handler: { _ in
-                        self.navigationController.dismiss(animated: true, completion: nil)
+                        self.navigationController?.dismiss(animated: true, completion: nil)
                     }))
-                    self.navigationController.present(alert, animated: true, completion: nil)
+                    self.navigationController?.present(alert, animated: true, completion: nil)
                 }
             })
         } else if let imageUrl = info[UIImagePickerController.InfoKey.imageURL] as? NSURL {
             self.delegate?.onImageSelected(url: imageUrl)
         }
-        navigationController.dismiss(animated: true, completion: nil)
+        navigationController?.dismiss(animated: true, completion: nil)
     }
 
     func didFinishAudioAtPath(path: String) {