Browse Source

add scroll to msg after search

nayooti 5 years ago
parent
commit
948d0ef9fa

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -155,6 +155,7 @@
 		AEE6EC3F2282C59C00EDC689 /* GroupMembersViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE6EC3E2282C59C00EDC689 /* GroupMembersViewController.swift */; };
 		AEE6EC412282DF5700EDC689 /* MailboxViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE6EC402282DF5700EDC689 /* MailboxViewController.swift */; };
 		AEE6EC482283045D00EDC689 /* EditSettingsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE6EC472283045D00EDC689 /* EditSettingsController.swift */; };
+		AEF6E8C82432449900AFC203 /* UINavigationController+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEF6E8C72432449900AFC203 /* UINavigationController+Extension.swift */; };
 		AEFBE22F23FEF23D0045327A /* ProviderInfoCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEFBE22E23FEF23D0045327A /* ProviderInfoCell.swift */; };
 		AEFBE23123FF09B20045327A /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEFBE23023FF09B20045327A /* TypeAlias.swift */; };
 		B21005DB23383664004C70C5 /* SettingsClassicViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B21005DA23383664004C70C5 /* SettingsClassicViewController.swift */; };
@@ -389,6 +390,7 @@
 		AEE6EC3E2282C59C00EDC689 /* GroupMembersViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupMembersViewController.swift; sourceTree = "<group>"; };
 		AEE6EC402282DF5700EDC689 /* MailboxViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MailboxViewController.swift; sourceTree = "<group>"; };
 		AEE6EC472283045D00EDC689 /* EditSettingsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditSettingsController.swift; sourceTree = "<group>"; };
+		AEF6E8C72432449900AFC203 /* UINavigationController+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UINavigationController+Extension.swift"; sourceTree = "<group>"; };
 		AEFBE22E23FEF23D0045327A /* ProviderInfoCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProviderInfoCell.swift; sourceTree = "<group>"; };
 		AEFBE23023FF09B20045327A /* TypeAlias.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeAlias.swift; sourceTree = "<group>"; };
 		B21005DA23383664004C70C5 /* SettingsClassicViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsClassicViewController.swift; sourceTree = "<group>"; };
@@ -477,6 +479,7 @@
 				3059620D234614E700C80F33 /* DcContact+Extension.swift */,
 				3059620F2346154D00C80F33 /* String+Extension.swift */,
 				302B84CD2397F6CD001C261F /* URL+Extension.swift */,
+				AEF6E8C72432449900AFC203 /* UINavigationController+Extension.swift */,
 			);
 			path = Extensions;
 			sourceTree = "<group>";
@@ -1164,6 +1167,7 @@
 				30A4D9AE2332672700544344 /* QrInviteViewController.swift in Sources */,
 				305961D62346125100C80F33 /* MessageInputBar.swift in Sources */,
 				305961ED2346125100C80F33 /* DetectorType.swift in Sources */,
+				AEF6E8C82432449900AFC203 /* UINavigationController+Extension.swift in Sources */,
 				305962062346125100C80F33 /* TypingIndicatorCellSizeCalculator.swift in Sources */,
 				AE851AC7227C776400ED86F0 /* Location.swift in Sources */,
 				7AE0A5491FC42F65005ECB4B /* NewChatViewController.swift in Sources */,

+ 1 - 1
deltachat-ios/Controller/ChatListController.swift

@@ -212,7 +212,7 @@ class ChatListController: UITableViewController {
             if chatId == DC_CHAT_ID_ARCHIVED_LINK {
                 coordinator?.showArchive()
             } else {
-                coordinator?.showChat(chatId: chatId)
+                coordinator?.showChat(chatId: chatId, msgId: chatData.msgId)
             }
         case .contact(let contactData):
             let contactId = contactData.contactId

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

@@ -142,6 +142,7 @@ class ChatViewController: MessagesViewController {
                              emptyStateView.constraintAlignTrailingTo(view, paddingTrailing: 40)])
     }
 
+    // MARK: - lifecycle
     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
         // this will be removed in viewWillDisappear
@@ -381,6 +382,17 @@ class ChatViewController: MessagesViewController {
         }
     }
 
