فهرست منبع

add basic searchController and accessory view to ChatViewController

cyberta 3 سال پیش
والد
کامیت
ae5c915eb2
1فایلهای تغییر یافته به همراه60 افزوده شده و 1 حذف شده
  1. 60 1
      deltachat-ios/Chat/ChatViewController.swift

+ 60 - 1
deltachat-ios/Chat/ChatViewController.swift

@@ -26,6 +26,7 @@ class ChatViewController: UITableViewController {
     private var ignoreInputBarChange = false
     private var isVisibleToUser: Bool = false
     private var keepKeyboard: Bool = false
+    private var isSearchActive: Bool = false
 
     lazy var isGroupChat: Bool = {
         return dcContext.getChat(chatId: chatId).isGroup
@@ -36,6 +37,17 @@ class ChatViewController: UITableViewController {
         return draft
     }()
 
+    private lazy var searchController: UISearchController = {
+        let searchController = UISearchController(searchResultsController: nil)
+        searchController.obscuresBackgroundDuringPresentation = false
+        searchController.searchBar.placeholder = String.localized("search")
+        searchController.searchBar.delegate = self
+        searchController.searchBar.inputAccessoryView = messageInputBar
+        searchController.searchBar.autocorrectionType = .yes
+        searchController.searchBar.keyboardType = .default
+        return searchController
+    }()
+
     /// The `InputBarAccessoryView` used as the `inputAccessoryView` in the view controller.
     open var messageInputBar = ChatInputBar()
 
@@ -54,6 +66,13 @@ class ChatViewController: UITableViewController {
         return view
     }()
 
+    public lazy var searchAccessoryBar: ChatSearchAccessoryBar = {
+        let view = ChatSearchAccessoryBar()
+        view.delegate = self
+        view.translatesAutoresizingMaskIntoConstraints = false
+        return view
+    }()
+
     public lazy var contactRequestBar: ChatContactRequestBar = {
         let chat = dcContext.getChat(chatId: chatId)
         let view = ChatContactRequestBar(useDeleteButton: chat.isGroup && !chat.isMailinglist)
@@ -261,6 +280,8 @@ class ChatViewController: UITableViewController {
         tableView.contentInsetAdjustmentBehavior = .never
         navigationController?.setNavigationBarHidden(false, animated: false)
         navigationItem.backButtonTitle = String.localized("chat")
+        navigationItem.searchController = searchController
+        definesPresentationContext = true
 
         if !dcContext.isConfigured() {
             // TODO: display message about nothing being configured
@@ -672,7 +693,12 @@ class ChatViewController: UITableViewController {
             messageInputBar.setRightStackViewWidthConstant(to: 40, animated: false)
             messageInputBar.padding = UIEdgeInsets(top: 6, left: 6, bottom: 6, right: 12)
         }
-        messageInputBar.setStackViewItems([draftArea], forStack: .top, animated: animated)
+
+        if isSearchActive {
+            messageInputBar.setStackViewItems([searchAccessoryBar, draftArea], forStack: .top, animated: false)
+        } else {
+            messageInputBar.setStackViewItems([draftArea], forStack: .top, animated: false)
+        }
     }
 
     override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
@@ -1750,6 +1776,39 @@ extension ChatViewController: ChatEditingDelegate {
     }
 }
 
+// MARK: - ChatSearchDelegate
+extension ChatViewController: ChatSearchDelegate {
+    func onSearchPreviousPressed() {
+        logger.debug("onSearch Previous Pressed")
+    }
+
+    func onSearchNextPressed() {
+        logger.debug("onSearch Next Pressed")
+
+    }
+}
+
+// MARK: - UISearchBarDelegate
+extension ChatViewController: UISearchBarDelegate {
+    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
+        logger.debug("searchbar: \(searchText)")
+    }
+
+    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
+        logger.debug("searchbar: searchBarShouldBeginEditing")
+        isSearchActive = true
+        configureDraftArea(draft: draft)
+        return true
+    }
+
+    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
+        logger.debug("searchbar: searchBarTextDidEndEditing")
+        isSearchActive = false
+        configureDraftArea(draft: draft)
+        self.tableView.becomeFirstResponder()
+    }
+}
+
 // MARK: - ChatContactRequestBar
 extension ChatViewController: ChatContactRequestDelegate {
     func onAcceptRequest() {