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 package im.vector.app.features.settings.devices.v2
import android.content.SharedPreferences
import com.airbnb.mvrx.MavericksViewModelFactory import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
import dagger.assisted.Assisted import dagger.assisted.Assisted
@ -51,7 +52,11 @@ class DevicesViewModel @AssistedInject constructor(
private val pendingAuthHandler: PendingAuthHandler, private val pendingAuthHandler: PendingAuthHandler,
refreshDevicesUseCase: RefreshDevicesUseCase, refreshDevicesUseCase: RefreshDevicesUseCase,
private val vectorPreferences: VectorPreferences, 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 @AssistedFactory
interface Factory : MavericksAssistedViewModelFactory<DevicesViewModel, DevicesViewState> { interface Factory : MavericksAssistedViewModelFactory<DevicesViewModel, DevicesViewState> {
@ -66,6 +71,20 @@ class DevicesViewModel @AssistedInject constructor(
refreshDevicesOnCryptoDevicesChange() refreshDevicesOnCryptoDevicesChange()
refreshDeviceList() refreshDeviceList()
refreshIpAddressVisibility() 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() { private fun refreshIpAddressVisibility() {
@ -126,12 +145,8 @@ class DevicesViewModel @AssistedInject constructor(
} }
} }
private fun handleToggleIpAddressVisibility() = withState { state -> private fun handleToggleIpAddressVisibility() {
val isShowingIpAddress = state.isShowingIpAddress toggleIpAddressVisibilityUseCase.execute()
setState {
copy(isShowingIpAddress = !isShowingIpAddress)
}
vectorPreferences.setIpAddressVisibilityInDeviceManagerScreens(!isShowingIpAddress)
} }
private fun handleVerifyCurrentSessionAction() { 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 package im.vector.app.features.settings.devices.v2.othersessions
import android.content.SharedPreferences
import com.airbnb.mvrx.MavericksViewModelFactory import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
import dagger.assisted.Assisted 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.VectorPreferences
import im.vector.app.features.settings.devices.v2.GetDeviceFullInfoListUseCase 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.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.VectorSessionsListViewModel
import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType
import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthNeeded import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthNeeded
@ -45,9 +47,10 @@ class OtherSessionsViewModel @AssistedInject constructor(
private val pendingAuthHandler: PendingAuthHandler, private val pendingAuthHandler: PendingAuthHandler,
refreshDevicesUseCase: RefreshDevicesUseCase, refreshDevicesUseCase: RefreshDevicesUseCase,
private val vectorPreferences: VectorPreferences, private val vectorPreferences: VectorPreferences,
private val toggleIpAddressVisibilityUseCase: ToggleIpAddressVisibilityUseCase,
) : VectorSessionsListViewModel<OtherSessionsViewState, OtherSessionsAction, OtherSessionsViewEvents>( ) : VectorSessionsListViewModel<OtherSessionsViewState, OtherSessionsAction, OtherSessionsViewEvents>(
initialState, activeSessionHolder, refreshDevicesUseCase initialState, activeSessionHolder, refreshDevicesUseCase
) { ), SharedPreferences.OnSharedPreferenceChangeListener {
@AssistedFactory @AssistedFactory
interface Factory : MavericksAssistedViewModelFactory<OtherSessionsViewModel, OtherSessionsViewState> { interface Factory : MavericksAssistedViewModelFactory<OtherSessionsViewModel, OtherSessionsViewState> {
@ -61,6 +64,20 @@ class OtherSessionsViewModel @AssistedInject constructor(
init { init {
observeDevices(initialState.currentFilter) observeDevices(initialState.currentFilter)
refreshIpAddressVisibility() 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() { private fun refreshIpAddressVisibility() {
@ -99,12 +116,8 @@ class OtherSessionsViewModel @AssistedInject constructor(
} }
} }
private fun handleToggleIpAddressVisibility() = withState { state -> private fun handleToggleIpAddressVisibility() {
val isShowingIpAddress = state.isShowingIpAddress toggleIpAddressVisibilityUseCase.execute()
setState {
copy(isShowingIpAddress = !isShowingIpAddress)
}
vectorPreferences.setIpAddressVisibilityInDeviceManagerScreens(!isShowingIpAddress)
} }
private fun handleFilterDevices(action: OtherSessionsAction.FilterDevices) { private fun handleFilterDevices(action: OtherSessionsAction.FilterDevices) {

View file

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

View file

@ -16,6 +16,7 @@
package im.vector.app.features.settings.devices.v2 package im.vector.app.features.settings.devices.v2
import android.content.SharedPreferences
import android.os.SystemClock import android.os.SystemClock
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
import com.airbnb.mvrx.test.MavericksTestRule import com.airbnb.mvrx.test.MavericksTestRule
@ -76,6 +77,7 @@ class DevicesViewModelTest {
private val fakePendingAuthHandler = FakePendingAuthHandler() private val fakePendingAuthHandler = FakePendingAuthHandler()
private val fakeRefreshDevicesUseCase = mockk<RefreshDevicesUseCase>(relaxUnitFun = true) private val fakeRefreshDevicesUseCase = mockk<RefreshDevicesUseCase>(relaxUnitFun = true)
private val fakeVectorPreferences = FakeVectorPreferences() private val fakeVectorPreferences = FakeVectorPreferences()
private val toggleIpAddressVisibilityUseCase = mockk<ToggleIpAddressVisibilityUseCase>()
private fun createViewModel(): DevicesViewModel { private fun createViewModel(): DevicesViewModel {
return DevicesViewModel( return DevicesViewModel(
@ -90,6 +92,7 @@ class DevicesViewModelTest {
pendingAuthHandler = fakePendingAuthHandler.instance, pendingAuthHandler = fakePendingAuthHandler.instance,
refreshDevicesUseCase = fakeRefreshDevicesUseCase, refreshDevicesUseCase = fakeRefreshDevicesUseCase,
vectorPreferences = fakeVectorPreferences.instance, vectorPreferences = fakeVectorPreferences.instance,
toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase,
) )
} }
@ -364,11 +367,15 @@ class DevicesViewModelTest {
// When // When
val viewModel = createViewModel() val viewModel = createViewModel()
val viewModelTest = viewModel.test() 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.handle(DevicesAction.ToggleIpAddressVisibility)
viewModel.onSharedPreferenceChanged(null, null)
// Then // Then
viewModelTest.assertLatestState { it.isShowingIpAddress == true } viewModelTest.assertLatestState { it.isShowingIpAddress == true }
every { fakeVectorPreferences.instance.setIpAddressVisibilityInDeviceManagerScreens(true) } just runs
viewModelTest.finish() 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.DeviceFullInfo
import im.vector.app.features.settings.devices.v2.GetDeviceFullInfoListUseCase 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.RefreshDevicesUseCase
import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase
import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType
import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakePendingAuthHandler import im.vector.app.test.fakes.FakePendingAuthHandler
@ -68,6 +69,7 @@ class OtherSessionsViewModelTest {
private val fakeSignoutSessionsUseCase = FakeSignoutSessionsUseCase() private val fakeSignoutSessionsUseCase = FakeSignoutSessionsUseCase()
private val fakePendingAuthHandler = FakePendingAuthHandler() private val fakePendingAuthHandler = FakePendingAuthHandler()
private val fakeVectorPreferences = FakeVectorPreferences() private val fakeVectorPreferences = FakeVectorPreferences()
private val toggleIpAddressVisibilityUseCase = mockk<ToggleIpAddressVisibilityUseCase>()
private fun createViewModel(viewState: OtherSessionsViewState = OtherSessionsViewState(defaultArgs)) = private fun createViewModel(viewState: OtherSessionsViewState = OtherSessionsViewState(defaultArgs)) =
OtherSessionsViewModel( OtherSessionsViewModel(
@ -78,6 +80,7 @@ class OtherSessionsViewModelTest {
pendingAuthHandler = fakePendingAuthHandler.instance, pendingAuthHandler = fakePendingAuthHandler.instance,
refreshDevicesUseCase = fakeRefreshDevicesUseCase, refreshDevicesUseCase = fakeRefreshDevicesUseCase,
vectorPreferences = fakeVectorPreferences.instance, vectorPreferences = fakeVectorPreferences.instance,
toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase,
) )
@Before @Before

View file

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