-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multi accounts pt.1
- Loading branch information
Showing
22 changed files
with
487 additions
and
71 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
app/src/main/java/tech/relaycorp/letro/account/SwitchAccountViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package tech.relaycorp.letro.account | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import tech.relaycorp.letro.account.model.Account | ||
import tech.relaycorp.letro.account.storage.repository.AccountRepository | ||
import tech.relaycorp.letro.account.utils.AccountsSorter | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SwitchAccountViewModel @Inject constructor( | ||
private val accountRepository: AccountRepository, | ||
private val accountsSorter: AccountsSorter, | ||
) : ViewModel() { | ||
|
||
private val accounts: MutableStateFlow<List<Account>> = MutableStateFlow(emptyList()) | ||
|
||
private val _switchAccountsBottomSheetState = MutableStateFlow(SwitchAccountsBottomSheetState()) | ||
val switchAccountBottomSheetState: StateFlow<SwitchAccountsBottomSheetState> | ||
get() = _switchAccountsBottomSheetState | ||
|
||
init { | ||
viewModelScope.launch { | ||
accountRepository.allAccounts.collect { | ||
accounts.emit( | ||
accountsSorter.withCurrentAccountFirst(it), | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun onSwitchAccountsClick() { | ||
setSwitchBottomSheetVisible(true) | ||
} | ||
|
||
fun onSwitchAccountRequested(account: Account) { | ||
setSwitchBottomSheetVisible(false) | ||
viewModelScope.launch(Dispatchers.IO) { | ||
accountRepository.switchAccount(account) | ||
} | ||
} | ||
|
||
fun onSwitchAccountDialogDismissed() { | ||
setSwitchBottomSheetVisible(false) | ||
} | ||
|
||
suspend fun onSwitchAccountRequested(accountId: String) { | ||
setSwitchBottomSheetVisible(false) | ||
accountRepository.switchAccount(accountId) | ||
} | ||
|
||
private fun setSwitchBottomSheetVisible(isVisible: Boolean) { | ||
_switchAccountsBottomSheetState.update { | ||
it.copy( | ||
isShown = isVisible, | ||
accounts = if (isVisible) accounts.value else emptyList(), | ||
) | ||
} | ||
} | ||
} | ||
|
||
data class SwitchAccountsBottomSheetState( | ||
val isShown: Boolean = false, | ||
val accounts: List<Account> = emptyList(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
app/src/main/java/tech/relaycorp/letro/account/ui/SwitchAccountsBottomSheet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package tech.relaycorp.letro.account.ui | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextOverflow | ||
import androidx.compose.ui.unit.dp | ||
import tech.relaycorp.letro.R | ||
import tech.relaycorp.letro.account.model.Account | ||
import tech.relaycorp.letro.ui.common.bottomsheet.LetroBottomSheet | ||
import tech.relaycorp.letro.ui.theme.BodyMediumProminent | ||
|
||
@Composable | ||
fun SwitchAccountsBottomSheet( | ||
accounts: List<Account>, | ||
onAccountClick: (Account) -> Unit, | ||
onManageContactsClick: () -> Unit, | ||
onDismissRequest: () -> Unit, | ||
) { | ||
LetroBottomSheet( | ||
onDismissRequest = { onDismissRequest() }, | ||
title = stringResource(id = R.string.switch_accounts), | ||
) { | ||
Column { | ||
for (i in accounts.indices) { | ||
Account( | ||
account = accounts[i], | ||
onClick = { onAccountClick(accounts[i]) }, | ||
) | ||
} | ||
ManageContactsButton(onClick = onManageContactsClick) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun Account( | ||
account: Account, | ||
onClick: () -> Unit, | ||
) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.clickable { onClick() } | ||
.padding( | ||
horizontal = 16.dp, | ||
vertical = 14.dp, | ||
), | ||
) { | ||
Text( | ||
text = account.accountId, | ||
style = MaterialTheme.typography.labelLarge, | ||
color = MaterialTheme.colorScheme.onSurface, | ||
maxLines = 1, | ||
overflow = TextOverflow.Ellipsis, | ||
modifier = Modifier.weight(1f), | ||
) | ||
Icon( | ||
painter = painterResource(id = if (account.isCurrent) R.drawable.radio_button_selected else R.drawable.radio_button_unselected), | ||
contentDescription = stringResource(id = if (account.isCurrent) R.string.content_description_selected_item else R.string.content_description_unselected_item), | ||
tint = MaterialTheme.colorScheme.onSurface, | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun ManageContactsButton( | ||
onClick: () -> Unit, | ||
) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.clickable { onClick() } | ||
.padding( | ||
horizontal = 16.dp, | ||
vertical = 14.dp, | ||
), | ||
) { | ||
Icon( | ||
painter = painterResource(id = R.drawable.ic_settings_18), | ||
contentDescription = null, | ||
tint = MaterialTheme.colorScheme.primary, | ||
) | ||
Spacer(modifier = Modifier.width(8.dp)) | ||
Text( | ||
text = stringResource(id = R.string.manage_accounts), | ||
style = MaterialTheme.typography.BodyMediumProminent, | ||
color = MaterialTheme.colorScheme.primary, | ||
) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/tech/relaycorp/letro/account/utils/AccountsSorter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package tech.relaycorp.letro.account.utils | ||
|
||
import tech.relaycorp.letro.account.model.Account | ||
import javax.inject.Inject | ||
|
||
interface AccountsSorter { | ||
fun withCurrentAccountFirst(accounts: List<Account>): List<Account> | ||
} | ||
|
||
class AccountsSorterImpl @Inject constructor() : AccountsSorter { | ||
|
||
override fun withCurrentAccountFirst(accounts: List<Account>): List<Account> { | ||
val currentIndex = accounts.indexOfFirst { it.isCurrent } | ||
if (currentIndex == -1) { | ||
return accounts | ||
} | ||
return arrayListOf<Account>().apply { | ||
add(accounts[currentIndex]) | ||
for (i in accounts.indices) { | ||
if (currentIndex == i) continue | ||
add(accounts[i]) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.