Commit 964d3017 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Gets the permission list.

parent 9500621e
...@@ -11,6 +11,7 @@ import chat.rocket.android.server.domain.GetCurrentServerInteractor ...@@ -11,6 +11,7 @@ import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.GetSettingsInteractor import chat.rocket.android.server.domain.GetSettingsInteractor
import chat.rocket.android.server.domain.PublicSettings import chat.rocket.android.server.domain.PublicSettings
import chat.rocket.android.server.domain.RefreshSettingsInteractor import chat.rocket.android.server.domain.RefreshSettingsInteractor
import chat.rocket.android.server.domain.RefreshPermissionsInteractor
import chat.rocket.android.server.domain.RemoveAccountInteractor import chat.rocket.android.server.domain.RemoveAccountInteractor
import chat.rocket.android.server.domain.SaveAccountInteractor import chat.rocket.android.server.domain.SaveAccountInteractor
import chat.rocket.android.server.domain.TokenRepository import chat.rocket.android.server.domain.TokenRepository
...@@ -46,6 +47,7 @@ class MainPresenter @Inject constructor( ...@@ -46,6 +47,7 @@ class MainPresenter @Inject constructor(
private val tokenRepository: TokenRepository, private val tokenRepository: TokenRepository,
private val serverInteractor: GetCurrentServerInteractor, private val serverInteractor: GetCurrentServerInteractor,
private val refreshSettingsInteractor: RefreshSettingsInteractor, private val refreshSettingsInteractor: RefreshSettingsInteractor,
private val refreshPermissionsInteractor: RefreshPermissionsInteractor,
private val localRepository: LocalRepository, private val localRepository: LocalRepository,
private val navHeaderMapper: NavHeaderUiModelMapper, private val navHeaderMapper: NavHeaderUiModelMapper,
private val saveAccountInteractor: SaveAccountInteractor, private val saveAccountInteractor: SaveAccountInteractor,
...@@ -157,6 +159,7 @@ class MainPresenter @Inject constructor( ...@@ -157,6 +159,7 @@ class MainPresenter @Inject constructor(
fun connect() { fun connect() {
refreshSettingsInteractor.refreshAsync(currentServer) refreshSettingsInteractor.refreshAsync(currentServer)
refreshPermissionsInteractor.refreshAsync(currentServer)
manager.connect() manager.connect()
} }
......
...@@ -5,12 +5,12 @@ import chat.rocket.core.model.Permission ...@@ -5,12 +5,12 @@ import chat.rocket.core.model.Permission
interface PermissionsRepository { interface PermissionsRepository {
/** /**
* Stores [permission] locally. * Stores a list of [Permission] locally.
* *
* @param url The server url to store the permission. * @param url The server url to store the permission.
* @param permission The permission to store. * @param permissionList The permission list to store.
*/ */
fun save(url: String, permission: Permission) fun save(url: String, permissionList: List<Permission>)
/** /**
* Gets permission given by the [permissionId] and for the server [url]. * Gets permission given by the [permissionId] and for the server [url].
......
package chat.rocket.android.server.domain
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.retryIO
import chat.rocket.core.internal.rest.permissions
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.launch
import timber.log.Timber
import javax.inject.Inject
/**
* This class reloads the current logged server permission whenever its used.
*/
class RefreshPermissionsInteractor @Inject constructor(
private val factory: RocketChatClientFactory,
private val repository: PermissionsRepository
) {
fun refreshAsync(server: String) {
launch(CommonPool) {
try {
factory.create(server).let { client ->
val permissions = retryIO(
description = "permissions",
times = 5,
maxDelay = 5000,
initialDelay = 300
) {
client.permissions()
}
repository.save(server, permissions)
}
} catch (ex: Exception) {
Timber.e(ex, "Error refreshing permissions for: $server")
}
}
}
}
\ No newline at end of file
...@@ -9,6 +9,9 @@ import kotlinx.coroutines.experimental.withContext ...@@ -9,6 +9,9 @@ import kotlinx.coroutines.experimental.withContext
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
/**
* This class reloads the current logged server settings whenever needed.
*/
class RefreshSettingsInteractor @Inject constructor( class RefreshSettingsInteractor @Inject constructor(
private val factory: RocketChatClientFactory, private val factory: RocketChatClientFactory,
private val repository: SettingsRepository private val repository: SettingsRepository
......
package chat.rocket.android.server.domain
import chat.rocket.core.model.Value
import javax.inject.Inject
class SaveSettingsInteractor @Inject constructor(private val repository: SettingsRepository) {
fun save(url: String, settings: Map<String, Value<Any>>) = repository.save(url, settings)
}
\ No newline at end of file
...@@ -9,11 +9,12 @@ class SharedPreferencesPermissionsRepository( ...@@ -9,11 +9,12 @@ class SharedPreferencesPermissionsRepository(
private val localRepository: LocalRepository, private val localRepository: LocalRepository,
moshi: Moshi moshi: Moshi
) : PermissionsRepository { ) : PermissionsRepository {
private val adapter = moshi.adapter(Permission::class.java) private val adapter = moshi.adapter(Permission::class.java)
override fun save(url: String, permission: Permission) { override fun save(url: String, permissionList: List<Permission>) {
localRepository.save(getPermissionKey(url, permission.id), adapter.toJson(permission)) for (permission in permissionList) {
localRepository.save(getPermissionKey(url, permission.id), adapter.toJson(permission))
}
} }
override fun get(url: String, permissionId: String): Permission? { override fun get(url: String, permissionId: String): Permission? {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment