ChatListViewModel.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import UIKit
  2. import DcCore
  3. // MARK: - ChatListViewModel
  4. class ChatListViewModel: NSObject {
  5. var onChatListUpdate: VoidFunction?
  6. enum ChatListSectionType {
  7. case chats
  8. case contacts
  9. }
  10. private let dcContext: DcContext
  11. var searchActive: Bool = false
  12. // if searchfield is empty we show default chat list
  13. private var showSearchResults: Bool {
  14. return searchActive && searchText.containsCharacters()
  15. }
  16. private var chatList: DcChatlist!
  17. // for search filtering
  18. private var searchText: String = ""
  19. private var searchResultChatList: DcChatlist?
  20. private var searchResultContactIds: [Int] = []
  21. // to manage sections dynamically
  22. private var searchResultsChatsSection: ChatListSectionType = .chats
  23. private var searchResultsContactsSection: ChatListSectionType = .contacts
  24. private var searchResultSections: [ChatListSectionType] = []
  25. init(dcContext: DcContext) {
  26. self.dcContext = dcContext
  27. super.init()
  28. updateChatList(notifyListener: true)
  29. }
  30. private func updateChatList(notifyListener: Bool) {
  31. self.chatList = dcContext.getChatlist(flags: DC_GCL_ADD_ALLDONE_HINT | DC_GCL_FOR_FORWARDING | DC_GCL_NO_SPECIALS, queryString: nil, queryId: 0)
  32. if notifyListener {
  33. onChatListUpdate?()
  34. }
  35. }
  36. func getChatId(section: Int, row: Int) -> Int? {
  37. if showSearchResults {
  38. switch searchResultSections[section] {
  39. case .chats:
  40. let list: DcChatlist? = searchResultChatList
  41. return list?.getChatId(index: row)
  42. case .contacts:
  43. return searchResultContactIds[row]
  44. }
  45. }
  46. return chatList.getChatId(index: row)
  47. }
  48. var numberOfSections: Int {
  49. if showSearchResults {
  50. return searchResultSections.count
  51. }
  52. return 1
  53. }
  54. func numberOfRowsIn(section: Int) -> Int {
  55. if showSearchResults {
  56. switch searchResultSections[section] {
  57. case .chats:
  58. return searchResultChatList?.length ?? 0
  59. case .contacts:
  60. return searchResultContactIds.count
  61. }
  62. }
  63. return chatList.length
  64. }
  65. func titleForHeaderIn(section: Int) -> String? {
  66. if showSearchResults {
  67. let title: String
  68. switch searchResultSections[section] {
  69. case .chats:
  70. title = "n_chats"
  71. case .contacts:
  72. title = "n_contacts"
  73. }
  74. return String.localized(stringID: title, count: numberOfRowsIn(section: section))
  75. }
  76. return nil
  77. }
  78. func refreshData() {
  79. updateChatList(notifyListener: true)
  80. }
  81. func beginSearch() {
  82. searchActive = true
  83. }
  84. func endSearch() {
  85. searchActive = false
  86. searchText = ""
  87. resetSearch()
  88. }
  89. var emptySearchText: String? {
  90. if searchActive && numberOfSections == 0 {
  91. return searchText
  92. }
  93. return nil
  94. }
  95. // MARK: - search
  96. func updateSearchResultSections() {
  97. var sections: [ChatListSectionType] = []
  98. if let chatList = searchResultChatList, chatList.length > 0 {
  99. sections.append(searchResultsChatsSection)
  100. }
  101. if !searchResultContactIds.isEmpty {
  102. sections.append(searchResultsContactsSection)
  103. }
  104. searchResultSections = sections
  105. }
  106. func resetSearch() {
  107. searchResultChatList = nil
  108. searchResultContactIds = []
  109. updateSearchResultSections()
  110. }
  111. func filterContentForSearchText(_ searchText: String) {
  112. if !searchText.isEmpty {
  113. filterAndUpdateList(searchText: searchText)
  114. } else {
  115. // when search input field empty we show default chatList
  116. resetSearch()
  117. }
  118. onChatListUpdate?()
  119. }
  120. func filterAndUpdateList(searchText: String) {
  121. // #1 chats with searchPattern in title bar
  122. var flags: Int32 = 0
  123. flags |= DC_GCL_NO_SPECIALS
  124. searchResultChatList = dcContext.getChatlist(flags: flags, queryString: searchText, queryId: 0)
  125. // #2 contacts with searchPattern in name or in email
  126. searchResultContactIds = dcContext.getContacts(flags: DC_GCL_ADD_SELF, queryString: searchText)
  127. updateSearchResultSections()
  128. }
  129. }
  130. // MARK: UISearchResultUpdating
  131. extension ChatListViewModel: UISearchResultsUpdating {
  132. func updateSearchResults(for searchController: UISearchController) {
  133. self.searchText = searchController.searchBar.text ?? ""
  134. if let searchText = searchController.searchBar.text {
  135. filterContentForSearchText(searchText)
  136. return
  137. }
  138. }
  139. }