Browse Source

photoController now presented via coordinator

Bastian van de Wetering 6 years ago
parent
commit
bfb9be9fe1

+ 2 - 28
deltachat-ios/Controller/ChatViewController.swift

@@ -5,7 +5,6 @@
 //  Created by Bastian van de Wetering on 08.11.17.
 //  Copyright © 2017 Jonas Reinsch. All rights reserved.
 //
-import ALCameraViewController
 import MapKit
 import MessageKit
 import QuickLook
@@ -779,34 +778,9 @@ extension ChatViewController: MessagesLayoutDelegate {
 	}
 
 	private func photoButtonPressed(_ action: UIAlertAction) {
-		if UIImagePickerController.isSourceTypeAvailable(.camera) {
-			let cameraViewController = CameraViewController { [weak self] image, _ in
-				self?.dismiss(animated: true, completion: nil)
-
-				DispatchQueue.global().async {
-					if let pickedImage = image {
-						let width = Int32(exactly: pickedImage.size.width)!
-						let height = Int32(exactly: pickedImage.size.height)!
-						let path = Utils.saveImage(image: pickedImage)
-						let msg = dc_msg_new(mailboxPointer, DC_MSG_IMAGE)
-						dc_msg_set_file(msg, path, "image/jpeg")
-						dc_msg_set_dimension(msg, width, height)
-						dc_send_msg(mailboxPointer, UInt32(self!.chatId), msg)
-						// cleanup
-						dc_msg_unref(msg)
-					}
-				}
-			}
-
-			present(cameraViewController, animated: true, completion: nil)
-		} else {
-			let alert = UIAlertController(title: "Camera is not available", message: nil, preferredStyle: .alert)
-			alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { _ in
-				self.dismiss(animated: true, completion: nil)
-			}))
-			present(alert, animated: true, completion: nil)
-		}
+		coordinator?.showCameraViewController()
 	}
+
 }
 
 // MARK: - MessageCellDelegate

+ 51 - 7
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -7,6 +7,8 @@
 //
 
 import UIKit
