Skip to content

Commit

Permalink
feat: added mapper function to LazyConsumer (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueSFernandes authored Oct 31, 2024
2 parents 0a8755d + 044f823 commit 905fcd7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
19 changes: 7 additions & 12 deletions packages/uni_app/lib/view/home/widgets/schedule_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class ScheduleCard extends GenericCard {
),
),
contentLoadingWidget: const ScheduleCardShimmer().build(context),
mapper: (lectures) => lectures
.where((lecture) => lecture.endTime.isAfter(DateTime.now()))
.toList(),
);
}

Expand All @@ -71,16 +74,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;
}

Expand All @@ -91,11 +86,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;
}
}
Expand Down
13 changes: 10 additions & 3 deletions packages/uni_app/lib/view/lazy_consumer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ class LazyConsumer<T1 extends StateProviderNotifier<T2>, T2>
required this.hasContent,
required this.onNullContent,
this.contentLoadingWidget,
this.mapper,
super.key,
});

final Widget Function(BuildContext, T2) builder;
final bool Function(T2) hasContent;
final Widget onNullContent;
final Widget? contentLoadingWidget;
final T2 Function(T2)? mapper;

static T2 _defaultMapper<T2>(T2 value) => value;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -89,8 +93,11 @@ class LazyConsumer<T1 extends StateProviderNotifier<T2>, 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);
Expand All @@ -99,7 +106,7 @@ class LazyConsumer<T1 extends StateProviderNotifier<T2>, T2>
}

return showContent
? builder(context, provider.state as T2)
? builder(context, mappedState)
: Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
Expand Down

0 comments on commit 905fcd7

Please sign in to comment.