Sharing data between two ViewModels - Jetpack Compose
I have two screens between which I switch using bottomNavigation. ScheduleScreenVM i use for this screen ScheduleScreen. As you can see, I select the days and the array is filled with it. 0 - transparent square. 1 - green square. the array is stored in daysChoosen. Sry for long list
daysTable:
@Entity()
data class DaysChoosenEntity(
var daysChoosen: Int,
@PrimaryKey(autoGenerate = true) var id: Int? = null
) {
}
daysDAO:
@Dao
interface DaysChoosenDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertDays(days: DaysChoosenEntity)
@Delete
suspend fun deleteDays(days: DaysChoosenEntity)
@Query("SELECT * FROM dayschoosenentity")
fun getDays(): Flow<List<DaysChoosenEntity>>
}
daysDB:
@Database(
entities = [DaysChoosenEntity::class],
version = 1
)
abstract class DaysChoosenDB: RoomDatabase() {
abstract val daysChoosenDAO: DaysChoosenDAO
companion object {
const val DATABASE_NAME = "dayschoosen_db"
}
}
daysRepo:
class DaysChoosenRepositoryImpl(
private val dao: DaysChoosenDAO
): DaysChoosenRepository {
override fun getDaysChoosen(): Flow<List<DaysChoosenEntity>> {
return dao.getDays()
}
override suspend fun insertDaysChoosen(days: DaysChoosenEntity) {
dao.insertDays(days)
}
override suspend fun deleteDaysChoosen(days: DaysChoosenEntity) {
dao.deleteDays(days)
}
}
daysUseCases:
data class DaysChoosenUseCase (
val getDaysChoosen: GetDaysChoosen,
val deleteDaysChoosen: DeleteDaysChoosen,
val addDaysChoosen: AddDaysChoosen
)
at first i would just like to learn how to add days. so addDaysChoose:
class AddDaysChoosen(
private val repository: DaysChoosenRepository
) {
suspend operator fun invoke(days: DaysChoosenEntity){
repository.insertDaysChoosen(days)
}
}
Now my ScheduleScreenVM:
@HiltViewModel
class ScheduleScreenVM @Inject constructor (
private val daysChoosenUseCase: DaysChoosenUseCase
): ViewModel() {
//states
val allDays = daysChoosenUseCase.getDaysChoosen
var daysChoosen by mutableStateOf(mutableListOf(0,0,0,0,0,0,0))
var rememem by mutableStateOf(
SnapshotStateList<Boolean>().also {
for(day in 0..6){
it.add(false)
}
}
)
//events
fun snapChanged(int: Int){ rememem[int] = !rememem[int] }
fun insertDays(int: Int) = viewModelScope.launch {
daysChoosenUseCase.addDaysChoosen(
DaysChoosenEntity(daysChoosen = int)
)
}
}
This is where a little difficulty begins. To add a day to the database I have to use the insertDays function from the VM. for example
DaysOfWeekButton(
onClick =
{
viewModel.snapChanged(day)
if(viewModel.rememem[day]) {
viewModel.daysChoosen.set(day, 1)
viewModel.insertDays(1)
}
else {
viewModel.daysChoosen.set(day, 0)
viewModel.insertDays(0)
}
},
I want to show the result like this (probably)
Text("${viewModel.allDays}")
I didn't even start saving anything, but I already got an error
.data_source.DaysChoosenDAO cannot be provided without an @Provides-annotated method.
UPD. i dont know how i repair this, but now i can run app, but when i navigate to ScheduleScreen i got this error
Cannot create an instance of class com.example.ic.ScheduleScreenVM
I don't really understand why and why it appeared. But the question remains the same. How do I store data in a table now..?
Comments
Post a Comment