Добавлен Yandex

This commit is contained in:
2026-04-19 00:22:05 +03:00
parent 586e2b61fd
commit b3c00b1719
24 changed files with 710 additions and 49 deletions

View File

@@ -19,7 +19,7 @@ import com.github.nullptroma.wallenc.domain.tasks.TaskLogLevel
import com.github.nullptroma.wallenc.presentation.ViewModelBase
import com.github.nullptroma.wallenc.presentation.extensions.toPrintable
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
@@ -70,26 +70,24 @@ class LocalVaultViewModel @Inject constructor(
private fun collectFlows() {
viewModelScope.launch {
manageLocalVaultUseCase.localStorages.combine(getOpenedStoragesUseCase.openedStorages) { local, opened ->
if(local == null)
return@combine null
if (local == null) {
return@combine Pair(true, emptyList<Tree<IStorageInfo>>())
}
val list = mutableListOf<Tree<IStorageInfo>>()
for (storage in local) {
var tree = Tree(storage)
list.add(tree)
while(opened.containsKey(tree.value.uuid)) {
while (opened.containsKey(tree.value.uuid)) {
val child = opened.getValue(tree.value.uuid)
val nextTree = Tree(child)
tree.children = listOf(nextTree)
tree = nextTree
}
}
return@combine list
}.collectLatest {
isLoading = it == null
val newState = state.value.copy(
storagesList = it ?: listOf()
)
updateState(newState)
return@combine Pair(false, list)
}.collect { (loading, trees) ->
isLoading = loading
updateState(state.value.copy(storagesList = trees))
}
}
}

View File

@@ -1,15 +1,241 @@
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
import androidx.compose.material3.ExperimentalMaterial3Api
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.runtime.getValue
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.github.nullptroma.wallenc.domain.auth.RemoteYandexAuthResult
import com.github.nullptroma.wallenc.domain.enums.VaultType
import com.github.nullptroma.wallenc.presentation.R
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun RemoteVaultsScreen(modifier: Modifier = Modifier,
viewModel: RemoteVaultsViewModel = hiltViewModel()) {
Text("Remote vault screen", modifier = modifier)
}
fun RemoteVaultsScreen(
modifier: Modifier = Modifier,
viewModel: RemoteVaultsViewModel = hiltViewModel(),
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val context = LocalContext.current
Box {
Scaffold(
modifier = modifier,
contentWindowInsets = WindowInsets(0.dp),
floatingActionButton = {
FloatingActionButton(
onClick = {
if (!uiState.isBusy) viewModel.setAddChoiceVisible(true)
},
) {
Icon(
Icons.Filled.Add,
contentDescription = stringResource(R.string.remote_vaults_add_cd),
)
}
},
) { innerPadding ->
if (uiState.vaults.isEmpty()) {
Text(
text = stringResource(R.string.remote_vaults_empty_hint),
modifier = Modifier
.padding(innerPadding)
.padding(24.dp),
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
} else {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
items(uiState.vaults, key = { it.uuid }) { item ->
ElevatedCard(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(16.dp),
elevation = CardDefaults.elevatedCardElevation(defaultElevation = 2.dp),
colors = CardDefaults.elevatedCardColors(
containerColor = MaterialTheme.colorScheme.surfaceContainerHigh,
),
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
) {
Column(modifier = Modifier.weight(1f)) {
Text(
text = item.label,
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onSurface,
)
Spacer(modifier = Modifier.height(4.dp))
Text(
text = when (item.type) {
VaultType.YANDEX ->
stringResource(R.string.remote_vault_type_yandex)
else -> item.type.name
},
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
IconButton(
onClick = { viewModel.requestDeleteVault(item) },
enabled = !uiState.isBusy,
) {
Icon(
Icons.Filled.Delete,
contentDescription = stringResource(R.string.remote_vault_delete_cd),
tint = MaterialTheme.colorScheme.error,
)
}
}
}
}
}
}
}
if (uiState.isBusy) {
CircularProgressIndicator(
modifier = Modifier
.align(Alignment.Center)
.padding(24.dp),
)
}
}
if (uiState.addChoiceVisible) {
Dialog(
onDismissRequest = { if (!uiState.isBusy) viewModel.setAddChoiceVisible(false) },
) {
Surface(
shape = RoundedCornerShape(28.dp),
color = MaterialTheme.colorScheme.surfaceContainerHigh,
tonalElevation = 3.dp,
) {
Column(
modifier = Modifier
.padding(24.dp)
.fillMaxWidth(),
) {
Text(
text = stringResource(R.string.remote_vaults_add_title),
style = MaterialTheme.typography.headlineSmall,
color = MaterialTheme.colorScheme.onSurface,
)
Spacer(modifier = Modifier.height(20.dp))
FilledTonalButton(
onClick = {
viewModel.setAddChoiceVisible(false)
viewModel.yandexSignIn.launch { outcome ->
when (outcome) {
is RemoteYandexAuthResult.Success ->
viewModel.onYandexAuthSuccess(outcome.accessToken)
is RemoteYandexAuthResult.Failure ->
Toast.makeText(context, outcome.message, Toast.LENGTH_LONG)
.show()
RemoteYandexAuthResult.Cancelled -> { }
}
}
},
modifier = Modifier.fillMaxWidth(),
enabled = !uiState.isBusy,
) {
Text(stringResource(R.string.remote_vaults_provider_yandex))
}
Spacer(modifier = Modifier.height(16.dp))
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End,
) {
Text(
text = stringResource(R.string.remote_vaults_add_cancel),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.primary,
modifier = Modifier
.clickable(
enabled = !uiState.isBusy,
interactionSource = remember { MutableInteractionSource() },
indication = null,
) { viewModel.setAddChoiceVisible(false) }
.padding(vertical = 8.dp, horizontal = 4.dp),
)
}
}
}
}
}
uiState.vaultPendingDelete?.let { pending ->
AlertDialog(
onDismissRequest = { if (!uiState.isBusy) viewModel.dismissDeleteVault() },
title = {
Text(stringResource(R.string.remote_vault_remove_title))
},
text = {
Text(stringResource(R.string.remote_vault_remove_message, pending.label))
},
confirmButton = {
TextButton(
onClick = { viewModel.confirmDeleteVault() },
enabled = !uiState.isBusy,
) {
Text(stringResource(R.string.remove))
}
},
dismissButton = {
TextButton(
onClick = { viewModel.dismissDeleteVault() },
enabled = !uiState.isBusy,
) {
Text(stringResource(R.string.remote_vaults_add_cancel))
}
},
)
}
}

View File

@@ -1,3 +1,18 @@
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
class RemoteVaultsScreenState
import com.github.nullptroma.wallenc.domain.enums.VaultType
import java.util.UUID
data class RemoteVaultListItem(
val uuid: UUID,
val type: VaultType,
val label: String,
)
data class RemoteVaultsScreenState(
val vaults: List<RemoteVaultListItem> = emptyList(),
val isBusy: Boolean = false,
val addChoiceVisible: Boolean = false,
/** Карточка, для которой показан диалог удаления */
val vaultPendingDelete: RemoteVaultListItem? = null,
)

View File

@@ -1,9 +1,84 @@
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
import androidx.lifecycle.viewModelScope
import com.github.nullptroma.wallenc.domain.auth.RemoteYandexSignInLauncher
import com.github.nullptroma.wallenc.domain.interfaces.IYandexVault
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
import com.github.nullptroma.wallenc.presentation.ViewModelBase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class RemoteVaultsViewModel @Inject constructor() :
ViewModelBase<RemoteVaultsScreenState>(RemoteVaultsScreenState())
class RemoteVaultsViewModel @Inject constructor(
private val vaultsManager: IVaultsManager,
val yandexSignIn: RemoteYandexSignInLauncher,
) : ViewModelBase<RemoteVaultsScreenState>(RemoteVaultsScreenState()) {
val uiState = combine(
vaultsManager.remoteVaults,
state,
) { remotes, base ->
base.copy(
vaults = remotes.map { v ->
val label = when (v) {
is IYandexVault -> v.accountEmail
else -> v.uuid.toString()
}
RemoteVaultListItem(
uuid = v.uuid,
type = v.type,
label = label,
)
},
)
}.stateIn(
viewModelScope,
SharingStarted.Eagerly,
RemoteVaultsScreenState(),
)
fun setAddChoiceVisible(visible: Boolean) {
updateState(state.value.copy(addChoiceVisible = visible))
}
fun setBusy(busy: Boolean) {
updateState(state.value.copy(isBusy = busy))
}
fun onYandexAuthSuccess(accessToken: String) {
viewModelScope.launch {
setBusy(true)
try {
vaultsManager.addYandexVault(accessToken)
} finally {
setBusy(false)
setAddChoiceVisible(false)
}
}
}
fun requestDeleteVault(item: RemoteVaultListItem) {
updateState(state.value.copy(vaultPendingDelete = item))
}
fun dismissDeleteVault() {
updateState(state.value.copy(vaultPendingDelete = null))
}
fun confirmDeleteVault() {
val pending = state.value.vaultPendingDelete ?: return
viewModelScope.launch {
setBusy(true)
try {
vaultsManager.removeRemoteVault(pending.uuid)
} finally {
setBusy(false)
dismissDeleteVault()
}
}
}
}

View File

@@ -34,4 +34,15 @@
<string name="task_state_cancelled">Cancelled</string>
<string name="task_state_failed">Failed: %1$s</string>
<string name="remote_vaults_add_cd">Add remote vault</string>
<string name="remote_vaults_empty_hint">No remote vaults yet. Tap + to add Yandex.</string>
<string name="remote_vaults_add_title">Add vault</string>
<string name="remote_vaults_add_pick_provider">Choose provider:</string>
<string name="remote_vaults_provider_yandex">Yandex</string>
<string name="remote_vaults_add_cancel">Cancel</string>
<string name="remote_vault_type_yandex">Yandex</string>
<string name="remote_vault_delete_cd">Remove remote vault</string>
<string name="remote_vault_remove_title">Remove remote vault?</string>
<string name="remote_vault_remove_message">Remove \"%1$s\" from this device? The account data on the server is not deleted.</string>
</resources>