Browse Source

put viewModel in separate file

nayooti 5 years ago
parent
commit
2731048114

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

@@ -119,6 +119,7 @@
 		7AE0A5491FC42F65005ECB4B /* NewChatViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AE0A5481FC42F65005ECB4B /* NewChatViewController.swift */; };
 		8B6D425BC604F7C43B65D436 /* Pods_deltachat_ios.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */; };
 		AE0D26FD1FB1FE88002FAFCE /* ChatListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */; };
+		AE10C9CD23D0D2E000428F60 /* AvatarCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE10C9CC23D0D2E000428F60 /* AvatarCellViewModel.swift */; };
 		AE18F294228C602A0007B1BE /* SecuritySettingsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */; };
 		AE25F09022807AD800CDEA66 /* AvatarSelectionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE25F08F22807AD800CDEA66 /* AvatarSelectionCell.swift */; };
 		AE38B31822672DFC00EC37A1 /* ActionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE38B31722672DFC00EC37A1 /* ActionCell.swift */; };
@@ -342,6 +343,7 @@
 		8DE110C607A0E4485C43B5FA /* Pods-deltachat-ios.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-ios.debug.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-ios/Pods-deltachat-ios.debug.xcconfig"; sourceTree = "<group>"; };
 		A8615D4600859851E53CAA9C /* Pods-deltachat-ios.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-ios.release.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-ios/Pods-deltachat-ios.release.xcconfig"; sourceTree = "<group>"; };
 		AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = ChatListController.swift; sourceTree = "<group>"; tabWidth = 4; };
+		AE10C9CC23D0D2E000428F60 /* AvatarCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarCellViewModel.swift; sourceTree = "<group>"; };
 		AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecuritySettingsController.swift; sourceTree = "<group>"; };
 		AE25F08F22807AD800CDEA66 /* AvatarSelectionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarSelectionCell.swift; sourceTree = "<group>"; };
 		AE38B31722672DFC00EC37A1 /* ActionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionCell.swift; sourceTree = "<group>"; };
@@ -766,6 +768,7 @@
 			isa = PBXGroup;
 			children = (
 				AEB7068C23CC9C3800504AB3 /* ChatListViewModel.swift */,
+				AE10C9CC23D0D2E000428F60 /* AvatarCellViewModel.swift */,
 			);
 			path = ViewModel;
 			sourceTree = "<group>";
@@ -1113,6 +1116,7 @@
 				305FE03623A81B4C0053BE90 /* PaddingLabel.swift in Sources */,
 				AEACE2DD1FB323CA00DCDD78 /* ChatViewController.swift in Sources */,
 				AEE6EC412282DF5700EDC689 /* MailboxViewController.swift in Sources */,
+				AE10C9CD23D0D2E000428F60 /* AvatarCellViewModel.swift in Sources */,
 				AEE6EC482283045D00EDC689 /* EditSettingsController.swift in Sources */,
 				305961DF2346125100C80F33 /* MessageCellDelegate.swift in Sources */,
 				302B84CE2397F6CD001C261F /* URL+Extension.swift in Sources */,

+ 92 - 0
deltachat-ios/ViewModel/AvatarCellViewModel.swift

@@ -0,0 +1,92 @@
+//
+//  AvatarCellViewModel.swift
+//  deltachat-ios
+//
+//  Created by Bastian van de Wetering on 16.01.20.
+//  Copyright © 2020 Jonas Reinsch. All rights reserved.
+//
+
+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)
+    }
+}

+ 0 - 85
deltachat-ios/ViewModel/ChatListViewModel.swift

@@ -1,31 +1,7 @@
 import UIKit
 
-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
-}
-
 protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
     var numberOfSections: Int { get }
-
     var onChatListUpdate: VoidFunction? { get set }
     var showArchive: Bool { get }
     var archivedChatsCount: Int { get }
@@ -36,71 +12,10 @@ protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
     func getCellViewModelFor(indexPath: IndexPath) -> AvatarCellViewModel
     func beginFiltering()
     func endFiltering()
-
     func archieveChat(chatId: Int)
     func deleteChat(chatId: 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)
-    }
-}
-
 class ChatListViewModel: NSObject, ChatListViewModelProtocol {
     func titleForHeaderIn(section: Int) -> String? {
         if searchActive {