mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-20 12:09:45 +01:00
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:
parent
a45166a9db
commit
a1a91c5c0f
@ -23,7 +23,6 @@
|
|||||||
|
|
||||||
package com.nextcloud.talk.ui.theme
|
package com.nextcloud.talk.ui.theme
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import com.nextcloud.talk.data.user.model.User
|
import com.nextcloud.talk.data.user.model.User
|
||||||
import com.nextcloud.talk.models.json.capabilities.Capabilities
|
import com.nextcloud.talk.models.json.capabilities.Capabilities
|
||||||
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
|
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
|
||||||
@ -37,25 +36,6 @@ internal class ServerThemeProviderImpl @Inject constructor(
|
|||||||
|
|
||||||
private val themeCache: ConcurrentHashMap<String, ServerTheme> = ConcurrentHashMap()
|
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 {
|
override fun getServerThemeForUser(user: User?): ServerTheme {
|
||||||
val url: String = if (user?.baseUrl != null) {
|
val url: String = if (user?.baseUrl != null) {
|
||||||
user.baseUrl!!
|
user.baseUrl!!
|
||||||
@ -71,7 +51,7 @@ internal class ServerThemeProviderImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getServerThemeForCurrentUser(): ServerTheme {
|
override fun getServerThemeForCurrentUser(): ServerTheme {
|
||||||
return getServerThemeForUser(currentUser)
|
return getServerThemeForUser(userProvider.currentUser.blockingGet())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getServerThemeForCapabilities(capabilities: Capabilities?): ServerTheme {
|
override fun getServerThemeForCapabilities(capabilities: Capabilities?): ServerTheme {
|
||||||
|
@ -28,25 +28,24 @@ import com.nextcloud.talk.data.user.model.User
|
|||||||
import com.nextcloud.talk.models.ExternalSignalingServer
|
import com.nextcloud.talk.models.ExternalSignalingServer
|
||||||
import com.nextcloud.talk.models.json.capabilities.Capabilities
|
import com.nextcloud.talk.models.json.capabilities.Capabilities
|
||||||
import com.nextcloud.talk.models.json.push.PushConfigurationState
|
import com.nextcloud.talk.models.json.push.PushConfigurationState
|
||||||
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
|
|
||||||
import io.reactivex.Maybe
|
import io.reactivex.Maybe
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.Single
|
import io.reactivex.Single
|
||||||
|
|
||||||
@Suppress("TooManyFunctions")
|
@Suppress("TooManyFunctions")
|
||||||
class UserManager internal constructor(private val userRepository: UsersRepository) : CurrentUserProviderNew {
|
class UserManager internal constructor(private val userRepository: UsersRepository) {
|
||||||
val users: Single<List<User>>
|
val users: Single<List<User>>
|
||||||
get() = userRepository.getUsers()
|
get() = userRepository.getUsers()
|
||||||
|
|
||||||
val usersScheduledForDeletion: Single<List<User>>
|
val usersScheduledForDeletion: Single<List<User>>
|
||||||
get() = userRepository.getUsersScheduledForDeletion()
|
get() = userRepository.getUsersScheduledForDeletion()
|
||||||
|
|
||||||
override val currentUser: Maybe<User>
|
val currentUser: Maybe<User>
|
||||||
get() {
|
get() {
|
||||||
return userRepository.getActiveUser()
|
return userRepository.getActiveUser()
|
||||||
}
|
}
|
||||||
|
|
||||||
override val currentUserObservable: Observable<User>
|
val currentUserObservable: Observable<User>
|
||||||
get() {
|
get() {
|
||||||
return userRepository.getActiveUserObservable()
|
return userRepository.getActiveUserObservable()
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
@ -22,9 +22,7 @@ package com.nextcloud.talk.utils.database.user
|
|||||||
|
|
||||||
import com.nextcloud.talk.data.user.model.User
|
import com.nextcloud.talk.data.user.model.User
|
||||||
import io.reactivex.Maybe
|
import io.reactivex.Maybe
|
||||||
import io.reactivex.Observable
|
|
||||||
|
|
||||||
interface CurrentUserProviderNew {
|
interface CurrentUserProviderNew {
|
||||||
val currentUser: Maybe<User>
|
val currentUser: Maybe<User>
|
||||||
val currentUserObservable: Observable<User>
|
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ import dagger.Provides
|
|||||||
abstract class UserModule {
|
abstract class UserModule {
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
abstract fun bindCurrentUserProviderNew(userManager: UserManager): CurrentUserProviderNew
|
abstract fun bindCurrentUserProviderNew(currentUserProviderImpl: CurrentUserProviderImpl): CurrentUserProviderNew
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@Provides
|
@Provides
|
||||||
|
Loading…
Reference in New Issue
Block a user