Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Use bottom sheet for password generation
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Jun 9, 2024
1 parent 82a2c8a commit 0de252c
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 249 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,75 @@
package dev.medzik.librepass.android.ui.components

import android.util.Log
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.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.AutoAwesome
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.EuroSymbol
import androidx.compose.material.icons.filled.Numbers
import androidx.compose.material.icons.filled.Remove
import androidx.compose.material.icons.filled.Title
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.google.gson.Gson
import dev.medzik.android.components.TextFieldValue
import dev.medzik.android.components.colorizePasswordTransformation
import dev.medzik.android.components.rememberMutable
import dev.medzik.android.components.rememberMutableString
import dev.medzik.android.components.ui.BaseBottomSheet
import dev.medzik.android.components.ui.GroupTitle
import dev.medzik.android.components.ui.SwitcherPreference
import dev.medzik.android.components.ui.rememberBottomSheetState
import dev.medzik.android.components.ui.textfield.AnimatedTextField
import dev.medzik.android.utils.runOnIOThread
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.database.datastore.PasswordGeneratorPreference
import dev.medzik.librepass.android.database.datastore.readPasswordGeneratorPreference
import dev.medzik.librepass.android.database.datastore.writePasswordGeneratorPreference
import dev.medzik.librepass.android.ui.screens.vault.OtpConfigure
import dev.medzik.librepass.android.ui.screens.vault.PasswordGenerator
import dev.medzik.librepass.types.cipher.Cipher
import dev.medzik.librepass.types.cipher.data.CipherLoginData
import kotlinx.coroutines.launch
import java.util.Random

enum class PasswordType(val literals: String) {
NUMERIC("1234567890"),
LOWERCASE("abcdefghijklmnopqrstuvwxyz"),
UPPERCASE("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
SYMBOLS("!@#\$%^&*()_+-=[]{}\\|;:'\",.<>/?")
}

@Composable
fun CipherEditFieldsLogin(
navController: NavController,
cipher: Cipher,
button: @Composable (cipher: Cipher) -> Unit
) {
val scope = rememberCoroutineScope()

var cipherData by rememberMutable(cipher.loginData!!)

// observe username and password from navController
// used to get password from password generator
navController
.currentBackStackEntry
?.savedStateHandle
?.getLiveData<String>("password")?.observeForever {
cipherData = cipherData.copy(password = it)
}
// observe otp uri from navController
// used to get otp uri from otp configuration screen
navController
Expand Down Expand Up @@ -95,6 +122,8 @@ fun CipherEditFieldsLogin(
onValueChange = { cipherData = cipherData.copy(username = it) }
)

val passwordGeneratorSheetState = rememberBottomSheetState()

TextInputFieldBase(
label = stringResource(R.string.Password),
modifier = Modifier
Expand All @@ -104,22 +133,214 @@ fun CipherEditFieldsLogin(
onValueChange = { cipherData = cipherData.copy(password = it) },
hidden = true
) {
IconButton(onClick = {
// save cipher data as json to navController
navController.currentBackStackEntry?.savedStateHandle?.set(
"cipher",
Gson().toJson(cipherData)
)

navController.navigate(PasswordGenerator)
}) {
IconButton(
onClick = { passwordGeneratorSheetState.show() }
) {
Icon(
imageVector = Icons.Default.AutoAwesome,
contentDescription = null
)
}
}

@Composable
fun PasswordGeneratorSheetContent(onSubmit: (String) -> Unit) {
val context = LocalContext.current
val clipboardManager = LocalClipboardManager.current

var generatedPassword by rememberMutableString()

var passwordGeneratorPreference by rememberMutable(PasswordGeneratorPreference())
LaunchedEffect(Unit) {
passwordGeneratorPreference = readPasswordGeneratorPreference(context)
}

fun generatePassword(): String {
var letters = PasswordType.LOWERCASE.literals

if (passwordGeneratorPreference.capitalize) {
letters += PasswordType.UPPERCASE.literals
}

if (passwordGeneratorPreference.includeNumbers) {
letters += PasswordType.NUMERIC.literals
}

if (passwordGeneratorPreference.includeSymbols) {
letters += PasswordType.SYMBOLS.literals
}

return (1..passwordGeneratorPreference.length)
.map { Random().nextInt(letters.length) }
.map(letters::get)
.joinToString("")
}

// regenerate on options change
LaunchedEffect(passwordGeneratorPreference) {
generatedPassword = generatePassword()
}

AnimatedTextField(
modifier = Modifier.padding(horizontal = 8.dp),
value = TextFieldValue(
value = generatedPassword,
// editable = false
),
readOnly = true,
visualTransformation = colorizePasswordTransformation(),
trailing = {
Row {
IconButton(
onClick = { clipboardManager.setText(AnnotatedString(generatedPassword)) }
) {
Icon(
imageVector = Icons.Default.ContentCopy,
contentDescription = null
)
}

IconButton(
onClick = { generatedPassword = generatePassword() }
) {
Icon(
imageVector = Icons.Default.AutoAwesome,
contentDescription = null
)
}
}
}
)

Spacer(
modifier = Modifier.padding(top = 8.dp)
)

AnimatedTextField(
modifier = Modifier.padding(horizontal = 8.dp),
value = TextFieldValue(
value = passwordGeneratorPreference.length.toString(),
onChange = {
try {
if (it.length in 1..3 && it.toInt() <= 256) {
passwordGeneratorPreference = passwordGeneratorPreference.copy(length = it.toInt())
runOnIOThread { writePasswordGeneratorPreference(context, passwordGeneratorPreference) }
}
} catch (e: NumberFormatException) {
// ignore, just do not update input value
}
}
),
label = stringResource(R.string.PasswordGenerator_Length),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number
),
trailing = {
IconButton(
onClick = {
if (passwordGeneratorPreference.length > 1) {
passwordGeneratorPreference = passwordGeneratorPreference.copy(
length = passwordGeneratorPreference.length - 1
)
runOnIOThread { writePasswordGeneratorPreference(context, passwordGeneratorPreference) }
}
}
) {
Icon(
imageVector = Icons.Default.Remove,
contentDescription = null
)
}

IconButton(
onClick = {
if (passwordGeneratorPreference.length < 256) {
passwordGeneratorPreference = passwordGeneratorPreference.copy(
length = passwordGeneratorPreference.length + 1
)
runOnIOThread { writePasswordGeneratorPreference(context, passwordGeneratorPreference) }
}
}
) {
Icon(
imageVector = Icons.Default.Add,
contentDescription = null
)
}
}
)

// Capital letters switch
SwitcherPreference(
title = stringResource(R.string.PasswordGenerator_CapitalLetters),
checked = passwordGeneratorPreference.capitalize,
onCheckedChange = {
passwordGeneratorPreference = passwordGeneratorPreference.copy(capitalize = it)
runOnIOThread { writePasswordGeneratorPreference(context, passwordGeneratorPreference) }
},
icon = {
Icon(
imageVector = Icons.Default.Title,
contentDescription = null
)
}
)

// Numeric switch
SwitcherPreference(
title = stringResource(R.string.PasswordGenerator_Numbers),
checked = passwordGeneratorPreference.includeNumbers,
onCheckedChange = {
passwordGeneratorPreference = passwordGeneratorPreference.copy(includeNumbers = it)
runOnIOThread { writePasswordGeneratorPreference(context, passwordGeneratorPreference) }
},
icon = {
Icon(
imageVector = Icons.Default.Numbers,
contentDescription = null
)
}
)

// Symbols switch
SwitcherPreference(
title = stringResource(R.string.PasswordGenerator_Symbols),
checked = passwordGeneratorPreference.includeSymbols,
onCheckedChange = {
passwordGeneratorPreference = passwordGeneratorPreference.copy(includeSymbols = it)
runOnIOThread { writePasswordGeneratorPreference(context, passwordGeneratorPreference) }
},
icon = {
Icon(
imageVector = Icons.Default.EuroSymbol,
contentDescription = null
)
}
)

Button(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 90.dp)
.padding(top = 16.dp, bottom = 8.dp),
onClick = { onSubmit(generatedPassword) }
) {
Text(stringResource(R.string.Submit))
}
}

BaseBottomSheet(state = passwordGeneratorSheetState) {
PasswordGeneratorSheetContent(
onSubmit = {
cipherData = cipherData.copy(password = it)

scope.launch {
passwordGeneratorSheetState.hide()
}
}
)
}

GroupTitle(
stringResource(R.string.WebsiteDetails),
modifier = Modifier.padding(top = 8.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.ui.DefaultScaffold
import dev.medzik.librepass.android.ui.TopBarWithBack
import dev.medzik.librepass.android.common.navtype.CipherTypeType
import dev.medzik.librepass.android.ui.DefaultScaffold
import dev.medzik.librepass.types.cipher.CipherType
import kotlin.reflect.typeOf

Expand Down Expand Up @@ -47,14 +45,6 @@ fun NavGraphBuilder.vaultNavigation(navController: NavController) {
OtpConfigureScreen(navController, args)
}

composable<PasswordGenerator> {
DefaultScaffold(
topBar = { TopBarWithBack(R.string.PasswordGenerator, navController) }
) {
PasswordGeneratorScreen(navController)
}
}

composable<Search> {
SearchScreen(navController)
}
Expand Down
Loading

0 comments on commit 0de252c

Please sign in to comment.