Rework FilterEntityQueries to fix issue of ghost Realm reference
This commit is contained in:
parent
7b4398404b
commit
3c6eb4bccf
|
@ -16,26 +16,27 @@
|
||||||
|
|
||||||
package im.vector.matrix.android.internal.database.query
|
package im.vector.matrix.android.internal.database.query
|
||||||
|
|
||||||
import im.vector.matrix.android.internal.database.awaitTransaction
|
|
||||||
import im.vector.matrix.android.internal.database.model.FilterEntity
|
import im.vector.matrix.android.internal.database.model.FilterEntity
|
||||||
import im.vector.matrix.android.internal.session.filter.FilterFactory
|
import im.vector.matrix.android.internal.session.filter.FilterFactory
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
|
import io.realm.kotlin.createObject
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current filter
|
||||||
|
*/
|
||||||
|
internal fun FilterEntity.Companion.get(realm: Realm): FilterEntity? {
|
||||||
|
return realm.where<FilterEntity>().findFirst()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current filter, create one if it does not exist
|
* Get the current filter, create one if it does not exist
|
||||||
*/
|
*/
|
||||||
internal suspend fun FilterEntity.Companion.getFilter(realm: Realm): FilterEntity {
|
internal fun FilterEntity.Companion.getOrCreate(realm: Realm): FilterEntity {
|
||||||
var filter = realm.where<FilterEntity>().findFirst()
|
return get(realm) ?: realm.createObject<FilterEntity>()
|
||||||
if (filter == null) {
|
.apply {
|
||||||
filter = FilterEntity().apply {
|
filterBodyJson = FilterFactory.createDefaultFilterBody().toJSONString()
|
||||||
filterBodyJson = FilterFactory.createDefaultFilterBody().toJSONString()
|
roomEventFilterJson = FilterFactory.createDefaultRoomFilter().toJSONString()
|
||||||
roomEventFilterJson = FilterFactory.createDefaultRoomFilter().toJSONString()
|
filterId = ""
|
||||||
filterId = ""
|
}
|
||||||
}
|
|
||||||
awaitTransaction(realm.configuration) {
|
|
||||||
it.insert(filter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filter
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,8 @@ package im.vector.matrix.android.internal.session.filter
|
||||||
import com.zhuinden.monarchy.Monarchy
|
import com.zhuinden.monarchy.Monarchy
|
||||||
import im.vector.matrix.android.internal.database.model.FilterEntity
|
import im.vector.matrix.android.internal.database.model.FilterEntity
|
||||||
import im.vector.matrix.android.internal.database.model.FilterEntityFields
|
import im.vector.matrix.android.internal.database.model.FilterEntityFields
|
||||||
import im.vector.matrix.android.internal.database.query.getFilter
|
import im.vector.matrix.android.internal.database.query.get
|
||||||
|
import im.vector.matrix.android.internal.database.query.getOrCreate
|
||||||
import im.vector.matrix.android.internal.util.awaitTransaction
|
import im.vector.matrix.android.internal.util.awaitTransaction
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
|
@ -29,9 +30,10 @@ internal class DefaultFilterRepository @Inject constructor(private val monarchy:
|
||||||
|
|
||||||
override suspend fun storeFilter(filterBody: FilterBody, roomEventFilter: RoomEventFilter): Boolean {
|
override suspend fun storeFilter(filterBody: FilterBody, roomEventFilter: RoomEventFilter): Boolean {
|
||||||
return Realm.getInstance(monarchy.realmConfiguration).use { realm ->
|
return Realm.getInstance(monarchy.realmConfiguration).use { realm ->
|
||||||
val filter = FilterEntity.getFilter(realm)
|
val filter = FilterEntity.get(realm)
|
||||||
// Filter has changed, or no filter Id yet
|
// Filter has changed, or no filter Id yet
|
||||||
filter.filterBodyJson != filterBody.toJSONString()
|
filter == null
|
||||||
|
|| filter.filterBodyJson != filterBody.toJSONString()
|
||||||
|| filter.filterId.isBlank()
|
|| filter.filterId.isBlank()
|
||||||
}.also { hasChanged ->
|
}.also { hasChanged ->
|
||||||
if (hasChanged) {
|
if (hasChanged) {
|
||||||
|
@ -42,7 +44,7 @@ internal class DefaultFilterRepository @Inject constructor(private val monarchy:
|
||||||
val filterBodyJson = filterBody.toJSONString()
|
val filterBodyJson = filterBody.toJSONString()
|
||||||
val roomEventFilterJson = roomEventFilter.toJSONString()
|
val roomEventFilterJson = roomEventFilter.toJSONString()
|
||||||
|
|
||||||
val filterEntity = FilterEntity.getFilter(realm)
|
val filterEntity = FilterEntity.getOrCreate(realm)
|
||||||
|
|
||||||
filterEntity.filterBodyJson = filterBodyJson
|
filterEntity.filterBodyJson = filterBodyJson
|
||||||
filterEntity.roomEventFilterJson = roomEventFilterJson
|
filterEntity.roomEventFilterJson = roomEventFilterJson
|
||||||
|
@ -68,7 +70,7 @@ internal class DefaultFilterRepository @Inject constructor(private val monarchy:
|
||||||
|
|
||||||
override suspend fun getFilter(): String {
|
override suspend fun getFilter(): String {
|
||||||
return Realm.getInstance(monarchy.realmConfiguration).use {
|
return Realm.getInstance(monarchy.realmConfiguration).use {
|
||||||
val filter = FilterEntity.getFilter(it)
|
val filter = FilterEntity.getOrCreate(it)
|
||||||
if (filter.filterId.isBlank()) {
|
if (filter.filterId.isBlank()) {
|
||||||
// Use the Json format
|
// Use the Json format
|
||||||
filter.filterBodyJson
|
filter.filterBodyJson
|
||||||
|
@ -81,7 +83,7 @@ internal class DefaultFilterRepository @Inject constructor(private val monarchy:
|
||||||
|
|
||||||
override suspend fun getRoomFilter(): String {
|
override suspend fun getRoomFilter(): String {
|
||||||
return Realm.getInstance(monarchy.realmConfiguration).use {
|
return Realm.getInstance(monarchy.realmConfiguration).use {
|
||||||
FilterEntity.getFilter(it).roomEventFilterJson
|
FilterEntity.getOrCreate(it).roomEventFilterJson
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue