Skip to content

Commit

Permalink
Wire up create and view from database
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintPatrck committed Apr 5, 2024
1 parent 788e279 commit ec1fd64
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fun ItemContent(
BitwardenTextField(
modifier = Modifier.padding(horizontal = 16.dp),
label = stringResource(id = R.string.name),
value = viewState.itemData.name,
value = viewState.itemData.issuer,
onValueChange = {},
readOnly = true,
singleLine = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ItemViewModel @Inject constructor(
}

ItemData(
name = item?.username.orEmpty(),
issuer = item?.issuer.orEmpty(),
alertThresholdSeconds = alertThresholdSeconds,
totpCodeItemData = totpData
)
Expand Down Expand Up @@ -133,7 +133,7 @@ class ItemViewModel @Inject constructor(
itemId = action.item?.id.orEmpty(),
viewState = ItemState.ViewState.Content(
itemData = ItemData(
name = action.item?.username.orEmpty(),
issuer = action.item?.issuer.orEmpty(),
alertThresholdSeconds = alertThreshold,
totpCodeItemData = totpItemData,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import kotlinx.parcelize.Parcelize
/**
* Represents the item data displayed to users.
*
* @property name Name of the account associated to the item.
* @property issuer Name of the item provider.
* @property alertThresholdSeconds Threshold, in seconds, at which an Item is considered near
* expiration.
* @property totpCodeItemData TOTP data for the account.
*/
@Parcelize
data class ItemData(
val name: String,
val issuer: String,
val alertThresholdSeconds: Int,
val totpCodeItemData: TotpCodeItemData?,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fun ItemListingScreen(
.fillMaxWidth()
.padding(horizontal = 16.dp),
startIcon = it.startIcon,
label = it.label,
issuer = it.issuer,
supportingLabel = it.supportingLabel,
timeLeftSeconds = it.timeLeftSeconds,
periodSeconds = it.periodSeconds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ sealed class ItemListingAction {
data class VerificationCodeDisplayItem(
val id: String,
val label: String,
val issuer: String?,
val supportingLabel: String?,
val timeLeftSeconds: Int,
val periodSeconds: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.x8bit.bitwarden.authenticator.ui.platform.theme.AuthenticatorTheme
* The verification code item displayed to the user.
*
* @param authCode The code for the item.
* @param label The label for the item.
* @param issuer The label for the item.
* @param periodSeconds The times span where the code is valid.
* @param timeLeftSeconds The seconds remaining until a new code is needed.
* @param startIcon The leading icon for the item.
Expand All @@ -45,7 +45,7 @@ import com.x8bit.bitwarden.authenticator.ui.platform.theme.AuthenticatorTheme
@Composable
fun VaultVerificationCodeItem(
authCode: String,
label: String,
issuer: String?,
periodSeconds: Int,
timeLeftSeconds: Int,
alertThresholdSeconds: Int,
Expand Down Expand Up @@ -80,13 +80,15 @@ fun VaultVerificationCodeItem(
verticalArrangement = Arrangement.SpaceEvenly,
modifier = Modifier.weight(1f),
) {
Text(
text = label,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurface,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
issuer?.let {
Text(
text = it,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurface,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}

supportingLabel?.let {
Text(
Expand Down Expand Up @@ -131,7 +133,7 @@ private fun VerificationCodeItem_preview() {
AuthenticatorTheme {
VaultVerificationCodeItem(
startIcon = IconData.Local(R.drawable.ic_login_item),
label = "Sample Label",
issuer = "Sample Label",
supportingLabel = "Supporting Label",
authCode = "1234567890".chunked(3).joinToString(" "),
timeLeftSeconds = 15,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fun VerificationCodeItem.toDisplayItem(alertThresholdSeconds: Int) =
VerificationCodeDisplayItem(
id = id,
label = label,
issuer = issuer,
supportingLabel = username,
timeLeftSeconds = timeLeftSeconds,
periodSeconds = periodSeconds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ fun ManualCodeEntryScreen(
.padding(horizontal = 16.dp),
)

Spacer(modifier = Modifier.height(8.dp))
BitwardenTextField(
label = stringResource(id = R.string.issuer),
value = state.issuer,
onValueChange = remember(viewModel) {
{
viewModel.trySendAction(
ManualCodeEntryAction.IssuerTextChange(it),
)
}
},
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
)

Spacer(modifier = Modifier.height(16.dp))
BitwardenFilledTonalButton(
label = stringResource(id = R.string.add_totp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package com.x8bit.bitwarden.authenticator.ui.authenticator.feature.manualcodeent

import android.os.Parcelable
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.x8bit.bitwarden.authenticator.data.authenticator.repository.AuthenticatorRepository
import com.x8bit.bitwarden.authenticator.data.authenticator.repository.model.TotpCodeResult
import com.x8bit.bitwarden.authenticator.ui.platform.base.BaseViewModel
import com.x8bit.bitwarden.authenticator.ui.platform.base.util.Text
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
import javax.inject.Inject

Expand All @@ -23,18 +24,25 @@ class ManualCodeEntryViewModel @Inject constructor(
private val authenticatorRepository: AuthenticatorRepository,
) : BaseViewModel<ManualCodeEntryState, ManualCodeEntryEvent, ManualCodeEntryAction>(
initialState = savedStateHandle[KEY_STATE]
?: ManualCodeEntryState(code = ""),
?: ManualCodeEntryState(code = "", issuer = ""),
) {
override fun handleAction(action: ManualCodeEntryAction) {
when (action) {
is ManualCodeEntryAction.CloseClick -> handleCloseClick()
is ManualCodeEntryAction.CodeTextChange -> handleCodeTextChange(action)
is ManualCodeEntryAction.IssuerTextChange -> handleIssuerTextChange(action)
is ManualCodeEntryAction.CodeSubmit -> handleCodeSubmit()
is ManualCodeEntryAction.ScanQrCodeTextClick -> handleScanQrCodeTextClick()
is ManualCodeEntryAction.SettingsClick -> handleSettingsClick()
}
}

private fun handleIssuerTextChange(action: ManualCodeEntryAction.IssuerTextChange) {
mutableStateFlow.update {
it.copy(issuer = action.issuer)
}
}

private fun handleCloseClick() {
sendEvent(ManualCodeEntryEvent.NavigateBack)
}
Expand All @@ -46,7 +54,9 @@ class ManualCodeEntryViewModel @Inject constructor(
}

private fun handleCodeSubmit() {
authenticatorRepository.emitTotpCodeResult(TotpCodeResult.Success(state.code))
viewModelScope.launch {
authenticatorRepository.createItem(state.code, state.issuer)
}
sendEvent(ManualCodeEntryEvent.NavigateBack)
}

Expand All @@ -65,6 +75,7 @@ class ManualCodeEntryViewModel @Inject constructor(
@Parcelize
data class ManualCodeEntryState(
val code: String,
val issuer: String,
) : Parcelable

/**
Expand Down Expand Up @@ -113,6 +124,11 @@ sealed class ManualCodeEntryAction {
*/
data class CodeTextChange(val code: String) : ManualCodeEntryAction()

/**
* The use has changed the issuer text.
*/
data class IssuerTextChange(val issuer: String) : ManualCodeEntryAction()

/**
* The text to switch to QR code scanning is clicked.
*/
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
<string name="add_code">Add code</string>
<string name="sync_items_with_bitwarden">Sync items from Bitwarden</string>
<string name="import_items">Import items</string>
<string name="issuer">Issuer</string>
</resources>

0 comments on commit ec1fd64

Please sign in to comment.