From ec55f431539e84223da34ca9255a235fd7d438d5 Mon Sep 17 00:00:00 2001 From: merenyimiklos Date: Thu, 19 Nov 2026 23:37:18 +0100 Subject: [PATCH] Revert "feat(timetable, substitutions): add date-based navigation and refactor A/B week filtering" This reverts commit a5d88bd9609bed3323f76d1828f5025b656acc90. --- .../components/SchoolWeekDaySelector.kt | 140 --------- .../petrik/filcapp/data/LessonWeekFilter.kt | 57 ---- .../filcapp/screens/SubstitutionScreen.kt | 186 +++-------- .../petrik/filcapp/screens/TimetableScreen.kt | 289 +++++++++++++----- .../components/SchoolWeekDaySelectorTest.kt | 50 --- .../filcapp/data/LessonWeekFilterTest.kt | 46 --- 6 files changed, 251 insertions(+), 517 deletions(-) delete mode 100644 composeApp/src/commonMain/kotlin/hu/petrik/filcapp/components/SchoolWeekDaySelector.kt delete mode 100644 composeApp/src/commonMain/kotlin/hu/petrik/filcapp/data/LessonWeekFilter.kt delete mode 100644 composeApp/src/commonTest/kotlin/hu/petrik/filcapp/components/SchoolWeekDaySelectorTest.kt delete mode 100644 composeApp/src/commonTest/kotlin/hu/petrik/filcapp/data/LessonWeekFilterTest.kt diff --git a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/components/SchoolWeekDaySelector.kt b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/components/SchoolWeekDaySelector.kt deleted file mode 100644 index e9ca638..0000000 --- a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/components/SchoolWeekDaySelector.kt +++ /dev/null @@ -1,140 +0,0 @@ -package hu.petrik.filcapp.components - -import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.heightIn -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Surface -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.unit.dp -import hu.petrik.filcapp.settings.tr -import kotlinx.datetime.DayOfWeek -import kotlinx.datetime.LocalDate - -/** Shared date picker for timetable and substitutions. */ -@Composable -fun SchoolWeekDaySelector( - today: LocalDate, - weekOffset: Int, - selectedDayOrder: Int, - onDaySelected: (Int) -> Unit, -) { - val monday = schoolWeekMonday(today, weekOffset) - - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy(4.dp), - ) { - (1..5).forEach { dayIndex -> - val date = LocalDate.fromEpochDays(monday.toEpochDays() + dayIndex - 1) - val selected = selectedDayOrder == dayIndex - - Surface( - modifier = - Modifier - .weight(1f) - .heightIn(min = 53.dp) - .clickable { - onDaySelected(dayIndex) - }, - shape = RoundedCornerShape(12.dp), - color = - if (selected) { - MaterialTheme.colorScheme.primary - } else { - MaterialTheme.colorScheme.surface - }, - shadowElevation = if (selected) 3.dp else 0.dp, - ) { - Column( - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Center, - modifier = Modifier.padding(vertical = 5.dp), - ) { - Text( - text = schoolShortDayName(dayIndex), - style = MaterialTheme.typography.bodySmall, - color = - if (selected) { - Color.White - } else { - MaterialTheme.colorScheme.onSurface.copy(alpha = 0.75f) - }, - fontWeight = if (selected) FontWeight.Bold else FontWeight.Medium, - ) - - Text( - text = date.day.toString(), - style = MaterialTheme.typography.titleSmall, - color = - if (selected) { - Color.White - } else { - MaterialTheme.colorScheme.onSurface - }, - fontWeight = if (selected) FontWeight.ExtraBold else FontWeight.Bold, - ) - } - } - } - } -} - -internal fun schoolWeekMonday( - date: LocalDate, - weekOffset: Int, -): LocalDate { - val daysSinceMonday = - when (date.dayOfWeek) { - DayOfWeek.MONDAY -> 0 - DayOfWeek.TUESDAY -> 1 - DayOfWeek.WEDNESDAY -> 2 - DayOfWeek.THURSDAY -> 3 - DayOfWeek.FRIDAY -> 4 - DayOfWeek.SATURDAY -> 5 - DayOfWeek.SUNDAY -> 6 - } - - return LocalDate.fromEpochDays( - date.toEpochDays() - daysSinceMonday + weekOffset * 7, - ) -} - -internal fun schoolDefaultDay(day: DayOfWeek): Int = - when (day) { - DayOfWeek.MONDAY -> 1 - DayOfWeek.TUESDAY -> 2 - DayOfWeek.WEDNESDAY -> 3 - DayOfWeek.THURSDAY -> 4 - DayOfWeek.FRIDAY -> 5 - DayOfWeek.SATURDAY, DayOfWeek.SUNDAY -> 1 - } - -private fun schoolShortDayName(dayOrder: Int): String = - when (dayOrder) { - 1 -> tr("Hét", "Mon") - 2 -> tr("Ked", "Tue") - 3 -> tr("Sze", "Wed") - 4 -> tr("Csü", "Thu") - 5 -> tr("Pén", "Fri") - else -> "" - } - -/** Translate a selected school weekday and week offset to the actual date. */ -internal fun schoolDateFor( - today: LocalDate, - weekOffset: Int, - dayOrder: Int, -): LocalDate { - require(dayOrder in 1..5) - return LocalDate.fromEpochDays(schoolWeekMonday(today, weekOffset).toEpochDays() + dayOrder - 1) -} diff --git a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/data/LessonWeekFilter.kt b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/data/LessonWeekFilter.kt deleted file mode 100644 index b4e5eca..0000000 --- a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/data/LessonWeekFilter.kt +++ /dev/null @@ -1,57 +0,0 @@ -package hu.petrik.filcapp.data - -import kotlinx.datetime.LocalDate - -internal enum class LessonWeekType { A, B, ALL } - -internal fun lessonWeekType(definition: WeekDefinitionDto?): LessonWeekType { - if (definition == null) return LessonWeekType.ALL - when (definition.short.trim().uppercase()) { - "A" -> return LessonWeekType.A - "B" -> return LessonWeekType.B - } - val name = definition.name.trim().lowercase().replace('é', 'e') - when (name) { - "het a", "a het", "week a" -> return LessonWeekType.A - "het b", "b het", "week b" -> return LessonWeekType.B - } - return when (definition.weeks.map { it.trim() }.toSet()) { - setOf("10") -> LessonWeekType.A - setOf("01") -> LessonWeekType.B - else -> LessonWeekType.ALL - } -} - -internal fun filterLessonsForSchoolWeek( - lessons: List, - week: LessonWeekType, -): List = - lessons.filter { item -> - val lessonType = lessonWeekType(item.weekDefinition) - week == LessonWeekType.ALL || lessonType == LessonWeekType.ALL || lessonType == week - } - -/** The first school week containing September 1 is A by convention. - * The timetable header allows manually inverting this convention when the - * school's actual A/B ordering is different. */ -internal fun schoolWeekTypeFor( - monday: LocalDate, - reversed: Boolean = false, -): LessonWeekType { - val friday = LocalDate.fromEpochDays(monday.toEpochDays() + 4) - val academicYear = if (friday.monthNumber >= 9) friday.year else friday.year - 1 - val septemberFirst = LocalDate(academicYear, 9, 1) - val daysSinceMonday = when (septemberFirst.dayOfWeek) { - kotlinx.datetime.DayOfWeek.MONDAY -> 0 - kotlinx.datetime.DayOfWeek.TUESDAY -> 1 - kotlinx.datetime.DayOfWeek.WEDNESDAY -> 2 - kotlinx.datetime.DayOfWeek.THURSDAY -> 3 - kotlinx.datetime.DayOfWeek.FRIDAY -> 4 - kotlinx.datetime.DayOfWeek.SATURDAY -> 5 - kotlinx.datetime.DayOfWeek.SUNDAY -> 6 - } - val startMonday = LocalDate.fromEpochDays(septemberFirst.toEpochDays() - daysSinceMonday) - val weekIndex = (monday.toEpochDays() - startMonday.toEpochDays()) / 7 - val parity = weekIndex + (if (reversed) 1 else 0) - return if (parity % 2 == 0) LessonWeekType.A else LessonWeekType.B -} diff --git a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/screens/SubstitutionScreen.kt b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/screens/SubstitutionScreen.kt index 7a5ee86..47ea0ad 100644 --- a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/screens/SubstitutionScreen.kt +++ b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/screens/SubstitutionScreen.kt @@ -1,6 +1,5 @@ package hu.petrik.filcapp.screens import androidx.compose.foundation.background -import androidx.compose.foundation.clickable import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -15,15 +14,12 @@ import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.KeyboardArrowLeft -import androidx.compose.material.icons.filled.KeyboardArrowRight import androidx.compose.material.icons.filled.SwapCalls import androidx.compose.material3.Button import androidx.compose.material3.Card import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.FilterChip import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text @@ -36,20 +32,14 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.rememberVectorPainter import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import cafe.adriel.voyager.navigator.tab.Tab import cafe.adriel.voyager.navigator.tab.TabOptions import hu.petrik.filcapp.auth.AuthState -import hu.petrik.filcapp.components.SchoolWeekDaySelector import hu.petrik.filcapp.components.SearchableSelection import hu.petrik.filcapp.components.TimetableFilterChips -import hu.petrik.filcapp.components.schoolDateFor -import hu.petrik.filcapp.components.schoolDefaultDay -import hu.petrik.filcapp.components.schoolWeekMonday import hu.petrik.filcapp.data.ChronosApi import hu.petrik.filcapp.data.CohortDto import hu.petrik.filcapp.data.MovedLessonItemDto @@ -64,11 +54,11 @@ import hu.petrik.filcapp.settings.tr import hu.petrik.filcapp.widgets.WidgetDestination import hu.petrik.filcapp.widgets.WidgetNavigation import hu.petrik.filcapp.widgets.WidgetSelectionType -import kotlin.time.Clock -import kotlin.time.ExperimentalTime import kotlinx.datetime.LocalDate import kotlinx.datetime.TimeZone import kotlinx.datetime.todayIn +import kotlin.time.Clock +import kotlin.time.ExperimentalTime private object SubstitutionSessionCache { var substitutions: List = emptyList() @@ -80,7 +70,6 @@ private object SubstitutionSessionCache { var error: String? = null } -@OptIn(ExperimentalTime::class) @Composable fun SubstitutionScreen() { var substitutions by remember { mutableStateOf(SubstitutionSessionCache.substitutions) } @@ -96,17 +85,6 @@ fun SubstitutionScreen() { var personalizedUserId by remember { mutableStateOf(null) } var appliedWidgetRequest by remember { mutableStateOf(0) } - val today = Clock.System.todayIn(TimeZone.currentSystemDefault()) - var weekOffset by remember { mutableStateOf(0) } - var selectedDayOrder by remember { mutableStateOf(schoolDefaultDay(today.dayOfWeek)) } - val selectedDate = schoolDateFor(today, weekOffset, selectedDayOrder) - val selectedDateKey = selectedDate.toString() - - fun moveWeek(delta: Int) { - weekOffset += delta - selectedDayOrder = if (weekOffset == 0) schoolDefaultDay(today.dayOfWeek) else 1 - } - LaunchedEffect(reloadKey) { if (reloadKey == 0 && SubstitutionSessionCache.loaded) { loading = false @@ -119,8 +97,8 @@ fun SubstitutionScreen() { cohorts = ChronosApi.getCohorts(latest.id).sortedBy { it.name } teachers = ChronosApi.getTeachers().sortedBy { it.displayName } classrooms = ChronosApi.getClassrooms().sortedBy { it.name } - substitutions = ChronosApi.getSubstitutions() - movedLessons = ChronosApi.getMovedLessons() + substitutions = ChronosApi.getSubstitutions().filter { isTodayOrFuture(it.substitution.date) } + movedLessons = ChronosApi.getMovedLessons().filter { isTodayOrFuture(it.movedLesson.date) } SubstitutionSessionCache.cohorts = cohorts SubstitutionSessionCache.teachers = teachers SubstitutionSessionCache.classrooms = classrooms @@ -197,12 +175,8 @@ fun SubstitutionScreen() { TimetableFilter.CLASSROOM -> classrooms.map { it.id to displayName(it) } } - val filteredSubstitutions = - filterSubstitutions(substitutions, filter, selectedId, cohorts) - .filter { normalizeDate(it.substitution.date) == selectedDateKey } - val filteredMovedLessons = - filterMovedLessons(movedLessons, filter, selectedId, cohorts) - .filter { normalizeDate(it.movedLesson.date) == selectedDateKey } + val filteredSubstitutions = filterSubstitutions(substitutions, filter, selectedId, cohorts) + val filteredMovedLessons = filterMovedLessons(movedLessons, filter, selectedId, cohorts) PullToRefreshBox( isRefreshing = loading, @@ -217,20 +191,7 @@ fun SubstitutionScreen() { .padding(horizontal = 12.dp, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(12.dp), ) { - SubstitutionWeekSwitcher( - today = today, - weekOffset = weekOffset, - onPreviousWeek = { moveWeek(-1) }, - onNextWeek = { moveWeek(1) }, - ) - SchoolWeekDaySelector( - today = today, - weekOffset = weekOffset, - selectedDayOrder = selectedDayOrder, - onDaySelected = { selectedDayOrder = it }, - ) SubstitutionOverviewHeader( - date = selectedDate, changeCount = filteredSubstitutions.size + filteredMovedLessons.size, ) @@ -303,8 +264,8 @@ fun SubstitutionScreen() { Card(modifier = Modifier.fillMaxWidth()) { Text( tr( - "Ezen a napon nincs helyettesítés vagy áthelyezett óra ehhez a szűréshez.", - "No substitutions or moved lessons on this day for this filter.", + "Nincs aktuális vagy közelgő helyettesítés ehhez a szűréshez.", + "No current or upcoming substitutions for this filter.", ), modifier = Modifier.padding(18.dp), color = MaterialTheme.colorScheme.onSurfaceVariant, @@ -312,118 +273,48 @@ fun SubstitutionScreen() { } } - if (error == null) { - filteredSubstitutions.forEach { substitution -> SubstitutionCard(substitution) } - filteredMovedLessons.forEach { movedLesson -> MovedLessonCard(movedLesson) } - } - } - } -} + val dates = + ( + filteredSubstitutions.map { normalizeDate(it.substitution.date) } + + filteredMovedLessons.map { normalizeDate(it.movedLesson.date) } + ).distinct() + .sorted() -@Composable -private fun SubstitutionWeekSwitcher( - today: LocalDate, - weekOffset: Int, - onPreviousWeek: () -> Unit, - onNextWeek: () -> Unit, -) { - val monday = schoolWeekMonday(today, weekOffset) - val friday = LocalDate.fromEpochDays(monday.toEpochDays() + 4) - val range = - if (monday.year == friday.year) { - "${monday.year}. ${monday.monthNumber}. ${monday.day}. – ${friday.monthNumber}. ${friday.day}." - } else { - "${monday.year}. ${monday.monthNumber}. ${monday.day}. – " + - "${friday.year}. ${friday.monthNumber}. ${friday.day}." - } + dates.forEach { date -> + Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { + Text(formatDate(date), style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.SemiBold) - Surface( - modifier = Modifier.fillMaxWidth(), - shape = RoundedCornerShape(12.dp), - color = MaterialTheme.colorScheme.surface, - shadowElevation = 1.dp, - ) { - Row( - modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 8.dp), - horizontalArrangement = Arrangement.SpaceBetween, - verticalAlignment = Alignment.CenterVertically, - ) { - SubstitutionWeekArrow( - icon = Icons.Default.KeyboardArrowLeft, - contentDescription = tr("Előző hét", "Previous week"), - onClick = onPreviousWeek, - ) - Column( - modifier = Modifier.weight(1f), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(2.dp), - ) { - Text( - text = range, - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold, - color = MaterialTheme.colorScheme.onSurface, - textAlign = TextAlign.Center, - ) - Text( - text = tr("Hétfő – péntek", "Monday – Friday"), - style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant, - ) - } - SubstitutionWeekArrow( - icon = Icons.Default.KeyboardArrowRight, - contentDescription = tr("Következő hét", "Next week"), - onClick = onNextWeek, - ) - } - } -} + filteredSubstitutions + .filter { normalizeDate(it.substitution.date) == date } + .forEach { substitution -> SubstitutionCard(substitution) } -@Composable -private fun SubstitutionWeekArrow( - icon: ImageVector, - contentDescription: String, - onClick: () -> Unit, -) { - Surface( - modifier = Modifier.size(32.dp).clickable(onClick = onClick), - shape = CircleShape, - color = MaterialTheme.colorScheme.surfaceVariant, - ) { - Box(contentAlignment = Alignment.Center) { - Icon( - imageVector = icon, - contentDescription = contentDescription, - modifier = Modifier.size(18.dp), - tint = MaterialTheme.colorScheme.onSurface, - ) + filteredMovedLessons + .filter { normalizeDate(it.movedLesson.date) == date } + .forEach { movedLesson -> MovedLessonCard(movedLesson) } + } + } } } } +@OptIn(ExperimentalTime::class) @Composable -private fun SubstitutionOverviewHeader( - date: LocalDate, - changeCount: Int, -) { +private fun SubstitutionOverviewHeader(changeCount: Int) { + val today = Clock.System.todayIn(TimeZone.currentSystemDefault()) Row( modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy(8.dp), + horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.Top, ) { - Column( - modifier = Modifier.weight(1f), - verticalArrangement = Arrangement.spacedBy(2.dp), - ) { + Column(verticalArrangement = Arrangement.spacedBy(2.dp)) { Text( text = tr("HIVATALOS NAPLÓ", "OFFICIAL LOG"), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) Text( - text = formatHungarianDate(date), + text = formatHungarianDate(today), style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.onBackground, @@ -448,9 +339,9 @@ private fun SubstitutionOverviewHeader( Text( text = if (changeCount == 1) { - tr("1 változás", "1 change") + tr("1 változás ma", "1 change today") } else { - tr("$changeCount változás", "$changeCount changes") + tr("$changeCount változás ma", "$changeCount changes today") }, style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.primary, @@ -858,7 +749,18 @@ private fun formatHungarianDate(date: LocalDate): String { private fun normalizeDate(value: String): String = value.take(10) +private fun formatDate(value: String): String { + val parts = normalizeDate(value).split("-") + return if (parts.size == 3) "${parts[0]}. ${parts[1]}. ${parts[2]}." else value +} +@OptIn(ExperimentalTime::class) +private fun isTodayOrFuture(value: String): Boolean = + runCatching { + val date = LocalDate.parse(normalizeDate(value)) + val today = Clock.System.todayIn(TimeZone.currentSystemDefault()) + date >= today + }.getOrDefault(true) private fun formatTime(value: String): String = value.take(5) diff --git a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/screens/TimetableScreen.kt b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/screens/TimetableScreen.kt index a8e518a..bb7e7d3 100644 --- a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/screens/TimetableScreen.kt +++ b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/screens/TimetableScreen.kt @@ -46,39 +46,32 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.rememberVectorPainter import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import cafe.adriel.voyager.navigator.tab.Tab import cafe.adriel.voyager.navigator.tab.TabOptions import hu.petrik.filcapp.auth.AuthState -import hu.petrik.filcapp.components.SchoolWeekDaySelector import hu.petrik.filcapp.components.SearchableSelection import hu.petrik.filcapp.components.TimetableFilterChips -import hu.petrik.filcapp.components.schoolDefaultDay -import hu.petrik.filcapp.components.schoolWeekMonday import hu.petrik.filcapp.data.ChronosApi import hu.petrik.filcapp.data.CohortDto import hu.petrik.filcapp.data.LessonDto -import hu.petrik.filcapp.data.LessonWeekType import hu.petrik.filcapp.data.NamedRefDto import hu.petrik.filcapp.data.TeacherDto import hu.petrik.filcapp.data.TimetableDto import hu.petrik.filcapp.data.TimetableFilter import hu.petrik.filcapp.data.WeekDefinitionDto import hu.petrik.filcapp.data.cohortLabels -import hu.petrik.filcapp.data.filterLessonsForSchoolWeek -import hu.petrik.filcapp.data.lessonWeekType -import hu.petrik.filcapp.data.schoolWeekTypeFor import hu.petrik.filcapp.settings.tr import hu.petrik.filcapp.widgets.WidgetDestination import hu.petrik.filcapp.widgets.WidgetNavigation import hu.petrik.filcapp.widgets.WidgetSelectionType -import kotlin.time.Clock -import kotlin.time.ExperimentalTime +import kotlinx.datetime.DayOfWeek import kotlinx.datetime.LocalDate import kotlinx.datetime.TimeZone import kotlinx.datetime.todayIn +import kotlin.time.Clock +import kotlin.time.ExperimentalTime private object TimetableSessionCache { var timetables: List = emptyList() @@ -115,11 +108,11 @@ fun TimetableScreen() { var filter by remember { mutableStateOf(TimetableFilter.COHORT) } var selectedId by remember { mutableStateOf(null) } var lessons by remember { mutableStateOf>(emptyList()) } - var reverseABWeeks by remember { mutableStateOf(false) } + var selectedWeekId by remember { mutableStateOf(null) } val today = Clock.System.todayIn(TimeZone.currentSystemDefault()) var weekOffset by remember { mutableStateOf(0) } - var selectedDayOrder by remember { mutableStateOf(schoolDefaultDay(today.dayOfWeek)) } + var selectedDayOrder by remember { mutableStateOf(defaultSchoolDay(today.dayOfWeek)) } var showTimetableSelector by remember { mutableStateOf(false) } var loadingReferenceData by remember { mutableStateOf(!TimetableSessionCache.referenceLoaded) } @@ -325,18 +318,24 @@ fun TimetableScreen() { .distinctBy { it.id } .sortedBy { it.name } - // Do not treat the "both weeks" definition as a third week. - val hasABWeeks = weekDefinitions.any { lessonWeekType(it) != LessonWeekType.ALL } - val selectedWeekType = schoolWeekTypeFor(schoolWeekMonday(today, weekOffset), reverseABWeeks) + LaunchedEffect(weekDefinitions) { + if (weekDefinitions.isEmpty()) { + selectedWeekId = null + } else if (selectedWeekId !in weekDefinitions.map { it.id }) { + selectedWeekId = weekDefinitions.first().id + } + } + val selectedWeek = - if (hasABWeeks) { - weekDefinitions.firstOrNull { lessonWeekType(it) == selectedWeekType } - ?: WeekDefinitionDto(short = selectedWeekType.name) + weekDefinitions.firstOrNull { it.id == selectedWeekId } + ?: weekDefinitions.firstOrNull() + + val visibleLessons = + if (selectedWeek == null) { + lessons } else { - null + lessons.filter { it.weekDefinition?.id == selectedWeek.id } } - val visibleLessons = - if (hasABWeeks) filterLessonsForSchoolWeek(lessons, selectedWeekType) else lessons val selectedDayLessons = visibleLessons @@ -392,7 +391,27 @@ fun TimetableScreen() { fun moveWeek(delta: Int) { weekOffset += delta - selectedDayOrder = if (weekOffset == 0) schoolDefaultDay(today.dayOfWeek) else 1 + + if (weekDefinitions.size > 1) { + val currentIndex = + weekDefinitions + .indexOfFirst { it.id == selectedWeekId } + .takeIf { it >= 0 } + ?: 0 + + val nextIndex = + (currentIndex + delta) + .mod(weekDefinitions.size) + + selectedWeekId = weekDefinitions[nextIndex].id + } + + selectedDayOrder = + if (weekOffset == 0) { + defaultSchoolDay(today.dayOfWeek) + } else { + 1 + } } PullToRefreshBox( @@ -412,13 +431,11 @@ fun TimetableScreen() { today = today, weekOffset = weekOffset, selectedWeek = selectedWeek, - canToggleWeek = hasABWeeks, - onToggleWeek = { reverseABWeeks = !reverseABWeeks }, onPreviousWeek = { moveWeek(-1) }, onNextWeek = { moveWeek(1) }, ) - SchoolWeekDaySelector( + TimetableDaySelector( today = today, weekOffset = weekOffset, selectedDayOrder = selectedDayOrder, @@ -498,12 +515,10 @@ private fun TimetableWeekSwitcher( today: LocalDate, weekOffset: Int, selectedWeek: WeekDefinitionDto?, - canToggleWeek: Boolean, - onToggleWeek: () -> Unit, onPreviousWeek: () -> Unit, onNextWeek: () -> Unit, ) { - val monday = schoolWeekMonday(today, weekOffset) + val monday = mondayOfWeek(today, weekOffset) val friday = LocalDate.fromEpochDays(monday.toEpochDays() + 4) Surface( @@ -512,75 +527,72 @@ private fun TimetableWeekSwitcher( color = MaterialTheme.colorScheme.surface, shadowElevation = 1.dp, ) { - Column( - modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 8.dp), - verticalArrangement = Arrangement.spacedBy(5.dp), + Row( + modifier = + Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 8.dp), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically, ) { Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy(6.dp), + horizontalArrangement = Arrangement.spacedBy(4.dp), verticalAlignment = Alignment.CenterVertically, ) { WeekArrowButton( icon = Icons.Default.KeyboardArrowLeft, onClick = onPreviousWeek, ) + Column( - modifier = Modifier.weight(1f), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(2.dp), + verticalArrangement = Arrangement.spacedBy(1.dp), ) { Text( text = formatWeekRange(monday, friday), style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onSurface, fontWeight = FontWeight.SemiBold, - textAlign = TextAlign.Center, ) + Text( - text = tr( - "${approximateTeachingWeek(monday)}. tanítási hét", - "School week ${approximateTeachingWeek(monday)}", - ), + text = + tr( + "${approximateTeachingWeek(monday)}. tanítási hét", + "School week ${approximateTeachingWeek(monday)}", + ), style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant, + color = MaterialTheme.colorScheme.onSurface, ) } + WeekArrowButton( icon = Icons.Default.KeyboardArrowRight, onClick = onNextWeek, ) } - Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) { - Surface( - modifier = Modifier.clickable(enabled = canToggleWeek, onClick = onToggleWeek), - shape = RoundedCornerShape(999.dp), - color = MaterialTheme.colorScheme.primary.copy(alpha = 0.12f), + + Surface( + shape = RoundedCornerShape(999.dp), + color = MaterialTheme.colorScheme.primary.copy(alpha = 0.12f), + ) { + Row( + modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp), + horizontalArrangement = Arrangement.spacedBy(6.dp), + verticalAlignment = Alignment.CenterVertically, ) { - Row( - modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp), - horizontalArrangement = Arrangement.spacedBy(6.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - Box( - modifier = Modifier.size(6.dp) + Box( + modifier = + Modifier + .size(6.dp) .background(MaterialTheme.colorScheme.primary, CircleShape), - ) - Text( - text = weekBadgeLabel(selectedWeek), - style = MaterialTheme.typography.labelMedium, - color = MaterialTheme.colorScheme.primary, - fontWeight = FontWeight.Bold, - ) - if (canToggleWeek) { - Icon( - imageVector = Icons.Default.KeyboardArrowDown, - contentDescription = tr("A/B hét felcserélése", "Swap A/B week"), - modifier = Modifier.size(16.dp), - tint = MaterialTheme.colorScheme.primary, - ) - } - } + ) + + Text( + text = weekBadgeLabel(selectedWeek), + style = MaterialTheme.typography.labelMedium, + color = MaterialTheme.colorScheme.primary, + fontWeight = FontWeight.Bold, + ) } } } @@ -611,6 +623,73 @@ private fun WeekArrowButton( } } +@Composable +private fun TimetableDaySelector( + today: LocalDate, + weekOffset: Int, + selectedDayOrder: Int, + onDaySelected: (Int) -> Unit, +) { + val monday = mondayOfWeek(today, weekOffset) + + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(4.dp), + ) { + (1..5).forEach { dayIndex -> + val date = LocalDate.fromEpochDays(monday.toEpochDays() + dayIndex - 1) + val selected = selectedDayOrder == dayIndex + + Surface( + modifier = + Modifier + .weight(1f) + .height(53.dp) + .clickable { + onDaySelected(dayIndex) + }, + shape = RoundedCornerShape(12.dp), + color = + if (selected) { + MaterialTheme.colorScheme.primary + } else { + MaterialTheme.colorScheme.surface + }, + shadowElevation = if (selected) 3.dp else 0.dp, + ) { + Column( + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center, + ) { + Text( + text = shortDayName(dayIndex), + style = MaterialTheme.typography.bodySmall, + color = + if (selected) { + Color.White + } else { + MaterialTheme.colorScheme.onSurface.copy(alpha = 0.75f) + }, + fontWeight = if (selected) FontWeight.Bold else FontWeight.Medium, + ) + + Text( + text = date.day.toString(), + style = MaterialTheme.typography.titleSmall, + color = + if (selected) { + Color.White + } else { + MaterialTheme.colorScheme.onSurface + }, + fontWeight = if (selected) FontWeight.ExtraBold else FontWeight.Bold, + ) + } + } + } + } +} + @Composable private fun FigmaLessonCard( lesson: LessonDto, @@ -1022,18 +1101,57 @@ private fun ErrorBlock( } } +private fun mondayOfWeek( + date: LocalDate, + weekOffset: Int, +): LocalDate { + val daysSinceMonday = + when (date.dayOfWeek) { + DayOfWeek.MONDAY -> 0 + DayOfWeek.TUESDAY -> 1 + DayOfWeek.WEDNESDAY -> 2 + DayOfWeek.THURSDAY -> 3 + DayOfWeek.FRIDAY -> 4 + DayOfWeek.SATURDAY -> 5 + DayOfWeek.SUNDAY -> 6 + } + + return LocalDate.fromEpochDays( + date.toEpochDays() - daysSinceMonday + weekOffset * 7, + ) +} + +private fun defaultSchoolDay(day: DayOfWeek): Int = + when (day) { + DayOfWeek.MONDAY -> 1 + DayOfWeek.TUESDAY -> 2 + DayOfWeek.WEDNESDAY -> 3 + DayOfWeek.THURSDAY -> 4 + DayOfWeek.FRIDAY -> 5 + DayOfWeek.SATURDAY, DayOfWeek.SUNDAY -> 1 + } + +private fun shortDayName(dayOrder: Int): String = + when (dayOrder) { + 1 -> tr("Hét", "Mon") + 2 -> tr("Ked", "Tue") + 3 -> tr("Sze", "Wed") + 4 -> tr("Csü", "Thu") + 5 -> tr("Pén", "Fri") + else -> "" + } + private fun formatWeekRange( monday: LocalDate, friday: LocalDate, ): String { val startMonth = monthName(monday.monthNumber) val endMonth = monthName(friday.monthNumber) - return when { - monday.year != friday.year -> - "${monday.year}. $startMonth ${monday.day}. – ${friday.year}. $endMonth ${friday.day}." - monday.monthNumber != friday.monthNumber -> - "${monday.year}. $startMonth ${monday.day}. – $endMonth ${friday.day}." - else -> "${monday.year}. $startMonth ${monday.day}–${friday.day}." + + return if (monday.monthNumber == friday.monthNumber) { + "${monday.year}. $startMonth ${monday.day} – ${friday.day}." + } else { + "${monday.year}. $startMonth ${monday.day} – $endMonth ${friday.day}." } } @@ -1055,10 +1173,17 @@ private fun monthName(month: Int): String = } private fun approximateTeachingWeek(monday: LocalDate): Int { - val friday = LocalDate.fromEpochDays(monday.toEpochDays() + 4) - val schoolYear = if (friday.monthNumber >= 9) friday.year else friday.year - 1 - val startMonday = schoolWeekMonday(LocalDate(schoolYear, 9, 1), 0) - return ((monday.toEpochDays() - startMonday.toEpochDays()) / 7L + 1L).toInt() + val schoolYear = + if (monday.monthNumber >= 9) { + monday.year + } else { + monday.year - 1 + } + + val schoolStart = LocalDate(schoolYear, 9, 1) + val elapsedDays = monday.toEpochDays() - schoolStart.toEpochDays() + + return (((elapsedDays.coerceAtLeast(0L)) / 7L) + 1L).toInt() } private fun weekBadgeLabel(week: WeekDefinitionDto?): String { @@ -1073,10 +1198,10 @@ private fun weekBadgeLabel(week: WeekDefinitionDto?): String { return tr("AKTUÁLIS", "CURRENT") } - return if (raw.contains("hét", ignoreCase = true) || raw.contains("week", ignoreCase = true)) { + return if (raw.contains("hét", ignoreCase = true)) { raw.uppercase() } else { - tr("${raw.uppercase()} HÉT", "WEEK ${raw.uppercase()}") + "${raw.uppercase()} HÉT" } } diff --git a/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/components/SchoolWeekDaySelectorTest.kt b/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/components/SchoolWeekDaySelectorTest.kt deleted file mode 100644 index e5cde2c..0000000 --- a/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/components/SchoolWeekDaySelectorTest.kt +++ /dev/null @@ -1,50 +0,0 @@ -package hu.petrik.filcapp.components - -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith -import kotlinx.datetime.DayOfWeek -import kotlinx.datetime.LocalDate - -class SchoolWeekDaySelectorTest { - private val thursday = LocalDate(2026, 11, 19) - - @Test - fun selectedDayResolvesToExactCalendarDate() { - assertEquals(LocalDate(2026, 11, 16), schoolDateFor(thursday, 0, 1)) - assertEquals(thursday, schoolDateFor(thursday, 0, 4)) - assertEquals(LocalDate(2026, 11, 20), schoolDateFor(thursday, 0, 5)) - } - - @Test - fun weekNavigationAdjustsTheSelectedDate() { - assertEquals(LocalDate(2026, 11, 9), schoolDateFor(thursday, -1, 1)) - assertEquals(LocalDate(2026, 11, 27), schoolDateFor(thursday, 1, 5)) - } - - @Test - fun shortMonthsAndLeapYearsHaveValidDates() { - assertEquals(LocalDate(2027, 2, 22), schoolDateFor(LocalDate(2027, 2, 28), 0, 1)) - assertEquals(LocalDate(2028, 2, 28), schoolDateFor(LocalDate(2028, 2, 29), 0, 1)) - assertEquals(LocalDate(2028, 3, 3), schoolDateFor(LocalDate(2028, 2, 29), 0, 5)) - } - - @Test - fun weekCalculationCrossesYearsCorrectly() { - assertEquals(LocalDate(2026, 12, 28), schoolWeekMonday(LocalDate(2027, 1, 1), 0)) - assertEquals(LocalDate(2027, 1, 1), schoolDateFor(LocalDate(2027, 1, 1), 0, 5)) - } - - @Test - fun weekendsDefaultToMondayAsInTimetable() { - assertEquals(1, schoolDefaultDay(DayOfWeek.SATURDAY)) - assertEquals(1, schoolDefaultDay(DayOfWeek.SUNDAY)) - assertEquals(4, schoolDefaultDay(DayOfWeek.THURSDAY)) - } - - @Test - fun onlySchoolDaysCanBeSelected() { - assertFailsWith { schoolDateFor(thursday, 0, 0) } - assertFailsWith { schoolDateFor(thursday, 0, 6) } - } -} diff --git a/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/data/LessonWeekFilterTest.kt b/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/data/LessonWeekFilterTest.kt deleted file mode 100644 index af9bda0..0000000 --- a/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/data/LessonWeekFilterTest.kt +++ /dev/null @@ -1,46 +0,0 @@ -package hu.petrik.filcapp.data - -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlinx.datetime.LocalDate - -class LessonWeekFilterTest { - private val both = WeekDefinitionDto(id = "all", short = "Mind", name = "Mind hét", weeks = listOf("11")) - private val a = WeekDefinitionDto(id = "a", short = "A", weeks = listOf("10")) - private val b = WeekDefinitionDto(id = "b", short = "B", weeks = listOf("01")) - private val entries = listOf( - LessonDto(id = "a", weekDefinition = a), - LessonDto(id = "b", weekDefinition = b), - LessonDto(id = "both", weekDefinition = both), - LessonDto(id = "null", weekDefinition = null), - ) - - @Test - fun aWeekIncludesBothWeekLessons() { - assertEquals(listOf("a", "both", "null"), filterLessonsForSchoolWeek(entries, LessonWeekType.A).map { it.id }) - } - - @Test - fun bWeekIncludesBothWeekLessons() { - assertEquals(listOf("b", "both", "null"), filterLessonsForSchoolWeek(entries, LessonWeekType.B).map { it.id }) - } - - @Test - fun fallbackClassificationMatchesFilcWebClient() { - assertEquals(LessonWeekType.A, lessonWeekType(WeekDefinitionDto(weeks = listOf("10")))) - assertEquals(LessonWeekType.B, lessonWeekType(WeekDefinitionDto(weeks = listOf("01")))) - assertEquals(LessonWeekType.A, lessonWeekType(WeekDefinitionDto(name = "A hét"))) - assertEquals(LessonWeekType.B, lessonWeekType(WeekDefinitionDto(name = "week b"))) - assertEquals(LessonWeekType.ALL, lessonWeekType(WeekDefinitionDto(weeks = listOf("11")))) - assertEquals(LessonWeekType.ALL, lessonWeekType(null)) - } - - @Test - fun alternatingWeeksAndYearBoundaries() { - assertEquals(LessonWeekType.A, schoolWeekTypeFor(LocalDate(2026, 8, 31))) - assertEquals(LessonWeekType.B, schoolWeekTypeFor(LocalDate(2026, 9, 7))) - assertEquals(LessonWeekType.A, schoolWeekTypeFor(LocalDate(2026, 9, 7), reversed = true)) - assertEquals(LessonWeekType.B, schoolWeekTypeFor(LocalDate(2026, 12, 28))) - assertEquals(LessonWeekType.A, schoolWeekTypeFor(LocalDate(2027, 1, 4))) - } -}