Przeglądaj źródła

add header as cell but not happy - try to add header as tableheader

nayooti 5 lat temu
rodzic
commit
52773f87c4

+ 45 - 26
deltachat-ios/Controller/ContactDetailViewController.swift

@@ -5,6 +5,18 @@ class ContactDetailViewController: UITableViewController {
     weak var coordinator: ContactDetailCoordinatorProtocol?
     private let viewModel: ContactDetailViewModelProtocol
 
+    private lazy var headerCell: ContactDetailHeader = {
+        let cell = ContactDetailHeader()
+        cell.updateDetails(title: viewModel.contact.displayName, subtitle: viewModel.contact.email)
+        if let img = viewModel.contact.profileImage {
+            cell.setImage(img)
+        } else {
+            cell.setBackupImage(name: viewModel.contact.displayName, color: viewModel.contact.color)
+        }
+        cell.setVerified(isVerified: viewModel.contact.isVerified)
+        return cell
+    }()
+
     private lazy var startChatCell: ActionCell = {
         let cell = ActionCell()
         cell.actionColor = SystemColor.blue.uiColor
@@ -47,6 +59,9 @@ class ContactDetailViewController: UITableViewController {
 
     // MARK: - setup and configuration
     private func configureTableView() {
+        tableView.estimatedSectionHeaderHeight = 20 // needed to trigger heighForHeaderInSection
+        tableView.sectionHeaderHeight = UITableView.automaticDimension
+        // tableView.estimatedSectionFooterHeight = 0 // needed to trigger heighForFooterInSection
         tableView.register(ActionCell.self, forCellReuseIdentifier: ActionCell.reuseIdentifier)
         tableView.register(ContactCell.self, forCellReuseIdentifier: ContactCell.reuseIdentifier)
     }
@@ -63,6 +78,8 @@ class ContactDetailViewController: UITableViewController {
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cellType = viewModel.typeFor(section: indexPath.section)
         switch cellType {
+        case .contact:
+            return headerCell
         case .blockContact:
             return blockContactCell
         case .startChat:
@@ -74,7 +91,6 @@ class ContactDetailViewController: UITableViewController {
             }
         }
         return UITableViewCell() // should never get here
-
     }
 
     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
@@ -88,44 +104,48 @@ class ContactDetailViewController: UITableViewController {
         case .sharedChats:
             let chatId = viewModel.getSharedChatIdAt(indexPath: indexPath)
             coordinator?.showChat(chatId: chatId)
+        case .contact:
+            break
         }
     }
 
-    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
-        if section == 0 {
-            return ContactDetailHeader.cellHeight
-        }
-        return 20
-    }
-
     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
         let type = viewModel.typeFor(section: indexPath.section)
         switch type {
         case .blockContact, .startChat:
             return 44
-        case .sharedChats:
+        case .sharedChats, .contact:
             return ContactCell.cellHeight
         }
     }
 
-    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
-        return viewModel.titleFor(section: section)
+    /*
+    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
+        let type = viewModel.typeFor(section: section)
+        switch type {
+        case .startChat:
+            return CGFloat.leastNonzeroMagnitude
+        default:
+            return CGFloat.leastNonzeroMagnitude
+        }
     }
-    
-    override func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
-        if section == 0 {
-            let header = ContactDetailHeader()
-            let displayName = viewModel.contact.displayName
-            header.updateDetails(title: displayName, subtitle: viewModel.contact.email)
-            if let img = viewModel.contact.profileImage {
-                header.setImage(img)
-            } else {
-                header.setBackupImage(name: displayName, color: viewModel.contact.color)
-            }
-            header.setVerified(isVerified: viewModel.contact.isVerified)
-            return header
+     */
+
+    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
+        let type = viewModel.typeFor(section: section)
+        switch type {
+        case .contact:
+            return 0
+//        case .blockContact:
+//            return 20
+        default:
+            return 20
         }
-        return nil
+    }
+
+
+    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
+        return viewModel.titleFor(section: section)
     }
 
     // MARK: -actions
@@ -145,7 +165,6 @@ class ContactDetailViewController: UITableViewController {
         present(alert, animated: true, completion: nil)
     }
 
-
     private func toggleBlockContact() {
         if viewModel.contact.isBlocked {
             let alert = UIAlertController(title: String.localized("ask_unblock_contact"), message: nil, preferredStyle: .safeActionSheet)

+ 3 - 30
deltachat-ios/ViewModel/ContactDetailViewModel.swift

@@ -6,7 +6,6 @@ protocol ContactDetailViewModelProtocol {
     var numberOfSections: Int { get }
     func numberOfRowsInSection(_ : Int) -> Int
     func typeFor(section: Int) -> ContactDetailViewModel.SectionType
-    func update(actionCell: ActionCell, section: Int)
     func update(sharedChatCell: ContactCell, row index: Int)
     func getSharedChatIdAt(indexPath: IndexPath) -> Int
     func titleFor(section: Int) -> String?
@@ -28,7 +27,7 @@ class ContactDetailViewModel: ContactDetailViewModelProtocol {
     private let sharedChats: DcChatlist
     private let startChatOption: Bool
 
-    private var sections: [SectionType] = []
+    private var sections: [SectionType] = [.contact]
 
     init(contactId: Int, startChatOption: Bool, context: DcContext) {
         self.context = context
@@ -61,38 +60,12 @@ class ContactDetailViewModel: ContactDetailViewModelProtocol {
         }
     }
 
-    func update(actionCell: ActionCell, section: Int) {
-        let type = sections[section]
-        switch type {
-        case .startChat:
-            update(startChatCell: actionCell)
-        case .blockContact:
-            update(blockContactCell: actionCell)
-        case .sharedChats:
-            break
-        }
-
-    }
-
     func getSharedChatIdAt(indexPath: IndexPath) -> Int {
         let index = indexPath.row
-        assert(sections[indexPath.section] == .sharedChats)
+        // assert(sections[indexPath.section] == .sharedChats)
         return sharedChats.getChatId(index: index)
     }
 
-    private func update(startChatCell cell: ActionCell) {
-        cell.actionColor = SystemColor.blue.uiColor
-        cell.actionTitle = String.localized("menu_new_chat")
-        cell.selectionStyle = .none
-    }
-
-    private func update(blockContactCell cell: ActionCell) {
-        let cell = ActionCell()
-        cell.actionTitle = contact.isBlocked ? String.localized("menu_unblock_contact") : String.localized("menu_block_contact")
-        cell.actionColor = contact.isBlocked ? SystemColor.blue.uiColor : UIColor.red
-        cell.selectionStyle = .none
-    }
-
     func update(sharedChatCell cell: ContactCell, row index: Int) {
         let chatId = sharedChats.getChatId(index: index)
         let summary = sharedChats.getSummary(index: index)
@@ -105,7 +78,7 @@ class ContactDetailViewModel: ContactDetailViewModelProtocol {
 
     func titleFor(section: Int) -> String? {
         if sections[section] == .sharedChats {
-            String.localized("menu_shared_chats")
+           return String.localized("menu_shared_chats")
         }
         return nil
       }