forked from GitHub-Mirror/riotX-android
Introduce MvRx in the application + start managing UI
This commit is contained in:
@ -2,22 +2,12 @@ package im.vector.matrix.android.api.failure
|
||||
|
||||
import java.io.IOException
|
||||
|
||||
sealed class Failure {
|
||||
sealed class Failure(cause: Throwable? = null) : Throwable(cause = cause) {
|
||||
|
||||
data class Unknown(val exception: Exception? = null) : Failure()
|
||||
data class NetworkConnection(val ioException: IOException) : Failure()
|
||||
data class ServerError(val error: MatrixError) : Failure()
|
||||
data class Unknown(val throwable: Throwable? = null) : Failure(throwable)
|
||||
data class NetworkConnection(val ioException: IOException) : Failure(ioException)
|
||||
data class ServerError(val error: MatrixError) : Failure(RuntimeException(error.toString()))
|
||||
|
||||
abstract class FeatureFailure : Failure()
|
||||
|
||||
fun toException(): Exception {
|
||||
return when (this) {
|
||||
is Unknown -> this.exception ?: RuntimeException("Unknown error")
|
||||
is NetworkConnection -> this.ioException
|
||||
is ServerError -> RuntimeException(this.error.toString())
|
||||
is FeatureFailure -> RuntimeException("Feature error")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -13,5 +13,9 @@ interface RoomService {
|
||||
|
||||
fun liveRoomSummaries(): LiveData<List<RoomSummary>>
|
||||
|
||||
fun lastSelectedRoom(): RoomSummary?
|
||||
|
||||
fun saveLastSelectedRoom(roomSummary: RoomSummary)
|
||||
|
||||
|
||||
}
|
@ -13,8 +13,20 @@ object RoomSummaryMapper {
|
||||
roomSummaryEntity.topic ?: ""
|
||||
)
|
||||
}
|
||||
|
||||
internal fun map(roomSummary: RoomSummary): RoomSummaryEntity {
|
||||
return RoomSummaryEntity(
|
||||
roomSummary.roomId,
|
||||
roomSummary.displayName,
|
||||
roomSummary.topic
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun RoomSummaryEntity.asDomain(): RoomSummary {
|
||||
return RoomSummaryMapper.map(this)
|
||||
}
|
||||
|
||||
fun RoomSummaryEntity.asEntity(): RoomSummary {
|
||||
return RoomSummaryMapper.map(this)
|
||||
}
|
@ -11,7 +11,8 @@ open class RoomSummaryEntity(@PrimaryKey var roomId: String = "",
|
||||
var lastMessage: EventEntity? = null,
|
||||
var heroes: RealmList<String> = RealmList(),
|
||||
var joinedMembersCount: Int? = 0,
|
||||
var invitedMembersCount: Int? = 0
|
||||
var invitedMembersCount: Int? = 0,
|
||||
var isLatestSelected: Boolean = false
|
||||
) : RealmObject() {
|
||||
|
||||
companion object
|
||||
|
@ -13,3 +13,9 @@ fun RoomSummaryEntity.Companion.where(realm: Realm, roomId: String? = null): Rea
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
fun RoomSummaryEntity.Companion.lastSelected(realm: Realm): RoomSummaryEntity? {
|
||||
return realm.where<RoomSummaryEntity>()
|
||||
.equalTo(RoomSummaryEntityFields.IS_LATEST_SELECTED, true)
|
||||
.findFirst()
|
||||
}
|
||||
|
@ -75,6 +75,14 @@ class DefaultSession(override val sessionParams: SessionParams) : Session, KoinC
|
||||
return roomService.liveRoomSummaries()
|
||||
}
|
||||
|
||||
override fun lastSelectedRoom(): RoomSummary? {
|
||||
return roomService.lastSelectedRoom()
|
||||
}
|
||||
|
||||
override fun saveLastSelectedRoom(roomSummary: RoomSummary) {
|
||||
roomService.saveLastSelectedRoom(roomSummary)
|
||||
}
|
||||
|
||||
// Private methods *****************************************************************************
|
||||
|
||||
private fun checkIsMainThread() {
|
||||
|
@ -8,6 +8,7 @@ import im.vector.matrix.android.api.session.room.model.RoomSummary
|
||||
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.query.lastSelected
|
||||
import im.vector.matrix.android.internal.database.query.where
|
||||
|
||||
class DefaultRoomService(private val monarchy: Monarchy) : RoomService {
|
||||
@ -42,5 +43,20 @@ class DefaultRoomService(private val monarchy: Monarchy) : RoomService {
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
@ -53,7 +53,7 @@ class TimelineBoundaryCallback(private val roomId: String,
|
||||
}
|
||||
|
||||
override fun onFailure(failure: Failure) {
|
||||
pagingRequestCallback.recordFailure(failure.toException())
|
||||
pagingRequestCallback.recordFailure(failure)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user