Эх сурвалжийг харах

cleaning and renaming to improve readabilty

nayooti 5 жил өмнө
parent
commit
a8327765a6

+ 21 - 13
deltachat-ios/Controller/ChatListController.swift

@@ -8,6 +8,11 @@ class ChatListController: UITableViewController {
     private let deadDropCellReuseIdentifier = "deaddrop_cell"
     private let contactCellReuseIdentifier = "contact_cell"
 
+    private var msgChangedObserver: Any?
+    private var incomingMsgObserver: Any?
+    private var viewChatObserver: Any?
+    private var deleteChatObserver: Any?
+
     private lazy var searchController: UISearchController = {
         let searchController = UISearchController(searchResultsController: nil)
         searchController.searchResultsUpdater = viewModel
@@ -17,11 +22,6 @@ class ChatListController: UITableViewController {
         return searchController
     }()
 
-    private var msgChangedObserver: Any?
-    private var incomingMsgObserver: Any?
-    private var viewChatObserver: Any?
-    private var deleteChatObserver: Any?
-
     private lazy var newButton: UIBarButtonItem = {
         let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.compose, target: self, action: #selector(didPressNewChat))
         button.tintColor = DcColors.primary
@@ -233,6 +233,7 @@ class ChatListController: UITableViewController {
     override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
 
         if viewModel.searchActive {
+            // no swipe actions during search
             return []
         }
 
@@ -248,7 +249,7 @@ class ChatListController: UITableViewController {
         let archiveActionTitle: String = String.localized(viewModel.isArchive ? "unarchive" : "archive")
 
         let archiveAction = UITableViewRowAction(style: .destructive, title: archiveActionTitle) { [unowned self] _, _ in
-            self.viewModel.archiveChat(chatId: chatId)
+            self.viewModel.archiveChatToggle(chatId: chatId)
             self.updateArchivedCell()
         }
         archiveAction.backgroundColor = UIColor.lightGray
@@ -256,7 +257,7 @@ class ChatListController: UITableViewController {
         let chat = DcChat(id: chatId)
         let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
         let pinAction = UITableViewRowAction(style: .destructive, title: String.localized(pinned ? "unpin" : "pin")) { [unowned self] _, _ in
-            self.viewModel.pinChat(chatId: chat.id)
+            self.viewModel.pinChatToggle(chatId: chat.id)
         }
         pinAction.backgroundColor = UIColor.systemGreen
 
@@ -269,7 +270,6 @@ class ChatListController: UITableViewController {
     }
 
     // MARK: updates
-
     private func updateTitle() {
         if RelayHelper.sharedInstance.isForwarding() {
             title = String.localized("forward_to")
@@ -377,8 +377,17 @@ class ChatListController: UITableViewController {
         self.present(alert, animated: true, completion: nil)
     }
 
-    private func deleteChat(chatId: Int, animated: Bool) {
+    // MARK: - actions
+    @objc func didPressNewChat() {
+        coordinator?.showNewChatController()
+    }
 
+    @objc func cancelButtonPressed() {
+        RelayHelper.sharedInstance.cancel()
+        updateTitle()
+    }
+
+    private func deleteChat(chatId: Int, animated: Bool) {
         if !animated {
             _ = viewModel.deleteChat(chatId: chatId)
             viewModel.refreshData()
@@ -390,15 +399,14 @@ class ChatListController: UITableViewController {
     }
 }
 
-
-// MARK
+// MARK: - uisearchbardelegate
 extension ChatListController: UISearchBarDelegate {
     func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
-        viewModel.beginFiltering()
+        viewModel.beginSearch()
         return true
     }
 
     func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
-        viewModel.endFiltering()
+        viewModel.endSearch()
     }
 }

+ 21 - 26
deltachat-ios/ViewModel/ChatListViewModel.swift

@@ -15,14 +15,14 @@ protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
 
     // search related
     var searchActive: Bool { get }
-    func beginFiltering()
-    func endFiltering()
-    func titleForHeaderIn(section: Int) -> String?
+    func beginSearch()
+    func endSearch()
+    func titleForHeaderIn(section: Int) -> String? // only visible on search results
 
     /// returns ROW of table
     func deleteChat(chatId: Int) -> Int
-    func archiveChat(chatId: Int)
-    func pinChat(chatId: Int)
+    func archiveChatToggle(chatId: Int)
+    func pinChatToggle(chatId: Int)
     func refreshData()
 
     var numberOfArchivedChats: Int { get }
@@ -62,6 +62,7 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
     var searchActive: Bool = false
     private var searchTextEmpty: Bool = true
 
+    // if searchfield is empty we show default chat list
     private var showSearchResults: Bool {
         return searchActive && !searchTextEmpty
     }
@@ -69,13 +70,13 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
     private var chatList: DcChatlist!
 
     // for search filtering
-    var filteredChats: ChatListSection = ChatListSection(type: .chats)
-    var filteredContacts: ChatListSection = ChatListSection(type: .contacts)
-    var filteredMessages: ChatListSection = ChatListSection(type: .messages)
+    private var searchResultsChats: ChatListSection = ChatListSection(type: .chats)
+    private var searchResultsContacts: ChatListSection = ChatListSection(type: .contacts)
+    private var searchResultsMessages: ChatListSection = ChatListSection(type: .messages)
 
     private var searchResultSections: [ChatListSection] {
-        return [filteredChats, filteredContacts, filteredMessages]
-            .filter { !$0.cellData.isEmpty } //
+        return [searchResultsChats, searchResultsContacts, searchResultsMessages]
+            .filter { !$0.cellData.isEmpty }
     }
 
     init(dcContext: DcContext, isArchive: Bool) {
@@ -157,17 +158,16 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         updateChatList(notifyListener: true)
     }
 
-    func beginFiltering() {
+    func beginSearch() {
         searchActive = true
     }
 
-    func endFiltering() {
+    func endSearch() {
         searchTextEmpty = true
         searchActive = false
     }
 
     func deleteChat(chatId: Int) -> Int {
-
         // find index of chatId
         let indexToDelete = Array(0..<chatList.length).filter { chatList.getChatId(index: $0) == chatId }.first
         dcContext.deleteChat(chatId: chatId)
@@ -176,12 +176,12 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         return indexToDelete ?? -1
     }
 
-    func archiveChat(chatId: Int) {
+    func archiveChatToggle(chatId: Int) {
         dcContext.archiveChat(chatId: chatId, archive: !self.isArchive)
         updateChatList(notifyListener: false)
     }
 
-    func pinChat(chatId: Int) {
+    func pinChatToggle(chatId: Int) {
         let chat = DcChat(id: chatId)
         let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
         self.dcContext.setChatVisibility(chatId: chatId, visibility: pinned ? DC_CHAT_VISIBILITY_NORMAL : DC_CHAT_VISIBILITY_PINNED)
@@ -192,7 +192,6 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         let chatList = dcContext.getChatlist(flags: DC_GCL_ARCHIVED_ONLY, queryString: nil, queryId: 0)
         return chatList.length
     }
-
 }
 
 // MARK: UISearchResultUpdating
@@ -241,7 +240,7 @@ extension ChatListViewModel: UISearchResultsUpdating {
             )
             filteredChatCellViewModels.append(viewModel)
         }
-        filteredChats.cellData = filteredChatCellViewModels
+        searchResultsChats.cellData = filteredChatCellViewModels
 
         // #2 contacts with searchPattern in name or in email
         var filteredContactCellViewModels: [ContactCellViewModel] = []
@@ -263,7 +262,7 @@ extension ChatListViewModel: UISearchResultsUpdating {
             )
             filteredContactCellViewModels.append(viewModel)
         }
-        filteredContacts.cellData = filteredContactCellViewModels
+        searchResultsContacts.cellData = filteredContactCellViewModels
 
         // #3 messages with searchPattern (filtered by dc_core)
         let msgIds = dcContext.searchMessages(searchText: searchText)
@@ -289,16 +288,12 @@ extension ChatListViewModel: UISearchResultsUpdating {
 
             filteredMessageCellViewModels.append(viewModel)
         }
-        filteredMessages.cellData = filteredMessageCellViewModels
+        searchResultsMessages.cellData = filteredMessageCellViewModels
     }
 
-
     private func resetSearch() {
-        filteredChats.cellData = []
-        filteredContacts.cellData = []
-        filteredMessages.cellData = []
+        searchResultsChats.cellData = []
+        searchResultsContacts.cellData = []
+        searchResultsMessages.cellData = []
     }
-
-
 }
-