From e531c5b83f80369f0a07554860a08dde64565888 Mon Sep 17 00:00:00 2001 From: StarProxima Date: Wed, 3 May 2023 22:24:41 +0300 Subject: [PATCH] LessonCardInfoPanelTimeToEnd --- .../current_lesson_time_to_end_provider.dart | 2 +- ...time_to_start_progress_value_provider.dart | 4 +- .../next_lesson_time_to_start_provider.dart | 6 +- .../features/lesson_card/ui/lesson_card.dart | 4 +- .../info_panel/lesson_card_info_panel.dart | 44 +++++--------- .../lesson_card_info_panel_time_to_end.dart | 59 +++++++++++++++++++ .../ui/widgets/lesson_card_time_left.dart | 25 -------- .../ui/widgets/lesson_card_time_to_start.dart | 11 +++- .../managers/timetable_lessons_manager.dart | 11 +++- .../state_holders/lessons/current_lesson.dart | 5 +- .../state_holders/lessons/last_lesson.dart | 2 +- .../state_holders/lessons/next_lesson.dart | 2 +- 12 files changed, 104 insertions(+), 71 deletions(-) create mode 100644 lib/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel_time_to_end.dart delete mode 100644 lib/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_left.dart diff --git a/lib/features/timetable_page/features/lesson_card/providers/current_lesson_time_to_end_provider.dart b/lib/features/timetable_page/features/lesson_card/providers/current_lesson_time_to_end_provider.dart index 787fec1..face12c 100644 --- a/lib/features/timetable_page/features/lesson_card/providers/current_lesson_time_to_end_provider.dart +++ b/lib/features/timetable_page/features/lesson_card/providers/current_lesson_time_to_end_provider.dart @@ -6,7 +6,7 @@ import 'package:cube_system/features/timetable_page/state_holders/lessons/curren final currentLessonTimeToEndProvider = Provider((ref) { final currentDateTime = ref.watch(currentDateTimeQuick); - final activeLesson = ref.watch(currentLesson); + final activeLesson = ref.watch(currentLessonStateHolder); if (activeLesson == null) return null; diff --git a/lib/features/timetable_page/features/lesson_card/providers/next_lesson_time_to_start_progress_value_provider.dart b/lib/features/timetable_page/features/lesson_card/providers/next_lesson_time_to_start_progress_value_provider.dart index 61dff2e..cc22aaf 100644 --- a/lib/features/timetable_page/features/lesson_card/providers/next_lesson_time_to_start_progress_value_provider.dart +++ b/lib/features/timetable_page/features/lesson_card/providers/next_lesson_time_to_start_progress_value_provider.dart @@ -17,8 +17,8 @@ final nextLessonTimeToStartProgressValueProvider = Provider((ref) { final timeLeft = ref.read(nextLessonTimeToStartProvider)?.duration; if (timeLeft == null) return 1; - final lessonNext = ref.watch(nextLesson); - final lessonLast = ref.watch(lastLesson); + final lessonNext = ref.watch(nextLessonStateHolder); + final lessonLast = ref.watch(lastLessonStateHolder); if (lessonNext == null || lessonLast == null) return 1; diff --git a/lib/features/timetable_page/features/lesson_card/providers/next_lesson_time_to_start_provider.dart b/lib/features/timetable_page/features/lesson_card/providers/next_lesson_time_to_start_provider.dart index 9c8592d..90adaac 100644 --- a/lib/features/timetable_page/features/lesson_card/providers/next_lesson_time_to_start_provider.dart +++ b/lib/features/timetable_page/features/lesson_card/providers/next_lesson_time_to_start_provider.dart @@ -16,13 +16,13 @@ final nextLessonTimeToStartProvider = Provider((ref) { if (condition.isNever) return null; if (condition.isOnlyRecessBetweenLessons) { - final last = ref.watch(lastLesson); - final next = ref.watch(nextLesson); + final last = ref.watch(lastLessonStateHolder); + final next = ref.watch(nextLessonStateHolder); if (last == null || next == null) return null; if (next.number - last.number != 1) return null; } - final next = ref.watch(nextLesson); + final next = ref.watch(nextLessonStateHolder); if (next == null) return null; diff --git a/lib/features/timetable_page/features/lesson_card/ui/lesson_card.dart b/lib/features/timetable_page/features/lesson_card/ui/lesson_card.dart index 17b730d..c4da686 100644 --- a/lib/features/timetable_page/features/lesson_card/ui/lesson_card.dart +++ b/lib/features/timetable_page/features/lesson_card/ui/lesson_card.dart @@ -24,14 +24,16 @@ import 'package:cube_system/features/settings/state_holders/app_lesson_colors.da import 'package:cube_system/features/timetable_page/state_holders/lessons/next_lesson.dart'; +import 'package:cube_system/features/timetable_page/managers/timetable_lessons_manager.dart'; + part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_body.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_footer.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_header.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel_icon.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel_chip.dart'; +part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel_time_to_end.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_indicator.dart'; -part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_left.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/next_lesson_time_to_start_progress_bar.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_lesson_type_chip.dart'; part 'package:cube_system/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_to_start.dart'; diff --git a/lib/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel.dart b/lib/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel.dart index 92f03d7..3b89ef1 100644 --- a/lib/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel.dart +++ b/lib/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel.dart @@ -7,17 +7,22 @@ class LessonCardInfoPanel extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - final isRemotely = - ref.watch(_lessonInLessonCard.select((value) => value.isRemotely)); + final lesson = ref.watch(_lessonInLessonCard); - final isCollision = - ref.watch(_lessonInLessonCard.select((value) => value.isCollision)); + final isRemotely = lesson.isRemotely; - final isCancelled = - ref.watch(_lessonInLessonCard.select((value) => value.isCancelled)); + final isCollision = lesson.isCollision; - final isActiveLessons = - ref.watch(_lessonInLessonCard) == ref.watch(currentLesson); + final isCancelled = lesson.isCancelled; + + final currentLesson = ref.watch(currentLessonStateHolder); + + final equelNumber = lesson.number == currentLesson?.number; + + final equelStartDateTime = lesson.dateTimings.startDateTime == + currentLesson?.dateTimings.startDateTime; + + final isActiveLessons = equelNumber && equelStartDateTime; // final isCollision = _random.nextInt(100) < 25; @@ -43,28 +48,7 @@ class LessonCardInfoPanel extends ConsumerWidget { LessonCardInfoPanelIcon( icon: Text('$numberOfNotes'), ), - if (isActiveLessons) - Consumer( - builder: (context, ref, _) { - ref.watch( - currentLessonTimeToEndProvider - .select((value) => value?.format().length), - ); - - final hasHours = - (ref.read(currentLessonTimeToEndProvider)?.hours ?? 0) > 0; - - return hasHours - ? const LessonCardInfoPanelChip( - minWidth: 66, - text: LessonCardTimeLeft(), - ) - : const LessonCardInfoPanelChip( - minWidth: 46, - text: LessonCardTimeLeft(), - ); - }, - ), + const LessonCardInfoPanelTimeToEnd(), if (isCollision) const LessonCardInfoPanelChip( text: Text('Коллизия'), diff --git a/lib/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel_time_to_end.dart b/lib/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel_time_to_end.dart new file mode 100644 index 0000000..6c29248 --- /dev/null +++ b/lib/features/timetable_page/features/lesson_card/ui/widgets/info_panel/lesson_card_info_panel_time_to_end.dart @@ -0,0 +1,59 @@ +part of 'package:cube_system/features/timetable_page/features/lesson_card/ui/lesson_card.dart'; + +class LessonCardInfoPanelTimeToEnd extends ConsumerWidget { + const LessonCardInfoPanelTimeToEnd({super.key}); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final lesson = ref.watch(_lessonInLessonCard); + + final currentLesson = ref.watch(currentLessonStateHolder); + + final manager = ref.read(timetableLessonsManager); + + final isActiveLessons = manager.isEquelLessons(lesson, currentLesson); + + if (!isActiveLessons) return const SizedBox(); + + return Consumer( + builder: (context, ref, _) { + ref.watch( + currentLessonTimeToEndProvider + .select((value) => value?.format().length), + ); + + final hasHours = + (ref.read(currentLessonTimeToEndProvider)?.hours ?? 0) > 0; + + return hasHours + ? const LessonCardInfoPanelChip( + minWidth: 66, + text: _LessonCardInfoPanelTimeToEndText(), + ) + : const LessonCardInfoPanelChip( + minWidth: 46, + text: _LessonCardInfoPanelTimeToEndText(), + ); + }, + ); + } +} + +class _LessonCardInfoPanelTimeToEndText extends ConsumerWidget { + const _LessonCardInfoPanelTimeToEndText({super.key}); + + @override + Widget build(BuildContext context, WidgetRef ref) { + return Consumer( + builder: (context, ref, _) { + final timeToEnd = ref.watch(currentLessonTimeToEndProvider); + final timeToEndStr = timeToEnd?.format() ?? '00:00'; + return Text( + timeToEndStr, + style: context.textStyles.chipLabel + .copyWith(color: context.colors.white), + ); + }, + ); + } +} diff --git a/lib/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_left.dart b/lib/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_left.dart deleted file mode 100644 index fef6a9e..0000000 --- a/lib/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_left.dart +++ /dev/null @@ -1,25 +0,0 @@ -part of 'package:cube_system/features/timetable_page/features/lesson_card/ui/lesson_card.dart'; - -class LessonCardTimeLeft extends ConsumerWidget { - const LessonCardTimeLeft({super.key}); - - @override - Widget build(BuildContext context, WidgetRef ref) { - final isActiveLessons = - ref.watch(_lessonInLessonCard) == ref.watch(currentLesson); - - if (!isActiveLessons) return const SizedBox(); - - return Consumer( - builder: (context, ref, _) { - final timeToEnd = ref.watch(currentLessonTimeToEndProvider); - final timeToEndStr = timeToEnd?.format() ?? '00:00'; - return Text( - timeToEndStr, - style: context.textStyles.chipLabel - .copyWith(color: context.colors.white), - ); - }, - ); - } -} diff --git a/lib/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_to_start.dart b/lib/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_to_start.dart index 8d43e3c..df20f74 100644 --- a/lib/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_to_start.dart +++ b/lib/features/timetable_page/features/lesson_card/ui/widgets/lesson_card_time_to_start.dart @@ -15,10 +15,15 @@ class LessonCardTimeToStart extends ConsumerWidget { nextLessonTimeToStartProvider.select((value) => value == null), ); - final lessonNext = ref.watch(nextLesson); - final lessonCurrent = ref.watch(currentLesson); + final nextLesson = ref.watch(nextLessonStateHolder); - if (dontShow || lesson != lessonNext || lessonCurrent != null) { + final currentLesson = ref.watch(currentLessonStateHolder); + + final manager = ref.read(timetableLessonsManager); + + final isActiveLessons = manager.isEquelLessons(lesson, nextLesson); + + if (dontShow || !isActiveLessons || currentLesson != null) { return const SizedBox(); } diff --git a/lib/features/timetable_page/managers/timetable_lessons_manager.dart b/lib/features/timetable_page/managers/timetable_lessons_manager.dart index 5df9dd3..41ebd68 100644 --- a/lib/features/timetable_page/managers/timetable_lessons_manager.dart +++ b/lib/features/timetable_page/managers/timetable_lessons_manager.dart @@ -31,9 +31,9 @@ final timetableLessonsManager = Provider((ref) { events: ref.watch(timetablePageEvents.notifier), currentDateTime: ref.watch(currentDateTimeQuick.notifier), selectedDate: ref.watch(selectedDate.notifier), - currentLesson: ref.watch(currentLesson.notifier), - nextLesson: ref.watch(nextLesson.notifier), - lastLesson: ref.watch(lastLesson.notifier), + currentLesson: ref.watch(currentLessonStateHolder.notifier), + nextLesson: ref.watch(nextLessonStateHolder.notifier), + lastLesson: ref.watch(lastLessonStateHolder.notifier), ); }); @@ -215,4 +215,9 @@ class TimetableLessonsManager { } } } + + bool isEquelLessons(Lesson? one, Lesson? two) { + return one?.dateTimings.startDateTime == two?.dateTimings.startDateTime && + one?.number == two?.number; + } } diff --git a/lib/features/timetable_page/state_holders/lessons/current_lesson.dart b/lib/features/timetable_page/state_holders/lessons/current_lesson.dart index 6a6c0da..8541336 100644 --- a/lib/features/timetable_page/state_holders/lessons/current_lesson.dart +++ b/lib/features/timetable_page/state_holders/lessons/current_lesson.dart @@ -1,6 +1,9 @@ import 'package:cube_system/models/lesson/lesson.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -final currentLesson = StateProvider((ref) { +// TODO: Объеденить current, next, last в один stateHolder +// TODO: Возможно, стоит хранить не Lesson, a LessonDateTimings и LessonTimings + +final currentLessonStateHolder = StateProvider((ref) { return null; }); diff --git a/lib/features/timetable_page/state_holders/lessons/last_lesson.dart b/lib/features/timetable_page/state_holders/lessons/last_lesson.dart index e5cc722..e2cc33f 100644 --- a/lib/features/timetable_page/state_holders/lessons/last_lesson.dart +++ b/lib/features/timetable_page/state_holders/lessons/last_lesson.dart @@ -2,6 +2,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:cube_system/models/lesson/lesson.dart'; -final lastLesson = StateProvider((ref) { +final lastLessonStateHolder = StateProvider((ref) { return null; }); diff --git a/lib/features/timetable_page/state_holders/lessons/next_lesson.dart b/lib/features/timetable_page/state_holders/lessons/next_lesson.dart index 6868f4a..9497853 100644 --- a/lib/features/timetable_page/state_holders/lessons/next_lesson.dart +++ b/lib/features/timetable_page/state_holders/lessons/next_lesson.dart @@ -2,6 +2,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:cube_system/models/lesson/lesson.dart'; -final nextLesson = StateProvider((ref) { +final nextLessonStateHolder = StateProvider((ref) { return null; });