ContactCellViewModel.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import Foundation
  2. protocol AvatarCellViewModel {
  3. var type: CellModel { get }
  4. var title: String { get }
  5. var titleHighlightIndexes: [Int] { get }
  6. var subtitle: String { get }
  7. var subtitleHighlightIndexes: [Int] { get }
  8. }
  9. enum CellModel {
  10. case contact(ContactCellData)
  11. case chat(ChatCellData)
  12. case deaddrop(DeaddropCellData)
  13. }
  14. struct ContactCellData {
  15. let contactId: Int
  16. let chatId: Int?
  17. }
  18. struct ChatCellData {
  19. let chatId: Int
  20. let summary: DcLot
  21. let unreadMessages: Int
  22. }
  23. struct DeaddropCellData {
  24. let chatId: Int
  25. let msgId: Int
  26. let summary: DcLot
  27. }
  28. class ContactCellViewModel: AvatarCellViewModel {
  29. private let contact: DcContact
  30. var type: CellModel
  31. var title: String {
  32. return contact.displayName
  33. }
  34. var subtitle: String {
  35. return contact.email
  36. }
  37. var avartarTitle: String {
  38. return Utils.getInitials(inputName: title)
  39. }
  40. var titleHighlightIndexes: [Int]
  41. var subtitleHighlightIndexes: [Int]
  42. init(contactData: ContactCellData, titleHighlightIndexes: [Int] = [], subtitleHighlightIndexes: [Int] = []) {
  43. type = CellModel.contact(contactData)
  44. self.titleHighlightIndexes = titleHighlightIndexes
  45. self.subtitleHighlightIndexes = subtitleHighlightIndexes
  46. self.contact = DcContact(id: contactData.contactId)
  47. }
  48. }
  49. class ChatCellViewModel: AvatarCellViewModel {
  50. private let chat: DcChat
  51. private var summary: DcLot
  52. var type: CellModel
  53. var title: String {
  54. return chat.name
  55. }
  56. var subtitle: String {
  57. let result1 = summary.text1 ?? ""
  58. let result2 = summary.text2 ?? ""
  59. let result: String
  60. if !result1.isEmpty, !result2.isEmpty {
  61. result = "\(result1): \(result2)"
  62. } else {
  63. result = "\(result1)\(result2)"
  64. }
  65. return result
  66. }
  67. var titleHighlightIndexes: [Int]
  68. var subtitleHighlightIndexes: [Int]
  69. init(chatData: ChatCellData, titleHighlightIndexes: [Int] = [], subtitleHighlightIndexes: [Int] = []) {
  70. self.type = CellModel.chat(chatData)
  71. self.titleHighlightIndexes = titleHighlightIndexes
  72. self.subtitleHighlightIndexes = subtitleHighlightIndexes
  73. self.summary = chatData.summary
  74. self.chat = DcChat(id: chatData.chatId)
  75. }
  76. init(dearddropCellData cellData: DeaddropCellData) {
  77. self.type = CellModel.deaddrop(cellData)
  78. self.titleHighlightIndexes = []
  79. self.subtitleHighlightIndexes = []
  80. self.chat = DcChat(id: cellData.chatId)
  81. self.summary = cellData.summary
  82. }
  83. }
  84. extension ContactCellViewModel {
  85. static func make(contactId: Int, searchText: String?, dcContext: DcContext) -> ContactCellViewModel {
  86. let contact = DcContact(id: contactId)
  87. let nameIndexes = contact.displayName.containsExact(subSequence: searchText)
  88. let emailIndexes = contact.email.containsExact(subSequence: searchText)
  89. let chatId: Int? = dcContext.getChatIdByContactId(contactId)
  90. // contact contains searchText
  91. let viewModel = ContactCellViewModel(
  92. contactData: ContactCellData(
  93. contactId: contact.id,
  94. chatId: chatId
  95. ),
  96. titleHighlightIndexes: nameIndexes,
  97. subtitleHighlightIndexes: emailIndexes
  98. )
  99. return viewModel
  100. }
  101. }