Modern Apps git

← Modern-Apps.git

notes: fixed creating new note

Commit
0688aaf71246cfa1fe77711b72dad6c8c08e1a49
Author
Vayun <[email protected]>
When
2026-01-17 22:53 UTC (10 months ago)
Parents
bc667df

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 18e6d049d8..fa0c749705 100644--- a/library/src/main/java/com/vayunmathur/library/util/RoomViewModel.kt+++ b/library/src/main/java/com/vayunmathur/library/util/RoomViewModel.kt@@ -74,43 +74,47 @@ class DatabaseViewModel(vararg daos: Pair<KClass<*>, TrueDao<*>>) : ViewModel()      @Composable     inline fun <reified E : DatabaseItem> getEditable(-        id: Long,+        initialId: Long,         crossinline default: () -> E? = { null }     ): MutableState<E> {         val scope = rememberCoroutineScope()-        val dao = getDaoInterface<E>()+        val daoInterface = getDaoInterface<E>() -        // 1. Get the source of truth from the DB-        val data by dao.data.collectAsState(initial = emptyList())+        // 1. Track the current ID. If it's 0, it will be updated after the first upsert.+        var currentId by remember { mutableLongStateOf(initialId) } -        // 2. Create a local state to hold the "in-progress" edits-        // We initialize it when the data first loads-        val localState = remember { mutableStateOf<E?>(null) }+        // 2. Observe the database state+        val data by daoInterface.data.collectAsState() -        var id by remember { mutableLongStateOf(id) }+        // 3. Local state for immediate UI feedback+        val localState = remember { mutableStateOf<E?>(null) } -        // Update local state when the DB record changes-        LaunchedEffect(data) {-            if (localState.value == null) {-                localState.value = data.firstOrNull { it.id == id }+        // Sync local state when the database data changes or the ID changes+        LaunchedEffect(data, currentId) {+            val dbItem = data.firstOrNull { it.id == currentId }+            if (dbItem != null) {+                localState.value = dbItem             }         } -        // 3. Return a custom MutableState+        // 4. Wrap in a custom MutableState         return remember {             object : MutableState<E> {                 override var value: E-                    get() = (localState.value ?: default())!!+                    get() = localState.value ?: default() ?: throw Exception("Entity not found and no default provided")                     set(newValue) {+                        // Optimistically update the UI local state                         localState.value = newValue-                        scope.launch {-                            dao.upsert(newValue) {-                                id = it++                        // Push to database+                        daoInterface.upsert(newValue) { newId ->+                            // If this was a new item (ID 0), update our pointer to the new ID+                            if (currentId == 0L) {+                                currentId = newId                             }                         }                     } -                // Standard boilerplate for MutableState implementation                 override fun component1(): E = value                 override fun component2(): (E) -> Unit = { value = it }             }
notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt b/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.ktindex b4cfe9f5c4..4201292a7a 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt@@ -3,6 +3,7 @@ package com.vayunmathur.notes.ui import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.material3.ExperimentalMaterial3Api+import androidx.compose.material3.IconButton import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TextField@@ -13,7 +14,9 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.navigation3.runtime.NavBackStack+import com.vayunmathur.library.ui.IconDelete import com.vayunmathur.library.ui.IconNavigation+import com.vayunmathur.library.ui.IconSave import com.vayunmathur.library.util.DatabaseViewModel import com.vayunmathur.library.util.pop import com.vayunmathur.notes.Route@@ -27,6 +30,14 @@ fun NotePage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel, noteI     Scaffold(topBar = {         TopAppBar({ Text("Note") }, navigationIcon = {             IconNavigation { backStack.pop() }+        }, actions = {+            IconButton(onClick = {+                viewModel.delete(note) {+                    backStack.pop()+                }+            }) {+                IconDelete()+            }         })     }) { paddingValues ->         Column(Modifier.padding(paddingValues)) {