Get enum safe with realm entities

This commit is contained in:
ganfra 2019-08-06 21:32:40 +02:00
parent d9f448c9aa
commit 245fbe86d9
3 changed files with 29 additions and 27 deletions

View File

@ -18,19 +18,19 @@ package im.vector.matrix.android.internal.database.model


import im.vector.matrix.android.api.session.room.model.Membership import im.vector.matrix.android.api.session.room.model.Membership
import io.realm.RealmObject import io.realm.RealmObject
import io.realm.annotations.Ignore
import io.realm.annotations.PrimaryKey import io.realm.annotations.PrimaryKey
import kotlin.properties.Delegates


internal open class GroupEntity(@PrimaryKey var groupId: String = "" internal open class GroupEntity(@PrimaryKey var groupId: String = ""


) : RealmObject() { ) : RealmObject() {


private var membershipStr: String = Membership.NONE.name private var membershipStr: String = Membership.NONE.name

var membership: Membership
@delegate:Ignore get() {
var membership: Membership by Delegates.observable(Membership.valueOf(membershipStr)) { _, _, newValue -> return Membership.valueOf(membershipStr)
membershipStr = newValue.name }
set(value) {
membershipStr = value.name
} }


companion object companion object

View File

@ -19,9 +19,7 @@ package im.vector.matrix.android.internal.database.model
import im.vector.matrix.android.api.session.room.model.Membership import im.vector.matrix.android.api.session.room.model.Membership
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmObject import io.realm.RealmObject
import io.realm.annotations.Ignore
import io.realm.annotations.PrimaryKey import io.realm.annotations.PrimaryKey
import kotlin.properties.Delegates


internal open class RoomEntity(@PrimaryKey var roomId: String = "", internal open class RoomEntity(@PrimaryKey var roomId: String = "",
var chunks: RealmList<ChunkEntity> = RealmList(), var chunks: RealmList<ChunkEntity> = RealmList(),
@ -31,10 +29,12 @@ internal open class RoomEntity(@PrimaryKey var roomId: String = "",
) : RealmObject() { ) : RealmObject() {


private var membershipStr: String = Membership.NONE.name private var membershipStr: String = Membership.NONE.name

var membership: Membership
@delegate:Ignore get() {
var membership: Membership by Delegates.observable(Membership.valueOf(membershipStr)) { _, _, newValue -> return Membership.valueOf(membershipStr)
membershipStr = newValue.name }
set(value) {
membershipStr = value.name
} }


companion object companion object

View File

@ -20,9 +20,7 @@ import im.vector.matrix.android.api.session.room.model.Membership
import im.vector.matrix.android.api.session.room.model.VersioningState import im.vector.matrix.android.api.session.room.model.VersioningState
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmObject import io.realm.RealmObject
import io.realm.annotations.Ignore
import io.realm.annotations.PrimaryKey import io.realm.annotations.PrimaryKey
import kotlin.properties.Delegates


internal open class RoomSummaryEntity(@PrimaryKey var roomId: String = "", internal open class RoomSummaryEntity(@PrimaryKey var roomId: String = "",
var displayName: String? = "", var displayName: String? = "",
@ -41,17 +39,21 @@ internal open class RoomSummaryEntity(@PrimaryKey var roomId: String = "",
) : RealmObject() { ) : RealmObject() {


private var membershipStr: String = Membership.NONE.name private var membershipStr: String = Membership.NONE.name
private var versioningStateStr: String = VersioningState.NONE.name var membership: Membership

get() {

return Membership.valueOf(membershipStr)
@delegate:Ignore }
var membership: Membership by Delegates.observable(Membership.valueOf(membershipStr)) { _, _, newValue -> set(value) {
membershipStr = newValue.name membershipStr = value.name
} }


@delegate:Ignore private var versioningStateStr: String = VersioningState.NONE.name
var versioningState: VersioningState by Delegates.observable(VersioningState.valueOf(versioningStateStr)) { _, _, newValue -> var versioningState: VersioningState
versioningStateStr = newValue.name get() {
return VersioningState.valueOf(versioningStateStr)
}
set(value) {
versioningStateStr = value.name
} }


companion object companion object