Realm kotlin: migrate UserAccountDataDataSource
This commit is contained in:
parent
981ab5bc87
commit
5b0f7b7b7c
@ -18,22 +18,22 @@ package org.matrix.android.sdk.internal.session.user.accountdata
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Transformations
|
||||
import com.zhuinden.monarchy.Monarchy
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmQuery
|
||||
import androidx.lifecycle.asLiveData
|
||||
import io.realm.kotlin.TypedRealm
|
||||
import io.realm.kotlin.query.RealmQuery
|
||||
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
|
||||
import org.matrix.android.sdk.api.util.Optional
|
||||
import org.matrix.android.sdk.api.util.toOptional
|
||||
import org.matrix.android.sdk.internal.database.RealmSessionProvider
|
||||
import org.matrix.android.sdk.internal.database.RealmInstance
|
||||
import org.matrix.android.sdk.internal.database.andIf
|
||||
import org.matrix.android.sdk.internal.database.mapper.AccountDataMapper
|
||||
import org.matrix.android.sdk.internal.database.model.UserAccountDataEntity
|
||||
import org.matrix.android.sdk.internal.database.model.UserAccountDataEntityFields
|
||||
import org.matrix.android.sdk.internal.database.queryIn
|
||||
import org.matrix.android.sdk.internal.di.SessionDatabase
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class UserAccountDataDataSource @Inject constructor(
|
||||
@SessionDatabase private val monarchy: Monarchy,
|
||||
private val realmSessionProvider: RealmSessionProvider,
|
||||
@SessionDatabase private val realmInstance: RealmInstance,
|
||||
private val accountDataMapper: AccountDataMapper
|
||||
) {
|
||||
|
||||
@ -48,23 +48,20 @@ internal class UserAccountDataDataSource @Inject constructor(
|
||||
}
|
||||
|
||||
fun getAccountDataEvents(types: Set<String>): List<UserAccountDataEvent> {
|
||||
return realmSessionProvider.withRealm {
|
||||
accountDataEventsQuery(it, types).findAll().map(accountDataMapper::map)
|
||||
}
|
||||
val realm = realmInstance.getBlockingRealm()
|
||||
return accountDataEventsQuery(realm, types).find().map(accountDataMapper::map)
|
||||
}
|
||||
|
||||
fun getLiveAccountDataEvents(types: Set<String>): LiveData<List<UserAccountDataEvent>> {
|
||||
return monarchy.findAllMappedWithChanges(
|
||||
{ accountDataEventsQuery(it, types) },
|
||||
accountDataMapper::map
|
||||
)
|
||||
return realmInstance.queryList(accountDataMapper::map){
|
||||
accountDataEventsQuery(it, types)
|
||||
}.asLiveData()
|
||||
}
|
||||
|
||||
private fun accountDataEventsQuery(realm: Realm, types: Set<String>): RealmQuery<UserAccountDataEntity> {
|
||||
val query = realm.where(UserAccountDataEntity::class.java)
|
||||
if (types.isNotEmpty()) {
|
||||
query.`in`(UserAccountDataEntityFields.TYPE, types.toTypedArray())
|
||||
}
|
||||
return query
|
||||
private fun accountDataEventsQuery(realm: TypedRealm, types: Set<String>): RealmQuery<UserAccountDataEntity> {
|
||||
return realm.query(UserAccountDataEntity::class)
|
||||
.andIf(types.isNotEmpty()) {
|
||||
queryIn("type", types)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user