Browse Source

manually copied code from other branch to update contact cell

nayooti 5 years ago
parent
commit
648caad4c3

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -132,6 +132,7 @@
 		AE52EA20229EB9F000C586C9 /* EditGroupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE52EA1F229EB9F000C586C9 /* EditGroupViewController.swift */; };
 		AE728F15229D5C390047565B /* PhotoPickerAlertAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE728F14229D5C390047565B /* PhotoPickerAlertAction.swift */; };
 		AE77838D23E32ED20093EABD /* ContactDetailViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE77838C23E32ED20093EABD /* ContactDetailViewModel.swift */; };
+		AE77838F23E4276D0093EABD /* ContactCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE77838E23E4276D0093EABD /* ContactCellViewModel.swift */; };
 		AE8519EA2272FDCA00ED86F0 /* DeviceContactsHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE8519E92272FDCA00ED86F0 /* DeviceContactsHandler.swift */; };
 		AE851A04227AECDE00ED86F0 /* deltachat_iosTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE851A03227AECDE00ED86F0 /* deltachat_iosTests.swift */; };
 		AE851AC5227C755A00ED86F0 /* Protocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE851AC4227C755A00ED86F0 /* Protocols.swift */; };
@@ -358,6 +359,7 @@
 		AE52EA1F229EB9F000C586C9 /* EditGroupViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditGroupViewController.swift; sourceTree = "<group>"; };
 		AE728F14229D5C390047565B /* PhotoPickerAlertAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhotoPickerAlertAction.swift; sourceTree = "<group>"; };
 		AE77838C23E32ED20093EABD /* ContactDetailViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactDetailViewModel.swift; sourceTree = "<group>"; };
+		AE77838E23E4276D0093EABD /* ContactCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactCellViewModel.swift; sourceTree = "<group>"; };
 		AE8519E92272FDCA00ED86F0 /* DeviceContactsHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceContactsHandler.swift; sourceTree = "<group>"; };
 		AE851A01227AECDE00ED86F0 /* deltachat-iosTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "deltachat-iosTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
 		AE851A03227AECDE00ED86F0 /* deltachat_iosTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = deltachat_iosTests.swift; sourceTree = "<group>"; };
@@ -687,6 +689,7 @@
 			isa = PBXGroup;
 			children = (
 				AE77838C23E32ED20093EABD /* ContactDetailViewModel.swift */,
+				AE77838E23E4276D0093EABD /* ContactCellViewModel.swift */,
 			);
 			path = ViewModel;
 			sourceTree = "<group>";
@@ -1135,6 +1138,7 @@
 				AE851AC7227C776400ED86F0 /* Location.swift in Sources */,
 				7AE0A5491FC42F65005ECB4B /* NewChatViewController.swift in Sources */,
 				305961E52346125100C80F33 /* LabelAlignment.swift in Sources */,
+				AE77838F23E4276D0093EABD /* ContactCellViewModel.swift in Sources */,
 				305961E82346125100C80F33 /* Sender.swift in Sources */,
 				305961EE2346125100C80F33 /* AvatarPosition.swift in Sources */,
 				3015634423A003BA00E9DEF4 /* AudioRecorderController.swift in Sources */,

+ 39 - 0
deltachat-ios/View/ContactCell.swift

