Unverified Commit 81dc71d8 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito Committed by GitHub

Merge pull request #2159 from RocketChat/fix/multi-server

[FIX] Cannot use multiple accounts
parents 8d63b2e7 4238b8ca
......@@ -85,20 +85,20 @@ class LoginPresenter @Inject constructor(
}
}
val myself = retryIO("me()") { client.me() }
if (myself.username != null) {
myself.username?.let { username ->
val user = User(
id = myself.id,
roles = myself.roles,
status = myself.status,
name = myself.name,
emails = myself.emails?.map { Email(it.address ?: "", it.verified) },
username = myself.username,
username = username,
utcOffset = myself.utcOffset
)
localRepository.saveCurrentUser(currentServer, user)
saveCurrentServer.save(currentServer)
localRepository.save(LocalRepository.CURRENT_USERNAME_KEY, myself.username)
saveAccount(myself.username!!)
localRepository.save(LocalRepository.CURRENT_USERNAME_KEY, username)
saveAccount(username)
saveToken(token)
analyticsManager.logLogin(
AuthenticationEvent.AuthenticationWithUserAndPassword,
......@@ -133,7 +133,7 @@ class LoginPresenter @Inject constructor(
fun forgotPassword() = navigator.toForgotPassword()
private suspend fun saveAccount(username: String) {
private fun saveAccount(username: String) {
val icon = settings.favicon()?.let {
currentServer.serverLogoUrl(it)
}
......
......@@ -173,7 +173,7 @@ class LoginOptionsPresenter @Inject constructor(
settings = settingsInteractor.get(currentServer)
}
private suspend fun saveAccount(username: String) {
private fun saveAccount(username: String) {
val icon = settings.favicon()?.let {
currentServer.serverLogoUrl(it)
}
......
......@@ -94,7 +94,7 @@ class TwoFAPresenter @Inject constructor(
}
}
private suspend fun saveAccount(me: Myself) {
private fun saveAccount(me: Myself) {
val icon = settings.favicon()?.let {
currentServer.serverLogoUrl(it)
}
......
......@@ -210,7 +210,7 @@ class MainPresenter @Inject constructor(
}
}
private suspend fun saveAccount(uiModel: NavHeaderUiModel) {
private fun saveAccount(uiModel: NavHeaderUiModel) {
val icon = settings.favicon()?.let {
currentServer.serverLogoUrl(it)
}
......
......@@ -3,6 +3,7 @@ package chat.rocket.android.server.domain
import chat.rocket.android.server.domain.model.Account
interface AccountsRepository {
fun save(account: Account)
fun load(): List<Account>
fun remove(serverUrl: String)
......
......@@ -3,7 +3,8 @@ package chat.rocket.android.server.domain
import javax.inject.Inject
class GetAccountInteractor @Inject constructor(val repository: AccountsRepository) {
suspend fun get(url: String) = repository.load().firstOrNull { account ->
fun get(url: String) = repository.load().firstOrNull { account ->
url == account.serverUrl
}
}
\ No newline at end of file
......@@ -3,7 +3,8 @@ package chat.rocket.android.server.domain
import javax.inject.Inject
class RemoveAccountInteractor @Inject constructor(val repository: AccountsRepository) {
suspend fun remove(serverUrl: String) {
fun remove(serverUrl: String) {
repository.remove(serverUrl)
}
}
\ No newline at end of file
......@@ -4,5 +4,5 @@ import chat.rocket.android.server.domain.model.Account
import javax.inject.Inject
class SaveAccountInteractor @Inject constructor(val repository: AccountsRepository) {
suspend fun save(account: Account) = repository.save(account)
fun save(account: Account) = repository.save(account)
}
\ No newline at end of file
......@@ -15,7 +15,12 @@ class SharedPreferencesAccountsRepository(
) : AccountsRepository {
override fun save(account: Account) {
save(load().filter { item -> item.serverUrl != item.serverUrl }.toMutableList().apply { add(0, account) })
load().filter { it.serverUrl != account.serverUrl }
.toMutableList()
.apply {
add(0, account)
save(this)
}
}
override fun load(): List<Account> {
......@@ -23,7 +28,7 @@ class SharedPreferencesAccountsRepository(
val type = Types.newParameterizedType(List::class.java, Account::class.java)
val adapter = moshi.adapter<List<Account>>(type)
return json?.let { adapter.fromJson(it) ?: emptyList() } ?: emptyList()
return json?.let { adapter.fromJson(it) } ?: emptyList()
}
override fun remove(serverUrl: String) {
......@@ -33,8 +38,6 @@ class SharedPreferencesAccountsRepository(
private fun save(accounts: List<Account>) {
val type = Types.newParameterizedType(List::class.java, Account::class.java)
val adapter = moshi.adapter<List<Account>>(type)
preferences.edit {
putString(ACCOUNTS_KEY, adapter.toJson(accounts))
}
preferences.edit { putString(ACCOUNTS_KEY, adapter.toJson(accounts)) }
}
}
\ No newline at end of file
......@@ -3,27 +3,27 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="?selectableItemBackground">
android:background="?selectableItemBackground"
android:padding="16dp">
<ImageView
android:id="@+id/server_logo"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_add_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:text="@string/action_add_account"
app:layout_constraintTop_toTopOf="parent"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/server_logo"
app:layout_constraintEnd_toEndOf="parent" />
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
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