I'm am making a note app using kotlin jetpack compose. Currently, I'm working on an update feature. I'm trying to update a note in ROOM Database but it doesn't update. I already read and follow some tutorials from documentation and youtube, however, I still haven't solved it.
Here is my code:
Data
@RequiresApi(Build.VERSION_CODES.O)
@Entity(tableName = "KeepNotesTable")
data class NoteData (
@PrimaryKey
val id: String = UUID.randomUUID().toString(),
@ColumnInfo(name = "noteTitle")
var title: String,
@ColumnInfo(name = "noteDescription")
var description: String,
@ColumnInfo(name = "noteDate")
val date: Date = Date.from(Instant.now())
)
DAO
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateNote(note: NoteData)
Repository
suspend fun updateNote(note: NoteData) = noteDatabaseDao.updateNote(note)
ViewModel
fun updateNote(note: NoteData) = viewModelScope.launch { repository.updateNote(note) }
Navigation
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun ScreenNavigation(noteViewModel: NoteViewModel = viewModel()) {
val noteList = noteViewModel.noteList.collectAsState().value
val navController = rememberNavController()
NavHost(navController = navController, startDestination = Screen.MainScreen.name) {
composable(Screen.MainScreen.name){
MainScreen(navController = navController, noteList = noteList, removeNote = { noteViewModel.removeNote(it) })}
composable(Screen.AddNoteScreen.name){
AddNoteScreen(navController = navController, addNote = { noteViewModel.addNote(it) })
}
composable(Screen.UpdateNoteScreen.name + "/{noteId}",
arguments = listOf(navArgument(name = "noteId") {type = NavType.StringType})
) { backStackEntry ->
UpdateNoteScreen(
noteList = noteList,
navController = navController,
updateNote = { noteViewModel.updateNote(it) },
NoteId = backStackEntry.arguments?.getString("noteId")
)
}
}
}
UpdateNoteScreen
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun UpdateNoteScreen(
noteList: List<NoteData>,
navController: NavController,
updateNote: (NoteData) -> Unit,
NoteId: String?,
) {
val fetchNote = noteList.filter { note ->
note.id == NoteId
}
val note = fetchNote.first()
var noteTitle = note.title
var noteDescription = note.description
var newTitle = ""
var newDescription = ""
var title by remember {
mutableStateOf(noteTitle + newTitle)
}
var description by remember {
mutableStateOf(noteDescription + newDescription)
}
val context = LocalContext.current
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.primaryVariant) {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally) {
Card(
contentColor = MaterialTheme.colors.primary,
backgroundColor = MaterialTheme.colors.background,
elevation = 8.dp,
modifier = Modifier
.fillMaxWidth()
.padding(start = 10.dp, end = 10.dp, top = 20.dp, bottom = 20.dp)) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxWidth()
.padding(start = 15.dp, end = 15.dp, top = 10.dp, bottom = 40.dp)) {
InputText(
modifier = Modifier.fillMaxWidth(),
text = title,
label = "",
onTextChange = {
newTitle = it
title = newTitle
}
)
Spacer(modifier = Modifier.height(10.dp))
InputText(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(0.5f),
text = description,
label = "",
maxLine = 1000,
imeAction = ImeAction.None,
onTextChange = {
newDescription = it
description = newDescription
}
)
}
}
Row() {
DiscardButton(text = "Cancel", onClick = {
navController.navigate(route = Screen.MainScreen.name)
})
Spacer(modifier = Modifier.width(10.dp))
SaveButton(text = "Update", onClick = {
if (title.isNotEmpty() && description.isNotEmpty()) {
updateNote(NoteData(title = title, description = description))
navController.navigate(route = Screen.MainScreen.name)
Toast.makeText(context, "Successfully Updated", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Fill the Title and Description", Toast.LENGTH_SHORT).show()
}
})
}
}
}
}
I reviewed my code again and noticed that an id parameter is needed to pass in the updateNote() to find and update the specific note. Therefore,
updateNoteScreen
updateNote(NoteData(title = title, description = description)
should be
updateNote(NoteData(id = note.id, title = title, description = description)