Code review fixes.

This commit is contained in:
Onuray Sahin 2022-11-22 16:05:37 +03:00
parent 6c45490dd1
commit ab749eee6a
7 changed files with 105 additions and 22 deletions

View file

@ -16,6 +16,7 @@
package im.vector.app.features.settings.devices.v2
import android.content.SharedPreferences
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.Success
import dagger.assisted.Assisted
@ -51,7 +52,11 @@ class DevicesViewModel @AssistedInject constructor(
private val pendingAuthHandler: PendingAuthHandler,
refreshDevicesUseCase: RefreshDevicesUseCase,
private val vectorPreferences: VectorPreferences,
) : VectorSessionsListViewModel<DevicesViewState, DevicesAction, DevicesViewEvent>(initialState, activeSessionHolder, refreshDevicesUseCase) {
private val toggleIpAddressVisibilityUseCase: ToggleIpAddressVisibilityUseCase,
) : VectorSessionsListViewModel<DevicesViewState,
DevicesAction,
DevicesViewEvent>(initialState, activeSessionHolder, refreshDevicesUseCase),
SharedPreferences.OnSharedPreferenceChangeListener {
@AssistedFactory
interface Factory : MavericksAssistedViewModelFactory<DevicesViewModel, DevicesViewState> {
@ -66,6 +71,20 @@ class DevicesViewModel @AssistedInject constructor(
refreshDevicesOnCryptoDevicesChange()
refreshDeviceList()
refreshIpAddressVisibility()
observePreferences()
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
refreshIpAddressVisibility()
}
private fun observePreferences() {
vectorPreferences.subscribeToChanges(this)
}
override fun onCleared() {
vectorPreferences.unsubscribeToChanges(this)
super.onCleared()
}
private fun refreshIpAddressVisibility() {
@ -126,12 +145,8 @@ class DevicesViewModel @AssistedInject constructor(
}
}
private fun handleToggleIpAddressVisibility() = withState { state ->
val isShowingIpAddress = state.isShowingIpAddress
setState {
copy(isShowingIpAddress = !isShowingIpAddress)
}
vectorPreferences.setIpAddressVisibilityInDeviceManagerScreens(!isShowingIpAddress)
private fun handleToggleIpAddressVisibility() {
toggleIpAddressVisibilityUseCase.execute()
}
private fun handleVerifyCurrentSessionAction() {

View file

@ -0,0 +1,30 @@
/*
* 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.devices.v2
import im.vector.app.features.settings.VectorPreferences
import javax.inject.Inject
class ToggleIpAddressVisibilityUseCase @Inject constructor(
private val vectorPreferences: VectorPreferences,
) {
fun execute() {
val currentVisibility = vectorPreferences.showIpAddressInDeviceManagerScreens()
vectorPreferences.setIpAddressVisibilityInDeviceManagerScreens(!currentVisibility)
}
}

View file

@ -16,6 +16,7 @@
package im.vector.app.features.settings.devices.v2.othersessions
import android.content.SharedPreferences
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.Success
import dagger.assisted.Assisted
@ -28,6 +29,7 @@ import im.vector.app.features.auth.PendingAuthHandler
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.settings.devices.v2.GetDeviceFullInfoListUseCase
import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase
import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel
import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType
import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthNeeded
@ -45,9 +47,10 @@ class OtherSessionsViewModel @AssistedInject constructor(
private val pendingAuthHandler: PendingAuthHandler,
refreshDevicesUseCase: RefreshDevicesUseCase,
private val vectorPreferences: VectorPreferences,
private val toggleIpAddressVisibilityUseCase: ToggleIpAddressVisibilityUseCase,
) : VectorSessionsListViewModel<OtherSessionsViewState, OtherSessionsAction, OtherSessionsViewEvents>(
initialState, activeSessionHolder, refreshDevicesUseCase
) {
), SharedPreferences.OnSharedPreferenceChangeListener {
@AssistedFactory
interface Factory : MavericksAssistedViewModelFactory<OtherSessionsViewModel, OtherSessionsViewState> {
@ -61,6 +64,20 @@ class OtherSessionsViewModel @AssistedInject constructor(
init {
observeDevices(initialState.currentFilter)
refreshIpAddressVisibility()
observePreferences()
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
refreshIpAddressVisibility()
}
private fun observePreferences() {
vectorPreferences.subscribeToChanges(this)
}
override fun onCleared() {
vectorPreferences.unsubscribeToChanges(this)
super.onCleared()
}
private fun refreshIpAddressVisibility() {
@ -99,12 +116,8 @@ class OtherSessionsViewModel @AssistedInject constructor(
}
}
private fun handleToggleIpAddressVisibility() = withState { state ->
val isShowingIpAddress = state.isShowingIpAddress
setState {
copy(isShowingIpAddress = !isShowingIpAddress)
}
vectorPreferences.setIpAddressVisibilityInDeviceManagerScreens(!isShowingIpAddress)
private fun handleToggleIpAddressVisibility() {
toggleIpAddressVisibilityUseCase.execute()
}
private fun handleFilterDevices(action: OtherSessionsAction.FilterDevices) {

View file

@ -16,6 +16,7 @@
package im.vector.app.features.settings.devices.v2.overview
import android.content.SharedPreferences
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.Success
import dagger.assisted.Assisted
@ -27,6 +28,7 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.features.auth.PendingAuthHandler
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase
import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel
import im.vector.app.features.settings.devices.v2.notification.GetNotificationsStatusUseCase
import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase
@ -56,9 +58,10 @@ class SessionOverviewViewModel @AssistedInject constructor(
private val getNotificationsStatusUseCase: GetNotificationsStatusUseCase,
refreshDevicesUseCase: RefreshDevicesUseCase,
private val vectorPreferences: VectorPreferences,
private val toggleIpAddressVisibilityUseCase: ToggleIpAddressVisibilityUseCase,
) : VectorSessionsListViewModel<SessionOverviewViewState, SessionOverviewAction, SessionOverviewViewEvent>(
initialState, activeSessionHolder, refreshDevicesUseCase
) {
), SharedPreferences.OnSharedPreferenceChangeListener {
companion object : MavericksViewModelFactory<SessionOverviewViewModel, SessionOverviewViewState> by hiltMavericksViewModelFactory()
@ -73,8 +76,21 @@ class SessionOverviewViewModel @AssistedInject constructor(
observeCurrentSessionInfo()
observeNotificationsStatus(initialState.deviceId)
refreshIpAddressVisibility()
observePreferences()
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
refreshIpAddressVisibility()
}
private fun observePreferences() {
vectorPreferences.subscribeToChanges(this)
}
override fun onCleared() {
vectorPreferences.unsubscribeToChanges(this)
super.onCleared()
}
private fun refreshIpAddressVisibility() {
val shouldShowIpAddress = vectorPreferences.showIpAddressInDeviceManagerScreens()
setState {
@ -125,12 +141,8 @@ class SessionOverviewViewModel @AssistedInject constructor(
}
}
private fun handleToggleIpAddressVisibility() = withState { state ->
val isShowingIpAddress = state.isShowingIpAddress
setState {
copy(isShowingIpAddress = !isShowingIpAddress)
}
vectorPreferences.setIpAddressVisibilityInDeviceManagerScreens(!isShowingIpAddress)
private fun handleToggleIpAddressVisibility() {
toggleIpAddressVisibilityUseCase.execute()
}
private fun handleVerifySessionAction() = withState { viewState ->

View file

@ -16,6 +16,7 @@
package im.vector.app.features.settings.devices.v2
import android.content.SharedPreferences
import android.os.SystemClock
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.test.MavericksTestRule
@ -76,6 +77,7 @@ class DevicesViewModelTest {
private val fakePendingAuthHandler = FakePendingAuthHandler()
private val fakeRefreshDevicesUseCase = mockk<RefreshDevicesUseCase>(relaxUnitFun = true)
private val fakeVectorPreferences = FakeVectorPreferences()
private val toggleIpAddressVisibilityUseCase = mockk<ToggleIpAddressVisibilityUseCase>()
private fun createViewModel(): DevicesViewModel {
return DevicesViewModel(
@ -90,6 +92,7 @@ class DevicesViewModelTest {
pendingAuthHandler = fakePendingAuthHandler.instance,
refreshDevicesUseCase = fakeRefreshDevicesUseCase,
vectorPreferences = fakeVectorPreferences.instance,
toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase,
)
}
@ -364,11 +367,15 @@ class DevicesViewModelTest {
// When
val viewModel = createViewModel()
val viewModelTest = viewModel.test()
every { toggleIpAddressVisibilityUseCase.execute() } just runs
every { fakeVectorPreferences.instance.setIpAddressVisibilityInDeviceManagerScreens(true) } just runs
every { fakeVectorPreferences.instance.showIpAddressInDeviceManagerScreens() } returns true
viewModel.handle(DevicesAction.ToggleIpAddressVisibility)
viewModel.onSharedPreferenceChanged(null, null)
// Then
viewModelTest.assertLatestState { it.isShowingIpAddress == true }
every { fakeVectorPreferences.instance.setIpAddressVisibilityInDeviceManagerScreens(true) } just runs
viewModelTest.finish()
}

View file

@ -22,6 +22,7 @@ import com.airbnb.mvrx.test.MavericksTestRule
import im.vector.app.features.settings.devices.v2.DeviceFullInfo
import im.vector.app.features.settings.devices.v2.GetDeviceFullInfoListUseCase
import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase
import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakePendingAuthHandler
@ -68,6 +69,7 @@ class OtherSessionsViewModelTest {
private val fakeSignoutSessionsUseCase = FakeSignoutSessionsUseCase()
private val fakePendingAuthHandler = FakePendingAuthHandler()
private val fakeVectorPreferences = FakeVectorPreferences()
private val toggleIpAddressVisibilityUseCase = mockk<ToggleIpAddressVisibilityUseCase>()
private fun createViewModel(viewState: OtherSessionsViewState = OtherSessionsViewState(defaultArgs)) =
OtherSessionsViewModel(
@ -78,6 +80,7 @@ class OtherSessionsViewModelTest {
pendingAuthHandler = fakePendingAuthHandler.instance,
refreshDevicesUseCase = fakeRefreshDevicesUseCase,
vectorPreferences = fakeVectorPreferences.instance,
toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase,
)
@Before

View file

@ -22,6 +22,7 @@ import com.airbnb.mvrx.Success
import com.airbnb.mvrx.test.MavericksTestRule
import im.vector.app.features.settings.devices.v2.DeviceFullInfo
import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase
import im.vector.app.features.settings.devices.v2.notification.NotificationsStatus
import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase
import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase
@ -79,6 +80,7 @@ class SessionOverviewViewModelTest {
private val fakeGetNotificationsStatusUseCase = FakeGetNotificationsStatusUseCase()
private val notificationsStatus = NotificationsStatus.ENABLED
private val fakeVectorPreferences = FakeVectorPreferences()
private val toggleIpAddressVisibilityUseCase = mockk<ToggleIpAddressVisibilityUseCase>()
private fun createViewModel() = SessionOverviewViewModel(
initialState = SessionOverviewViewState(args),
@ -92,6 +94,7 @@ class SessionOverviewViewModelTest {
togglePushNotificationUseCase = togglePushNotificationUseCase.instance,
getNotificationsStatusUseCase = fakeGetNotificationsStatusUseCase.instance,
vectorPreferences = fakeVectorPreferences.instance,
toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase,
)
@Before