Fixed potential deadlock

This commit is contained in:
Sylvain Berfini 2023-12-12 12:42:18 +01:00
parent 4a4ce57864
commit 8ad466e091
2 changed files with 22 additions and 38 deletions

View file

@ -50,7 +50,7 @@ class ContactViewModelFactory(private val friend: Friend) :
}
}
class ContactViewModel(friend: Friend, async: Boolean = false) : MessageNotifierViewModel(), ContactDataInterface {
class ContactViewModel(friend: Friend) : MessageNotifierViewModel(), ContactDataInterface {
override val contact: MutableLiveData<Friend> = MutableLiveData<Friend>()
override val displayName: MutableLiveData<String> = MutableLiveData<String>()
override val securityLevel: MutableLiveData<ChatRoom.SecurityLevel> = MutableLiveData<ChatRoom.SecurityLevel>()
@ -143,21 +143,12 @@ class ContactViewModel(friend: Friend, async: Boolean = false) : MessageNotifier
init {
fullName = friend.name ?: ""
if (async) {
contact.postValue(friend)
displayName.postValue(friend.name)
isNativeContact.postValue(friend.refKey != null)
presenceStatus.postValue(friend.consolidatedPresence)
readOnlyNativeAddressBook.postValue(corePreferences.readOnlyNativeContacts)
hasLongTermPresence.postValue(friend.hasLongTermPresence())
} else {
contact.value = friend
displayName.value = friend.name
isNativeContact.value = friend.refKey != null
presenceStatus.value = friend.consolidatedPresence
readOnlyNativeAddressBook.value = corePreferences.readOnlyNativeContacts
hasLongTermPresence.value = friend.hasLongTermPresence()
}
friend.addListener {
presenceStatus.value = it.consolidatedPresence

View file

@ -150,34 +150,27 @@ class ContactsListViewModel : ViewModel() {
Log.i("[Contacts] Processing ${results.size} results")
contactsList.value.orEmpty().forEach(ContactViewModel::destroy)
viewModelScope.launch {
withContext(Dispatchers.IO) {
val list = arrayListOf<ContactViewModel>()
for (result in results) {
val friend = result.friend
val viewModel = if (friend != null) {
ContactViewModel(friend, true)
ContactViewModel(friend)
} else {
Log.w("[Contacts] SearchResult [$result] has no Friend!")
val fakeFriend = coreContext.contactsManager.createFriendFromSearchResult(
result
)
ContactViewModel(fakeFriend, true)
ContactViewModel(fakeFriend)
}
list.add(viewModel)
}
contactsList.postValue(list)
}
withContext(Dispatchers.Main) {
contactsList.value = list
Log.i("[Contacts] Processed ${results.size} results")
}
}
}
fun deleteContact(friend: Friend) {
friend.remove() // TODO: FIXME: friend is const here!