Ver código fonte

Chatlist now uses coordinator - merged tableviewdelegate + datasource into chatlist extension

Bastian van de Wetering 6 anos atrás
pai
commit
76b5d09fa5

+ 9 - 56
deltachat-ios/Controller/ChatListController.swift

@@ -14,16 +14,12 @@ class ChatListController: UIViewController {
 
   lazy var chatTable: UITableView = {
     let chatTable = UITableView()
-    chatTable.dataSource = chatTableDataSource
-    chatTableDelegate.chatPresenter = self
-    chatTable.delegate = chatTableDelegate
+    chatTable.dataSource = self
+    chatTable.delegate = self
     chatTable.rowHeight = 80
     return chatTable
   }()
 
-  let chatTableDataSource = ChatTableDataSource()
-  let chatTableDelegate = ChatTableDelegate()
-
   var msgChangedObserver: Any?
   var incomingMsgObserver: Any?
   var viewChatObserver: Any?
@@ -66,8 +62,8 @@ class ChatListController: UIViewController {
     viewChatObserver = nc.addObserver(forName: dcNotificationViewChat, object: nil, queue: nil) {
       notification in
       if let chatId = notification.userInfo?["chat_id"] as? Int {
-        self.displayChatForId(chatId: chatId)
-      }
+				self.coordinator?.showChat(chatId: chatId)
+	    }
     }
   }
 
@@ -109,12 +105,6 @@ class ChatListController: UIViewController {
 
   @objc func didPressNewChat() {
 		coordinator?.showNewChatController()
-		/*
-    let ncv = NewChatViewController()
-    ncv.chatDisplayer = self
-    let nav = UINavigationController(rootViewController: ncv)
-    present(nav, animated: true, completion: nil)
-		*/
 	}
 
   func getChatList() {
@@ -123,42 +113,11 @@ class ChatListController: UIViewController {
     }
     // ownership of chatlistPointer transferred here to ChatList object
     chatList = MRChatList(chatListPointer: chatlistPointer)
-
-    chatTableDataSource.chatList = chatList
     chatTable.reloadData()
   }
 }
 
-extension ChatListController: ChatPresenter {
-  func displayChat(index: Int) {
-    guard let chatList = self.chatList else {
-      fatalError("chatList was nil in ChatPresenter extension")
-    }
-
-    let chatId = chatList.getChatId(index: index)
-    let chatVC = ChatViewController(chatId: chatId)
-
-    chatVC.hidesBottomBarWhenPushed = true
-    navigationController?.pushViewController(chatVC, animated: true)
-  }
-}
-
-extension ChatListController: ChatDisplayer {
-  func displayNewChat(contactId: Int) {
-    let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
-    displayChatForId(chatId: Int(chatId))
-  }
-
-  func displayChatForId(chatId: Int) {
-    let chatVC = ChatViewController(chatId: chatId)
-
-    chatVC.hidesBottomBarWhenPushed = true
-    navigationController?.pushViewController(chatVC, animated: true)
-  }
-}
-
-class ChatTableDataSource: NSObject, UITableViewDataSource {
-  weak var chatList: MRChatList?
+extension ChatListController: UITableViewDataSource, UITableViewDelegate {
 
   func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
     guard let chatList = self.chatList else {
@@ -205,17 +164,11 @@ class ChatTableDataSource: NSObject, UITableViewDataSource {
     cell.emailLabel.text = result
     return cell
   }
-}
-
-protocol ChatPresenter: class {
-  func displayChat(index: Int)
-}
-
-class ChatTableDelegate: NSObject, UITableViewDelegate {
-  weak var chatPresenter: ChatPresenter?
 
-  func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
+	func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
     let row = indexPath.row
-    chatPresenter?.displayChat(index: row)
+		if let chatId = chatList?.getChatId(index: row) {
+			coordinator?.showChat(chatId: chatId)
+		}
   }
 }

+ 0 - 2
deltachat-ios/Controller/NewChatViewController.swift

@@ -10,8 +10,6 @@ import ALCameraViewController
 import Contacts
 import UIKit
 
-
-
 class NewChatViewController: UITableViewController {
 	weak var coordinator: NewChatCoordinator?
 

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

@@ -152,6 +152,13 @@ class ChatListCoordinator: Coordinator {
 		newChatVC.hidesBottomBarWhenPushed = true
 		navigationController.pushViewController(newChatVC, animated: true)
 	}
+
+	func showChat(chatId: Int) {
+		let chatVC = ChatViewController(chatId: chatId)
+		chatVC.hidesBottomBarWhenPushed = true
+		navigationController.pushViewController(chatVC, animated: true)
+
+	}
 }
 
 class SettingsCoordinator: Coordinator {