From 871b0ad1c3ec9fa5de5978f1ab08db39bae7cf2a Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Thu, 31 Oct 2024 01:12:07 +0000 Subject: [PATCH 1/6] feat: added mapper function to LazyConsumer --- packages/uni_app/lib/view/lazy_consumer.dart | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/uni_app/lib/view/lazy_consumer.dart b/packages/uni_app/lib/view/lazy_consumer.dart index 49e9f39b6..97a7a5283 100644 --- a/packages/uni_app/lib/view/lazy_consumer.dart +++ b/packages/uni_app/lib/view/lazy_consumer.dart @@ -28,6 +28,7 @@ class LazyConsumer, T2> required this.hasContent, required this.onNullContent, this.contentLoadingWidget, + this.mapper, super.key, }); @@ -35,6 +36,9 @@ class LazyConsumer, T2> final bool Function(T2) hasContent; final Widget onNullContent; final Widget? contentLoadingWidget; + final T2 Function(T2)? mapper; + + static T2 _defaultMapper(T2 value) => value; @override Widget build(BuildContext context) { @@ -89,8 +93,10 @@ class LazyConsumer, T2> } Widget requestDependantWidget(BuildContext context, T1 provider) { - final showContent = - provider.state != null && hasContent(provider.state as T2); + final mappedState = provider.state != null + ? (mapper ?? _defaultMapper)(provider.state as T2) + : null; + final showContent = provider.state != null && hasContent(mappedState as T2); if (provider.requestStatus == RequestStatus.busy && !showContent) { return loadingWidget(context); @@ -99,7 +105,7 @@ class LazyConsumer, T2> } return showContent - ? builder(context, provider.state as T2) + ? builder(context, mappedState) : Center( child: Padding( padding: const EdgeInsets.symmetric(vertical: 10), From 1c1c5e36915225baf7e2a233c7548ca9e0d43828 Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Thu, 31 Oct 2024 01:24:25 +0000 Subject: [PATCH 2/6] fix(schedule_card): filter past lectures in mapper --- packages/uni_app/lib/view/home/widgets/schedule_card.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/uni_app/lib/view/home/widgets/schedule_card.dart b/packages/uni_app/lib/view/home/widgets/schedule_card.dart index 411051471..2966ae641 100644 --- a/packages/uni_app/lib/view/home/widgets/schedule_card.dart +++ b/packages/uni_app/lib/view/home/widgets/schedule_card.dart @@ -49,6 +49,9 @@ class ScheduleCard extends GenericCard { ), ), contentLoadingWidget: const ScheduleCardShimmer().build(context), + mapper: (lectures) => lectures + .where((lecture) => lecture.endTime.compareTo(DateTime.now()) > 0) + .toList(), ); } From 35d63292a7d47e2699a1a016f01943a77f9ecc5e Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Thu, 31 Oct 2024 11:03:20 +0000 Subject: [PATCH 3/6] fix(schedule_card): Changed date compareTo to isAfter and removed duplicate code --- .../lib/view/home/widgets/schedule_card.dart | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/uni_app/lib/view/home/widgets/schedule_card.dart b/packages/uni_app/lib/view/home/widgets/schedule_card.dart index 2966ae641..bdfecde3c 100644 --- a/packages/uni_app/lib/view/home/widgets/schedule_card.dart +++ b/packages/uni_app/lib/view/home/widgets/schedule_card.dart @@ -10,6 +10,7 @@ import 'package:uni/model/utils/time/week.dart'; import 'package:uni/utils/navigation_items.dart'; import 'package:uni/view/common_widgets/date_rectangle.dart'; import 'package:uni/view/common_widgets/generic_card.dart'; +import 'package:uni/view/exams/exams.dart'; import 'package:uni/view/home/widgets/schedule_card_shimmer.dart'; import 'package:uni/view/lazy_consumer.dart'; import 'package:uni/view/locale_notifier.dart'; @@ -50,7 +51,11 @@ class ScheduleCard extends GenericCard { ), contentLoadingWidget: const ScheduleCardShimmer().build(context), mapper: (lectures) => lectures - .where((lecture) => lecture.endTime.compareTo(DateTime.now()) > 0) + .where( + (lecture) => + lecture.endTime.isAfter(DateTime.now()) || + lecture.startTime.weekday != DateTime.now().weekday, + ) .toList(), ); } @@ -74,16 +79,8 @@ class ScheduleCard extends GenericCard { for (final dayLectures in lecturesByDay.sublist(0, min(2, lecturesByDay.length))) { final day = dayLectures.key; - final lectures = dayLectures.value - .where( - (element) => - // Hide finished lectures from today - element.startTime.weekday != DateTime.now().weekday || - element.endTime.isAfter(DateTime.now()), - ) - .toList(); - if (lectures.isEmpty) { + if (dayLectures.value.isEmpty) { continue; } @@ -94,11 +91,11 @@ class ScheduleCard extends GenericCard { ), ); - for (final lecture in lectures) { + for (final lecture in dayLectures.value) { rows.add(createRowFromLecture(context, lecture)); } - if (lectures.length >= 2) { + if (dayLectures.value.length >= 2) { break; } } From df13b7706a2540ac2e7815183178c8c6a45a99b3 Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Thu, 31 Oct 2024 11:42:10 +0000 Subject: [PATCH 4/6] fix(lazy_consumer): updated mapper type to handle dynamic values --- packages/uni_app/lib/view/home/widgets/schedule_card.dart | 3 +-- packages/uni_app/lib/view/lazy_consumer.dart | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/uni_app/lib/view/home/widgets/schedule_card.dart b/packages/uni_app/lib/view/home/widgets/schedule_card.dart index bdfecde3c..477ed4904 100644 --- a/packages/uni_app/lib/view/home/widgets/schedule_card.dart +++ b/packages/uni_app/lib/view/home/widgets/schedule_card.dart @@ -10,7 +10,6 @@ import 'package:uni/model/utils/time/week.dart'; import 'package:uni/utils/navigation_items.dart'; import 'package:uni/view/common_widgets/date_rectangle.dart'; import 'package:uni/view/common_widgets/generic_card.dart'; -import 'package:uni/view/exams/exams.dart'; import 'package:uni/view/home/widgets/schedule_card_shimmer.dart'; import 'package:uni/view/lazy_consumer.dart'; import 'package:uni/view/locale_notifier.dart'; @@ -50,7 +49,7 @@ class ScheduleCard extends GenericCard { ), ), contentLoadingWidget: const ScheduleCardShimmer().build(context), - mapper: (lectures) => lectures + mapper: (lectures) => (lectures as List) .where( (lecture) => lecture.endTime.isAfter(DateTime.now()) || diff --git a/packages/uni_app/lib/view/lazy_consumer.dart b/packages/uni_app/lib/view/lazy_consumer.dart index 97a7a5283..11f797343 100644 --- a/packages/uni_app/lib/view/lazy_consumer.dart +++ b/packages/uni_app/lib/view/lazy_consumer.dart @@ -36,9 +36,9 @@ class LazyConsumer, T2> final bool Function(T2) hasContent; final Widget onNullContent; final Widget? contentLoadingWidget; - final T2 Function(T2)? mapper; + final T2 Function(dynamic)? mapper; - static T2 _defaultMapper(T2 value) => value; + static T2 _defaultMapper(dynamic value) => value as T2; @override Widget build(BuildContext context) { @@ -94,8 +94,9 @@ class LazyConsumer, T2> Widget requestDependantWidget(BuildContext context, T1 provider) { final mappedState = provider.state != null - ? (mapper ?? _defaultMapper)(provider.state as T2) + ? (mapper ?? _defaultMapper)(provider.state as dynamic) : null; + final showContent = provider.state != null && hasContent(mappedState as T2); if (provider.requestStatus == RequestStatus.busy && !showContent) { From 08f43e5ee1b1fd8de8d638b30ab6e9fff1874c2a Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Thu, 31 Oct 2024 11:45:25 +0000 Subject: [PATCH 5/6] fix(schedule_card): removed useless weekday check in lecture filtering --- packages/uni_app/lib/view/home/widgets/schedule_card.dart | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/uni_app/lib/view/home/widgets/schedule_card.dart b/packages/uni_app/lib/view/home/widgets/schedule_card.dart index 477ed4904..bf5ac7d9a 100644 --- a/packages/uni_app/lib/view/home/widgets/schedule_card.dart +++ b/packages/uni_app/lib/view/home/widgets/schedule_card.dart @@ -50,11 +50,7 @@ class ScheduleCard extends GenericCard { ), contentLoadingWidget: const ScheduleCardShimmer().build(context), mapper: (lectures) => (lectures as List) - .where( - (lecture) => - lecture.endTime.isAfter(DateTime.now()) || - lecture.startTime.weekday != DateTime.now().weekday, - ) + .where((lecture) => lecture.endTime.isAfter(DateTime.now())) .toList(), ); } From 044f8232be64cd6356db9746fbe40c91a0620812 Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Thu, 31 Oct 2024 13:03:11 +0000 Subject: [PATCH 6/6] fix(lazy_consumer): reverted mapper function type --- packages/uni_app/lib/view/home/widgets/schedule_card.dart | 2 +- packages/uni_app/lib/view/lazy_consumer.dart | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/uni_app/lib/view/home/widgets/schedule_card.dart b/packages/uni_app/lib/view/home/widgets/schedule_card.dart index bf5ac7d9a..32edc2752 100644 --- a/packages/uni_app/lib/view/home/widgets/schedule_card.dart +++ b/packages/uni_app/lib/view/home/widgets/schedule_card.dart @@ -49,7 +49,7 @@ class ScheduleCard extends GenericCard { ), ), contentLoadingWidget: const ScheduleCardShimmer().build(context), - mapper: (lectures) => (lectures as List) + mapper: (lectures) => lectures .where((lecture) => lecture.endTime.isAfter(DateTime.now())) .toList(), ); diff --git a/packages/uni_app/lib/view/lazy_consumer.dart b/packages/uni_app/lib/view/lazy_consumer.dart index 11f797343..795e249e2 100644 --- a/packages/uni_app/lib/view/lazy_consumer.dart +++ b/packages/uni_app/lib/view/lazy_consumer.dart @@ -36,9 +36,9 @@ class LazyConsumer, T2> final bool Function(T2) hasContent; final Widget onNullContent; final Widget? contentLoadingWidget; - final T2 Function(dynamic)? mapper; + final T2 Function(T2)? mapper; - static T2 _defaultMapper(dynamic value) => value as T2; + static T2 _defaultMapper(T2 value) => value; @override Widget build(BuildContext context) { @@ -94,7 +94,7 @@ class LazyConsumer, T2> Widget requestDependantWidget(BuildContext context, T1 provider) { final mappedState = provider.state != null - ? (mapper ?? _defaultMapper)(provider.state as dynamic) + ? (mapper ?? _defaultMapper)(provider.state as T2) : null; final showContent = provider.state != null && hasContent(mappedState as T2);