Skip to content

Commit

Permalink
- fixes an issue where the app wouldn't switch to the correct year on…
Browse files Browse the repository at this point in the history
… tablets
  • Loading branch information
pablo03v committed Apr 14, 2024
1 parent 9599ac5 commit 0a66c39
Showing 1 changed file with 56 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ fun CalendarTab(
itemEvent: (ItemEvent) -> Unit,
onNavigate: () -> Unit,
) {
val selectedYear by remember { mutableIntStateOf(itemState.selectedYearCalendar) }

LaunchedEffect(selectedYear) {
if (itemState.selectedYearCalendar != LocalDate.now().year) {
itemEvent(ItemEvent.SetSelectedYearCalendar(LocalDate.now().year))
}
}

Scaffold(
topBar = {
OverloadTopAppBar(
Expand All @@ -77,6 +69,62 @@ fun CalendarTab(
Box(modifier = Modifier.fillMaxSize()) {
Column(modifier = Modifier.padding(paddingValues)) {
AnimatedVisibility(visible = contentType == OverloadContentType.DUAL_PANE) {
val selectedYear by remember { mutableIntStateOf(itemState.selectedYearCalendar) }
val selectedDay = getLocalDate(itemState.selectedDayCalendar)

val firstYear =
if (itemState.items.isEmpty()) {
LocalDate.now().year
} else {
itemState.items.minByOrNull { it.startTime }
?.let { convertStringToLocalDateTime(it.startTime).year }
?: LocalDate.now().year
}
val firstDay = LocalDate.of(firstYear, 1, 1)
val lastDay = LocalDate.now()

val daysCount = ChronoUnit.DAYS.between(firstDay, lastDay).toInt() + 1

var scrollToPage = true
val pagerState =
rememberPagerState(
initialPage = daysCount,
initialPageOffsetFraction = 0f,
pageCount = { daysCount },
)

LaunchedEffect(selectedYear) {
if (itemState.selectedYearCalendar != selectedDay.year) {
itemEvent(ItemEvent.SetSelectedYearCalendar(selectedDay.year))
}
}

LaunchedEffect(pagerState.currentPage) {
scrollToPage = false
itemEvent(
ItemEvent.SetSelectedDayCalendar(
LocalDate.now()
.minusDays((daysCount - pagerState.currentPage - 1).toLong())
.toString(),
),
)
}

LaunchedEffect(itemState.selectedDayCalendar) {
if (scrollToPage) {
val highlightedDay = LocalDate.now().minusDays((daysCount - pagerState.currentPage - 1).toLong())
if (getLocalDate(itemState.selectedDayCalendar) != highlightedDay) {
pagerState.scrollToPage(ChronoUnit.DAYS.between(firstDay, selectedDay).toInt())
}
} else {
scrollToPage = true
}

if (selectedYear != selectedDay.year) {
itemEvent(ItemEvent.SetSelectedYearCalendar(selectedDay.year))
}
}

Row(modifier = Modifier.fillMaxSize()) {
Box(
modifier = Modifier.weight(1f),
Expand All @@ -103,56 +151,6 @@ fun CalendarTab(
Box(
modifier = Modifier.weight(1f),
) {
val selectedDay = getLocalDate(itemState.selectedDayCalendar)

val firstYear =
if (itemState.items.isEmpty()) {
LocalDate.now().year
} else {
itemState.items.minByOrNull { it.startTime }
?.let { convertStringToLocalDateTime(it.startTime).year }
?: LocalDate.now().year
}

val firstDay = LocalDate.of(firstYear, 1, 1)
val lastDay = LocalDate.now()
val daysCount = ChronoUnit.DAYS.between(firstDay, lastDay).toInt() + 1

var scrollToPage = true

val pagerState =
rememberPagerState(
initialPage = daysCount,
initialPageOffsetFraction = 0f,
pageCount = { daysCount },
)

LaunchedEffect(pagerState.currentPage) {
scrollToPage = false
itemEvent(
ItemEvent.SetSelectedDayCalendar(
LocalDate.now()
.minusDays((daysCount - pagerState.currentPage - 1).toLong())
.toString(),
),
)

if (selectedYear != selectedDay.year) {
itemEvent(ItemEvent.SetSelectedYearCalendar(selectedDay.year))
}
}

LaunchedEffect(itemState.selectedDayCalendar) {
if (scrollToPage) {
val highlightedDay = LocalDate.now().minusDays((daysCount - pagerState.currentPage - 1).toLong())
if (getLocalDate(itemState.selectedDayCalendar) != highlightedDay) {
pagerState.scrollToPage(ChronoUnit.DAYS.between(firstDay, selectedDay).toInt())
}
} else {
scrollToPage = true
}
}

HorizontalPager(
state = pagerState,
) { page ->
Expand Down

0 comments on commit 0a66c39

Please sign in to comment.