From 9183769ff48318373f466b1141db1f56e6319ba8 Mon Sep 17 00:00:00 2001 From: Armand <4831c0@proton.me> Date: Sun, 7 Sep 2025 12:03:40 +0200 Subject: [PATCH] tt: jump to current day closes #42 --- .../ui/phone/pages/home/home_timetable.dart | 117 ++++++++++-------- 1 file changed, 64 insertions(+), 53 deletions(-) diff --git a/firka/lib/ui/phone/pages/home/home_timetable.dart b/firka/lib/ui/phone/pages/home/home_timetable.dart index 69e47b8a..2e8231ad 100644 --- a/firka/lib/ui/phone/pages/home/home_timetable.dart +++ b/firka/lib/ui/phone/pages/home/home_timetable.dart @@ -43,6 +43,43 @@ class _HomeTimetableScreen extends State { _HomeTimetableScreen(); + void setActiveToToday() { + final todayMid = now!.getMidnight(); + int idx = dates!.indexWhere((d) => + d.year == todayMid.year && + d.month == todayMid.month && + d.day == todayMid.day); + + if (idx >= 0) { + final todaysLessons = lessons?.where((lesson) => + lesson.start.isAfter(todayMid) && + lesson.end.isBefore(todayMid.add(Duration(hours: 23, minutes: 59)))); + + if (todaysLessons != null && todaysLessons.isNotEmpty) { + final lastLessonToday = + todaysLessons.reduce((a, b) => a.end.isAfter(b.end) ? a : b); + + if (now!.isAfter(lastLessonToday.end)) { + int nextIdx = idx + 1; + if (nextIdx < dates!.length) { + active = nextIdx; + } else { + active = idx; + } + } else { + active = idx; + } + } else { + active = idx; + } + } else if (now!.isAfter(dates!.last)) { + active = dates!.length - 1; + } else { + idx = dates!.indexWhere((d) => d.isAfter(todayMid)); + active = idx >= 0 ? idx : 0; + } + } + Future initForWeek(DateTime now, {bool forceCache = true}) async { var monday = now.getMonday().getMidnight(); var sunday = monday.add(Duration(days: 6)); @@ -80,41 +117,8 @@ class _HomeTimetableScreen extends State { if (!mounted) return; setState(() { this.dates = dates; - final todayMid = now.getMidnight(); - int idx = dates.indexWhere((d) => - d.year == todayMid.year && - d.month == todayMid.month && - d.day == todayMid.day); - if (idx >= 0) { - final todaysLessons = lessons?.where((lesson) => - lesson.start.isAfter(todayMid) && - lesson.end - .isBefore(todayMid.add(Duration(hours: 23, minutes: 59)))); - - if (todaysLessons != null && todaysLessons.isNotEmpty) { - final lastLessonToday = - todaysLessons.reduce((a, b) => a.end.isAfter(b.end) ? a : b); - - if (now.isAfter(lastLessonToday.end)) { - int nextIdx = idx + 1; - if (nextIdx < dates.length) { - active = nextIdx; - } else { - active = idx; - } - } else { - active = idx; - } - } else { - active = idx; - } - } else if (now.isAfter(dates.last)) { - active = dates.length - 1; - } else { - idx = dates.indexWhere((d) => d.isAfter(todayMid)); - active = idx >= 0 ? idx : 0; - } + setActiveToToday(); }); } @@ -304,25 +308,32 @@ class _HomeTimetableScreen extends State { }); }, ), - Row( - children: [ - Text( - now!.format( - widget.data.l10n, FormatMode.yyyymmddwedd), - style: appStyle.fonts.B_14R - .apply(color: appStyle.colors.textPrimary)), - SizedBox(width: 4), - Text("•", - style: appStyle.fonts.B_16R - .apply(color: appStyle.colors.accent)), - SizedBox(width: 4), - Text( - now!.isAWeek() - ? widget.data.l10n.a_week - : widget.data.l10n.b_week, - style: appStyle.fonts.B_14R - .apply(color: appStyle.colors.textPrimary)), - ], + GestureDetector( + child: Row( + children: [ + Text( + now!.format(widget.data.l10n, + FormatMode.yyyymmddwedd), + style: appStyle.fonts.B_14R.apply( + color: appStyle.colors.textPrimary)), + SizedBox(width: 4), + Text("•", + style: appStyle.fonts.B_16R + .apply(color: appStyle.colors.accent)), + SizedBox(width: 4), + Text( + now!.isAWeek() + ? widget.data.l10n.a_week + : widget.data.l10n.b_week, + style: appStyle.fonts.B_14R.apply( + color: appStyle.colors.textPrimary)), + ], + ), + onTap: () { + now = timeNow(); + setActiveToToday(); + _controller.jumpToPage(active); + }, ), GestureDetector( child: FirkaIconWidget(