Browse Source

handling empty search result in chatlist

nayooti 5 years ago
parent
commit
fb6ecc6533

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

@@ -33,6 +33,12 @@ class ChatListController: UITableViewController {
         return button
         return button
     }()
     }()
 
 
+    private lazy var emptySearchStateLabel: EmptyStateLabel = {
+        let label = EmptyStateLabel()
+        label.isHidden = false
+        return label
+    }()
+
     func getArchiveCell(title: String) -> UITableViewCell {
     func getArchiveCell(title: String) -> UITableViewCell {
         let cell = UITableViewCell()
         let cell = UITableViewCell()
         cell.textLabel?.textColor = .systemBlue
         cell.textLabel?.textColor = .systemBlue
@@ -64,6 +70,7 @@ class ChatListController: UITableViewController {
             navigationItem.searchController = searchController
             navigationItem.searchController = searchController
         }
         }
         configureTableView()
         configureTableView()
+        setupSubviews()
     }
     }
 
 
     override func viewWillAppear(_ animated: Bool) {
     override func viewWillAppear(_ animated: Bool) {
@@ -116,6 +123,15 @@ class ChatListController: UITableViewController {
             nc.removeObserver(viewChatObserver)
             nc.removeObserver(viewChatObserver)
         }
         }
     }
     }
+    // MARK: - setup
+    private func setupSubviews() {
+        view.addSubview(emptySearchStateLabel)
+        emptySearchStateLabel.translatesAutoresizingMaskIntoConstraints = false
+        emptySearchStateLabel.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
+        emptySearchStateLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40).isActive = true
+        emptySearchStateLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40).isActive = true
+        emptySearchStateLabel.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
+    }
 
 
     // MARK: - configuration
     // MARK: - configuration
     private func configureTableView() {
     private func configureTableView() {
@@ -155,7 +171,6 @@ class ChatListController: UITableViewController {
         return viewModel.numberOfRowsIn(section: section)
         return viewModel.numberOfRowsIn(section: section)
     }
     }
 
 
-
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
 
         let cellData = viewModel.cellDataFor(section: indexPath.section, row: indexPath.row)
         let cellData = viewModel.cellDataFor(section: indexPath.section, row: indexPath.row)
@@ -268,6 +283,18 @@ class ChatListController: UITableViewController {
 
 
     func handleChatListUpdate() {
     func handleChatListUpdate() {
         tableView.reloadData()
         tableView.reloadData()
+
+        if let emptySearchText = viewModel.emptySearchText {
+            let text = String.localizedStringWithFormat(
+                String.localized("search_no_result_for_x"),
+                emptySearchText
+            )
+            emptySearchStateLabel.text = text
+            emptySearchStateLabel.isHidden = false
+        } else {
+            emptySearchStateLabel.text = nil
+            emptySearchStateLabel.isHidden = true
+        }
     }
     }
 
 
     func getArchiveCell(_ tableView: UITableView, title: String) -> UITableViewCell {
     func getArchiveCell(_ tableView: UITableView, title: String) -> UITableViewCell {

+ 9 - 1
deltachat-ios/ViewModel/ChatListViewModel.swift

@@ -20,7 +20,8 @@ protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
     func beginSearch()
     func beginSearch()
     func endSearch()
     func endSearch()
     func titleForHeaderIn(section: Int) -> String? // only visible on search results
     func titleForHeaderIn(section: Int) -> String? // only visible on search results
-    
+    var emptySearchText: String? { get }
+
     /// returns ROW of table
     /// returns ROW of table
     func deleteChat(chatId: Int) -> Int
     func deleteChat(chatId: Int) -> Int
     func archiveChatToggle(chatId: Int)
     func archiveChatToggle(chatId: Int)
@@ -167,6 +168,13 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         resetSearch()
         resetSearch()
     }
     }
 
 
+    var emptySearchText: String? {
+        if searchActive && numberOfSections == 0 {
+            return searchText
+        }
+        return nil
+    }
+
     func deleteChat(chatId: Int) -> Int {
     func deleteChat(chatId: Int) -> Int {
         // find index of chatId
         // find index of chatId
         let indexToDelete = Array(0..<chatList.length).filter { chatList.getChatId(index: $0) == chatId }.first
         let indexToDelete = Array(0..<chatList.length).filter { chatList.getChatId(index: $0) == chatId }.first