+    func scrollToMessage(id: Int, animated: Bool) {
+        for (index, msg) in messageList.enumerated() {
+            if msg.id == id {
+                let indexPath = IndexPath(row: 0, section: index)
+                messagesCollectionView.scrollToItem(at: indexPath, at: .top, animated: animated)
+                return
+            }
+        }
+        safe_fatalError("message could not be found")
+    }
+
     @objc private func setTextDraft() {
         if let text = self.messageInputBar.inputTextView.text {
             let draft = dc_msg_new(mailboxPointer, DC_MSG_TEXT)

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

@@ -185,12 +185,17 @@ class ChatListCoordinator: Coordinator {
         navigationController.pushViewController(newChatVC, animated: true)
     }
 
-    func showChat(chatId: Int) {
+    func showChat(chatId: Int, msgId: Int? = nil) {
         let chatVC = ChatViewController(dcContext: dcContext, chatId: chatId)
         let coordinator = ChatViewCoordinator(dcContext: dcContext, navigationController: navigationController, chatId: chatId)
         childCoordinators.append(coordinator)
         chatVC.coordinator = coordinator
-        navigationController.pushViewController(chatVC, animated: true)
+
+        navigationController.pushViewController(chatVC, animated: true, completion: {
+            if let msgId = msgId {
+                chatVC.scrollToMessage(id: msgId, animated: true)
+            }
+        })
     }
 
     func showArchive() {

+ 30 - 0
deltachat-ios/Extensions/UINavigationController+Extension.swift

@@ -0,0 +1,30 @@
+import UIKit
+
+extension UINavigationController {
+    public func pushViewController(
+        _ viewController: UIViewController,
+        animated: Bool,
+        completion: @escaping () -> Void) {
+        pushViewController(viewController, animated: animated)
+
+        guard animated, let coordinator = transitionCoordinator else {
+            DispatchQueue.main.async { completion() }
+            return
+        }
+
+        coordinator.animate(alongsideTransition: nil) { _ in completion() }
+    }
+
+    func popViewController(
+        animated: Bool,
+        completion: @escaping () -> Void) {
+        popViewController(animated: animated)
+
+        guard animated, let coordinator = transitionCoordinator else {
+            DispatchQueue.main.async { completion() }
+            return
+        }
+
+        coordinator.animate(alongsideTransition: nil) { _ in completion() }
+    }
+}

+ 4 - 2
deltachat-ios/ViewModel/ChatListViewModel.swift

@@ -216,7 +216,8 @@ private extension ChatListViewModel {
             chatData: ChatCellData(
                 chatId: chatId,
                 summary: summary,
-                unreadMessages: unreadMessages
+                unreadMessages: unreadMessages,
+                msgId: nil
             ),
             titleHighlightIndexes: chatTitleIndexes
         )
@@ -234,7 +235,8 @@ private extension ChatListViewModel {
             chatData: ChatCellData(
                 chatId: chatId,
                 summary: summary,
-                unreadMessages: unreadMessages
+                unreadMessages: unreadMessages,
+                msgId: msgId
             )
         )
         let subtitle = viewModel.subtitle

+ 1 - 0
deltachat-ios/ViewModel/ContactCellViewModel.swift

@@ -23,6 +23,7 @@ struct ChatCellData {
     let chatId: Int
     let summary: DcLot
     let unreadMessages: Int
+    let msgId: Int?
 }
 
 struct DeaddropCellData {

+ 1 - 1
deltachat-ios/ViewModel/ContactDetailViewModel.swift

@@ -115,7 +115,7 @@ class ContactDetailViewModel: ContactDetailViewModelProtocol {
         let summary = sharedChats.getSummary(index: index)
         let unreadMessages = context.getUnreadMessages(chatId: chatId)
 
-        let cellData = ChatCellData(chatId: chatId, summary: summary, unreadMessages: unreadMessages)
+        let cellData = ChatCellData(chatId: chatId, summary: summary, unreadMessages: unreadMessages, msgId: nil)
         let cellViewModel = ChatCellViewModel(chatData: cellData)
         cell.updateCell(cellViewModel: cellViewModel)
     }