diff --git a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/notifications/NotificationDisplay.kt b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/notifications/NotificationDisplay.kt new file mode 100644 index 0000000..d508f97 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/notifications/NotificationDisplay.kt @@ -0,0 +1,81 @@ +package hu.petrik.filcapp.notifications + +import hu.petrik.filcapp.settings.AppLanguage + +/** Localized text for the inbox, independent of how Chronos stored the notification. */ +internal data class NotificationDisplay( + val title: String, + val content: String, +) + +private val teacherUuid = + Regex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}") + +private val hungarianDate = + Regex( + "(Hétfő|Kedd|Szerda|Csütörtök|Péntek|Szombat|Vasárnap),\\s*(\\d{4})\\.\\s*(\\d{1,2})\\.\\s*(\\d{1,2})\\.,?", + RegexOption.IGNORE_CASE, + ) + +private val announcementPrefix = + Regex("^(?:New announcement|Új bejelentés|Új közlemény):\\s*", RegexOption.IGNORE_CASE) + +private val systemMessagePrefix = + Regex("^(?:System notice|Rendszerüzenet):\\s*", RegexOption.IGNORE_CASE) + +internal fun notificationNeedsTeacherNames(entry: NotificationEntry): Boolean = + (entry.type == "substitution" || entry.type == "substitution_teacher") && teacherUuid.containsMatchIn(entry.content) + +internal fun displayNotification( + entry: NotificationEntry, + teacherNames: Map, + language: AppLanguage, +): NotificationDisplay { + val hu = language == AppLanguage.HU + val title = + when (entry.type) { + "announcement" -> if (hu) "Új közlemény" else "New announcement" + "substitution" -> if (hu) "Új helyettesítés" else "New substitution" + "system_message" -> if (hu) "Rendszerüzenet" else "System message" + "blog_post" -> if (hu) "Új hír" else "New post" + "moved_lesson" -> if (hu) "Áthelyezett óra" else "Moved lesson" + else -> entry.title + } + val content = + when (entry.type) { + "announcement" -> announcementPrefix.replace(entry.content, "") + "system_message" -> systemMessagePrefix.replace(entry.content, "") + "substitution", "substitution_teacher" -> + readableSubstitutionContent(entry.content, teacherNames, hu) + else -> entry.content + } + return NotificationDisplay(title = title, content = content) +} + +private fun readableSubstitutionContent( + content: String, + teacherNames: Map, + hu: Boolean, +): String { + val withNames = + teacherUuid.replace(content) { match -> + teacherNames[match.value.lowercase()]?.takeIf { it.isNotBlank() } + ?: if (hu) "Helyettesítő tanár" else "Substitute teacher" + } + return hungarianDate.replace(withNames) { match -> + val weekday = + when (match.groupValues[1].lowercase()) { + "hétfő" -> "hétfőn" + "kedd" -> "kedden" + "szerda" -> "szerdán" + "csütörtök" -> "csütörtökön" + "péntek" -> "pénteken" + "szombat" -> "szombaton" + else -> "vasárnap" + } + val year = match.groupValues[2] + val month = match.groupValues[3].padStart(2, '0') + val day = match.groupValues[4].padStart(2, '0') + "$weekday ($year. $month. $day.)," + } +} diff --git a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/notifications/NotificationScreens.kt b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/notifications/NotificationScreens.kt index ec9cb8b..63345cd 100644 --- a/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/notifications/NotificationScreens.kt +++ b/composeApp/src/commonMain/kotlin/hu/petrik/filcapp/notifications/NotificationScreens.kt @@ -28,6 +28,8 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import hu.petrik.filcapp.auth.AuthState +import hu.petrik.filcapp.data.ChronosApi +import hu.petrik.filcapp.settings.AppSettings import hu.petrik.filcapp.settings.tr import kotlinx.coroutines.CancellationException import kotlinx.coroutines.launch @@ -41,17 +43,36 @@ fun NotificationInboxScreen( val refresh = NotificationEvents.version val scope = rememberCoroutineScope() var items by remember(userId) { mutableStateOf>(emptyList()) } + var teacherNames by remember(userId) { mutableStateOf>(emptyMap()) } var busy by remember(userId) { mutableStateOf(true) } var error by remember(userId) { mutableStateOf(null) } LaunchedEffect(userId, refresh) { if (userId == null) { + busy = false return@LaunchedEffect } busy = true + teacherNames = emptyMap() try { - items = NotificationRepository.list() + val loaded = NotificationRepository.list() + items = loaded error = null + // Render the inbox before the optional teacher-name lookup finishes. + busy = false + if (loaded.any(::notificationNeedsTeacherNames)) { + try { + teacherNames = + ChronosApi.getTeachers().associate { teacher -> + teacher.id.lowercase() to teacher.displayName + } + } catch (cancelled: CancellationException) { + throw cancelled + } catch (_: Exception) { + // The inbox still works if the directory is unavailable; IDs get a generic label. + teacherNames = emptyMap() + } + } } catch (cancelled: CancellationException) { throw cancelled } catch (exception: Exception) { @@ -97,6 +118,7 @@ fun NotificationInboxScreen( Text(tr("Nincs értesítés.", "No notifications yet.")) } else { items.forEach { entry -> + val display = displayNotification(entry, teacherNames, AppSettings.language.value) Surface( modifier = Modifier.fillMaxWidth().clickable { @@ -127,9 +149,9 @@ fun NotificationInboxScreen( modifier = Modifier.fillMaxWidth().padding(14.dp), verticalArrangement = Arrangement.spacedBy(6.dp), ) { - Text(entry.title, style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold) - if (entry.content.isNotBlank()) { - Text(entry.content, style = MaterialTheme.typography.bodyMedium) + Text(display.title, style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold) + if (display.content.isNotBlank()) { + Text(display.content, style = MaterialTheme.typography.bodyMedium) } Text(entry.createdAt.take(16).replace("T", " "), style = MaterialTheme.typography.labelSmall) } diff --git a/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/notifications/NotificationDisplayTest.kt b/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/notifications/NotificationDisplayTest.kt new file mode 100644 index 0000000..d1c66d4 --- /dev/null +++ b/composeApp/src/commonTest/kotlin/hu/petrik/filcapp/notifications/NotificationDisplayTest.kt @@ -0,0 +1,75 @@ +package hu.petrik.filcapp.notifications + +import hu.petrik.filcapp.settings.AppLanguage +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class NotificationDisplayTest { + private val teacherId = "04e6606c-618e-4cfa-a8e8-25d4d27d7183" + + private fun entry( + type: String, + title: String, + content: String, + ) = NotificationEntry("1", title, content, type, "2026-11-16T08:57:00", false) + + @Test + fun resolvesTeacherUuidAndFormatsHungarianDate() { + val item = + entry( + "substitution", + "Új helyettesítés", + "$teacherId helyettesít Szerda, 2026. 11. 18., 1 órádat tartja meg.", + ) + val rendered = displayNotification(item, mapOf(teacherId to "Kovács Péter"), AppLanguage.HU) + assertTrue(notificationNeedsTeacherNames(item)) + assertEquals("Új helyettesítés", rendered.title) + assertEquals( + "Kovács Péter helyettesít szerdán (2026. 11. 18.), 1 órádat tartja meg.", + rendered.content, + ) + } + + @Test + fun neverShowsUnknownTeacherUuid() { + val item = entry("substitution", "Új helyettesítés", "$teacherId helyettesít holnap.") + assertEquals( + "Helyettesítő tanár helyettesít holnap.", + displayNotification(item, emptyMap(), AppLanguage.HU).content, + ) + assertEquals( + "Substitute teacher helyettesít holnap.", + displayNotification(item, emptyMap(), AppLanguage.EN).content, + ) + } + + @Test + fun translatesEnglishAnnouncementHeaderAndRemovesDuplicatePrefix() { + val item = + entry( + "announcement", + "New Announcement", + "New announcement: Kémia OKTV 14:00 Petrik Lajos Előadó", + ) + assertEquals( + NotificationDisplay("Új közlemény", "Kémia OKTV 14:00 Petrik Lajos Előadó"), + displayNotification(item, emptyMap(), AppLanguage.HU), + ) + assertEquals( + NotificationDisplay("New announcement", "Kémia OKTV 14:00 Petrik Lajos Előadó"), + displayNotification(item, emptyMap(), AppLanguage.EN), + ) + assertFalse(notificationNeedsTeacherNames(item)) + } + + @Test + fun leavesUnrelatedNotificationContentUntouched() { + val item = entry("test", "Custom title", "Reference: $teacherId") + assertEquals( + NotificationDisplay("Custom title", "Reference: $teacherId"), + displayNotification(item, emptyMap(), AppLanguage.HU), + ) + } +}