Моя ViewModel:
Код: Выделить всё
@HiltViewModel
class CalendarViewModel @Inject constructor(private val scheduleDao: ScheduleDao) : ViewModel() {
init {
fillScheduleEntities()
}
val timeFormat = LocalTime.Format { hour(); char(':'); minute(); char(':'); second() }
val schedule: StateFlow = scheduleDao.getAll().stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = emptyList(),
)
private val selectedDate = MutableStateFlow(LocalDate(2026, 1, 1))
val selectedDateUiState = selectedDate.asStateFlow()
val selectedDateAvailableTime: StateFlow = selectedDate.map { date ->
getAvailableTimeForDate(date)
}.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = emptyList(),
)
fun onDateChanged(dateMillis: Long?) {
if (dateMillis != null) {
selectedDate.value = Instant.fromEpochMilliseconds(dateMillis)
.toLocalDateTime(TimeZone.currentSystemDefault()).date
}
}
private fun getAvailableTimeForDate(date: LocalDate): List {
val availableTime = mutableListOf()
try {
if (date.year > 2000) {
availableTime.plus(schedule.value.firstOrNull { it.date == date }?.availableTime)
}
} catch (e: Exception) {
availableTime.clear()
Log.e("CalendarViewModel.getAvailableTimeForDate", e.toString())
throw (e)
}
return availableTime
}
private fun getScheduleEntities(): List {
val scheduleEntities = mutableListOf()
try {
val startDate = LocalDate(2000, 1, 1)
val endDate = java.time.LocalDate.now().toKotlinLocalDate()
val startTime = java.time.LocalTime.of(9, 0, 0)
val endTime = java.time.LocalTime.of(18, 0, 0)
val timeList = generateSequence(startTime) { it.plusMinutes(30) }
.takeWhile { it item.format(timeFormat) }
@TypeConverter
fun stringToListLocalTime(value: String?): List? =
value?.split(',')?.map { item -> LocalTime.parse(item.trim(), timeFormat) }
}
Код: Выделить всё
@Dao
interface ScheduleDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(scheduleEntities: List): List
@Query("SELECT * FROM ScheduleEntity")
fun getAll(): Flow
}
Код: Выделить всё
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainLayout() {
val viewModel: CalendarViewModel = hiltViewModel()
val schedule by viewModel.schedule.collectAsStateWithLifecycle()
val selectedDate by viewModel.selectedDateUiState.collectAsStateWithLifecycle()
val time by viewModel.selectedDateAvailableTime.collectAsStateWithLifecycle()
val datePickerState = rememberDatePickerState(initialSelectedDate = selectedDate.toJavaLocalDate())
LaunchedEffect(datePickerState.selectedDateMillis) {
viewModel.onDateChanged(datePickerState.selectedDateMillis)
}
Column {
DatePicker(datePickerState)
LazyVerticalGrid(
columns = GridCells.Fixed(7),
) {
items(time) { curTime ->
Text(curTime.format(viewModel.timeFormat))
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... m-database
Мобильная версия