I'm trying to convert the returned date from a calendar into a string value, but it has an unwanted number. I've made a text that views the selected date value for today, and it's successfully printed in the wanted format (2024-03-09). But, when trying to view the (formattedString) value, it views (2024-03-69). This allows the API to view different data. If anyone can help.
Edit: I'm trying to view football matches by date from Calendar. The calling function in LaunchedEffect will be executed every time date changed by the Calendar.
The app is working perfectly with "yyyy-MM-DD" format but crashes with "yyyy-MM-dd" while running on a physical device or a virtual device.
val viewModel: MainViewModel = hiltViewModel()
val state = rememberUseCaseState()
val selectedDate = rememberSaveable { mutableStateOf<LocalDate?>(LocalDate.now()) }
val disabledDates = listOf(
LocalDate.now().minusDays(30),
LocalDate.now().plusDays(30),
)
val localDate = selectedDate.value //For reference
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-DD")
val formattedString = localDate!!.format(formatter)
viewModel.saveDialogueDate(formattedString)
CalendarDialog(
state = state,
config = CalendarConfig(
yearSelection = false,
monthSelection = true,
style = CalendarStyle.MONTH,
disabledDates = disabledDates
),
selection = CalendarSelection.Date { newDate ->
selectedDate.value = newDate
viewModel.saveCalendarClicked(true)
}
)
Fetch API Data
@Composable
fun FetchTodayGamesData(
mainViewModel: MainViewModel = hiltViewModel()
) {
val date = mainViewModel.dialogueDate.observeAsState().value!!
if (mainViewModel.isClicked.value!!) {
LaunchedEffect(key1 = null) {
mainViewModel.getTodayMatches(
date, 2023
)
mainViewModel.saveCalendarClicked(false)
}
}
when (val state =
mainViewModel.todayMatchesState.collectAsState().value
) {
is TodayMatchesState.Empty -> {}
is TodayMatchesState.Loading -> {}
is TodayMatchesState.Error -> {}
is TodayMatchesState.Success -> {
TodayMatchesLazy(
match = state.data.body()!!.response?.get(0)!!,
matchesList = state.data.body()!!.response!!
)
}
}
}
ViewModel
private var _todayMatchesState = MutableStateFlow<TodayMatchesState>(TodayMatchesState.Empty)
val todayMatchesState: StateFlow<TodayMatchesState> = _todayMatchesState
//Calendar Dialogue Date
private var _dialogueDate = MutableLiveData("")
val dialogueDate: LiveData<String> = _dialogueDate
private var _isClicked = MutableLiveData(true)
val isClicked: LiveData<Boolean> = _isClicked
fun saveCalendarClicked(isClicked: Boolean) {
_isClicked.value = isClicked
}
fun saveDialogueDate(date: String) {
_dialogueDate.value = date
}
fun getTodayMatches(date: String, season: Int) {
_todayMatchesState.value = TodayMatchesState.Loading
viewModelScope.launch(Dispatchers.IO) {
try {
val todayMatchesResponse = todayMatchesRepository.getTodayMatches(date, season)
_todayMatchesState.value = TodayMatchesState.Success(todayMatchesResponse)
}
catch (exception: HttpException) {
_todayMatchesState.value = TodayMatchesState.Error("Something went wrong")
}
catch (exception: IOException) {
_todayMatchesState.value = TodayMatchesState.Error("No internet connection")
}
}
}
