Move current user listening logic out of UserModule and into a new CurrentUserProvider

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-08-05 17:06:10 +02:00 committed by Andy Scherzinger
parent a45166a9db
commit a1a91c5c0f
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
5 changed files with 54 additions and 28 deletions

View File

@ -23,7 +23,6 @@
package com.nextcloud.talk.ui.theme
import android.annotation.SuppressLint
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.json.capabilities.Capabilities
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
@ -37,25 +36,6 @@ internal class ServerThemeProviderImpl @Inject constructor(
private val themeCache: ConcurrentHashMap<String, ServerTheme> = ConcurrentHashMap()
// 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 {
val url: String = if (user?.baseUrl != null) {
user.baseUrl!!
@ -71,7 +51,7 @@ internal class ServerThemeProviderImpl @Inject constructor(
}
override fun getServerThemeForCurrentUser(): ServerTheme {
return getServerThemeForUser(currentUser)
return getServerThemeForUser(userProvider.currentUser.blockingGet())
}
override fun getServerThemeForCapabilities(capabilities: Capabilities?): ServerTheme {

View File

@ -28,25 +28,24 @@ import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.ExternalSignalingServer
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")
class UserManager internal constructor(private val userRepository: UsersRepository) : CurrentUserProviderNew {
class UserManager internal constructor(private val userRepository: UsersRepository) {
val users: Single<List<User>>
get() = userRepository.getUsers()
val usersScheduledForDeletion: Single<List<User>>
get() = userRepository.getUsersScheduledForDeletion()
override val currentUser: Maybe<User>
val currentUser: Maybe<User>
get() {
return userRepository.getActiveUser()
}
override val currentUserObservable: Observable<User>
val currentUserObservable: Observable<User>
get() {
return userRepository.getActiveUserObservable()
}

View File

@ -0,0 +1,49 @@
/*
* Nextcloud Talk application
*
* @author Álvaro Brey
* Copyright (C) 2022 Álvaro Brey
* Copyright (C) 2022 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils.database.user
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.users.UserManager
import io.reactivex.Maybe
import io.reactivex.disposables.Disposable
import javax.inject.Inject
/**
* Listens to changes in the database and provides the current user without needing to query the database everytime.
*/
class CurrentUserProviderImpl @Inject constructor(private val userManager: UserManager) : CurrentUserProviderNew {
private var _currentUser: User? = null
private var currentUserObserver: Disposable? = null
override val currentUser: Maybe<User>
get() {
if (_currentUser == null) {
// immediately get a result synchronously
_currentUser = userManager.currentUser.blockingGet()
if (currentUserObserver == null) {
// start observable for auto-updates
currentUserObserver = userManager.currentUserObservable.subscribe { _currentUser = it }
}
}
return _currentUser?.let { Maybe.just(it) } ?: Maybe.empty()
}
}

View File

@ -22,9 +22,7 @@ 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>
}

View File

@ -30,7 +30,7 @@ import dagger.Provides
abstract class UserModule {
@Binds
abstract fun bindCurrentUserProviderNew(userManager: UserManager): CurrentUserProviderNew
abstract fun bindCurrentUserProviderNew(currentUserProviderImpl: CurrentUserProviderImpl): CurrentUserProviderNew
companion object {
@Provides