mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-22 09:25:49 +03:00
Clean some of the session code API
This commit is contained in:
parent
b696e4a6de
commit
5a75e3db81
6 changed files with 48 additions and 74 deletions
|
@ -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())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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<HomeViewState>(initialState) {
|
||||
class HomeViewModel(initialState: HomeViewState,
|
||||
private val session: Session,
|
||||
private val roomSelectionRepository: RoomSelectionRepository) : BaseMvRxViewModel<HomeViewState>(initialState) {
|
||||
|
||||
companion object : MvRxViewModelFactory<HomeViewState> {
|
||||
|
||||
@JvmStatic
|
||||
override fun create(activity: FragmentActivity, state: HomeViewState): HomeViewModel {
|
||||
val currentSession = Matrix.getInstance().currentSession
|
||||
return HomeViewModel(state, currentSession)
|
||||
val roomSelectionRepository = activity.get<RoomSelectionRepository>()
|
||||
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,
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -7,15 +7,6 @@ interface RoomService {
|
|||
|
||||
fun getRoom(roomId: String): Room?
|
||||
|
||||
fun getAllRooms(): List<Room>
|
||||
|
||||
fun liveRooms(): LiveData<List<Room>>
|
||||
|
||||
fun liveRoomSummaries(): LiveData<List<RoomSummary>>
|
||||
|
||||
fun lastSelectedRoom(): RoomSummary?
|
||||
|
||||
fun saveLastSelectedRoom(roomSummary: RoomSummary)
|
||||
|
||||
|
||||
}
|
|
@ -69,31 +69,12 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi
|
|||
return roomService.getRoom(roomId)
|
||||
}
|
||||
|
||||
override fun getAllRooms(): List<Room> {
|
||||
assert(isOpen)
|
||||
return roomService.getAllRooms()
|
||||
}
|
||||
|
||||
override fun liveRooms(): LiveData<List<Room>> {
|
||||
assert(isOpen)
|
||||
return roomService.liveRooms()
|
||||
}
|
||||
|
||||
override fun liveRoomSummaries(): LiveData<List<RoomSummary>> {
|
||||
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? {
|
||||
|
|
|
@ -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<Room> {
|
||||
var rooms: List<Room> = 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<List<Room>> {
|
||||
return monarchy.findAllMappedWithChanges(
|
||||
{ realm -> RoomEntity.where(realm) },
|
||||
{ it.asDomain() }
|
||||
)
|
||||
}
|
||||
|
||||
override fun liveRoomSummaries(): LiveData<List<RoomSummary>> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue