samedi 28 octobre 2023

Room Database Update a column (CHeckbox)

I recently started programming apps and don't have that much experience yet. Especially not with database programming.

I have a shoppinglist and want to check items.

I still can't manage to update the column. I found a few things on the internet but can't put it all together. For example, I don't know how to get the id from the item. Or do I need the ID at all?

I ask for help. Thanks!

Entity:

@Entity
data class ShoppingList(
    val Name: String,
    val Einheit: String,
    val Menge: String,
    @ColumnInfo(name = "isChecked", defaultValue = "0")
    val isChecked: Boolean,
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0

Dao:

@Dao
interface ShoppingListDao {

    @Upsert
    suspend fun upsertItem(items: ShoppingList)

    @Query("UPDATE ShoppingList SET isChecked = :isChecked WHERE id = :id")
    suspend fun updateItem(isChecked: Boolean, id: Int)

    @Delete
    suspend fun deleteItem(items: ShoppingList)

    @Query("SELECT * FROM ShoppingList ORDER BY Name ASC")
    fun getItemsOrderByName(): Flow<List<ShoppingList>>

}

Event:

sealed interface ShoppingListEvent{
    object SaveItem: ShoppingListEvent
    data class SetName(val Name: String): ShoppingListEvent
    data class SetEinheit(val Einheit: String): ShoppingListEvent
    data class SetMenge(val Menge: String): ShoppingListEvent
    data class SetCheckbox(val isChecked: Boolean): ShoppingListEvent
    object ShowDialog: ShoppingListEvent
    object HideDialog: ShoppingListEvent
    data class SortItems(val sortType: SortType): ShoppingListEvent
    data class DeleteItem(val items: ShoppingList): ShoppingListEvent
}

State:

data class ShoppingListState (
    val items: List<ShoppingList> = emptyList(),
    val Name: String = "",
    val Einheit: String = "",
    val Menge: String = "",
    val isChecked: Boolean = false,
    val isAddingItem: Boolean = false,
    val sortType: SortType = SortType.Name
)

Viewmodel:

class ShoppingListViewModel(
    private val  dao: ShoppingListDao
): ViewModel() {

    private val _sortType = MutableStateFlow(SortType.Name)
    private val _items = _sortType
        .flatMapLatest { sortType ->
            when(sortType) {
                SortType.Name -> dao.getItemsOrderByName()
            }
        }
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())

    private val _state = MutableStateFlow(ShoppingListState())
    val state = combine(_state, _sortType, _items) { state, sortType, items ->
        state.copy(
            items = items,
            sortType = sortType
        )
    }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), ShoppingListState())


    fun onEvent(event: ShoppingListEvent){
        when(event){

            ....

            is ShoppingListEvent.SetCheckbox -> {
                
                //I don't know what to do here. 
                
                val isChecked = state.value.isChecked

                viewModelScope.launch {
                    dao.updateItem(isChecked)
                }

            ...

            }

            
        }
    }
}

And the Checkbox:

Checkbox(
    checked = state.isChecked,
    onCheckedChange ={
          onEvent(ShoppingListEvent.SetCheckbox(it))
    }
)

I tried a few things. Sometimes nothing happens and sometimes all Items got checked.




Aucun commentaire:

Enregistrer un commentaire