kotlin @使用Jetpack合成更新ROOM数据库

px9o7tmv  于 2022-11-25  发布在  Kotlin
关注(0)|答案(1)|浏览(197)

我正在使用Kotlinjetpack compose制作一个笔记应用程序。目前,我正在开发一个更新功能。我试图更新ROOM数据库中的一个笔记,但它没有更新。我已经阅读并遵循了文档和youtube中的一些教程,但我仍然没有解决这个问题。
下面是我的代码:

数据

@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())
)

数据访问控制

@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateNote(note: NoteData)

存储库

suspend fun updateNote(note: NoteData) = noteDatabaseDao.updateNote(note)

视图模型

fun updateNote(note: NoteData) = viewModelScope.launch { repository.updateNote(note) }

导览

@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")
                )

        }

    }
}

更新便笺屏幕

@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()
                    }
                })
            }
        }
    }
}
wpx232ag

wpx232ag1#

我再次检查了代码并注意到需要一个id参数来传入updateNote()以查找和更新特定的注解,

更新备注屏幕

updateNote(NoteData(title = title, description = description)
应该是
updateNote(NoteData(id = note.id, title = title, description = description)

相关问题