password: fix various crashes
- Commit
199561302f99bf3e107cf732d86e1ac509919102- Author
- Vayun <[email protected]>
- When
- 2026-01-16 04:03 UTC (10 months ago)
- Parents
e3d87fc
Diff
library/src/main/java/com/vayunmathur/library/util/RoomViewModel.kt
diff --git a/library/src/main/java/com/vayunmathur/library/util/RoomViewModel.kt b/library/src/main/java/com/vayunmathur/library/util/RoomViewModel.ktindex a7f94ec0e1..5d146bbe74 100644--- a/library/src/main/java/com/vayunmathur/library/util/RoomViewModel.kt+++ b/library/src/main/java/com/vayunmathur/library/util/RoomViewModel.kt@@ -58,16 +58,9 @@ class DatabaseViewModel(vararg daos: Pair<KClass<*>, TrueDao<*>>) : ViewModel() } @Composable- inline fun <reified E: DatabaseItem> get(id: Long): State<E> {+ inline fun <reified E: DatabaseItem> get(id: Long, crossinline default: () -> E? = {null}): State<E> { val data by getDaoInterface<E>().data.collectAsState()- val derived = remember { derivedStateOf { data.first { it.id == id } } }- return derived- }-- @Composable- inline fun <reified E: DatabaseItem> getNullable(id: Long): State<E?> {- val data by getDaoInterface<E>().data.collectAsState()- val derived = remember { derivedStateOf { data.firstOrNull { it.id == id } } }+ val derived = remember { derivedStateOf { (data.firstOrNull { it.id == id } ?: default())!! } } return derived }
passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditPage.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditPage.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditPage.ktindex ef1d66c75b..cd43fe29a6 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditPage.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditPage.kt@@ -27,8 +27,7 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable fun PasswordEditPage(backStack: NavBackStack<Route>, id: Long, viewModel: DatabaseViewModel) {- val passDatabase by viewModel.getNullable<Password>(id)- val pass = passDatabase ?: Password()+ val pass by viewModel.get<Password>(id) { Password() } var name by remember { mutableStateOf(pass.name) } var userId by remember { mutableStateOf(pass.userId) } var password by remember { mutableStateOf(pass.password) }
passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordPage.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordPage.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordPage.ktindex c00146e6c3..87de7ae493 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordPage.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordPage.kt@@ -26,6 +26,7 @@ import com.vayunmathur.passwords.Password import com.vayunmathur.passwords.Route import com.vayunmathur.passwords.R import androidx.core.net.toUri+import com.vayunmathur.library.ui.IconDelete import com.vayunmathur.library.ui.IconNavigation import com.vayunmathur.passwords.TOTP import kotlinx.coroutines.delay@@ -34,7 +35,8 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable fun PasswordPage(backStack: NavBackStack<Route>, id: Long, viewModel: DatabaseViewModel) {- val password by viewModel.get<Password>(id)+ val password by viewModel.get<Password>(id){Password()}+ if(password == Password()) return val context = LocalContext.current var showPassword by remember { mutableStateOf(false) } val snackbarHostState = remember { SnackbarHostState() }@@ -44,6 +46,11 @@ fun PasswordPage(backStack: NavBackStack<Route>, id: Long, viewModel: DatabaseVi topBar = { TopAppBar( title = { Text(password.name.ifBlank { "Password" }) },+ actions = {+ IconButton(onClick = { viewModel.delete(password); backStack.removeLastOrNull() }) {+ IconDelete()+ }+ }, navigationIcon = { IconNavigation{ backStack.removeLastOrNull() } }@@ -193,7 +200,7 @@ fun PasswordPage(backStack: NavBackStack<Route>, id: Long, viewModel: DatabaseVi .fillMaxWidth() .clickable { // open link- val intent = Intent(Intent.ACTION_VIEW, w.toUri())+ val intent = Intent(Intent.ACTION_VIEW, sanitizeUrl(w).toUri()) context.startActivity(intent) } .padding(vertical = 6.dp)) {@@ -206,4 +213,13 @@ fun PasswordPage(backStack: NavBackStack<Route>, id: Long, viewModel: DatabaseVi } } }+}++fun sanitizeUrl(input: String): String {+ val trimmed = input.trim()+ return if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {+ trimmed+ } else {+ "https://$trimmed"+ } }\ No newline at end of file