From f753e475d853154fabc1ab443053aa0956440aea Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 12 Oct 2022 15:38:36 +0200 Subject: [PATCH] Deleting/Updating the client Info when changing the lab flag --- .../app/core/di/MavericksViewModelModule.kt | 6 ++ .../DeleteMatrixClientInfoUseCase.kt | 42 ++++++++++++ .../settings/labs/VectorSettingsLabsAction.kt | 24 +++++++ .../labs/VectorSettingsLabsFragment.kt | 17 +++++ .../labs/VectorSettingsLabsViewModel.kt | 68 +++++++++++++++++++ .../labs/VectorSettingsLabsViewState.kt | 21 ++++++ 6 files changed, 178 insertions(+) create mode 100644 vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCase.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsAction.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsViewModel.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsViewState.kt diff --git a/vector/src/main/java/im/vector/app/core/di/MavericksViewModelModule.kt b/vector/src/main/java/im/vector/app/core/di/MavericksViewModelModule.kt index 38b62e1511..d2afdb65e8 100644 --- a/vector/src/main/java/im/vector/app/core/di/MavericksViewModelModule.kt +++ b/vector/src/main/java/im/vector/app/core/di/MavericksViewModelModule.kt @@ -100,6 +100,7 @@ import im.vector.app.features.settings.devtools.KeyRequestViewModel import im.vector.app.features.settings.font.FontScaleSettingViewModel import im.vector.app.features.settings.homeserver.HomeserverSettingsViewModel import im.vector.app.features.settings.ignored.IgnoredUsersViewModel +import im.vector.app.features.settings.labs.VectorSettingsLabsViewModel import im.vector.app.features.settings.legals.LegalsViewModel import im.vector.app.features.settings.locale.LocalePickerViewModel import im.vector.app.features.settings.push.PushGatewaysViewModel @@ -665,4 +666,9 @@ interface MavericksViewModelModule { @IntoMap @MavericksViewModelKey(SessionLearnMoreViewModel::class) fun sessionLearnMoreViewModelFactory(factory: SessionLearnMoreViewModel.Factory): MavericksAssistedViewModelFactory<*, *> + + @Binds + @IntoMap + @MavericksViewModelKey(VectorSettingsLabsViewModel::class) + fun vectorSettingsLabsViewModelFactory(factory: VectorSettingsLabsViewModel.Factory): MavericksAssistedViewModelFactory<*, *> } diff --git a/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCase.kt new file mode 100644 index 0000000000..3957c81997 --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCase.kt @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.core.session.clientinfo + +import im.vector.app.core.di.ActiveSessionHolder +import timber.log.Timber +import javax.inject.Inject + +/** + * This use case delete the account data event containing extended client info. + */ +class DeleteMatrixClientInfoUseCase @Inject constructor( + private val activeSessionHolder: ActiveSessionHolder, + private val setMatrixClientInfoUseCase: SetMatrixClientInfoUseCase, +) { + + // TODO add unit tests + suspend fun execute(): Result = runCatching { + Timber.d("deleting recorded client info") + val session = activeSessionHolder.getActiveSession() + val emptyClientInfo = MatrixClientInfoContent( + name = "", + version = "", + url = "", + ) + setMatrixClientInfoUseCase.execute(session, emptyClientInfo) + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsAction.kt b/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsAction.kt new file mode 100644 index 0000000000..db48c3400b --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsAction.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.labs + +import im.vector.app.core.platform.VectorViewModelAction + +sealed class VectorSettingsLabsAction : VectorViewModelAction { + object UpdateClientInfo : VectorSettingsLabsAction() + object DeleteRecordedClientInfo : VectorSettingsLabsAction() +} diff --git a/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsFragment.kt b/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsFragment.kt index 541811a650..5840093bee 100644 --- a/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsFragment.kt @@ -20,7 +20,9 @@ import android.os.Bundle import android.text.method.LinkMovementMethod import android.widget.TextView import androidx.preference.Preference +import androidx.preference.Preference.OnPreferenceChangeListener import androidx.preference.SwitchPreference +import com.airbnb.mvrx.fragmentViewModel import com.google.android.material.dialog.MaterialAlertDialogBuilder import dagger.hilt.android.AndroidEntryPoint import im.vector.app.R @@ -39,6 +41,8 @@ import javax.inject.Inject class VectorSettingsLabsFragment : VectorSettingsBaseFragment() { + private val viewModel: VectorSettingsLabsViewModel by fragmentViewModel() + @Inject lateinit var vectorPreferences: VectorPreferences @Inject lateinit var lightweightSettingsStorage: LightweightSettingsStorage @Inject lateinit var threadsManager: ThreadsManager @@ -87,6 +91,7 @@ class VectorSettingsLabsFragment : } configureUnreadNotificationsAsTabPreference() + configureEnableClientInfoRecordingPreference() } private fun configureUnreadNotificationsAsTabPreference() { @@ -142,4 +147,16 @@ class VectorSettingsLabsFragment : private fun onNewLayoutPreferenceClicked() { configureUnreadNotificationsAsTabPreference() } + + private fun configureEnableClientInfoRecordingPreference() { + findPreference(VectorPreferences.SETTINGS_LABS_CLIENT_INFO_RECORDING_KEY)?.onPreferenceChangeListener = + OnPreferenceChangeListener { _, newValue -> + when { + (newValue as? Boolean) == false -> viewModel.handle(VectorSettingsLabsAction.DeleteRecordedClientInfo) + (newValue as? Boolean) == true -> viewModel.handle(VectorSettingsLabsAction.UpdateClientInfo) + else -> Unit + } + true + } + } } diff --git a/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsViewModel.kt new file mode 100644 index 0000000000..2f4cc0562b --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsViewModel.kt @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.labs + +import com.airbnb.mvrx.MavericksViewModelFactory +import dagger.assisted.Assisted +import dagger.assisted.AssistedFactory +import dagger.assisted.AssistedInject +import im.vector.app.core.di.ActiveSessionHolder +import im.vector.app.core.di.MavericksAssistedViewModelFactory +import im.vector.app.core.di.hiltMavericksViewModelFactory +import im.vector.app.core.platform.EmptyViewEvents +import im.vector.app.core.platform.VectorViewModel +import im.vector.app.core.session.clientinfo.DeleteMatrixClientInfoUseCase +import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase +import kotlinx.coroutines.launch + +class VectorSettingsLabsViewModel @AssistedInject constructor( + @Assisted initialState: VectorSettingsLabsViewState, + private val activeSessionHolder: ActiveSessionHolder, + private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase, + private val deleteMatrixClientInfoUseCase: DeleteMatrixClientInfoUseCase, +) : VectorViewModel(initialState) { + + @AssistedFactory + interface Factory : MavericksAssistedViewModelFactory { + override fun create(initialState: VectorSettingsLabsViewState): VectorSettingsLabsViewModel + } + + companion object : MavericksViewModelFactory by hiltMavericksViewModelFactory() + + // TODO add unit tests + override fun handle(action: VectorSettingsLabsAction) { + when (action) { + VectorSettingsLabsAction.UpdateClientInfo -> handleUpdateClientInfo() + VectorSettingsLabsAction.DeleteRecordedClientInfo -> handleDeleteRecordedClientInfo() + } + } + + private fun handleUpdateClientInfo() { + viewModelScope.launch { + activeSessionHolder.getSafeActiveSession() + ?.let { session -> + updateMatrixClientInfoUseCase.execute(session) + } + } + } + + private fun handleDeleteRecordedClientInfo() { + viewModelScope.launch { + deleteMatrixClientInfoUseCase.execute() + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsViewState.kt b/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsViewState.kt new file mode 100644 index 0000000000..c246d30994 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/labs/VectorSettingsLabsViewState.kt @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.labs + +import com.airbnb.mvrx.MavericksState + +class VectorSettingsLabsViewState : MavericksState