library/passwords: make ListPage() allow trailing content (for totp codes)
- Commit
0fa3c9a41768cb8f75decab02c7df77e09bcad8b- Author
- Vayun <[email protected]>
- When
- 2026-01-18 01:05 UTC (10 months ago)
- Parents
4438098
Diff
library/src/main/java/com/vayunmathur/library/ui/ListPage.kt
diff --git a/library/src/main/java/com/vayunmathur/library/ui/ListPage.kt b/library/src/main/java/com/vayunmathur/library/ui/ListPage.ktindex 31f8a0a2d9..e8ec0d3638 100644--- a/library/src/main/java/com/vayunmathur/library/ui/ListPage.kt+++ b/library/src/main/java/com/vayunmathur/library/ui/ListPage.kt@@ -53,12 +53,11 @@ inline fun <reified T : DatabaseItem<T>, Route : NavKey, reified EditPage : Rout crossinline editPage: () -> Route, settingsPage: Route? = null, crossinline otherActions: @Composable () -> Unit = {},- isReorderable: Boolean = false+ isReorderable: Boolean = false,+ crossinline trailingContent: @Composable (T) -> Unit = {}, ) { val dbData by viewModel.data<T>().collectAsState() -- val hapticFeedback = LocalHapticFeedback.current // 1. Initialize the reorderable state@@ -129,18 +128,30 @@ inline fun <reified T : DatabaseItem<T>, Route : NavKey, reified EditPage : Rout ListItem({headlineContent(item)}, Modifier.clickable{ backStack.add(viewPage(item.id)) }, {}, {supportingContent(item)}, {}, {- IconButton(- modifier = Modifier.draggableHandle(- onDragStarted = {- hapticFeedback.performHapticFeedback(HapticFeedbackType.GestureThresholdActivate)- },- onDragStopped = {- hapticFeedback.performHapticFeedback(HapticFeedbackType.GestureEnd)- },- ),- onClick = {},- ) {- Icon(painterResource(R.drawable.drag_handle_24px), contentDescription = "Reorder")+ Row {+ trailingContent(item)+ if(isReorderable) {+ IconButton(+ modifier = Modifier.draggableHandle(+ onDragStarted = {+ hapticFeedback.performHapticFeedback(+ HapticFeedbackType.GestureThresholdActivate+ )+ },+ onDragStopped = {+ hapticFeedback.performHapticFeedback(+ HapticFeedbackType.GestureEnd+ )+ },+ ),+ onClick = {},+ ) {+ Icon(+ painterResource(R.drawable.drag_handle_24px),+ contentDescription = "Reorder"+ )+ }+ } } }, ListItemDefaults.colors(), elevation, elevation) }
notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.kt b/notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.ktindex 0def88a9be..7c726e6d08 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.kt@@ -17,5 +17,5 @@ fun NotesListPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) Text(it.title) }, { Text(it.content.substringBefore('\n').take(40))- }, { Route.Note(it) }, { Route.Note(0) })+ }, { Route.Note(it) }, { Route.Note(0) }, isReorderable = true) }\ No newline at end of file
passwords/src/main/java/com/vayunmathur/passwords/MainActivity.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/MainActivity.kt b/passwords/src/main/java/com/vayunmathur/passwords/MainActivity.ktindex 62f8e6549d..fa0e4dc06e 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/MainActivity.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/MainActivity.kt@@ -6,6 +6,7 @@ import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.runtime.Composable import androidx.navigation3.runtime.NavKey+import androidx.room.migration.Migration import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.library.util.MainNavigation import com.vayunmathur.library.util.DatabaseViewModel@@ -24,7 +25,9 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge()- val database = buildDatabase<PasswordDatabase>()+ val database = buildDatabase<PasswordDatabase>(listOf(Migration(1, 2, {+ it.execSQL("ALTER TABLE passwords ADD COLUMN position REAL NOT NULL DEFAULT 0.0")+ }))) val viewModel = DatabaseViewModel(Password::class to database.passwordDao()) setContent { DynamicTheme {
passwords/src/main/java/com/vayunmathur/passwords/data/PasswordDatabase.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/data/PasswordDatabase.kt b/passwords/src/main/java/com/vayunmathur/passwords/data/PasswordDatabase.ktindex 9451838581..e184bc17e6 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/data/PasswordDatabase.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/data/PasswordDatabase.kt@@ -7,7 +7,7 @@ import androidx.room.RoomDatabase import androidx.room.TypeConverters import com.vayunmathur.passwords.Password -@Database(entities = [Password::class], version = 1)+@Database(entities = [Password::class], version = 2) @TypeConverters(Converters::class) abstract class PasswordDatabase : RoomDatabase() { abstract fun passwordDao(): PasswordDao
passwords/src/main/java/com/vayunmathur/passwords/ui/MenuPage.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/MenuPage.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/MenuPage.ktindex 7b6e9de15f..f5db5fad87 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/ui/MenuPage.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/MenuPage.kt@@ -43,49 +43,33 @@ import kotlinx.coroutines.delay @OptIn(ExperimentalMaterial3Api::class) @Composable fun MenuPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {- ListPage<Password, Route, Route.PasswordEditPage>(backStack, viewModel, "Passwords", ::PasswordListItem, { Route.PasswordPage(it) }, { Route.PasswordEditPage(0) }, Route.Settings)-}--@Composable-private fun PasswordListItem(pass: Password, modifier: Modifier, onClick: () -> Unit) { val context = LocalContext.current-- if (pass.totpSecret.isNullOrBlank()) {- ListItem(- { Text(pass.name.ifBlank { "(no name)" }) },- Modifier.clickable { onClick() },- supportingContent = { Text(pass.userId) }- )- return- }-- var currentCode by remember { mutableStateOf("") }- var progress by remember { mutableFloatStateOf(1f) }- LaunchedEffect(Unit) {- while (true) {- val timeBucket = System.currentTimeMillis() / 1000 / 30- currentCode = tryOrDefault("----") { TOTP.generate(pass.totpSecret, timeBucket * 30) }- progress = 1f - (System.currentTimeMillis() / 30000f) % 1f- delay(50)+ ListPage<Password, Route, Route.PasswordEditPage>(backStack, viewModel, "Passwords", {+ Text(it.name.ifBlank {"(no name)"})+ }, {+ Text(it.userId)+ }, { Route.PasswordPage(it) }, { Route.PasswordEditPage(0) }, Route.Settings, trailingContent = {+ if(it.totpSecret.isNullOrBlank()) return@ListPage+ var currentCode by remember { mutableStateOf("") }+ var progress by remember { mutableFloatStateOf(1f) }+ LaunchedEffect(Unit) {+ while (true) {+ val timeBucket = System.currentTimeMillis() / 1000 / 30+ currentCode = tryOrDefault("----") { TOTP.generate(it.totpSecret, timeBucket * 30) }+ progress = 1f - (System.currentTimeMillis() / 30000f) % 1f+ delay(50)+ } }- }-- ListItem(- { Text(pass.name.ifBlank { "(no name)" }) },- modifier.clickable { onClick() },- supportingContent = { Text(pass.userId) },- trailingContent = {- Row(Modifier.clickable {- val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager- clipboard.setPrimaryClip(ClipData.newPlainText("totp", currentCode))- }.wrapContentHeight(), verticalAlignment = Alignment.CenterVertically) {- Text(currentCode, style = MaterialTheme.typography.bodyLarge)- Spacer(Modifier.width(8.dp))- Box(contentAlignment = Alignment.Center) {- CircularProgressIndicator({progress}, Modifier.size(40.dp))- Icon(painterResource(R.drawable.content_copy_24px), contentDescription = "Copy TOTP", Modifier.size(16.dp))- }+ Row(Modifier.clickable {+ val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager+ clipboard.setPrimaryClip(ClipData.newPlainText("totp", currentCode))+ }.wrapContentHeight(), verticalAlignment = Alignment.CenterVertically) {+ Text(currentCode, style = MaterialTheme.typography.bodyLarge)+ Spacer(Modifier.width(8.dp))+ Box(contentAlignment = Alignment.Center) {+ CircularProgressIndicator({progress}, Modifier.size(40.dp))+ Icon(painterResource(R.drawable.content_copy_24px), contentDescription = "Copy TOTP", Modifier.size(16.dp)) } }- )+ }) }\ No newline at end of file