+import ALCameraViewController
+
 
 class AppCoordinator: NSObject, Coordinator, UITabBarControllerDelegate {
 	private let window: UIWindow
@@ -138,7 +140,7 @@ class ContactListCoordinator: Coordinator {
 
 	func showChat(chatId: Int) {
 		let chatVC = ChatViewController(chatId: chatId)
-		let coordinator = ChatViewCoordinator(navigationController: navigationController)
+		let coordinator = ChatViewCoordinator(navigationController: navigationController, chatId: chatId)
 		childCoordinators.append(coordinator)
 		chatVC.coordinator = coordinator
 		navigationController.pushViewController(chatVC, animated: true)
@@ -147,9 +149,18 @@ class ContactListCoordinator: Coordinator {
 
 // since mailbox and chatView -tab both use ChatViewController we want to be able to assign different functionality via coordinators -> therefore we override unneeded functions such as showChatDetail -> maybe find better solution in longterm
 class MailboxCoordinator: ChatViewCoordinator {
+
+	init(navigationController: UINavigationController) {
+		super.init(navigationController: navigationController, chatId: -1)
+	}
+
 	override func showChatDetail(chatId _: Int) {
 		// ignore for now
 	}
+
+	override func showCameraViewController() {
+		// ignore
+	}
 }
 
 class ProfileCoordinator: Coordinator {
@@ -179,7 +190,7 @@ class ChatListCoordinator: Coordinator {
 
 	func showChat(chatId: Int) {
 		let chatVC = ChatViewController(chatId: chatId)
-		let coordinator = ChatViewCoordinator(navigationController: navigationController)
+		let coordinator = ChatViewCoordinator(navigationController: navigationController, chatId: chatId)
 		childCoordinators.append(coordinator)
 		chatVC.coordinator = coordinator
 		navigationController.pushViewController(chatVC, animated: true)
@@ -319,7 +330,7 @@ class NewChatCoordinator: Coordinator {
 
 	func showChat(chatId: Int) {
 		let chatViewController = ChatViewController(chatId: chatId)
-		let coordinator = ChatViewCoordinator(navigationController: navigationController)
+		let coordinator = ChatViewCoordinator(navigationController: navigationController, chatId: chatId)
 		childCoordinators.append(coordinator)
 		chatViewController.coordinator = coordinator
 		navigationController.pushViewController(chatViewController, animated: true)
@@ -360,11 +371,13 @@ class GroupChatDetailCoordinator: Coordinator {
 
 class ChatViewCoordinator: Coordinator {
 	let navigationController: UINavigationController
+	let chatId: Int
 
 	var childCoordinators: [Coordinator] = []
 
-	init(navigationController: UINavigationController) {
+	init(navigationController: UINavigationController, chatId: Int) {
 		self.navigationController = navigationController
+		self.chatId = chatId
 	}
 
 	func showChatDetail(chatId: Int) {
@@ -397,6 +410,37 @@ class ChatViewCoordinator: Coordinator {
 		navigationController.pushViewController(contactDetailController, animated: true)
 		// navigationController.present(nav, animated: true, completion: nil)
 	}
+
+	func showCameraViewController() {
+		if UIImagePickerController.isSourceTypeAvailable(.camera) {
+			let cameraViewController = CameraViewController { [weak self] image, _ in
+				self?.navigationController.dismiss(animated: true, completion: nil)
+
+				DispatchQueue.global().async {
+					if let pickedImage = image {
+						let width = Int32(exactly: pickedImage.size.width)!
+						let height = Int32(exactly: pickedImage.size.height)!
+						let path = Utils.saveImage(image: pickedImage)
+						let msg = dc_msg_new(mailboxPointer, DC_MSG_IMAGE)
+						dc_msg_set_file(msg, path, "image/jpeg")
+						dc_msg_set_dimension(msg, width, height)
+						dc_send_msg(mailboxPointer, UInt32(self!.chatId), msg)
+						// cleanup
+						dc_msg_unref(msg)
+					}
+				}
+			}
+
+			navigationController.present(cameraViewController, animated: true, completion: nil)
+		} else {
+			let alert = UIAlertController(title: "Camera is not available", message: nil, preferredStyle: .alert)
+			alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { _ in
+				self.navigationController.dismiss(animated: true, completion: nil)
+			}))
+			navigationController.present(alert, animated: true, completion: nil)
+		}
+
+	}
 }
 
 class NewGroupCoordinator: Coordinator {
@@ -428,7 +472,7 @@ class GroupNameCoordinator: Coordinator {
 
 	func showGroupChat(chatId: Int) {
 		let chatViewController = ChatViewController(chatId: chatId)
-		let coordinator = ChatViewCoordinator(navigationController: navigationController)
+		let coordinator = ChatViewCoordinator(navigationController: navigationController, chatId: chatId)
 		childCoordinators.append(coordinator)
 		chatViewController.coordinator = coordinator
 		navigationController.popToRootViewController(animated: false)
@@ -447,7 +491,7 @@ class ContactDetailCoordinator: Coordinator, ContactDetailCoordinatorProtocol {
 
 	func showChat(chatId: Int) {
 		let chatViewController = ChatViewController(chatId: chatId)
-		let coordinator = ChatViewCoordinator(navigationController: navigationController)
+		let coordinator = ChatViewCoordinator(navigationController: navigationController, chatId: chatId)
 		childCoordinators.append(coordinator)
 		chatViewController.coordinator = coordinator
 		navigationController.popToRootViewController(animated: false)
@@ -491,7 +535,7 @@ class EditContactCoordinator: Coordinator, EditContactCoordinatorProtocol {
 
 	func showChat(chatId: Int) {
 		let chatViewController = ChatViewController(chatId: chatId)
-		let coordinator = ChatViewCoordinator(navigationController: navigationController)
+		let coordinator = ChatViewCoordinator(navigationController: navigationController, chatId: chatId)
 		childCoordinators.append(coordinator)
 		chatViewController.coordinator = coordinator
 		navigationController.popToRootViewController(animated: false)

+ 19 - 0
deltachat-ios/Helper/Extensions.swift

@@ -135,3 +135,22 @@ extension MRContact {
     }
   }
 }
+
+extension UIImage {
+	// from: https://stackoverflow.com/questions/29726643/how-to-compress-of-reduce-the-size-of-an-image-before-uploading-to-parse-as-pffi
+	enum JPEGQuality: CGFloat {
+		case lowest  = 0
+		case low     = 0.25
+		case medium  = 0.5
+		case high    = 0.75
+		case highest = 1
+	}
+
+	/// Returns the data for the specified image in JPEG format.
+	/// If the image object’s underlying image data has been purged, calling this function forces that data to be reloaded into memory.
+	/// - returns: A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
+	func jpeg(_ jpegQuality: JPEGQuality) -> Data? {
+		return jpegData(compressionQuality: jpegQuality.rawValue)
+	}
+
+}