ChatListViewModel.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import UIKit
  2. // MARK: - ChatListViewModelProtocol
  3. protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
  4. var onChatListUpdate: VoidFunction? { get set }
  5. var isArchive: Bool { get }
  6. var numberOfSections: Int { get }
  7. func numberOfRowsIn(section: Int) -> Int
  8. func cellDataFor(section: Int, row: Int) -> AvatarCellViewModel
  9. func msgIdFor(row: Int) -> Int?
  10. func chatIdFor(section: Int, row: Int) -> Int? // needed to differentiate betweeen deaddrop / archive / default
  11. // search related
  12. var searchActive: Bool { get }
  13. func beginSearch()
  14. func endSearch()
  15. func titleForHeaderIn(section: Int) -> String? // only visible on search results
  16. /// returns ROW of table
  17. func deleteChat(chatId: Int) -> Int
  18. func archiveChatToggle(chatId: Int)
  19. func pinChatToggle(chatId: Int)
  20. func refreshData()
  21. }
  22. // MARK: - ChatListViewModel
  23. class ChatListViewModel: NSObject, ChatListViewModelProtocol {
  24. var onChatListUpdate: VoidFunction?
  25. enum ChatListSectionType {
  26. case chats
  27. case contacts
  28. case messages
  29. }
  30. var isArchive: Bool
  31. private let dcContext: DcContext
  32. var searchActive: Bool = false
  33. // if searchfield is empty we show default chat list
  34. private var showSearchResults: Bool {
  35. return searchActive && searchText.containsCharacters()
  36. }
  37. private var chatList: DcChatlist!
  38. // for search filtering
  39. private var searchText: String = ""
  40. private var searchResultChatList: DcChatlist?
  41. private var searchResultContactIds: [Int] = []
  42. private var searchResultMessageIds: [Int] = []
  43. // to manage sections dynamically
  44. private var searchResultsChatsSection: ChatListSectionType = .chats
  45. private var searchResultsContactsSection: ChatListSectionType = .contacts
  46. private var searchResultsMessagesSection: ChatListSectionType = .messages
  47. private var searchResultSections: [ChatListSectionType] = []
  48. init(dcContext: DcContext, isArchive: Bool) {
  49. self.isArchive = isArchive
  50. self.dcContext = dcContext
  51. super.init()
  52. updateChatList(notifyListener: true)
  53. }
  54. private func updateChatList(notifyListener: Bool) {
  55. var gclFlags: Int32 = 0
  56. if isArchive {
  57. gclFlags |= DC_GCL_ARCHIVED_ONLY
  58. } else if RelayHelper.sharedInstance.isForwarding() {
  59. gclFlags |= DC_GCL_FOR_FORWARDING
  60. }
  61. self.chatList = dcContext.getChatlist(flags: gclFlags, queryString: nil, queryId: 0)
  62. if notifyListener {
  63. onChatListUpdate?()
  64. }
  65. }
  66. var numberOfSections: Int {
  67. if showSearchResults {
  68. return searchResultSections.count
  69. }
  70. return 1
  71. }
  72. func numberOfRowsIn(section: Int) -> Int {
  73. if showSearchResults {
  74. switch searchResultSections[section] {
  75. case .chats:
  76. return searchResultChatList?.length ?? 0
  77. case .contacts:
  78. return searchResultContactIds.count
  79. case .messages:
  80. return searchResultMessageIds.count
  81. }
  82. }
  83. return chatList.length
  84. }
  85. func cellDataFor(section: Int, row: Int) -> AvatarCellViewModel {
  86. if showSearchResults {
  87. switch searchResultSections[section] {
  88. case .chats:
  89. return makeChatCellViewModel(index: row, searchText: searchText)
  90. case .contacts:
  91. return ContactCellViewModel.make(contactId: searchResultContactIds[row], searchText: searchText, dcContext: dcContext)
  92. case .messages:
  93. return makeMessageCellViewModel(msgId: searchResultMessageIds[row])
  94. }
  95. }
  96. return makeChatCellViewModel(index: row, searchText: "")
  97. }
  98. func titleForHeaderIn(section: Int) -> String? {
  99. if showSearchResults {
  100. let title: String
  101. switch searchResultSections[section] {
  102. case .chats:
  103. title = "n_chats"
  104. case .contacts:
  105. title = "n_contacts"
  106. case .messages:
  107. title = "n_messages"
  108. }
  109. return String.localizedStringWithFormat(NSLocalizedString(title, comment: ""), numberOfRowsIn(section: section))
  110. }
  111. return nil
  112. }
  113. func chatIdFor(section: Int, row: Int) -> Int? {
  114. let cellData = cellDataFor(section: section, row: row)
  115. switch cellData.type {
  116. case .deaddrop(let data):
  117. return data.chatId
  118. case .chat(let data):
  119. return data.chatId
  120. case .contact:
  121. return nil
  122. }
  123. }
  124. func msgIdFor(row: Int) -> Int? {
  125. if showSearchResults {
  126. return nil
  127. }
  128. return chatList.getMsgId(index: row)
  129. }
  130. func refreshData() {
  131. updateChatList(notifyListener: true)
  132. }
  133. func beginSearch() {
  134. searchActive = true
  135. }
  136. func endSearch() {
  137. searchActive = false
  138. searchText = ""
  139. resetSearch()
  140. }
  141. func deleteChat(chatId: Int) -> Int {
  142. // find index of chatId
  143. let indexToDelete = Array(0..<chatList.length).filter { chatList.getChatId(index: $0) == chatId }.first
  144. dcContext.deleteChat(chatId: chatId)
  145. updateChatList(notifyListener: false)
  146. safe_assert(indexToDelete != nil)
  147. return indexToDelete ?? -1
  148. }
  149. func archiveChatToggle(chatId: Int) {
  150. dcContext.archiveChat(chatId: chatId, archive: !self.isArchive)
  151. updateChatList(notifyListener: false)
  152. }
  153. func pinChatToggle(chatId: Int) {
  154. let chat: DcChat = dcContext.getChat(chatId: chatId)
  155. let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
  156. self.dcContext.setChatVisibility(chatId: chatId, visibility: pinned ? DC_CHAT_VISIBILITY_NORMAL : DC_CHAT_VISIBILITY_PINNED)
  157. updateChatList(notifyListener: false)
  158. }
  159. }
  160. private extension ChatListViewModel {
  161. /// MARK: - avatarCellViewModel factory
  162. func makeChatCellViewModel(index: Int, searchText: String) -> AvatarCellViewModel {
  163. let list: DcChatlist = searchResultChatList ?? chatList
  164. let chatId = list.getChatId(index: index)
  165. let summary = list.getSummary(index: index)
  166. if let msgId = msgIdFor(row: index), chatId == DC_CHAT_ID_DEADDROP {
  167. return ChatCellViewModel(dcContext: dcContext, deaddropCellData: DeaddropCellData(chatId: chatId, msgId: msgId, summary: summary))
  168. }
  169. let chat = dcContext.getChat(chatId: chatId)
  170. let unreadMessages = dcContext.getUnreadMessages(chatId: chatId)
  171. var chatTitleIndexes: [Int] = []
  172. if searchText.containsCharacters() {
  173. let chatName = chat.name
  174. chatTitleIndexes = chatName.containsExact(subSequence: searchText)
  175. }
  176. let viewModel = ChatCellViewModel(
  177. dcContext: dcContext,
  178. chatData: ChatCellData(
  179. chatId: chatId,
  180. summary: summary,
  181. unreadMessages: unreadMessages
  182. ),
  183. titleHighlightIndexes: chatTitleIndexes
  184. )
  185. return viewModel
  186. }
  187. func makeMessageCellViewModel(msgId: Int) -> AvatarCellViewModel {
  188. let msg: DcMsg = DcMsg(id: msgId)
  189. let chatId: Int = msg.chatId
  190. let chat: DcChat = dcContext.getChat(chatId: chatId)
  191. let summary: DcLot = msg.summary(chat: chat)
  192. let unreadMessages = dcContext.getUnreadMessages(chatId: chatId)
  193. let viewModel = ChatCellViewModel(
  194. dcContext: dcContext,
  195. chatData: ChatCellData(
  196. chatId: chatId,
  197. summary: summary,
  198. unreadMessages: unreadMessages
  199. )
  200. )
  201. let subtitle = viewModel.subtitle
  202. viewModel.subtitleHighlightIndexes = subtitle.containsExact(subSequence: searchText)
  203. return viewModel
  204. }
  205. // MARK: - search
  206. func updateSearchResultSections() {
  207. var sections: [ChatListSectionType] = []
  208. if let chatList = searchResultChatList, chatList.length > 0 {
  209. sections.append(searchResultsChatsSection)
  210. }
  211. if !searchResultContactIds.isEmpty {
  212. sections.append(searchResultsContactsSection)
  213. }
  214. if !searchResultMessageIds.isEmpty {
  215. sections.append(searchResultsMessagesSection)
  216. }
  217. searchResultSections = sections
  218. }
  219. func resetSearch() {
  220. searchResultChatList = nil
  221. searchResultContactIds = []
  222. searchResultMessageIds = []
  223. updateSearchResultSections()
  224. }
  225. func filterContentForSearchText(_ searchText: String) {
  226. if !searchText.isEmpty {
  227. filterAndUpdateList(searchText: searchText)
  228. } else {
  229. // when search input field empty we show default chatList
  230. resetSearch()
  231. }
  232. onChatListUpdate?()
  233. }
  234. func filterAndUpdateList(searchText: String) {
  235. // #1 chats with searchPattern in title bar
  236. var flags: Int32 = 0
  237. flags |= DC_GCL_NO_SPECIALS
  238. searchResultChatList = dcContext.getChatlist(flags: flags, queryString: searchText, queryId: 0)
  239. // #2 contacts with searchPattern in name or in email
  240. searchResultContactIds = dcContext.getContacts(flags: DC_GCL_ADD_SELF, queryString: searchText)
  241. // #3 messages with searchPattern (filtered by dc_core)
  242. searchResultMessageIds = dcContext.searchMessages(searchText: searchText)
  243. updateSearchResultSections()
  244. }
  245. }
  246. // MARK: UISearchResultUpdating
  247. extension ChatListViewModel: UISearchResultsUpdating {
  248. func updateSearchResults(for searchController: UISearchController) {
  249. self.searchText = searchController.searchBar.text ?? ""
  250. if let searchText = searchController.searchBar.text {
  251. filterContentForSearchText(searchText)
  252. return
  253. }
  254. }
  255. }