Skip to content

Commit

Permalink
Encapsulate card refresh logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmendes committed Jul 12, 2023
1 parent c41385e commit 0564e6f
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 63 deletions.
4 changes: 3 additions & 1 deletion uni/lib/view/common_widgets/generic_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ abstract class GenericCard extends StatefulWidget {

String getTitle();

dynamic onClick(BuildContext context);
void onClick(BuildContext context);

void onRefresh(BuildContext context);

Text getInfoText(String text, BuildContext context) {
return Text(text,
Expand Down
3 changes: 3 additions & 0 deletions uni/lib/view/course_units/widgets/course_unit_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ class CourseUnitCard extends GenericCard {
MaterialPageRoute(
builder: (context) => CourseUnitDetailPageView(courseUnit)));
}

@override
void onRefresh(BuildContext context) {}
}
52 changes: 8 additions & 44 deletions uni/lib/view/home/home.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/providers/lazy/bus_stop_provider.dart';
import 'package:uni/model/providers/lazy/exam_provider.dart';
import 'package:uni/model/providers/lazy/home_page_provider.dart';
import 'package:uni/model/providers/lazy/lecture_provider.dart';
import 'package:uni/model/providers/lazy/library_occupation_provider.dart';
import 'package:uni/model/providers/lazy/reference_provider.dart';
import 'package:uni/model/providers/startup/profile_provider.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/utils/favorite_widget_type.dart';
import 'package:uni/view/common_widgets/pages_layouts/general/general.dart';
import 'package:uni/view/home/widgets/main_cards_list.dart';

Expand All @@ -23,47 +15,19 @@ class HomePageView extends StatefulWidget {
class HomePageViewState extends GeneralPageViewState {
@override
Widget getBody(BuildContext context) {
return MainCardsList();
return const MainCardsList();
}

@override
Future<void> handleRefresh(BuildContext context) async {
final homePageProvider =
Provider.of<HomePageProvider>(context, listen: false);
final favoriteCardTypes = context.read<HomePageProvider>().favoriteCards;
final cards = favoriteCardTypes
.map((e) =>
MainCardsList.cardCreators[e]!(const Key(""), false, () => null))
.toList();

final providersToUpdate = <StateProviderNotifier>{};

for (final cardType in homePageProvider.favoriteCards) {
switch (cardType) {
case FavoriteWidgetType.account:
providersToUpdate
.add(Provider.of<ProfileProvider>(context, listen: false));
providersToUpdate
.add(Provider.of<ReferenceProvider>(context, listen: false));
break;
case FavoriteWidgetType.exams:
providersToUpdate
.add(Provider.of<ExamProvider>(context, listen: false));
break;
case FavoriteWidgetType.schedule:
providersToUpdate
.add(Provider.of<LectureProvider>(context, listen: false));
break;
case FavoriteWidgetType.printBalance:
providersToUpdate
.add(Provider.of<ProfileProvider>(context, listen: false));
break;
case FavoriteWidgetType.libraryOccupation:
providersToUpdate.add(
Provider.of<LibraryOccupationProvider>(context, listen: false));
break;
case FavoriteWidgetType.busStops:
providersToUpdate
.add(Provider.of<BusStopProvider>(context, listen: false));
break;
}
for (final card in cards) {
card.onRefresh(context);
}

Future.wait(providersToUpdate.map((e) => e.forceRefresh(context)));
}
}
6 changes: 6 additions & 0 deletions uni/lib/view/home/widgets/bus_stop_card.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/entities/bus_stop.dart';
import 'package:uni/model/providers/lazy/bus_stop_provider.dart';
import 'package:uni/model/request_status.dart';
Expand Down Expand Up @@ -31,6 +32,11 @@ class BusStopCard extends GenericCard {
},
);
}

@override
void onRefresh(BuildContext context) {
Provider.of<BusStopProvider>(context, listen: false).forceRefresh(context);
}
}

/// Returns a widget with the bus stop card final content
Expand Down
6 changes: 6 additions & 0 deletions uni/lib/view/home/widgets/exam_card.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/entities/exam.dart';
import 'package:uni/model/providers/lazy/exam_provider.dart';
import 'package:uni/utils/drawer_items.dart';
Expand Down Expand Up @@ -26,6 +27,11 @@ class ExamCard extends GenericCard {
onClick(BuildContext context) =>
Navigator.pushNamed(context, '/${DrawerItem.navExams.title}');

@override
void onRefresh(BuildContext context) {
Provider.of<ExamProvider>(context, listen: false).forceRefresh(context);
}

/// Returns a widget with all the exams card content.
///
/// If there are no exams, a message telling the user
Expand Down
4 changes: 2 additions & 2 deletions uni/lib/view/home/widgets/main_cards_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef CardCreator = GenericCard Function(
Key key, bool isEditingMode, dynamic Function()? onDelete);

class MainCardsList extends StatelessWidget {
final Map<FavoriteWidgetType, CardCreator> cardCreators = {
static Map<FavoriteWidgetType, CardCreator> cardCreators = {
FavoriteWidgetType.schedule: (k, em, od) =>
ScheduleCard.fromEditingInformation(k, em, od),
FavoriteWidgetType.exams: (k, em, od) =>
Expand All @@ -37,7 +37,7 @@ class MainCardsList extends StatelessWidget {
LibraryOccupationCard.fromEditingInformation(k, em, od)
};

MainCardsList({super.key});
const MainCardsList({super.key});

@override
Widget build(BuildContext context) {
Expand Down
9 changes: 8 additions & 1 deletion uni/lib/view/home/widgets/restaurant_card.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/providers/lazy/restaurant_provider.dart';
import 'package:uni/view/common_widgets/date_rectangle.dart';
import 'package:uni/view/common_widgets/generic_card.dart';
Expand All @@ -18,7 +19,13 @@ class RestaurantCard extends GenericCard {
String getTitle() => 'Cantinas';

@override
onClick(BuildContext context) => null;
onClick(BuildContext context) {}

@override
void onRefresh(BuildContext context) {
Provider.of<RestaurantProvider>(context, listen: false)
.forceRefresh(context);
}

@override
Widget buildCardContent(BuildContext context) {
Expand Down
6 changes: 6 additions & 0 deletions uni/lib/view/home/widgets/schedule_card.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/entities/lecture.dart';
import 'package:uni/model/entities/time_utilities.dart';
import 'package:uni/model/providers/lazy/lecture_provider.dart';
Expand All @@ -21,6 +22,11 @@ class ScheduleCard extends GenericCard {
final double leftPadding = 12.0;
final List<Lecture> lectures = <Lecture>[];

@override
void onRefresh(BuildContext context) {
Provider.of<LectureProvider>(context, listen: false).forceRefresh(context);
}

@override
Widget buildCardContent(BuildContext context) {
return LazyConsumer<LectureProvider>(
Expand Down
7 changes: 7 additions & 0 deletions uni/lib/view/library/widgets/library_occupation_card.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:percent_indicator/percent_indicator.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/providers/lazy/library_occupation_provider.dart';
import 'package:uni/model/request_status.dart';
import 'package:uni/utils/drawer_items.dart';
Expand All @@ -22,6 +23,12 @@ class LibraryOccupationCard extends GenericCard {
onClick(BuildContext context) =>
Navigator.pushNamed(context, '/${DrawerItem.navLibrary.title}');

@override
void onRefresh(BuildContext context) {
Provider.of<LibraryOccupationProvider>(context, listen: false)
.forceRefresh(context);
}

@override
Widget buildCardContent(BuildContext context) {
return LazyConsumer<LibraryOccupationProvider>(
Expand Down
8 changes: 8 additions & 0 deletions uni/lib/view/profile/widgets/account_info_card.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/entities/reference.dart';
import 'package:uni/model/providers/lazy/reference_provider.dart';
import 'package:uni/model/providers/startup/profile_provider.dart';
Expand All @@ -16,6 +17,13 @@ class AccountInfoCard extends GenericCard {
Key key, bool editingMode, Function()? onDelete)
: super.fromEditingInformation(key, editingMode, onDelete);

@override
void onRefresh(BuildContext context) {
Provider.of<ProfileProvider>(context, listen: false).forceRefresh(context);
Provider.of<ReferenceProvider>(context, listen: false)
.forceRefresh(context);
}

@override
Widget buildCardContent(BuildContext context) {
return LazyConsumer<ProfileProvider>(
Expand Down
50 changes: 36 additions & 14 deletions uni/lib/view/profile/widgets/course_info_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,76 +18,95 @@ class CourseInfoCard extends GenericCard {
Container(
margin: const EdgeInsets.only(top: 20.0, bottom: 8.0, left: 20.0),
child: Text('Ano curricular atual: ',
style: Theme.of(context).textTheme.titleSmall),
style: Theme
.of(context)
.textTheme
.titleSmall),
),
Container(
margin:
const EdgeInsets.only(top: 20.0, bottom: 8.0, right: 20.0),
const EdgeInsets.only(top: 20.0, bottom: 8.0, right: 20.0),
child: getInfoText(course.currYear ?? 'Indisponível', context),
)
]),
TableRow(children: [
Container(
margin: const EdgeInsets.only(top: 10.0, bottom: 8.0, left: 20.0),
child: Text('Estado atual: ',
style: Theme.of(context).textTheme.titleSmall),
style: Theme
.of(context)
.textTheme
.titleSmall),
),
Container(
margin:
const EdgeInsets.only(top: 10.0, bottom: 8.0, right: 20.0),
const EdgeInsets.only(top: 10.0, bottom: 8.0, right: 20.0),
child: getInfoText(course.state ?? 'Indisponível', context),
)
]),
TableRow(children: [
Container(
margin: const EdgeInsets.only(top: 10.0, bottom: 8.0, left: 20.0),
child: Text('Ano da primeira inscrição: ',
style: Theme.of(context).textTheme.titleSmall),
style: Theme
.of(context)
.textTheme
.titleSmall),
),
Container(
margin:
const EdgeInsets.only(top: 10.0, bottom: 8.0, right: 20.0),
const EdgeInsets.only(top: 10.0, bottom: 8.0, right: 20.0),
child: getInfoText(
course.firstEnrollment != null
? '${course.firstEnrollment}/${course.firstEnrollment! + 1}'
? '${course.firstEnrollment}/${course.firstEnrollment! +
1}'
: '?',
context))
]),
TableRow(children: [
Container(
margin: const EdgeInsets.only(top: 10.0, bottom: 8.0, left: 20.0),
child: Text('Faculdade: ',
style: Theme.of(context).textTheme.titleSmall),
style: Theme
.of(context)
.textTheme
.titleSmall),
),
Container(
margin:
const EdgeInsets.only(top: 10.0, bottom: 8.0, right: 20.0),
const EdgeInsets.only(top: 10.0, bottom: 8.0, right: 20.0),
child: getInfoText(
course.faculty?.toUpperCase() ?? 'Indisponível', context))
]),
TableRow(children: [
Container(
margin: const EdgeInsets.only(top: 10.0, bottom: 8.0, left: 20.0),
child: Text('Média: ',
style: Theme.of(context).textTheme.titleSmall),
style: Theme
.of(context)
.textTheme
.titleSmall),
),
Container(
margin:
const EdgeInsets.only(top: 10.0, bottom: 8.0, right: 20.0),
const EdgeInsets.only(top: 10.0, bottom: 8.0, right: 20.0),
child: getInfoText(
course.currentAverage?.toString() ?? 'Indisponível',
context))
]),
TableRow(children: [
Container(
margin:
const EdgeInsets.only(top: 10.0, bottom: 20.0, left: 20.0),
const EdgeInsets.only(top: 10.0, bottom: 20.0, left: 20.0),
child: Text('ECTs realizados: ',
style: Theme.of(context).textTheme.titleSmall),
style: Theme
.of(context)
.textTheme
.titleSmall),
),
Container(
margin:
const EdgeInsets.only(top: 10.0, bottom: 20.0, right: 20.0),
const EdgeInsets.only(top: 10.0, bottom: 20.0, right: 20.0),
child: getInfoText(
course.finishedEcts?.toString().replaceFirst('.0', '') ??
'?',
Expand All @@ -103,4 +122,7 @@ class CourseInfoCard extends GenericCard {

@override
onClick(BuildContext context) {}

@override
void onRefresh(BuildContext context) {}
}
6 changes: 6 additions & 0 deletions uni/lib/view/profile/widgets/print_info_card.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/providers/startup/profile_provider.dart';
import 'package:uni/view/common_widgets/generic_card.dart';
import 'package:uni/view/lazy_consumer.dart';
Expand Down Expand Up @@ -63,4 +64,9 @@ class PrintInfoCard extends GenericCard {

@override
onClick(BuildContext context) {}

@override
void onRefresh(BuildContext context) {
Provider.of<ProfileProvider>(context, listen: false).forceRefresh(context);
}
}
5 changes: 4 additions & 1 deletion uni/lib/view/restaurant/widgets/restaurant_page_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RestaurantPageCard extends GenericCard {

RestaurantPageCard(this.restaurantName, this.meals, {super.key})
: super.customStyle(
editingMode: false, onDelete: () => null, smallTitle: true);
editingMode: false, onDelete: () => null, smallTitle: true);

@override
Widget buildCardContent(BuildContext context) {
Expand All @@ -21,4 +21,7 @@ class RestaurantPageCard extends GenericCard {

@override
onClick(BuildContext context) {}

@override
void onRefresh(BuildContext context) {}
}

0 comments on commit 0564e6f

Please sign in to comment.