diff --git a/firka/lib/helpers/ui/firka_card.dart b/firka/lib/helpers/ui/firka_card.dart index 5ff58393..b2583357 100644 --- a/firka/lib/helpers/ui/firka_card.dart +++ b/firka/lib/helpers/ui/firka_card.dart @@ -5,28 +5,59 @@ import '../../ui/model/style.dart'; class FirkaCard extends StatelessWidget { List left; List? right; + Widget? extra; - FirkaCard({required this.left, this.right, super.key}); + FirkaCard({required this.left, this.right, this.extra, super.key}); @override Widget build(BuildContext context) { var right = this.right ?? []; - return SizedBox( - width: MediaQuery.of(context).size.width, - child: Card( - color: appStyle.colors.card, - child: Padding( - padding: const EdgeInsets.all(16.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Row(children: left), - Row(children: right), - ], + if (extra != null) { + return SizedBox( + width: MediaQuery + .of(context) + .size + .width, + child: Card( + color: appStyle.colors.card, + child: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Row(children: left), + Row(children: right), + ], + ), + extra ?? SizedBox(), + ], + ), ), ), - ), - ); + ); + } else { + return SizedBox( + width: MediaQuery + .of(context) + .size + .width, + child: Card( + color: appStyle.colors.card, + child: Padding( + padding: const EdgeInsets.all(16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Row(children: left), + Row(children: right), + ], + ), + ), + ), + ); + } } } diff --git a/firka/lib/ui/phone/pages/home/home_main.dart b/firka/lib/ui/phone/pages/home/home_main.dart index 9a6016ec..cdcdc232 100644 --- a/firka/lib/ui/phone/pages/home/home_main.dart +++ b/firka/lib/ui/phone/pages/home/home_main.dart @@ -11,6 +11,7 @@ import '../../../../helpers/api/model/timetable.dart'; import '../../../../helpers/debug_helper.dart'; import '../../../../main.dart'; import '../../widgets/home_main_welcome.dart'; +import '../../widgets/lesson_big.dart'; class HomeMainScreen extends StatefulWidget { final AppInitialization data; @@ -69,17 +70,34 @@ class _HomeMainScreen extends State { @override Widget build(BuildContext context) { - Widget startingSoon = SizedBox(); + Widget welcomeWidget = SizedBox(); Widget nextClass = SizedBox(); - if (lessons != null && - lessons!.isNotEmpty && - now.isBefore(lessons!.first.start)) { - startingSoon = StartingSoonWidget(now, lessons!); + if (lessons != null && lessons!.isNotEmpty) { + if (now.isBefore(lessons!.first.start)) { + welcomeWidget = StartingSoonWidget(now, lessons!); + } else { + var currentLesson = lessons!.firstWhereOrNull( + (lesson) => now.isAfter(lesson.start) && now.isBefore(lesson.end)); + // "fun" fact if your clock was exactly when the class ends then isBefore + // and isAfter would fail, so to work around that we just add 1ms to the + // current time + var prevLesson = lessons!.firstWhereOrNull((lesson) => + lesson.end.isBefore(now.add(Duration(milliseconds: 1)))); + var nextLesson = lessons!.firstWhereOrNull((lesson) => + lesson.start.isAfter(now.add(Duration(milliseconds: 1)))); + int? lessonIndex; + + if (currentLesson != null) lessonIndex = currentLesson.getNo(lessons!); + + welcomeWidget = LessonBigWidget( + now, lessonIndex, currentLesson, prevLesson, nextLesson); + } } if (lessons != null && lessons!.isNotEmpty) { - var nextLessons = lessons!.where((lesson) => lesson.start.isAfter(now)); - if (nextLessons.isNotEmpty) nextClass = LessonSmallWidget(nextLessons.first); + var nextLesson = + lessons!.firstWhereOrNull((lesson) => lesson.start.isAfter(now)); + if (nextLesson != null) nextClass = LessonSmallWidget(nextLesson); } if (student != null && lessons != null) { @@ -95,7 +113,7 @@ class _HomeMainScreen extends State { children: [ WelcomeWidget(now, student!, lessons!), SizedBox(height: 48), - startingSoon, + welcomeWidget, nextClass ], ), diff --git a/firka/lib/ui/phone/widgets/lesson_big.dart b/firka/lib/ui/phone/widgets/lesson_big.dart new file mode 100644 index 00000000..bd26419e --- /dev/null +++ b/firka/lib/ui/phone/widgets/lesson_big.dart @@ -0,0 +1,225 @@ +import 'package:firka/helpers/extensions.dart'; +import 'package:firka/helpers/ui/firka_card.dart'; +import 'package:firka/l10n/app_localizations.dart'; +import 'package:firka/ui/model/style.dart'; +import 'package:firka/ui/widget/firka_icon.dart'; +import 'package:flutter/material.dart'; + +import '../../../helpers/api/model/timetable.dart'; +import '../../widget/class_icon_widget.dart'; + +class LessonBigWidget extends StatelessWidget { + final DateTime now; + final int? lessonNo; + final Lesson? lesson; + final Lesson? prevLesson; + final Lesson? nextLesson; + + const LessonBigWidget( + this.now, this.lessonNo, this.lesson, this.prevLesson, this.nextLesson, + {super.key}); + + @override + Widget build(BuildContext context) { + var hasLesson = lesson != null; + var hasPrevLesson = prevLesson != null; + var hasNextLesson = nextLesson != null; + + if (!hasLesson && (!hasPrevLesson || !hasNextLesson)) { + throw Exception( + '!hasLesson($hasLesson) && (!hasPrevLesson($hasPrevLesson) || ' + '!hasNextLesson($hasNextLesson))'); + } + + if (hasLesson) { + var timeLeft = lesson!.end.difference(now); + var duration = lesson!.end.difference(lesson!.start).inMilliseconds; + var progress = now.difference(lesson!.start).inMilliseconds; + + var minsLeft = timeLeft.inMinutes; + var secsLeft = timeLeft.inSeconds; + + var timeLeftStr = + "$minsLeft ${minsLeft == 1 ? AppLocalizations.of(context)!.starting_min : AppLocalizations.of(context)!.starting_min_plural}"; + if (minsLeft < 1) { + timeLeftStr = + "$secsLeft ${secsLeft == 1 ? AppLocalizations.of(context)!.starting_sec : AppLocalizations.of(context)!.starting_sec_plural}"; + } + + return Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + FirkaCard( + left: [ + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Card( + // TODO: improve this to match design + shadowColor: Colors.transparent, + color: appStyle.colors.a15p, + child: Padding( + padding: EdgeInsets.all(4), + child: Text(lessonNo.toString(), + style: appStyle.fonts.B_12R + .apply(color: appStyle.colors.secondary)), + ), + ), + Card( + shadowColor: Colors.transparent, + color: appStyle.colors.a15p, + child: Padding( + padding: EdgeInsets.all(4), + child: ClassIconWidget( + color: wearStyle.colors.accent, + size: 24, + uid: lesson!.uid, + className: lesson!.name, + category: lesson!.subject?.name ?? '', + ), + ), + ), + Text(lesson!.subject?.name ?? 'N/A', + style: appStyle.fonts.B_16SB + .apply(color: appStyle.colors.textPrimary)), + ], + ), + Row( + children: [ + Text(timeLeftStr, + style: appStyle.fonts.B_12R + .apply(color: appStyle.colors.textSecondary)), + ], + ), + ], + ) + ], + right: [ + Column( + children: [ + Row( + children: [ + Text( + lesson!.start + .toLocal() + .format(context, FormatMode.hmm), + style: appStyle.fonts.B_14R + .apply(color: appStyle.colors.textPrimary)), + Card( + shadowColor: Colors.transparent, + color: appStyle.colors.a15p, + child: Padding( + padding: EdgeInsets.all(4), + child: Text(lesson!.roomName ?? '?', + style: appStyle.fonts.B_12R + .apply(color: appStyle.colors.secondary)), + ), + ) + ], + ), + Row( + children: [ + SizedBox(width: 18), + Text( + lesson!.end.toLocal().format(context, FormatMode.hmm), + style: appStyle.fonts.B_12R + .apply(color: appStyle.colors.textSecondary)), + ], + ) + ], + ) + ], + extra: LinearProgressIndicator( + // TODO: Make this rounded + value: progress / duration, + backgroundColor: appStyle.colors.a15p, + color: appStyle.colors.accent, + ), + ) + ], + ); + } else { + var duration = + nextLesson!.start.difference(prevLesson!.end).inMilliseconds; + var progress = + duration - nextLesson!.start.difference(now).inMilliseconds; + var timeLeft = nextLesson!.start.difference(now); + + var timeLeftStr = + AppLocalizations.of(context)!.timeLeft(timeLeft.inMinutes + 1); + + return Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + FirkaCard( + left: [ + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Card( + shadowColor: Colors.transparent, + color: appStyle.colors.a15p, + child: Padding( + padding: EdgeInsets.all(4), + child: FirkaIconWidget( + FirkaIconType.MajesticonsLocal, 'cupFilled', + color: wearStyle.colors.accent, size: 24), + ), + ), + Text(AppLocalizations.of(context)!.breakTxt, + style: appStyle.fonts.B_16SB + .apply(color: appStyle.colors.textPrimary)), + ], + ), + Row( + children: [ + Text(timeLeftStr, + style: appStyle.fonts.B_12R + .apply(color: appStyle.colors.textSecondary)), + ], + ), + ], + ) + ], + right: [ + Column( + children: [ + Row( + children: [ + Text( + prevLesson!.end + .toLocal() + .format(context, FormatMode.hmm), + style: appStyle.fonts.B_14R + .apply(color: appStyle.colors.textPrimary)), + ], + ), + Row( + children: [ + Text( + nextLesson!.start + .toLocal() + .format(context, FormatMode.hmm), + style: appStyle.fonts.B_14R + .apply(color: appStyle.colors.textPrimary)), + ], + ) + ], + ) + ], + extra: LinearProgressIndicator( + // TODO: Make this rounded + value: progress / duration, + backgroundColor: appStyle.colors.a15p, + color: appStyle.colors.accent, + ), + ) + ], + ); + } + } +}