Skip to content

Commit

Permalink
Display folder path under password label on details view
Browse files Browse the repository at this point in the history
  • Loading branch information
hegocre committed Sep 7, 2024
1 parent 36aa9bd commit f4236f1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ fun NextcloudPasswordsApp(
searchQuery = searchQuery,
isAutofillRequest = isAutofillRequest,
modalSheetState = modalSheetState,
openPasswordDetails = { password ->
passwordsViewModel.setVisiblePassword(password)
openPasswordDetails = { password, folderPath ->
passwordsViewModel.setVisiblePassword(password, folderPath)
keyboardController?.hide()
openBottomSheet = true
},
Expand Down Expand Up @@ -289,7 +289,7 @@ fun NextcloudPasswordsApp(
sheetState = modalSheetState
) {
PasswordItem(
password = passwordsViewModel.visiblePassword.value,
passwordInfo = passwordsViewModel.visiblePassword.value,
onEditPassword = if (sessionOpen) {
{
coroutineScope.launch {
Expand All @@ -299,7 +299,7 @@ fun NextcloudPasswordsApp(
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 @@ -14,6 +14,7 @@ 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 @@ -55,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 @@ -64,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 @@ -79,7 +85,7 @@ fun PasswordItem(

@Composable
fun PasswordItemContent(
password: Password,
passwordInfo: Pair<Password, List<String>>,
onEditPassword: (() -> Unit)?,
modifier: Modifier = Modifier
) {
Expand All @@ -88,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 @@ -101,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 @@ -151,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 @@ -490,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 @@ -518,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

0 comments on commit f4236f1

Please sign in to comment.