From 5a75e3db810a1a127512838b95b2f1e2a9de3181 Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 28 Dec 2018 12:56:52 +0100 Subject: [PATCH] Clean some of the session code API --- .../vector/riotredesign/core/di/AppModule.kt | 10 ++++++ .../features/home/HomeViewModel.kt | 31 +++++++++-------- .../home/room/list/RoomSelectionRepository.kt | 20 +++++++++++ .../android/api/session/room/RoomService.kt | 9 ----- .../internal/session/DefaultSession.kt | 19 ----------- .../session/room/DefaultRoomService.kt | 33 ------------------- 6 files changed, 48 insertions(+), 74 deletions(-) create mode 100644 app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSelectionRepository.kt diff --git a/app/src/main/java/im/vector/riotredesign/core/di/AppModule.kt b/app/src/main/java/im/vector/riotredesign/core/di/AppModule.kt index 92daa091f8..7a25d46d1e 100644 --- a/app/src/main/java/im/vector/riotredesign/core/di/AppModule.kt +++ b/app/src/main/java/im/vector/riotredesign/core/di/AppModule.kt @@ -1,7 +1,9 @@ package im.vector.riotredesign.core.di import android.content.Context +import android.content.Context.MODE_PRIVATE import im.vector.riotredesign.core.resources.LocaleProvider +import im.vector.riotredesign.features.home.room.list.RoomSelectionRepository import org.koin.dsl.module.module class AppModule(private val context: Context) { @@ -12,5 +14,13 @@ class AppModule(private val context: Context) { LocaleProvider(context.resources) } + single { + context.getSharedPreferences("im.vector.riot", MODE_PRIVATE) + } + + single { + RoomSelectionRepository(get()) + } + } } \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeViewModel.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeViewModel.kt index 3e7d437d7a..e6487c14cb 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeViewModel.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeViewModel.kt @@ -7,15 +7,20 @@ import im.vector.matrix.android.api.Matrix import im.vector.matrix.android.api.permalinks.PermalinkData import im.vector.matrix.android.api.session.Session import im.vector.matrix.rx.rx +import im.vector.riotredesign.features.home.room.list.RoomSelectionRepository +import org.koin.android.ext.android.get -class HomeViewModel(initialState: HomeViewState, private val session: Session) : BaseMvRxViewModel(initialState) { +class HomeViewModel(initialState: HomeViewState, + private val session: Session, + private val roomSelectionRepository: RoomSelectionRepository) : BaseMvRxViewModel(initialState) { companion object : MvRxViewModelFactory { @JvmStatic override fun create(activity: FragmentActivity, state: HomeViewState): HomeViewModel { val currentSession = Matrix.getInstance().currentSession - return HomeViewModel(state, currentSession) + val roomSelectionRepository = activity.get() + return HomeViewModel(state, currentSession, roomSelectionRepository) } } @@ -26,9 +31,9 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) : fun accept(action: HomeActions) { when (action) { - is HomeActions.SelectRoom -> handleSelectRoom(action) - is HomeActions.SelectGroup -> handleSelectGroup(action) - is HomeActions.RoomDisplayed -> setState { copy(shouldOpenRoomDetail = false) } + is HomeActions.SelectRoom -> handleSelectRoom(action) + is HomeActions.SelectGroup -> handleSelectGroup(action) + is HomeActions.RoomDisplayed -> setState { copy(shouldOpenRoomDetail = false) } is HomeActions.PermalinkClicked -> handlePermalinkClicked(action) } } @@ -38,16 +43,16 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) : private fun handlePermalinkClicked(action: HomeActions.PermalinkClicked) { withState { state -> when (action.permalinkData) { - is PermalinkData.EventLink -> { + is PermalinkData.EventLink -> { } - is PermalinkData.RoomLink -> { + is PermalinkData.RoomLink -> { } - is PermalinkData.GroupLink -> { + is PermalinkData.GroupLink -> { } - is PermalinkData.UserLink -> { + is PermalinkData.UserLink -> { } is PermalinkData.FallbackLink -> { @@ -60,7 +65,7 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) : private fun handleSelectRoom(action: HomeActions.SelectRoom) { withState { state -> if (state.selectedRoomId != action.roomSummary.roomId) { - session.saveLastSelectedRoom(action.roomSummary) + roomSelectionRepository.saveLastSelectedRoom(action.roomSummary.roomId) setState { copy(selectedRoomId = action.roomSummary.roomId, shouldOpenRoomDetail = true) } } } @@ -85,9 +90,9 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) : val groupRooms = summaries?.filter { !it.isDirect } ?: emptyList() val selectedRoomId = selectedRoomId - ?: session.lastSelectedRoom()?.roomId - ?: directRooms.firstOrNull()?.roomId - ?: groupRooms.firstOrNull()?.roomId + ?: roomSelectionRepository.lastSelectedRoom() + ?: directRooms.firstOrNull()?.roomId + ?: groupRooms.firstOrNull()?.roomId copy( asyncRooms = async, diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSelectionRepository.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSelectionRepository.kt new file mode 100644 index 0000000000..f2a7af753b --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSelectionRepository.kt @@ -0,0 +1,20 @@ +package im.vector.riotredesign.features.home.room.list + +import android.content.SharedPreferences + +private const val SHARED_PREFS_SELECTED_ROOM_KEY = "SHARED_PREFS_SELECTED_ROOM_KEY" + +class RoomSelectionRepository(private val sharedPreferences: SharedPreferences) { + + fun lastSelectedRoom(): String? { + return sharedPreferences.getString(SHARED_PREFS_SELECTED_ROOM_KEY, null) + } + + fun saveLastSelectedRoom(roomId: String) { + sharedPreferences.edit() + .putString(SHARED_PREFS_SELECTED_ROOM_KEY, roomId) + .apply() + } + +} + diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt index 79170bdbd8..3f02d2032e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt @@ -7,15 +7,6 @@ interface RoomService { fun getRoom(roomId: String): Room? - fun getAllRooms(): List - - fun liveRooms(): LiveData> - fun liveRoomSummaries(): LiveData> - fun lastSelectedRoom(): RoomSummary? - - fun saveLastSelectedRoom(roomSummary: RoomSummary) - - } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt index 7047b1dcc0..2841f089b9 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt @@ -69,31 +69,12 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi return roomService.getRoom(roomId) } - override fun getAllRooms(): List { - assert(isOpen) - return roomService.getAllRooms() - } - - override fun liveRooms(): LiveData> { - assert(isOpen) - return roomService.liveRooms() - } override fun liveRoomSummaries(): LiveData> { assert(isOpen) return roomService.liveRoomSummaries() } - override fun lastSelectedRoom(): RoomSummary? { - assert(isOpen) - return roomService.lastSelectedRoom() - } - - override fun saveLastSelectedRoom(roomSummary: RoomSummary) { - assert(isOpen) - roomService.saveLastSelectedRoom(roomSummary) - } - // GROUP SERVICE override fun getGroup(groupId: String): Group? { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt index 90c1065960..e9b0aac43c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt @@ -9,19 +9,10 @@ import im.vector.matrix.android.internal.database.mapper.asDomain import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields -import im.vector.matrix.android.internal.database.query.lastSelected import im.vector.matrix.android.internal.database.query.where internal class DefaultRoomService(private val monarchy: Monarchy) : RoomService { - override fun getAllRooms(): List { - var rooms: List = emptyList() - monarchy.doWithRealm { realm -> - rooms = RoomEntity.where(realm).findAll().map { it.asDomain() } - } - return rooms - } - override fun getRoom(roomId: String): Room? { var room: Room? = null monarchy.doWithRealm { realm -> @@ -30,34 +21,10 @@ internal class DefaultRoomService(private val monarchy: Monarchy) : RoomService return room } - override fun liveRooms(): LiveData> { - return monarchy.findAllMappedWithChanges( - { realm -> RoomEntity.where(realm) }, - { it.asDomain() } - ) - } - override fun liveRoomSummaries(): LiveData> { return monarchy.findAllMappedWithChanges( { realm -> RoomSummaryEntity.where(realm).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) }, { it.asDomain() } ) } - - override fun lastSelectedRoom(): RoomSummary? { - var lastSelected: RoomSummary? = null - monarchy.doWithRealm { realm -> - lastSelected = RoomSummaryEntity.lastSelected(realm)?.asDomain() - } - return lastSelected - } - - override fun saveLastSelectedRoom(roomSummary: RoomSummary) { - monarchy.writeAsync { realm -> - val lastSelected = RoomSummaryEntity.lastSelected(realm) - val roomSummaryEntity = RoomSummaryEntity.where(realm, roomSummary.roomId).findFirst() - lastSelected?.isLatestSelected = false - roomSummaryEntity?.isLatestSelected = true - } - } } \ No newline at end of file