mirror of
https://gitlab.linphone.org/BC/public/linphone-iphone.git
synced 2026-01-17 02:58:07 +00:00
Debounce ContactLoaded notification to reduce redundant calls
This commit is contained in:
parent
a7e1a8b210
commit
914177000b
5 changed files with 44 additions and 17 deletions
|
|
@ -284,6 +284,7 @@ struct ConversationForwardMessageFragment: View {
|
|||
|
||||
Text(String(contactsManager.lastSearchSuggestions[index].address!.asStringUriOnly().dropFirst(4)))
|
||||
.default_text_style(styleSize: 16)
|
||||
.lineLimit(1)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.foregroundStyle(Color.orangeMain500)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -276,6 +276,7 @@ struct StartConversationFragment: View {
|
|||
|
||||
Text(String(contactsManager.lastSearchSuggestions[index].address!.asStringUriOnly().dropFirst(4)))
|
||||
.default_text_style(styleSize: 16)
|
||||
.lineLimit(1)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.foregroundStyle(Color.orangeMain500)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -410,6 +410,7 @@ struct StartCallFragment: View {
|
|||
|
||||
Text(String(contactsManager.lastSearchSuggestions[index].address!.asStringUriOnly().dropFirst(4)))
|
||||
.default_text_style(styleSize: 16)
|
||||
.lineLimit(1)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.foregroundStyle(Color.orangeMain500)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -294,6 +294,7 @@ struct AddParticipantsFragment: View {
|
|||
|
||||
Text(String(contactsManager.lastSearchSuggestions[index].address!.asStringUriOnly().dropFirst(4)))
|
||||
.default_text_style(styleSize: 16)
|
||||
.lineLimit(1)
|
||||
.lineLimit(1)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.foregroundStyle(Color.orangeMain500)
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ final class MagicSearchSingleton: ObservableObject {
|
|||
private var domainDefaultAccount = ""
|
||||
|
||||
var searchDelegate: MagicSearchDelegate?
|
||||
|
||||
private var contactLoadedDebounceWorkItem: DispatchWorkItem?
|
||||
|
||||
func destroyMagicSearch() {
|
||||
magicSearch = nil
|
||||
|
|
@ -59,13 +61,17 @@ final class MagicSearchSingleton: ObservableObject {
|
|||
var lastSearchFriend: [SearchResult] = []
|
||||
var lastSearchSuggestions: [SearchResult] = []
|
||||
|
||||
magicSearch.lastSearch.forEach { searchResult in
|
||||
if searchResult.friend != nil {
|
||||
lastSearchFriend.append(searchResult)
|
||||
} else {
|
||||
lastSearchSuggestions.append(searchResult)
|
||||
}
|
||||
}
|
||||
magicSearch.lastSearch.forEach { searchResult in
|
||||
if let friend = searchResult.friend,
|
||||
let address = searchResult.address,
|
||||
!lastSearchFriend.contains(where: { $0.address?.weakEqual(address2: address) ?? false }) {
|
||||
|
||||
lastSearchFriend.append(searchResult)
|
||||
} else {
|
||||
lastSearchSuggestions.append(searchResult)
|
||||
}
|
||||
}
|
||||
|
||||
lastSearchSuggestions.sort(by: {
|
||||
$0.address!.asStringUriOnly() < $1.address!.asStringUriOnly()
|
||||
})
|
||||
|
|
@ -99,20 +105,37 @@ final class MagicSearchSingleton: ObservableObject {
|
|||
self.contactsManager.avatarListModel.forEach { contactAvatarModel in
|
||||
contactAvatarModel.removeFriendDelegate()
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.contactsManager.lastSearch = sortedLastSearch
|
||||
self.contactsManager.lastSearchSuggestions = lastSearchSuggestions
|
||||
|
||||
self.contactsManager.avatarListModel.removeAll()
|
||||
self.contactsManager.avatarListModel += addedAvatarListModel
|
||||
|
||||
NotificationCenter.default.post(name: NSNotification.Name("ContactLoaded"), object: nil)
|
||||
}
|
||||
|
||||
self.updateContacts(sortedLastSearch: sortedLastSearch, lastSearchSuggestions: lastSearchSuggestions, addedAvatarListModel: addedAvatarListModel)
|
||||
})
|
||||
self.magicSearch.addDelegate(delegate: self.searchDelegate!)
|
||||
}
|
||||
}
|
||||
|
||||
func updateContacts(
|
||||
sortedLastSearch: [SearchResult],
|
||||
lastSearchSuggestions: [SearchResult],
|
||||
addedAvatarListModel: [ContactAvatarModel]
|
||||
) {
|
||||
DispatchQueue.main.async {
|
||||
self.contactsManager.lastSearch = sortedLastSearch
|
||||
self.contactsManager.lastSearchSuggestions = lastSearchSuggestions
|
||||
|
||||
self.contactsManager.avatarListModel.removeAll()
|
||||
self.contactsManager.avatarListModel += addedAvatarListModel
|
||||
|
||||
// Cancel previous debounce task
|
||||
self.contactLoadedDebounceWorkItem?.cancel()
|
||||
|
||||
// Schedule new debounce task
|
||||
let workItem = DispatchWorkItem {
|
||||
NotificationCenter.default.post(name: NSNotification.Name("ContactLoaded"), object: nil)
|
||||
}
|
||||
|
||||
self.contactLoadedDebounceWorkItem = workItem
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: workItem)
|
||||
}
|
||||
}
|
||||
|
||||
func searchForContacts() {
|
||||
coreContext.doOnCoreQueue { _ in
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue