Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password sheet enhancements #122

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ fun NextcloudPasswordsApp(
backstackEntry.value?.destination?.route
)

val modalSheetState = rememberModalBottomSheetState()
var openBottomSheet by rememberSaveable { mutableStateOf(false) }
val modalSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)

val needsMasterPassword by passwordsViewModel.needsMasterPassword.collectAsState()
val masterPasswordInvalid by passwordsViewModel.masterPasswordInvalid.collectAsState()
Expand Down Expand Up @@ -220,12 +221,10 @@ fun NextcloudPasswordsApp(
searchQuery = searchQuery,
isAutofillRequest = isAutofillRequest,
modalSheetState = modalSheetState,
openPasswordDetails = { password ->
passwordsViewModel.setVisiblePassword(password)
openPasswordDetails = { password, folderPath ->
passwordsViewModel.setVisiblePassword(password, folderPath)
keyboardController?.hide()
coroutineScope.launch {
modalSheetState.show()
}
openBottomSheet = true
},
replyAutofill = replyAutofill,
searchVisibility = searchExpanded,
Expand Down Expand Up @@ -279,23 +278,24 @@ fun NextcloudPasswordsApp(
)
}

if (modalSheetState.isVisible) {
if (openBottomSheet) {
ModalBottomSheet(
onDismissRequest = {
coroutineScope.launch {
modalSheetState.hide()
}
},
contentWindowInsets = { WindowInsets.navigationBars }
onDismissRequest = { openBottomSheet = false },
contentWindowInsets = { WindowInsets.navigationBars },
sheetState = modalSheetState
) {
PasswordItem(
password = passwordsViewModel.visiblePassword.value,
passwordInfo = passwordsViewModel.visiblePassword.value,
onEditPassword = if (sessionOpen) {
{
coroutineScope.launch {
modalSheetState.hide()
}.invokeOnCompletion {
if (!modalSheetState.isVisible) {
openBottomSheet = false
}
}
navController.navigate("${NCPScreen.PasswordEdit.name}/${passwordsViewModel.visiblePassword.value?.id ?: "none"}")
navController.navigate("${NCPScreen.PasswordEdit.name}/${passwordsViewModel.visiblePassword.value?.first?.id ?: "none"}")
}
} else null,
modifier = Modifier.padding(bottom = 16.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fun NCPNavHost(
modifier: Modifier = Modifier,
searchQuery: String = "",
isAutofillRequest: Boolean,
openPasswordDetails: (Password) -> Unit,
openPasswordDetails: (Password, List<String>) -> Unit,
replyAutofill: ((String, String, String) -> Unit)? = null,
modalSheetState: SheetState? = null,
searchVisibility: Boolean? = null,
Expand Down Expand Up @@ -101,11 +101,23 @@ fun NCPNavHost(
} ?: emptyList())
}

val baseFolderName = stringResource(R.string.top_level_folder_name)
val onPasswordClick: (Password) -> Unit = { password ->
if (isAutofillRequest && replyAutofill != null) {
replyAutofill(password.label, password.username, password.password)
} else {
openPasswordDetails(password)
val folderPath = mutableListOf<String>()
var nextFolderUuid = password.folder
while (nextFolderUuid != FoldersApi.DEFAULT_FOLDER_UUID) {
val nextFolder =
foldersDecryptionState.decryptedList?.find { it.id == nextFolderUuid }
nextFolder?.label?.let {
folderPath.add(it)
}
nextFolderUuid = nextFolder?.parent ?: FoldersApi.DEFAULT_FOLDER_UUID
}
folderPath.add(baseFolderName)
openPasswordDetails(password, folderPath.toList())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Folder
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.twotone.AccountCircle
import androidx.compose.material.icons.twotone.AlternateEmail
Expand Down Expand Up @@ -54,6 +56,7 @@ import com.hegocre.nextcloudpasswords.R
import com.hegocre.nextcloudpasswords.data.password.CustomField
import com.hegocre.nextcloudpasswords.data.password.Password
import com.hegocre.nextcloudpasswords.ui.components.markdown.MDDocument
import com.hegocre.nextcloudpasswords.ui.theme.ContentAlpha
import com.hegocre.nextcloudpasswords.ui.theme.NextcloudPasswordsTheme
import com.hegocre.nextcloudpasswords.ui.theme.favoriteColor
import com.hegocre.nextcloudpasswords.utils.copyToClipboard
Expand All @@ -63,12 +66,16 @@ import org.commonmark.parser.Parser

@Composable
fun PasswordItem(
password: Password?,
passwordInfo: Pair<Password, List<String>>?,
modifier: Modifier = Modifier,
onEditPassword: (() -> Unit)? = null,
) {
password?.let { pass ->
PasswordItemContent(password = pass, onEditPassword = onEditPassword, modifier = modifier)
passwordInfo?.let { pass ->
PasswordItemContent(
passwordInfo = pass,
onEditPassword = onEditPassword,
modifier = modifier
)
} ?: Text(
text = stringResource(R.string.password),
style = MaterialTheme.typography.headlineMedium,
Expand All @@ -78,7 +85,7 @@ fun PasswordItem(

@Composable
fun PasswordItemContent(
password: Password,
passwordInfo: Pair<Password, List<String>>,
onEditPassword: (() -> Unit)?,
modifier: Modifier = Modifier
) {
Expand All @@ -87,6 +94,18 @@ fun PasswordItemContent(

val uriHandler = LocalUriHandler.current

val password = passwordInfo.first
val folderPath = remember {
buildAnnotatedString {
appendInlineContent("folder")
append(" ")
passwordInfo.second.reversed().forEachIndexed { index, folderName ->
if (index != 0) append(" /")
append(" $folderName")
}
}
}

val customFields by remember {
derivedStateOf {
if (password.customFields.isNotBlank()) {
Expand All @@ -100,7 +119,7 @@ fun PasswordItemContent(
Column(modifier = modifier) {
Row(
modifier = Modifier
.padding(bottom = 8.dp)
.padding(bottom = 4.dp)
.padding(horizontal = 16.dp),
verticalAlignment = CenterVertically
) {
Expand Down Expand Up @@ -150,6 +169,37 @@ fun PasswordItemContent(
}
}
LazyColumn {
item(key = "${password.id}_path") {
val folderInlineContent = mapOf(
Pair(
"folder",
InlineTextContent(
placeholder = Placeholder(
width = LocalTextStyle.current.fontSize,
height = LocalTextStyle.current.fontSize,
placeholderVerticalAlign = PlaceholderVerticalAlign.Center
)
) {
Icon(
imageVector = Icons.Default.Folder,
contentDescription = stringResource(
id = R.string.folder
),
tint = MaterialTheme.colorScheme.onSurface
.copy(alpha = ContentAlpha.medium)
)
}
)
)
Text(
text = folderPath,
inlineContent = folderInlineContent,
modifier = Modifier
.padding(bottom = 16.dp)
.padding(horizontal = 16.dp)
)
}

if (password.username.isNotBlank()) {
item(key = "${password.id}_username") {
val usernameLabel = stringResource(id = R.string.password_attr_username)
Expand Down Expand Up @@ -475,7 +525,11 @@ fun PasswordMarkdownField(
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
MDDocument(root)
SelectionContainer {
Column {
MDDocument(root)
}
}
}
}

Expand All @@ -485,7 +539,8 @@ fun PasswordItemPreview() {
NextcloudPasswordsTheme {
Surface {
PasswordItem(
password = Password(
passwordInfo = Pair(
Password(
id = "",
label = "Nextcloud with a really long label",
username = "john_doe",
Expand Down Expand Up @@ -513,6 +568,7 @@ fun PasswordItemPreview() {
edited = 0,
created = 0,
updated = 0
), listOf("Second", "Home")
),
onEditPassword = {},
modifier = Modifier.padding(bottom = 16.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class PasswordsViewModel(application: Application) : AndroidViewModel(applicatio
val folders: LiveData<List<Folder>>
get() = FolderController.getInstance(getApplication()).getFolders()

var visiblePassword = mutableStateOf<Password?>(null)
var visiblePassword = mutableStateOf<Pair<Password, List<String>>?>(null)
private set
var visibleFolder = mutableStateOf<Folder?>(null)
private set
Expand Down Expand Up @@ -208,8 +208,8 @@ class PasswordsViewModel(application: Application) : AndroidViewModel(applicatio
}
}

fun setVisiblePassword(password: Password) {
visiblePassword.value = password
fun setVisiblePassword(password: Password, folderPath: List<String>) {
visiblePassword.value = Pair(password, folderPath)
}

fun setVisibleFolder(folder: Folder?) {
Expand Down