mirror of
https://github.com/bitwarden/android.git
synced 2026-06-08 08:06:32 -05:00
Subscribe to vault SendData (#485)
This commit is contained in:
committed by
Álison Fernandes
parent
11fcaa6678
commit
c5989d117e
@@ -1,11 +1,16 @@
|
||||
package com.x8bit.bitwarden.ui.tools.feature.send
|
||||
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
/**
|
||||
@@ -17,6 +22,19 @@ fun SendContent(
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
LazyColumn(modifier = modifier) {
|
||||
item {
|
||||
// TODO: Populate with real data BIT-481
|
||||
Text(
|
||||
text = "Not yet implemented",
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 16.dp)
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
|
||||
item {
|
||||
Spacer(modifier = Modifier.height(88.dp))
|
||||
Spacer(modifier = Modifier.navigationBarsPadding())
|
||||
|
||||
@@ -3,15 +3,21 @@ package com.x8bit.bitwarden.ui.tools.feature.send
|
||||
import android.os.Parcelable
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.DataState
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.SendData
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.Text
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.concat
|
||||
import com.x8bit.bitwarden.ui.tools.feature.send.util.toViewState
|
||||
import com.x8bit.bitwarden.ui.vault.feature.item.VaultItemScreen
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -33,11 +39,11 @@ class SendViewModel @Inject constructor(
|
||||
) {
|
||||
|
||||
init {
|
||||
// TODO: Remove this once we start listening to real vault data BIT-481
|
||||
viewModelScope.launch {
|
||||
delay(timeMillis = 3_000L)
|
||||
mutableStateFlow.update { it.copy(viewState = SendState.ViewState.Empty) }
|
||||
}
|
||||
vaultRepo
|
||||
.sendDataStateFlow
|
||||
.map { SendAction.Internal.SendDataReceive(it) }
|
||||
.onEach(::sendAction)
|
||||
.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
override fun handleAction(action: SendAction): Unit = when (action) {
|
||||
@@ -47,6 +53,55 @@ class SendViewModel @Inject constructor(
|
||||
SendAction.RefreshClick -> handleRefreshClick()
|
||||
SendAction.SearchClick -> handleSearchClick()
|
||||
SendAction.SyncClick -> handleSyncClick()
|
||||
is SendAction.Internal -> handleInternalAction(action)
|
||||
}
|
||||
|
||||
private fun handleInternalAction(action: SendAction.Internal): Unit = when (action) {
|
||||
is SendAction.Internal.SendDataReceive -> handleSendDataReceive(action)
|
||||
}
|
||||
|
||||
private fun handleSendDataReceive(action: SendAction.Internal.SendDataReceive) {
|
||||
when (val dataState = action.sendDataState) {
|
||||
is DataState.Error -> {
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
viewState = SendState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is DataState.Loaded -> {
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = dataState.data.toViewState())
|
||||
}
|
||||
}
|
||||
|
||||
DataState.Loading -> {
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = SendState.ViewState.Loading)
|
||||
}
|
||||
}
|
||||
|
||||
is DataState.NoNetwork -> {
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
viewState = SendState.ViewState.Error(
|
||||
message = R.string.internet_connection_required_title
|
||||
.asText()
|
||||
.concat(R.string.internet_connection_required_message.asText()),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is DataState.Pending -> {
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = dataState.data.toViewState())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleAboutSendClick() {
|
||||
@@ -164,6 +219,18 @@ sealed class SendAction {
|
||||
* User clicked the sync button.
|
||||
*/
|
||||
data object SyncClick : SendAction()
|
||||
|
||||
/**
|
||||
* Models actions that the [SendViewModel] itself will send.
|
||||
*/
|
||||
sealed class Internal : SendAction() {
|
||||
/**
|
||||
* Indicates that the send data has been received.
|
||||
*/
|
||||
data class SendDataReceive(
|
||||
val sendDataState: DataState<SendData>,
|
||||
) : Internal()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.x8bit.bitwarden.ui.tools.feature.send.util
|
||||
|
||||
import com.bitwarden.core.SendView
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.SendData
|
||||
import com.x8bit.bitwarden.ui.tools.feature.send.SendState
|
||||
|
||||
/**
|
||||
* Transforms [SendData] into [SendState.ViewState].
|
||||
*/
|
||||
fun SendData.toViewState(): SendState.ViewState =
|
||||
this
|
||||
.sendViewList
|
||||
.takeUnless { it.isEmpty() }
|
||||
?.toSendContent()
|
||||
?: SendState.ViewState.Empty
|
||||
|
||||
private fun List<SendView>.toSendContent(): SendState.ViewState.Content {
|
||||
// TODO: Populate with real data BIT-481
|
||||
return SendState.ViewState.Content
|
||||
}
|
||||
Reference in New Issue
Block a user