Create interface for UiStateRepository and an implementation with SharedPrefs

This commit is contained in:
Benoit Marty 2019-09-24 13:43:50 +02:00
parent 1c9cf7a810
commit 0d80750507
3 changed files with 64 additions and 30 deletions

View File

@ -28,6 +28,8 @@ import im.vector.matrix.android.api.auth.Authenticator
import im.vector.matrix.android.api.session.Session
import im.vector.riotx.features.navigation.DefaultNavigator
import im.vector.riotx.features.navigation.Navigator
import im.vector.riotx.features.ui.SharedPreferencesUiStateRepository
import im.vector.riotx.features.ui.UiStateRepository
@Module
abstract class VectorModule {
@ -70,5 +72,7 @@ abstract class VectorModule {
@Binds
abstract fun bindNavigator(navigator: DefaultNavigator): Navigator
@Binds
abstract fun bindUiStateRepository(uiStateRepository: SharedPreferencesUiStateRepository): UiStateRepository
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2019 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.riotx.features.ui
import android.content.SharedPreferences
import androidx.core.content.edit
import im.vector.riotx.features.home.room.list.RoomListFragment
import javax.inject.Inject
/**
* This class is used to persist UI state across application restart
*/
class SharedPreferencesUiStateRepository @Inject constructor(private val sharedPreferences: SharedPreferences) : UiStateRepository {
override fun getDisplayMode(): RoomListFragment.DisplayMode {
return when (sharedPreferences.getInt(KEY_DISPLAY_MODE, VALUE_DISPLAY_MODE_CATCHUP)) {
VALUE_DISPLAY_MODE_PEOPLE -> RoomListFragment.DisplayMode.PEOPLE
VALUE_DISPLAY_MODE_ROOMS -> RoomListFragment.DisplayMode.ROOMS
else -> RoomListFragment.DisplayMode.HOME
}
}
override fun storeDisplayMode(displayMode: RoomListFragment.DisplayMode) {
sharedPreferences.edit {
putInt(KEY_DISPLAY_MODE,
when (displayMode) {
RoomListFragment.DisplayMode.PEOPLE -> VALUE_DISPLAY_MODE_PEOPLE
RoomListFragment.DisplayMode.ROOMS -> VALUE_DISPLAY_MODE_ROOMS
else -> VALUE_DISPLAY_MODE_CATCHUP
})
}
}
companion object {
private const val KEY_DISPLAY_MODE = "UI_STATE_DISPLAY_MODE"
private const val VALUE_DISPLAY_MODE_CATCHUP = 0
private const val VALUE_DISPLAY_MODE_PEOPLE = 1
private const val VALUE_DISPLAY_MODE_ROOMS = 2
}
}

View File

@ -16,41 +16,15 @@
package im.vector.riotx.features.ui
import android.content.SharedPreferences
import androidx.core.content.edit
import im.vector.riotx.features.home.room.list.RoomListFragment
import javax.inject.Inject
/**
* This class is used to persist UI state across application restart
* This interface is used to persist UI state across application restart
*/
class UiStateRepository @Inject constructor(private val sharedPreferences: SharedPreferences) {
interface UiStateRepository {
fun getDisplayMode(): RoomListFragment.DisplayMode {
return when (sharedPreferences.getInt(KEY_DISPLAY_MODE, VALUE_DISPLAY_MODE_CATCHUP)) {
VALUE_DISPLAY_MODE_PEOPLE -> RoomListFragment.DisplayMode.PEOPLE
VALUE_DISPLAY_MODE_ROOMS -> RoomListFragment.DisplayMode.ROOMS
else -> RoomListFragment.DisplayMode.HOME
}
}
fun getDisplayMode(): RoomListFragment.DisplayMode
fun storeDisplayMode(displayMode: RoomListFragment.DisplayMode) {
sharedPreferences.edit {
putInt(KEY_DISPLAY_MODE,
when (displayMode) {
RoomListFragment.DisplayMode.PEOPLE -> VALUE_DISPLAY_MODE_PEOPLE
RoomListFragment.DisplayMode.ROOMS -> VALUE_DISPLAY_MODE_ROOMS
else -> VALUE_DISPLAY_MODE_CATCHUP
})
}
}
companion object {
private const val KEY_DISPLAY_MODE = "UI_STATE_DISPLAY_MODE"
private const val VALUE_DISPLAY_MODE_CATCHUP = 0
private const val VALUE_DISPLAY_MODE_PEOPLE = 1
private const val VALUE_DISPLAY_MODE_ROOMS = 2
}
fun storeDisplayMode(displayMode: RoomListFragment.DisplayMode)
}