Create direct room: start creating all the required stuff

This commit is contained in:
ganfra
2019-07-17 18:30:14 +02:00
parent bb3b5788ba
commit 838003b68a
21 changed files with 442 additions and 21 deletions

View File

@ -36,6 +36,12 @@ interface UserService {
* @param userId the userId to look for.
* @return a Livedata of user with userId
*/
fun observeUser(userId: String): LiveData<User?>
fun liveUser(userId: String): LiveData<User?>
/**
* Observe a live list of users sorted alphabetically
* @return a Livedata of users
*/
fun liveUsers(): LiveData<List<User>>
}

View File

@ -24,6 +24,7 @@ import im.vector.matrix.android.api.session.user.model.User
import im.vector.matrix.android.internal.database.RealmLiveData
import im.vector.matrix.android.internal.database.mapper.asDomain
import im.vector.matrix.android.internal.database.model.UserEntity
import im.vector.matrix.android.internal.database.model.UserEntityFields
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.session.SessionScope
import im.vector.matrix.android.internal.util.fetchCopied
@ -33,12 +34,12 @@ internal class DefaultUserService @Inject constructor(private val monarchy: Mona
override fun getUser(userId: String): User? {
val userEntity = monarchy.fetchCopied { UserEntity.where(it, userId).findFirst() }
?: return null
?: return null
return userEntity.asDomain()
}
override fun observeUser(userId: String): LiveData<User?> {
override fun liveUser(userId: String): LiveData<User?> {
val liveRealmData = RealmLiveData(monarchy.realmConfiguration) { realm ->
UserEntity.where(realm, userId)
}
@ -48,4 +49,13 @@ internal class DefaultUserService @Inject constructor(private val monarchy: Mona
.firstOrNull()
}
}
override fun liveUsers(): LiveData<List<User>> {
val liveRealmData = RealmLiveData(monarchy.realmConfiguration) { realm ->
realm.where(UserEntity::class.java).sort(UserEntityFields.DISPLAY_NAME)
}
return Transformations.map(liveRealmData) { results ->
results.map { it.asDomain() }
}
}
}

View File

@ -16,6 +16,7 @@
package im.vector.matrix.android.internal.util
import im.vector.matrix.android.api.MatrixPatterns
import timber.log.Timber
/**
@ -49,3 +50,10 @@ fun convertFromUTF8(s: String): String? {
null
}
}
fun String?.firstLetterOfDisplayName(): String {
if (this.isNullOrEmpty()) return ""
val isUserId = MatrixPatterns.isUserId(this)
val firstLetterIndex = if (isUserId) 1 else 0
return this[firstLetterIndex].toString().toUpperCase()
}