From 63aa00c4436af6b7e99da8d68af59ae9ee60d0d4 Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Sat, 16 Nov 2024 16:00:55 +0000 Subject: [PATCH 1/6] feat: added up_menus dependency --- packages/uni_app/pubspec.lock | 16 ++++++++++++++++ packages/uni_app/pubspec.yaml | 1 + 2 files changed, 17 insertions(+) diff --git a/packages/uni_app/pubspec.lock b/packages/uni_app/pubspec.lock index 04778b73c..1f8db98e1 100644 --- a/packages/uni_app/pubspec.lock +++ b/packages/uni_app/pubspec.lock @@ -1147,6 +1147,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.28.0" + scrollable_positioned_list: + dependency: transitive + description: + name: scrollable_positioned_list + sha256: "1b54d5f1329a1e263269abc9e2543d90806131aa14fe7c6062a8054d57249287" + url: "https://pub.dev" + source: hosted + version: "0.3.8" sentry: dependency: transitive description: @@ -1511,6 +1519,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.2" + up_menus: + dependency: "direct main" + description: + name: up_menus + sha256: b973555072c6056b757f2740cf7c10fc190ff7f7cd3df3f26c303faf261ce112 + url: "https://pub.dev" + source: hosted + version: "1.0.1" upgrader: dependency: "direct main" description: diff --git a/packages/uni_app/pubspec.yaml b/packages/uni_app/pubspec.yaml index 7c5d3d057..29f7c0f3a 100644 --- a/packages/uni_app/pubspec.yaml +++ b/packages/uni_app/pubspec.yaml @@ -69,6 +69,7 @@ dependencies: ua_client_hints: ^1.3.1 uni_ui: path: ../uni_ui + up_menus: ^1.0.1 upgrader: ^10.3.0 url_launcher: ^6.2.2 workmanager: ^0.5.2 From 9e8f5c093eca2a5b7c3195a0da27238a8e8c2c3d Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Sat, 16 Nov 2024 20:13:16 +0000 Subject: [PATCH 2/6] feat: Updated day of the week parser to include datetime --- .../uni_app/lib/model/utils/day_of_week.dart | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/uni_app/lib/model/utils/day_of_week.dart b/packages/uni_app/lib/model/utils/day_of_week.dart index 77f78d9d3..579c4e991 100644 --- a/packages/uni_app/lib/model/utils/day_of_week.dart +++ b/packages/uni_app/lib/model/utils/day_of_week.dart @@ -8,6 +8,28 @@ enum DayOfWeek { sunday } +DayOfWeek parseDateTime(DateTime dateTime) { + final weekDay = dateTime.weekday; + switch (weekDay) { + case DateTime.monday: + return DayOfWeek.monday; + case DateTime.tuesday: + return DayOfWeek.tuesday; + case DateTime.wednesday: + return DayOfWeek.wednesday; + case DateTime.thursday: + return DayOfWeek.thursday; + case DateTime.friday: + return DayOfWeek.friday; + case DateTime.saturday: + return DayOfWeek.saturday; + case DateTime.sunday: + return DayOfWeek.sunday; + default: + throw Exception('Invalid day of week'); + } +} + DayOfWeek? parseDayOfWeek(String str) { final weekDay = str.replaceAll(' ', '').toLowerCase(); if (weekDay == 'segunda-feira') { From ece6d191d08046b2ea91e9817bba1f8689e162da Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Sat, 16 Nov 2024 20:16:35 +0000 Subject: [PATCH 3/6] partially implemented new restaurants api Co-authored-by: AugustoVSoares --- .../fetchers/restaurant_fetcher.dart | 86 ++++++++++++++++++- .../providers/lazy/restaurant_provider.dart | 16 +++- 2 files changed, 96 insertions(+), 6 deletions(-) diff --git a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart index 979759046..6569f471b 100644 --- a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart +++ b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart @@ -1,7 +1,12 @@ +import 'dart:convert'; + import 'package:uni/controller/networking/network_router.dart'; import 'package:uni/controller/parsers/parser_restaurants.dart'; +import 'package:uni/model/entities/meal.dart'; import 'package:uni/model/entities/restaurant.dart'; +import 'package:uni/model/utils/day_of_week.dart'; import 'package:uni/session/flows/base/session.dart'; +import 'package:up_menus/up_menus.dart'; /// Class for fetching the menu class RestaurantFetcher { @@ -16,6 +21,22 @@ class RestaurantFetcher { // List the Restaurant sheet names in the Google Sheets Document final List restaurantSheets = ['Cantina']; + // TODO: remove later + void printRestaurants(List restaurants) { + for (final restaurant in restaurants) { + print(restaurant.id); + print(restaurant.name); + print(restaurant.reference); + final meals = restaurant.meals; + meals.forEach((day, mealList) { + print(day); + for (final meal in mealList) { + print(' - ${meal.name}'); + } + }); + } + } + // Generate the Gsheets endpoints list based on a list of sheets String buildGSheetsEndpoint(String sheet) { return Uri.encodeFull( @@ -44,6 +65,65 @@ class RestaurantFetcher { ); } + Future> getSASUPRestaurants() async { + // TODO: change to accomodate changes for the new UI. + final upMenus = UPMenusApi(); + final establishments = await upMenus.establishments.list(); + final restaurants = []; + + final icbas = await upMenus.establishments.get(4); + print(icbas.toJson()); + + print("74"); + // For every establishement... + for (final establishment in establishments) { + // Get the menu for the current week + if (establishment.dayMenu == false) continue; + final dayMenus = + (await upMenus.dayMenus.get(establishment.id, Period.lunch)) + .followedBy( + await upMenus.dayMenus.get(establishment.id, Period.dinner), + ) + .followedBy( + await upMenus.dayMenus.get(establishment.id, Period.snackBar), + ) + .followedBy( + await upMenus.dayMenus.get(establishment.id, Period.breakfast), + ); + print("89"); + print("${establishment.namePt} id: ${establishment.id}"); + final meals = []; + // For every day... + print("93"); + for (final dayMenu in dayMenus) { + print("94"); + // And for every dish... + for (final dish in dayMenu.dishes) { + // Extract the information about the meal. + print("96"); + meals.add( + Meal( + dish.dishType.namePt, + dish.dish.namePt, + parseDateTime(dayMenu.day), + dayMenu.day, + ), + ); + } + } + print("110"); + + restaurants.add( + Restaurant(establishment.id, establishment.namePt, '', meals: meals), + ); + print("115"); + printRestaurants(restaurants); + } + print("hereee"); + print(restaurants); + return restaurants; + } + final List sigarraMenuEndpoints = [ '${NetworkRouter.getBaseUrl('feup')}CANTINA.EMENTASHOW', ]; @@ -64,7 +144,9 @@ class RestaurantFetcher { } Future> getRestaurants(Session session) async { - final restaurants = await fetchSigarraRestaurants(session); + final restaurants = await getSASUPRestaurants(); + print(restaurants); + // final restaurants = await fetchSigarraRestaurants(session); // Check for restaurants without associated meals and attempt to parse them // from GSheets @@ -90,7 +172,7 @@ class RestaurantFetcher { ) ..insert(0, gSheetsRestaurant); } - + // printRestaurants(restaurants); return restaurants; } } diff --git a/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart b/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart index f6f9599f7..258633ae2 100644 --- a/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart +++ b/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart @@ -9,13 +9,20 @@ import 'package:uni/model/providers/state_providers.dart'; class RestaurantProvider extends StateProviderNotifier> { RestaurantProvider() : super(cacheDuration: const Duration(days: 1)); + // @override + // Future> loadFromStorage( + // StateProviders stateProviders, + // ) async { + // final restaurantDb = RestaurantDatabase(); + // final restaurants = await restaurantDb.getRestaurants(); + // return restaurants; + // } + @override Future> loadFromStorage( StateProviders stateProviders, ) async { - final restaurantDb = RestaurantDatabase(); - final restaurants = await restaurantDb.getRestaurants(); - return restaurants; + return loadFromRemote(stateProviders); } @override @@ -26,6 +33,7 @@ class RestaurantProvider extends StateProviderNotifier> { final db = RestaurantDatabase(); unawaited(db.saveIfPersistentSession(restaurants)); - return filterPastMeals(restaurants); + // return filterPastMeals(restaurants); + return restaurants; } } From bdc94974da6c12ad8efc2014510f98db9f174ad6 Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Mon, 18 Nov 2024 17:59:10 +0000 Subject: [PATCH 4/6] refactor: removed GSheets fetching logic --- .../fetchers/restaurant_fetcher.dart | 135 +++++------------- 1 file changed, 38 insertions(+), 97 deletions(-) diff --git a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart index 6569f471b..552037ea8 100644 --- a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart +++ b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart @@ -1,5 +1,3 @@ -import 'dart:convert'; - import 'package:uni/controller/networking/network_router.dart'; import 'package:uni/controller/parsers/parser_restaurants.dart'; import 'package:uni/model/entities/meal.dart'; @@ -10,17 +8,6 @@ import 'package:up_menus/up_menus.dart'; /// Class for fetching the menu class RestaurantFetcher { - final String spreadSheetUrl = 'https://docs.google.com/spreadsheets/d/' - '1TJauM0HwIf2RauQU2GmhdZZ1ZicFLMHuBkxWwVOw3Q4'; - final String jsonEndpoint = '/gviz/tq?tqx=out:json'; - - // Format: Date(dd/mm/yyyy), Meal("AlmoƧo", "Jantar), Dish("Sopa", "Carne", - // "Peixe", "Dieta", "Vegetariano", "Salada"), Description(String) - final String sheetsColumnRange = 'A:D'; - - // List the Restaurant sheet names in the Google Sheets Document - final List restaurantSheets = ['Cantina']; - // TODO: remove later void printRestaurants(List restaurants) { for (final restaurant in restaurants) { @@ -37,90 +24,69 @@ class RestaurantFetcher { } } - // Generate the Gsheets endpoints list based on a list of sheets - String buildGSheetsEndpoint(String sheet) { - return Uri.encodeFull( - '$spreadSheetUrl$jsonEndpoint&sheet=$sheet&range=$sheetsColumnRange', - ); - } - - String getRestaurantGSheetName(Restaurant restaurant) { - return restaurantSheets.firstWhere( - (sheetName) => - restaurant.name.toLowerCase().contains(sheetName.toLowerCase()), - orElse: () => '', - ); - } - - Future fetchGSheetsRestaurant( - String url, - String restaurantName, - Session session, { - bool isDinner = false, - }) async { - return getRestaurantFromGSheets( - await NetworkRouter.getWithCookies(url, {}, session), - restaurantName, - isDinner: isDinner, - ); - } - Future> getSASUPRestaurants() async { - // TODO: change to accomodate changes for the new UI. + // TODO: change the implementation to accomodate changes for the new UI. final upMenus = UPMenusApi(); final establishments = await upMenus.establishments.list(); final restaurants = []; - final icbas = await upMenus.establishments.get(4); - print(icbas.toJson()); - - print("74"); // For every establishement... for (final establishment in establishments) { // Get the menu for the current week - if (establishment.dayMenu == false) continue; - final dayMenus = - (await upMenus.dayMenus.get(establishment.id, Period.lunch)) - .followedBy( - await upMenus.dayMenus.get(establishment.id, Period.dinner), - ) - .followedBy( - await upMenus.dayMenus.get(establishment.id, Period.snackBar), - ) - .followedBy( - await upMenus.dayMenus.get(establishment.id, Period.breakfast), - ); - print("89"); - print("${establishment.namePt} id: ${establishment.id}"); + if (establishment.dayMenu == false) { + continue; + } + // HACK: hardcoded week number, because SASUP hasn't published the menus for the current week. + final dayMenus = (await upMenus.dayMenus + .get(establishment.id, Period.lunch, weekNumber: 40, year: 2024)) + .followedBy( + await upMenus.dayMenus.get( + establishment.id, + Period.dinner, + weekNumber: 40, + year: 2024, + ), + ) + .followedBy( + await upMenus.dayMenus.get( + establishment.id, + Period.snackBar, + weekNumber: 40, + year: 2024, + ), + ) + .followedBy( + await upMenus.dayMenus.get( + establishment.id, + Period.breakfast, + weekNumber: 40, + year: 2024, + ), + ); final meals = []; // For every day... - print("93"); for (final dayMenu in dayMenus) { - print("94"); // And for every dish... for (final dish in dayMenu.dishes) { // Extract the information about the meal. - print("96"); meals.add( Meal( dish.dishType.namePt, dish.dish.namePt, - parseDateTime(dayMenu.day), - dayMenu.day, + DayOfWeek.monday, + dayMenu.day.weekday == DateTime.monday + ? DateTime.now() + : dayMenu.day, ), ); } } - print("110"); restaurants.add( Restaurant(establishment.id, establishment.namePt, '', meals: meals), ); - print("115"); printRestaurants(restaurants); } - print("hereee"); - print(restaurants); return restaurants; } @@ -144,35 +110,10 @@ class RestaurantFetcher { } Future> getRestaurants(Session session) async { - final restaurants = await getSASUPRestaurants(); - print(restaurants); - // final restaurants = await fetchSigarraRestaurants(session); - - // Check for restaurants without associated meals and attempt to parse them - // from GSheets - final restaurantsWithoutMeals = - restaurants.where((restaurant) => restaurant.meals.isEmpty).toList(); + final restaurants = + await getSASUPRestaurants() + await fetchSigarraRestaurants(session); - for (final restaurant in restaurantsWithoutMeals) { - final sheetName = getRestaurantGSheetName(restaurant); - if (sheetName.isEmpty) { - continue; - } - - final gSheetsRestaurant = await fetchGSheetsRestaurant( - buildGSheetsEndpoint(sheetName), - restaurant.name, - session, - isDinner: restaurant.name.toLowerCase().contains('jantar'), - ); - - restaurants - ..removeWhere( - (restaurant) => restaurant.name == gSheetsRestaurant.name, - ) - ..insert(0, gSheetsRestaurant); - } - // printRestaurants(restaurants); + printRestaurants(restaurants); return restaurants; } } From 2f4d9dde52ea04dd528a4c99a90b97972ef3ca06 Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Mon, 18 Nov 2024 18:20:32 +0000 Subject: [PATCH 5/6] refactor: removed debug prints --- .../fetchers/restaurant_fetcher.dart | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart index 552037ea8..96b103235 100644 --- a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart +++ b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart @@ -8,23 +8,23 @@ import 'package:up_menus/up_menus.dart'; /// Class for fetching the menu class RestaurantFetcher { - // TODO: remove later - void printRestaurants(List restaurants) { - for (final restaurant in restaurants) { - print(restaurant.id); - print(restaurant.name); - print(restaurant.reference); - final meals = restaurant.meals; - meals.forEach((day, mealList) { - print(day); - for (final meal in mealList) { - print(' - ${meal.name}'); - } - }); - } - } + // Auxliary function to print a list of restaurants. + // void printRestaurants(List restaurants) { + // for (final restaurant in restaurants) { + // print(restaurant.id); + // print(restaurant.name); + // print(restaurant.reference); + // final meals = restaurant.meals; + // meals.forEach((day, mealList) { + // print(day); + // for (final meal in mealList) { + // print(' - ${meal.name}'); + // } + // }); + // } + // } - Future> getSASUPRestaurants() async { + Future> fetchSASUPRestaurants() async { // TODO: change the implementation to accomodate changes for the new UI. final upMenus = UPMenusApi(); final establishments = await upMenus.establishments.list(); @@ -37,8 +37,12 @@ class RestaurantFetcher { continue; } // HACK: hardcoded week number, because SASUP hasn't published the menus for the current week. - final dayMenus = (await upMenus.dayMenus - .get(establishment.id, Period.lunch, weekNumber: 40, year: 2024)) + final dayMenus = (await upMenus.dayMenus.get( + establishment.id, + Period.lunch, + weekNumber: 40, + year: 2024, + )) .followedBy( await upMenus.dayMenus.get( establishment.id, @@ -73,10 +77,8 @@ class RestaurantFetcher { Meal( dish.dishType.namePt, dish.dish.namePt, - DayOfWeek.monday, - dayMenu.day.weekday == DateTime.monday - ? DateTime.now() - : dayMenu.day, + parseDateTime(dayMenu.day), + dayMenu.day, ), ); } @@ -85,7 +87,6 @@ class RestaurantFetcher { restaurants.add( Restaurant(establishment.id, establishment.namePt, '', meals: meals), ); - printRestaurants(restaurants); } return restaurants; } @@ -111,9 +112,8 @@ class RestaurantFetcher { Future> getRestaurants(Session session) async { final restaurants = - await getSASUPRestaurants() + await fetchSigarraRestaurants(session); + await fetchSASUPRestaurants() + await fetchSigarraRestaurants(session); - printRestaurants(restaurants); return restaurants; } } From 5b2568df41e1df265d79d6275de8837089e5c807 Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Mon, 18 Nov 2024 18:41:00 +0000 Subject: [PATCH 6/6] refactor: disable past meals filtering temporarily --- .../database/app_restaurant_database.dart | 6 +++++- .../model/providers/lazy/restaurant_provider.dart | 15 +++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart b/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart index 70c15204f..045c19fed 100644 --- a/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart +++ b/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart @@ -70,7 +70,11 @@ class RestaurantDatabase extends AppDatabase> { } }); - return filterPastMeals(restaurants); + // TODO: reimplement the filter. + + // return filterPastMeals(restaurants); + + return restaurants; } Future> getRestaurantMeals( diff --git a/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart b/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart index 258633ae2..a4b70a388 100644 --- a/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart +++ b/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart @@ -9,20 +9,13 @@ import 'package:uni/model/providers/state_providers.dart'; class RestaurantProvider extends StateProviderNotifier> { RestaurantProvider() : super(cacheDuration: const Duration(days: 1)); - // @override - // Future> loadFromStorage( - // StateProviders stateProviders, - // ) async { - // final restaurantDb = RestaurantDatabase(); - // final restaurants = await restaurantDb.getRestaurants(); - // return restaurants; - // } - @override Future> loadFromStorage( StateProviders stateProviders, ) async { - return loadFromRemote(stateProviders); + final restaurantDb = RestaurantDatabase(); + final restaurants = await restaurantDb.getRestaurants(); + return restaurants; } @override @@ -33,6 +26,8 @@ class RestaurantProvider extends StateProviderNotifier> { final db = RestaurantDatabase(); unawaited(db.saveIfPersistentSession(restaurants)); + // TODO: enable past meals filtering after SASUP publishes the new menus (ALSO CHANGE IN THE STORAGE DATABASE!!!!). + // return filterPastMeals(restaurants); return restaurants; }