Removed code now done in SDK

This commit is contained in:
Sylvain Berfini 2022-04-08 10:37:24 +02:00
parent de20faaaf2
commit d699ba423f

View file

@ -25,7 +25,6 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import java.util.* import java.util.*
import kotlin.collections.HashMap
import kotlinx.coroutines.* import kotlinx.coroutines.*
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.LinphoneApplication.Companion.corePreferences
@ -33,7 +32,6 @@ import org.linphone.contact.ContactsUpdatedListenerStub
import org.linphone.core.* import org.linphone.core.*
import org.linphone.core.tools.Log import org.linphone.core.tools.Log
import org.linphone.utils.Event import org.linphone.utils.Event
import org.linphone.utils.LinphoneUtils
class ContactsListViewModel : ViewModel() { class ContactsListViewModel : ViewModel() {
val sipContactsSelected = MutableLiveData<Boolean>() val sipContactsSelected = MutableLiveData<Boolean>()
@ -60,7 +58,7 @@ class ContactsListViewModel : ViewModel() {
private val magicSearchListener = object : MagicSearchListenerStub() { private val magicSearchListener = object : MagicSearchListenerStub() {
override fun onSearchResultsReceived(magicSearch: MagicSearch) { override fun onSearchResultsReceived(magicSearch: MagicSearch) {
Log.i("[Contacts Loader] Magic search contacts available") Log.i("[Contacts] Magic search contacts available")
searchResultsPending = false searchResultsPending = false
processMagicSearchResults(magicSearch.lastSearch) processMagicSearchResults(magicSearch.lastSearch)
// Use coreContext.contactsManager.fetchInProgress instead of false in case contacts are still being loaded // Use coreContext.contactsManager.fetchInProgress instead of false in case contacts are still being loaded
@ -103,10 +101,11 @@ class ContactsListViewModel : ViewModel() {
val domain = if (sipContactsSelected.value == true) coreContext.core.defaultAccount?.params?.domain ?: "" else "" val domain = if (sipContactsSelected.value == true) coreContext.core.defaultAccount?.params?.domain ?: "" else ""
val filter = MagicSearchSource.Friends.toInt() or MagicSearchSource.LdapServers.toInt() val filter = MagicSearchSource.Friends.toInt() or MagicSearchSource.LdapServers.toInt()
val aggregation = MagicSearchAggregation.Friend
searchResultsPending = true searchResultsPending = true
fastFetchJob?.cancel() fastFetchJob?.cancel()
Log.i("[Contacts Loader] Asking Magic search for contacts matching filter [$filterValue], domain [$domain] and in sources [$filter]") Log.i("[Contacts] Asking Magic search for contacts matching filter [$filterValue], domain [$domain] and in sources [$filter]")
coreContext.contactsManager.magicSearch.getContactsAsync(filterValue, domain, filter) coreContext.contactsManager.magicSearch.getContactsListAsync(filterValue, domain, filter, aggregation)
val spinnerDelay = corePreferences.delayBeforeShowingContactsSearchSpinner.toLong() val spinnerDelay = corePreferences.delayBeforeShowingContactsSearchSpinner.toLong()
fastFetchJob = viewModelScope.launch { fastFetchJob = viewModelScope.launch {
@ -122,41 +121,32 @@ class ContactsListViewModel : ViewModel() {
} }
private fun processMagicSearchResults(results: Array<SearchResult>) { private fun processMagicSearchResults(results: Array<SearchResult>) {
Log.i("[Contacts Loader] Processing ${results.size} results") Log.i("[Contacts] Processing ${results.size} results")
contactsList.value.orEmpty().forEach(ContactViewModel::destroy) contactsList.value.orEmpty().forEach(ContactViewModel::destroy)
viewModelScope.launch { viewModelScope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val list = arrayListOf<ContactViewModel>() val list = arrayListOf<ContactViewModel>()
val viewModels = HashMap<String, ContactViewModel>()
for (result in results) { for (result in results) {
val friend = result.friend val friend = result.friend
val name = friend?.name ?: LinphoneUtils.getDisplayName(result.address)
val found = viewModels[name]
if (found != null && friend != null) {
continue
}
val viewModel = if (friend != null) { val viewModel = if (friend != null) {
ContactViewModel(friend, true) ContactViewModel(friend, true)
} else { } else {
Log.w("[Contacts] SearchResult [$result] has no Friend!")
val fakeFriend = coreContext.contactsManager.createFriendFromSearchResult(result) val fakeFriend = coreContext.contactsManager.createFriendFromSearchResult(result)
ContactViewModel(fakeFriend, true) ContactViewModel(fakeFriend, true)
} }
list.add(viewModel) list.add(viewModel)
if (found == null) {
viewModels[name] = viewModel
}
} }
contactsList.postValue(list) contactsList.postValue(list)
viewModels.clear()
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
Log.i("[Contacts Loader] Processed ${results.size} results") Log.i("[Contacts] Processed ${results.size} results")
} }
} }
} }