From 31d92abcdf4198c3ff41afb157abb8d3bafe7b40 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Sun, 29 Sep 2024 14:16:27 +0200 Subject: [PATCH] Update devices list when removing one --- .../main/settings/model/AccountDeviceModel.kt | 4 +- .../viewmodel/AccountProfileViewModel.kt | 39 +++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/linphone/ui/main/settings/model/AccountDeviceModel.kt b/app/src/main/java/org/linphone/ui/main/settings/model/AccountDeviceModel.kt index 2783d73c0..537a9e1a5 100644 --- a/app/src/main/java/org/linphone/ui/main/settings/model/AccountDeviceModel.kt +++ b/app/src/main/java/org/linphone/ui/main/settings/model/AccountDeviceModel.kt @@ -27,7 +27,7 @@ import org.linphone.utils.TimestampUtils class AccountDeviceModel @WorkerThread constructor( private val accountDevice: AccountDevice, - private val onRemove: (accountDevice: AccountDevice) -> (Unit) + private val onRemove: (model: AccountDeviceModel, accountDevice: AccountDevice) -> (Unit) ) { companion object { const val TAG = "[Account Device Model]" @@ -63,6 +63,6 @@ class AccountDeviceModel @WorkerThread constructor( } fun removeDevice() { - onRemove(accountDevice) + onRemove(this, accountDevice) } } diff --git a/app/src/main/java/org/linphone/ui/main/settings/viewmodel/AccountProfileViewModel.kt b/app/src/main/java/org/linphone/ui/main/settings/viewmodel/AccountProfileViewModel.kt index a7c0eff60..54b008524 100644 --- a/app/src/main/java/org/linphone/ui/main/settings/viewmodel/AccountProfileViewModel.kt +++ b/app/src/main/java/org/linphone/ui/main/settings/viewmodel/AccountProfileViewModel.kt @@ -31,6 +31,7 @@ import org.linphone.core.AccountDevice import org.linphone.core.AccountManagerServices import org.linphone.core.AccountManagerServicesRequest import org.linphone.core.AccountManagerServicesRequestListenerStub +import org.linphone.core.Address import org.linphone.core.DialPlan import org.linphone.core.Dictionary import org.linphone.core.Factory @@ -98,7 +99,7 @@ class AccountProfileViewModel @UiThread constructor() : GenericViewModel() { val devicesList = arrayListOf() for (accountDevice in accountDevices) { devicesList.add( - AccountDeviceModel(accountDevice) { device -> + AccountDeviceModel(accountDevice) { model, device -> if (::accountManagerServices.isInitialized) { val identityAddress = account.params.identityAddress if (identityAddress != null) { @@ -111,6 +112,11 @@ class AccountProfileViewModel @UiThread constructor() : GenericViewModel() { ) deleteRequest.addListener(this) deleteRequest.submit() + + val newList = arrayListOf() + newList.addAll(devices.value.orEmpty()) + newList.remove(model) + devices.postValue(newList) } else { Log.e("$TAG Account identity address is null, can't delete device!") } @@ -122,6 +128,12 @@ class AccountProfileViewModel @UiThread constructor() : GenericViewModel() { devicesFetchInProgress.postValue(false) } + override fun onRequestSuccessful(request: AccountManagerServicesRequest, data: String?) { + if (request.type == AccountManagerServicesRequest.Type.DeleteDevice) { + Log.i("$TAG Device successfully deleted: $data") + } + } + @WorkerThread override fun onRequestError( request: AccountManagerServicesRequest, @@ -204,16 +216,7 @@ class AccountProfileViewModel @UiThread constructor() : GenericViewModel() { val defaultDomain = corePreferences.defaultDomain isOnDefaultDomain.postValue(domain == defaultDomain) if (domain == defaultDomain) { - Log.i( - "$TAG Request list of known devices for account [${identityAddress.asStringUriOnly()}]" - ) - accountManagerServices = core.createAccountManagerServices() - accountManagerServices.language = Locale.getDefault().language // Returns en, fr, etc... - val request = accountManagerServices.createGetDevicesListRequest( - identityAddress - ) - request.addListener(accountManagerServicesListener) - request.submit() + requestDevicesList(identityAddress) } else { Log.i( "$TAG Account with domain [$domain] can't get devices list, only works with [$defaultDomain] domain" @@ -385,4 +388,18 @@ class AccountProfileViewModel @UiThread constructor() : GenericViewModel() { showDeviceId.value = true return true } + + @WorkerThread + private fun requestDevicesList(identityAddress: Address) { + Log.i( + "$TAG Request devices list for identity address [${identityAddress.asStringUriOnly()}]" + ) + accountManagerServices = coreContext.core.createAccountManagerServices() + accountManagerServices.language = Locale.getDefault().language // Returns en, fr, etc... + val request = accountManagerServices.createGetDevicesListRequest( + identityAddress + ) + request.addListener(accountManagerServicesListener) + request.submit() + } }