Bläddra i källkod

chatview updates now after image or vid was sent

Bastian van de Wetering 6 år sedan
förälder
incheckning
38c79ba2c9

+ 2 - 1
Pods/MessageKit/Sources/Views/Cells/MediaMessageCell.swift

@@ -73,7 +73,8 @@ open class MediaMessageCell: MessageContentCell {
             imageView.image = mediaItem.image ?? mediaItem.placeholderImage
             playButtonView.isHidden = true
         case .video(let mediaItem):
-            imageView.image = mediaItem.image ?? mediaItem.placeholderImage
+			let image = mediaItem.image ?? mediaItem.placeholderImage
+			imageView.image = image //mediaItem.image ?? mediaItem.placeholderImage
             playButtonView.isHidden = false
         default:
             break

+ 12 - 21
deltachat-ios/Controller/ChatViewController.swift

@@ -11,6 +11,16 @@ import QuickLook
 import UIKit
 import InputBarAccessoryView
 
+protocol MediaSendHandler {
+	func onSuccess()
+}
+
+extension ChatViewController: MediaSendHandler {
+	func onSuccess() {
+		refreshMessages()
+	}
+}
+
 class ChatViewController: MessagesViewController {
 	weak var coordinator: ChatViewCoordinator?
 
@@ -342,21 +352,6 @@ class ChatViewController: MessagesViewController {
 				}.onTouchUpInside { _ in
 					self.clipperButtonPressed()
 			}
-			/*
-			InputBarButtonItem()
-			.configure {
-			$0.spacing = .fixed(0)
-			$0.image = UIImage(named: "camera")?.withRenderingMode(.alwaysTemplate)
-			$0.setSize(CGSize(width: 36, height: 36), animated: false)
-			$0.tintColor = UIColor(white: 0.8, alpha: 1)
-			}.onSelected {
-			$0.tintColor = DCColors.primary
-			}.onDeselected {
-			$0.tintColor = UIColor(white: 0.8, alpha: 1)
-			}.onTouchUpInside { _ in
-			self.didPressPhotoButton()
-			},
-			*/
 		]
 
 		messageInputBar.setStackViewItems(leftItems, forStack: .left, animated: false)
@@ -373,11 +368,7 @@ class ChatViewController: MessagesViewController {
 				})
 		}
 	}
-	/*
-	let rightItems = [
-	let sendButtonImage = UIImage(named: "paper_plane")?.withRenderingMode(.alwaysTemplate)
-	]
-	*/
+
 	@objc private func chatProfilePressed() {
 		coordinator?.showChatDetail(chatId: chatId)
 	}
@@ -818,8 +809,8 @@ extension ChatViewController: MessageCellDelegate {
 					next = Int(dc_get_next_media(mailboxPointer, UInt32(nextMessage.id), 1, Int32(nextMessage.type), 0, 0))
 				}
 
+				// these are the files user will be able to swip trough
 				let mediaUrls: [URL] = previousUrls + [url] + nextUrls
-
 				previewController = PreviewController(currentIndex: previousUrls.count, urls: mediaUrls)
 				present(previewController!.qlController, animated: true)
 			}

+ 53 - 43
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -107,8 +107,8 @@ class AppCoordinator: NSObject, Coordinator, UITabBarControllerDelegate {
 
 	func presentLoginController() {
 		let accountSetupController = AccountSetupController()
-		let accountSetupNavigationController = DCNavigationController(rootViewController: accountSetupController)
-		rootViewController.present(accountSetupNavigationController, animated: false, completion: nil)
+		let accountSetupNav = DCNavigationController(rootViewController: accountSetupController)
+		rootViewController.present(accountSetupNav, animated: false, completion: nil)
 	}
 }
 
@@ -119,7 +119,7 @@ extension AppCoordinator: UITabBarDelegate {
 
 	func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
 		print("shouldSelect")
-		return true 
+		return true
 	}
 }
 
@@ -415,25 +415,49 @@ class ChatViewCoordinator: NSObject, Coordinator {
 		// navigationController.present(nav, animated: true, completion: nil)
 	}
 
