Automatically update current user with room observable

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-07-15 14:24:13 +02:00 committed by Andy Scherzinger
parent 9bbeb9b420
commit a8e99862f1
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
8 changed files with 44 additions and 3 deletions

View file

@ -93,6 +93,7 @@ import com.nextcloud.talk.repositories.unifiedsearch.UnifiedSearchRepository;
import com.nextcloud.talk.ui.dialog.ChooseAccountDialogFragment;
import com.nextcloud.talk.ui.dialog.ConversationsListBottomDialog;
import com.nextcloud.talk.ui.theme.ServerTheme;
import com.nextcloud.talk.ui.theme.ServerThemeProvider;
import com.nextcloud.talk.ui.theme.ViewThemeUtils;
import com.nextcloud.talk.users.UserManager;
import com.nextcloud.talk.utils.ApiUtils;

View file

@ -32,6 +32,7 @@ import androidx.room.Transaction
import androidx.room.Update
import com.nextcloud.talk.data.user.model.UserEntity
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@Dao
@ -41,6 +42,10 @@ abstract class UsersDao {
@Query("SELECT * FROM User where current = 1")
abstract fun getActiveUser(): Maybe<UserEntity>
// get active user
@Query("SELECT * FROM User where current = 1")
abstract fun getActiveUserObservable(): Observable<UserEntity>
@Query("SELECT * FROM User where current = 1")
abstract fun getActiveUserSynchronously(): UserEntity?

View file

@ -24,11 +24,13 @@ package com.nextcloud.talk.data.user
import com.nextcloud.talk.data.user.model.User
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@Suppress("TooManyFunctions")
interface UsersRepository {
fun getActiveUser(): Maybe<User>
fun getActiveUserObservable(): Observable<User>
fun getUsers(): Single<List<User>>
fun getUserWithId(id: Long): Maybe<User>
fun getUserWithIdNotScheduledForDeletion(id: Long): Maybe<User>

View file

@ -24,6 +24,7 @@ package com.nextcloud.talk.data.user
import com.nextcloud.talk.data.user.model.User
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@Suppress("TooManyFunctions")
@ -33,6 +34,10 @@ class UsersRepositoryImpl(private val usersDao: UsersDao) : UsersRepository {
return usersDao.getActiveUser().map { UserMapper.toModel(it) }
}
override fun getActiveUserObservable(): Observable<User> {
return usersDao.getActiveUserObservable().map { UserMapper.toModel(it) }
}
override fun getUsers(): Single<List<User>> {
return usersDao.getUsers().map { UserMapper.toModel(it) }
}

View file

@ -26,6 +26,6 @@ import com.nextcloud.talk.models.json.capabilities.Capabilities
interface ServerThemeProvider {
fun getServerThemeForUser(user: User): ServerTheme
fun getServerThemeForCurrentUser(): ServerTheme
fun getServerThemeForCapabilities(capabilities: Capabilities): ServerTheme
fun getServerThemeForCurrentUser(): ServerTheme
}

View file

@ -21,6 +21,7 @@
package com.nextcloud.talk.ui.theme
import android.annotation.SuppressLint
import android.content.Context
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.json.capabilities.Capabilities
@ -32,12 +33,31 @@ internal class ServerThemeProviderImpl(
private val userProvider: CurrentUserProviderNew
) :
ServerThemeProvider {
// TODO move this logic to currentUserProvider or something
private var _currentUser: User? = null
private val currentUser: User
@SuppressLint("CheckResult")
get() {
return when (_currentUser) {
null -> {
// immediately get a result synchronously
_currentUser = userProvider.currentUser.blockingGet()
// start observable for auto-updates
userProvider.currentUserObservable.subscribe { _currentUser = it }
_currentUser!!
}
else -> {
_currentUser!!
}
}
}
override fun getServerThemeForUser(user: User): ServerTheme {
return getServerThemeForCapabilities(user.capabilities!!)
}
override fun getServerThemeForCurrentUser(): ServerTheme {
return getServerThemeForUser(userProvider.currentUser.blockingGet())
return getServerThemeForUser(currentUser)
}
override fun getServerThemeForCapabilities(capabilities: Capabilities): ServerTheme {

View file

@ -30,6 +30,7 @@ import com.nextcloud.talk.models.json.capabilities.Capabilities
import com.nextcloud.talk.models.json.push.PushConfigurationState
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@Suppress("TooManyFunctions")
@ -45,7 +46,12 @@ class UserManager internal constructor(private val userRepository: UsersReposito
return userRepository.getActiveUser()
}
fun deleteUser(internalId: Long): Int {
override val currentUserObservable: Observable<User>
get() {
return userRepository.getActiveUserObservable()
}
fun deleteUser(internalId: Long) {
return userRepository.deleteUser(userRepository.getUserWithId(internalId).blockingGet())
}

View file

@ -22,7 +22,9 @@ package com.nextcloud.talk.utils.database.user
import com.nextcloud.talk.data.user.model.User
import io.reactivex.Maybe
import io.reactivex.Observable
interface CurrentUserProviderNew {
val currentUser: Maybe<User>
val currentUserObservable: Observable<User>
}