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

View file

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