+	private func sendImage(_ image:UIImage) {
+		DispatchQueue.global().async {
+			if let compressedImage = image.dcCompress() {
+				// at this point image is compressed by 85% by default
+				let pixelSize = compressedImage.imageSizeInPixel()
+				let width = Int32(exactly: pixelSize.width)!
+				let height =  Int32(exactly: pixelSize.height)!
+				let path = Utils.saveImage(image: compressedImage)
+				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)
+			}
+		}
+	}
+
+	private func sendVideo(url: NSURL) {
+		let msg = dc_msg_new(mailboxPointer, DC_MSG_VIDEO)
+		if let path = url.relativePath?.cString(using: .utf8) { //absoluteString?.cString(using: .utf8) {
+			dc_msg_set_file(msg, path, nil)
+			dc_send_msg(mailboxPointer, UInt32(chatId), msg)
+			dc_msg_unref(msg)
+		}
+	}
+
+	private func handleMediaMessageSuccess() {
+		if let chatViewController = self.navigationController.visibleViewController as? MediaSendHandler {
+			DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
+				chatViewController.onSuccess()
+			}
+		}
+	}
+
 	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 compressedImage = image?.dcCompress() {
-						// at this point image is compressed by 85% by default
-						let pixelSize = compressedImage.imageSizeInPixel()
-						let width = Int32(exactly: pixelSize.width)!
-						let height =  Int32(exactly: pixelSize.height)!
-						let path = Utils.saveImage(image: compressedImage)
-						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)
-					}
+				self?.navigationController.dismiss(animated: true, completion: {
+					self?.handleMediaMessageSuccess()
+				})
+				if let image = image {
+					self?.sendImage(image)
 				}
 			}
 
@@ -449,10 +473,11 @@ class ChatViewCoordinator: NSObject, Coordinator {
 	}
 	func showVideoLibrary() {
 		if PHPhotoLibrary.authorizationStatus() != .authorized {
-			PHPhotoLibrary.requestAuthorization{status in
-				DispatchQueue.main.async() { [weak self] in
+			PHPhotoLibrary.requestAuthorization { status in
+				DispatchQueue.main.async {
+					[weak self] in
 					switch status {
-					case  .denied,.notDetermined,.restricted:
+					case  .denied, .notDetermined, .restricted:
 						print("denied")
 					case .authorized:
 						self?.presentVideoLibrary()
@@ -465,7 +490,7 @@ class ChatViewCoordinator: NSObject, Coordinator {
 	}
 
 	private func presentVideoLibrary() {
-		if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
+		if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
 			let videoPicker = UIImagePickerController()
 			videoPicker.title = "Videos"
 			videoPicker.delegate = self
@@ -478,26 +503,13 @@ class ChatViewCoordinator: NSObject, Coordinator {
 
 extension ChatViewCoordinator: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
 
-	func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
-		if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL{
-			print("videourl: ", videoUrl)
-			//trying compression of video
-			let data = NSData(contentsOf: videoUrl as URL)!
-			print("File size before compression: \(Double(data.length / 1048576)) mb")
-			let size = Double(data.length / 1048576)
-			print(size)
-			let msg = dc_msg_new(mailboxPointer, DC_MSG_VIDEO)
-			if let path = videoUrl.relativePath?.cString(using: .utf8) { //absoluteString?.cString(using: .utf8) {
-				dc_msg_set_file(msg, path, nil)
-				dc_send_msg(mailboxPointer, UInt32(chatId), msg)
-				dc_msg_unref(msg)
-			}
-			// self.videoPickedBlock?(videoUrl, size)
+	func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
+		if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL {
+			sendVideo(url: videoUrl)
 		}
-		else{
-			print("Something went wrong in  video")
+		navigationController.dismiss(animated: true) {
+				self.handleMediaMessageSuccess()
 		}
-		navigationController.dismiss(animated: true, completion: nil)
 	}
 }
 
@@ -519,8 +531,6 @@ class NewGroupCoordinator: Coordinator {
 	}
 }
 
-
-
 class GroupNameCoordinator: Coordinator {
 	let navigationController: UINavigationController