@@ -191,4 +191,43 @@ class ContactCell: UITableViewCell {
     required init?(coder _: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
+
+
+    /*
+     this method can be deleted after searchBarContactList-branch was merged into master
+     */
+
+    func updateCell(cellViewModel: AvatarCellViewModel) {
+        // subtitle
+        emailLabel.attributedText = cellViewModel.subtitle.boldAt(indexes: cellViewModel.subtitleHighlightIndexes, fontSize: emailLabel.font.pointSize)
+
+        switch cellViewModel.type {
+        case .CHAT(let chatData):
+            let chat = DcChat(id: chatData.chatId)
+
+            // text bold if chat contains unread messages - otherwise hightlight search results if needed
+            if chatData.unreadMessages > 0 {
+                nameLabel.attributedText = NSAttributedString(string: cellViewModel.title, attributes: [ .font: UIFont.systemFont(ofSize: 16, weight: .bold) ])
+            } else {
+                nameLabel.attributedText = cellViewModel.title.boldAt(indexes: cellViewModel.titleHighlightIndexes, fontSize: nameLabel.font.pointSize)
+            }
+
+            if let img = chat.profileImage {
+                resetBackupImage()
+                setImage(img)
+            } else {
+              setBackupImage(name: chat.name, color: chat.color)
+            }
+            setVerified(isVerified: chat.isVerified)
+            setTimeLabel(chatData.summary.timestamp)
+            setUnreadMessageCounter(chatData.unreadMessages)
+            setDeliveryStatusIndicator(chatData.summary.state)
+
+        case .CONTACT(let contactData):
+            let contact = DcContact(id: contactData.contactId)
+            nameLabel.attributedText = cellViewModel.title.boldAt(indexes: cellViewModel.titleHighlightIndexes, fontSize: nameLabel.font.pointSize)
+            avatar.setName(cellViewModel.title)
+            avatar.setColor(contact.color)
+        }
+    }
 }

+ 97 - 0
deltachat-ios/ViewModel/ContactCellViewModel.swift

@@ -0,0 +1,97 @@
+//
+//  ContactCellViewModel.swift
+//  deltachat-ios
+//
+//  Created by Bastian van de Wetering on 31.01.20.
+//  Copyright © 2020 Jonas Reinsch. All rights reserved.
+//
+
+/*
+ this file and the containing classes are manually imported from searchBarContactList-branch which has not been merged into master at this time. Once it has been merged, this file can be deleted.
+ */
+
+import Foundation
+
+protocol AvatarCellViewModel {
+    var type: CellModel { get }
+    var title: String { get }
+    var titleHighlightIndexes: [Int] { get }
+    var subtitle: String { get }
+    var subtitleHighlightIndexes: [Int] { get }
+}
+
+enum CellModel {
+    case CONTACT(ContactCellData)
+    case CHAT(ChatCellData)
+}
+
+struct ContactCellData {
+    let contactId: Int
+}
+
+struct ChatCellData {
+    let chatId: Int
+    let summary: DcLot
+    let unreadMessages: Int
+}
+
+class ContactCellViewModel: AvatarCellViewModel {
+
+    private let contact: DcContact
+
+    var type: CellModel
+    var title: String {
+        return contact.displayName
+    }
+    var subtitle: String {
+        return contact.email
+    }
+
+    var avartarTitle: String {
+        return Utils.getInitials(inputName: title)
+    }
+
+    var titleHighlightIndexes: [Int]
+    var subtitleHighlightIndexes: [Int]
+
+    init(contactData: ContactCellData, titleHighlightIndexes: [Int] = [], subtitleHighlightIndexes: [Int] = []) {
+        type = CellModel.CONTACT(contactData)
+        self.titleHighlightIndexes = titleHighlightIndexes
+        self.subtitleHighlightIndexes = subtitleHighlightIndexes
+        self.contact = DcContact(id: contactData.contactId)
+    }
+}
+
+class ChatCellViewModel: AvatarCellViewModel{
+
+    private let chat: DcChat
+    private let summary: DcLot
+
+    var type: CellModel
+    var title: String {
+        return chat.name
+    }
+
+    var subtitle: String {
+        let result1 = summary.text1 ?? ""
+        let result2 = summary.text2 ?? ""
+        let result: String
+        if !result1.isEmpty, !result2.isEmpty {
+            result = "\(result1): \(result2)"
+        } else {
+            result = "\(result1)\(result2)"
+        }
+        return result
+    }
+
+    var titleHighlightIndexes: [Int]
+    var subtitleHighlightIndexes: [Int]
+
+    init(chatData: ChatCellData, titleHighlightIndexes: [Int] = [], subtitleHighlightIndexes: [Int] = []) {
+        self.type = CellModel.CHAT(chatData)
+        self.titleHighlightIndexes = titleHighlightIndexes
+        self.subtitleHighlightIndexes = subtitleHighlightIndexes
+        self.summary = chatData.summary
+        self.chat = DcChat(id: chatData.chatId)
+    }
+}