From 93a68403b009a6e814dd4e9a06f1eb2aec73559e Mon Sep 17 00:00:00 2001 From: thePeras Date: Fri, 3 Jan 2025 19:19:50 +0000 Subject: [PATCH] Restaurants nosql db migration --- packages/uni_app/lib/controller/cleanup.dart | 3 + .../fetchers/restaurant_fetcher.dart | 4 +- .../database-nosql/database.dart | 6 + .../database-nosql/restaurants_database.dart | 6 + .../database/app_restaurant_database.dart | 136 ---------- .../parsers/parser_restaurants.dart | 14 +- .../lib/generated/model/entities/meal.g.dart | 17 +- .../model/entities/restaurant.g.dart | 21 +- packages/uni_app/lib/main.dart | 9 +- .../lib/model/converters/json_encodable.dart | 3 - .../lib/model/entities/floor_occupation.dart | 3 +- packages/uni_app/lib/model/entities/meal.dart | 22 +- .../lib/model/entities/restaurant.dart | 36 ++- .../providers/lazy/restaurant_provider.dart | 10 +- packages/uni_app/lib/objectbox-model.json | 102 +++++++- packages/uni_app/lib/objectbox.g.dart | 245 +++++++++++++++++- .../view/home/widgets/restaurant_card.dart | 2 +- .../view/restaurant/restaurant_page_view.dart | 5 +- 18 files changed, 440 insertions(+), 204 deletions(-) create mode 100644 packages/uni_app/lib/controller/local_storage/database-nosql/restaurants_database.dart delete mode 100644 packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart delete mode 100644 packages/uni_app/lib/model/converters/json_encodable.dart diff --git a/packages/uni_app/lib/controller/cleanup.dart b/packages/uni_app/lib/controller/cleanup.dart index ac2b3b94d..8a94c5991 100644 --- a/packages/uni_app/lib/controller/cleanup.dart +++ b/packages/uni_app/lib/controller/cleanup.dart @@ -10,6 +10,7 @@ import 'package:uni/controller/local_storage/database-nosql/courses_database.dar import 'package:uni/controller/local_storage/database-nosql/database.dart'; import 'package:uni/controller/local_storage/database-nosql/exams_database.dart'; import 'package:uni/controller/local_storage/database-nosql/lectures_database.dart'; +import 'package:uni/controller/local_storage/database-nosql/restaurants_database.dart'; import 'package:uni/controller/local_storage/database/app_bus_stop_database.dart'; import 'package:uni/controller/local_storage/preferences_controller.dart'; import 'package:uni/model/providers/state_providers.dart'; @@ -25,6 +26,8 @@ Future cleanupStoredData(BuildContext context) async { ExamsDatabase().deleteAll(), CoursesDatabase().deleteAll(), CourseUnitsDatabase().deleteAll(), + RestaurantsDatabase().deleteAll(), + AppBusStopDatabase().deleteBusStops(), PreferencesController.removeSavedSession(), ]); diff --git a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart index 720bff589..968e36ae6 100644 --- a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart +++ b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart @@ -22,8 +22,8 @@ class RestaurantFetcher { dish.dishType.namePt, dish.dish.namePt, dish.dish.nameEn ?? dish.dish.namePt, - parseDateTime(dayMenu.day), dayMenu.day, + dbDayOfWeek: parseDateTime(dayMenu.day).index, ), ); } @@ -34,7 +34,7 @@ class RestaurantFetcher { establishment.nameEn, period, '', - meals: meals, + meals, ); } diff --git a/packages/uni_app/lib/controller/local_storage/database-nosql/database.dart b/packages/uni_app/lib/controller/local_storage/database-nosql/database.dart index 48e50a9cb..2488e8015 100644 --- a/packages/uni_app/lib/controller/local_storage/database-nosql/database.dart +++ b/packages/uni_app/lib/controller/local_storage/database-nosql/database.dart @@ -39,4 +39,10 @@ class Database { void close() { _store.close(); } + + Future remove() async { + final appDir = await getApplicationDocumentsDirectory(); + final storePath = '${appDir.path}/database'; + Store.removeDbFiles(storePath); + } } diff --git a/packages/uni_app/lib/controller/local_storage/database-nosql/restaurants_database.dart b/packages/uni_app/lib/controller/local_storage/database-nosql/restaurants_database.dart new file mode 100644 index 000000000..464ecf797 --- /dev/null +++ b/packages/uni_app/lib/controller/local_storage/database-nosql/restaurants_database.dart @@ -0,0 +1,6 @@ +import 'package:uni/controller/local_storage/database-nosql/app_database.dart'; +import 'package:uni/model/entities/restaurant.dart'; + +class RestaurantsDatabase extends NoSQLDatabase { + RestaurantsDatabase() : super('restaurants'); +} 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 deleted file mode 100644 index ce067524e..000000000 --- a/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart +++ /dev/null @@ -1,136 +0,0 @@ -import 'package:intl/intl.dart'; -import 'package:sqflite/sqflite.dart'; -import 'package:uni/controller/local_storage/database/app_database.dart'; -import 'package:uni/model/entities/meal.dart'; -import 'package:uni/model/entities/restaurant.dart'; -import 'package:uni/model/utils/day_of_week.dart'; - -class RestaurantDatabase extends AppDatabase> { - RestaurantDatabase() - : super( - 'restaurant.db', - [ - ''' - CREATE TABLE RESTAURANTS( - id INTEGER PRIMARY KEY, - ref TEXT, - namePt TEXT - namePt TEXT) - ''', - ''' - CREATE TABLE MEALS( - id INTEGER PRIMARY KEY AUTOINCREMENT, - day TEXT, - type TEXT, - date TEXT, - namePt TEXT, - nameEn TEXT, - id_restaurant INTEGER, - FOREIGN KEY (id_restaurant) REFERENCES RESTAURANTS(id)) - ''' - ], - ); - - /// Get all restaurants and meals, if day is null, all meals are returned - Future> restaurants({DayOfWeek? day}) async { - final db = await getDatabase(); - var restaurants = []; - - await db.transaction((txn) async { - final List> restaurantMaps = - await db.query('restaurants'); - - restaurants = await Future.wait( - restaurantMaps.map((map) async { - final restaurantId = map['id'] as int; - final meals = await getRestaurantMeals(txn, restaurantId, day: day); - - return Restaurant( - restaurantId, - map['namePt'] as String, - map['nameEn'] as String, - map['period'] as String, - map['ref'] as String, - meals: meals, - ); - }).toList(), - ); - }); - - return restaurants; - } - - Future> getRestaurants() async { - final db = await getDatabase(); - final restaurants = []; - await db.transaction((txn) async { - final List> restaurantsFromDB = - await txn.query('RESTAURANTS'); - for (final restaurantMap in restaurantsFromDB) { - final id = restaurantMap['id'] as int; - final meals = await getRestaurantMeals(txn, id); - final restaurant = Restaurant.fromMap(restaurantMap, meals); - restaurants.add(restaurant); - } - }); - - return restaurants; - } - - Future> getRestaurantMeals( - Transaction txn, - int restaurantId, { - DayOfWeek? day, - }) async { - final whereArgs = [restaurantId]; - var whereQuery = 'id_restaurant = ? '; - if (day != null) { - whereQuery += ' and day = ?'; - whereArgs.add(toString(day)); - } - - // Get restaurant meals - final List> mealsMaps = - await txn.query('meals', where: whereQuery, whereArgs: whereArgs); - - // Retrieve data from query - final meals = mealsMaps.map((map) { - final day = parseDayOfWeek(map['day'] as String); - final type = map['type'] as String; - final namePt = map['namePt'] as String; - final nameEn = map['nameEn'] as String; - final format = DateFormat('d-M-y'); - final date = format.parseUtc(map['date'] as String); - return Meal(type, namePt, nameEn, day!, date); - }).toList(); - - return meals; - } - - /// Insert restaurant and meals in database - Future insertRestaurant(Transaction txn, Restaurant restaurant) async { - final id = await txn.insert('RESTAURANTS', restaurant.toJson()); - restaurant.meals.forEach((dayOfWeak, meals) async { - for (final meal in meals) { - await txn.insert('MEALS', meal.toMap(id)); - } - }); - } - - /// Deletes all restaurants and meals - Future deleteAll(Transaction txn) async { - await txn.delete('meals'); - await txn.delete('restaurants'); - } - - @override - Future saveToDatabase(List data) async { - final db = await getDatabase(); - await db.transaction((transaction) async { - await deleteAll(transaction); - for (final restaurant in data) { - await insertRestaurant(transaction, restaurant); - } - }); - } -} diff --git a/packages/uni_app/lib/controller/parsers/parser_restaurants.dart b/packages/uni_app/lib/controller/parsers/parser_restaurants.dart index 647ad25ee..65e9498b9 100644 --- a/packages/uni_app/lib/controller/parsers/parser_restaurants.dart +++ b/packages/uni_app/lib/controller/parsers/parser_restaurants.dart @@ -1,6 +1,7 @@ import 'package:html/parser.dart'; import 'package:http/http.dart'; import 'package:intl/intl.dart'; +import 'package:objectbox/objectbox.dart'; import 'package:uni/model/entities/meal.dart'; import 'package:uni/model/entities/restaurant.dart'; import 'package:uni/model/utils/day_of_week.dart'; @@ -57,7 +58,13 @@ List getRestaurantsFromHtml(Response response) { } } else { type = document.querySelector('#$header')?.text; - final meal = Meal(type ?? '', value, value, dayOfWeek!, date!); + final meal = Meal( + type ?? '', + value, + value, + date!, + dbDayOfWeek: dayOfWeek!.index, + ); meals.add(meal); } } @@ -65,13 +72,16 @@ List getRestaurantsFromHtml(Response response) { break; } } + + final mealsToMany = ToMany(items: meals); + return Restaurant( null, restaurantTuple.$2, restaurantTuple.$2, restaurantTuple.$1, '', - meals: meals, + mealsToMany, ); }).toList(); return restaurants; diff --git a/packages/uni_app/lib/generated/model/entities/meal.g.dart b/packages/uni_app/lib/generated/model/entities/meal.g.dart index c7b042248..a2281fb9a 100644 --- a/packages/uni_app/lib/generated/model/entities/meal.g.dart +++ b/packages/uni_app/lib/generated/model/entities/meal.g.dart @@ -10,24 +10,15 @@ Meal _$MealFromJson(Map json) => Meal( json['type'] as String, json['namePt'] as String, json['nameEn'] as String, - $enumDecode(_$DayOfWeekEnumMap, json['dayOfWeek']), const DateTimeConverter().fromJson(json['date'] as String), - ); + dbDayOfWeek: (json['dbDayOfWeek'] as num?)?.toInt(), + )..id = (json['id'] as num?)?.toInt(); Map _$MealToJson(Meal instance) => { + 'id': instance.id, 'type': instance.type, 'namePt': instance.namePt, 'nameEn': instance.nameEn, - 'dayOfWeek': _$DayOfWeekEnumMap[instance.dayOfWeek]!, + 'dbDayOfWeek': instance.dbDayOfWeek, 'date': const DateTimeConverter().toJson(instance.date), }; - -const _$DayOfWeekEnumMap = { - DayOfWeek.monday: 'monday', - DayOfWeek.tuesday: 'tuesday', - DayOfWeek.wednesday: 'wednesday', - DayOfWeek.thursday: 'thursday', - DayOfWeek.friday: 'friday', - DayOfWeek.saturday: 'saturday', - DayOfWeek.sunday: 'sunday', -}; diff --git a/packages/uni_app/lib/generated/model/entities/restaurant.g.dart b/packages/uni_app/lib/generated/model/entities/restaurant.g.dart index 592854cea..f157aeeba 100644 --- a/packages/uni_app/lib/generated/model/entities/restaurant.g.dart +++ b/packages/uni_app/lib/generated/model/entities/restaurant.g.dart @@ -12,28 +12,17 @@ Restaurant _$RestaurantFromJson(Map json) => Restaurant( json['nameEn'] as String, json['period'] as String, json['ref'] as String, - meals: (json['meals'] as List) - .map((e) => Meal.fromJson(e as Map)) - .toList(), - ); + const _MealRelToManyConverter() + .fromJson(json['meals'] as List>?), + )..uniqueId = (json['uniqueId'] as num?)?.toInt(); Map _$RestaurantToJson(Restaurant instance) => { + 'uniqueId': instance.uniqueId, 'id': instance.id, 'namePt': instance.namePt, 'nameEn': instance.nameEn, 'period': instance.period, 'ref': instance.reference, - 'meals': - instance.meals.map((k, e) => MapEntry(_$DayOfWeekEnumMap[k]!, e)), + 'meals': const _MealRelToManyConverter().toJson(instance.meals), }; - -const _$DayOfWeekEnumMap = { - DayOfWeek.monday: 'monday', - DayOfWeek.tuesday: 'tuesday', - DayOfWeek.wednesday: 'wednesday', - DayOfWeek.thursday: 'thursday', - DayOfWeek.friday: 'friday', - DayOfWeek.saturday: 'saturday', - DayOfWeek.sunday: 'sunday', -}; diff --git a/packages/uni_app/lib/main.dart b/packages/uni_app/lib/main.dart index 7321c812e..be816f0aa 100644 --- a/packages/uni_app/lib/main.dart +++ b/packages/uni_app/lib/main.dart @@ -140,7 +140,14 @@ Future main() async { } // Nosql single database - await Database().init(); + try{ + await Database().init(); + } catch (err) { + if (err.toString().contains('ObjectBoxException')) { + await Database().remove(); + await Database().init(); + } + } final savedTheme = PreferencesController.getThemeMode(); final savedLocale = PreferencesController.getLocale(); diff --git a/packages/uni_app/lib/model/converters/json_encodable.dart b/packages/uni_app/lib/model/converters/json_encodable.dart deleted file mode 100644 index 34ebf0b01..000000000 --- a/packages/uni_app/lib/model/converters/json_encodable.dart +++ /dev/null @@ -1,3 +0,0 @@ -abstract class JsonEncodable { - Map toJson(); -} diff --git a/packages/uni_app/lib/model/entities/floor_occupation.dart b/packages/uni_app/lib/model/entities/floor_occupation.dart index 578661a34..e331e47e5 100644 --- a/packages/uni_app/lib/model/entities/floor_occupation.dart +++ b/packages/uni_app/lib/model/entities/floor_occupation.dart @@ -1,4 +1,3 @@ - import 'dart:math'; import 'package:json_annotation/json_annotation.dart'; @@ -27,4 +26,4 @@ class FloorOccupation { } Map toJson() => _$FloorOccupationToJson(this); -} \ No newline at end of file +} diff --git a/packages/uni_app/lib/model/entities/meal.dart b/packages/uni_app/lib/model/entities/meal.dart index fb183b979..279a82284 100644 --- a/packages/uni_app/lib/model/entities/meal.dart +++ b/packages/uni_app/lib/model/entities/meal.dart @@ -1,4 +1,5 @@ import 'package:json_annotation/json_annotation.dart'; +import 'package:objectbox/objectbox.dart'; import 'package:uni/model/entities/reference.dart'; import 'package:uni/model/utils/day_of_week.dart'; @@ -6,14 +7,31 @@ part '../../generated/model/entities/meal.g.dart'; @DateTimeConverter() @JsonSerializable() +@Entity() class Meal { - Meal(this.type, this.namePt, this.nameEn, this.dayOfWeek, this.date); + Meal( + this.type, + this.namePt, + this.nameEn, + this.date, { + int? dbDayOfWeek, + }) : dayOfWeek = DayOfWeek.values[dbDayOfWeek ?? 0]; factory Meal.fromJson(Map json) => _$MealFromJson(json); + + @Id() + int? id; final String type; final String namePt; final String nameEn; - final DayOfWeek dayOfWeek; + @JsonKey(includeFromJson: false, includeToJson: false) + late DayOfWeek dayOfWeek; + + int get dbDayOfWeek => dayOfWeek.index; + set dbDayOfWeek(int? value) { + dayOfWeek = DayOfWeek.values[value ?? 0]; + } + final DateTime date; Map toJson() => _$MealToJson(this); diff --git a/packages/uni_app/lib/model/entities/restaurant.dart b/packages/uni_app/lib/model/entities/restaurant.dart index 6090c52fe..98ff53b6d 100644 --- a/packages/uni_app/lib/model/entities/restaurant.dart +++ b/packages/uni_app/lib/model/entities/restaurant.dart @@ -1,29 +1,35 @@ import 'package:collection/collection.dart'; import 'package:json_annotation/json_annotation.dart'; +import 'package:objectbox/objectbox.dart'; import 'package:uni/model/entities/meal.dart'; import 'package:uni/model/utils/day_of_week.dart'; part '../../generated/model/entities/restaurant.g.dart'; @JsonSerializable() +@Entity() class Restaurant { Restaurant( this.id, this.namePt, this.nameEn, this.period, - this.reference, { - required List meals, - }) : meals = groupBy(meals, (meal) => meal.dayOfWeek); + this.reference, + List? meals, + ) : meals = ToMany(items: meals ?? []); factory Restaurant.fromMap(Map map, List meals) { final object = Restaurant.fromJson(map); - object.meals = object.groupMealsByDayOfWeek(meals); + object.meals.addAll(meals); return object; } factory Restaurant.fromJson(Map json) => _$RestaurantFromJson(json); + + @Id() + int? uniqueId; + @JsonKey(name: 'id') final int? id; @JsonKey(name: 'namePt') @@ -34,8 +40,8 @@ class Restaurant { final String period; @JsonKey(name: 'ref') final String reference; // Used only in html parser - @JsonKey(includeToJson: true) - late final Map> meals; + @_MealRelToManyConverter() + final ToMany meals; bool get isNotEmpty { return meals.isNotEmpty; @@ -44,10 +50,24 @@ class Restaurant { Map toJson() => _$RestaurantToJson(this); List getMealsOfDay(DayOfWeek dayOfWeek) { - return meals[dayOfWeek] ?? []; + return groupMealsByDayOfWeek()[dayOfWeek] ?? []; } - Map> groupMealsByDayOfWeek(List meals) { + Map> groupMealsByDayOfWeek() { return groupBy(meals, (meal) => meal.dayOfWeek); } } + +class _MealRelToManyConverter + implements JsonConverter, List>?> { + const _MealRelToManyConverter(); + + @override + ToMany fromJson(List>? json) => ToMany( + items: json?.map(Meal.fromJson).toList(), + ); + + @override + List>? toJson(ToMany rel) => + rel.map((obj) => obj.toJson()).toList(); +} 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 a422bfba6..eb83241e5 100644 --- a/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart +++ b/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart @@ -1,7 +1,7 @@ import 'dart:async'; import 'package:uni/controller/fetchers/restaurant_fetcher.dart'; -import 'package:uni/controller/local_storage/database/app_restaurant_database.dart'; +import 'package:uni/controller/local_storage/database-nosql/restaurants_database.dart'; import 'package:uni/model/entities/restaurant.dart'; import 'package:uni/model/providers/state_provider_notifier.dart'; import 'package:uni/model/providers/state_providers.dart'; @@ -13,11 +13,7 @@ class RestaurantProvider extends StateProviderNotifier> { Future> loadFromStorage( StateProviders stateProviders, ) async { - // TODO: remove this line after PR #1380 (fix: Added meals column to the RESTAURANTS table) is merged - return loadFromRemote(stateProviders); - // final restaurantDb = RestaurantDatabase(); - // final restaurants = await restaurantDb.getRestaurants(); - // return restaurants; + return RestaurantsDatabase().getAll(); } @override @@ -25,7 +21,7 @@ class RestaurantProvider extends StateProviderNotifier> { final session = stateProviders.sessionProvider.state!; final restaurants = await RestaurantFetcher().getRestaurants(session); - final db = RestaurantDatabase(); + final db = RestaurantsDatabase(); unawaited(db.saveIfPersistentSession(restaurants)); return restaurants; diff --git a/packages/uni_app/lib/objectbox-model.json b/packages/uni_app/lib/objectbox-model.json index f24d5d0f1..74b73c6bd 100644 --- a/packages/uni_app/lib/objectbox-model.json +++ b/packages/uni_app/lib/objectbox-model.json @@ -359,17 +359,109 @@ } ], "relations": [] + }, + { + "id": "9:8593806623526644439", + "lastPropertyId": "6:1619262703215131232", + "name": "Restaurant", + "properties": [ + { + "id": "1:8412443093606687605", + "name": "id", + "type": 6 + }, + { + "id": "2:2346213477361850697", + "name": "namePt", + "type": 9 + }, + { + "id": "3:5477430405475543381", + "name": "nameEn", + "type": 9 + }, + { + "id": "4:276897755102017976", + "name": "period", + "type": 9 + }, + { + "id": "5:5740732395773374659", + "name": "reference", + "type": 9 + }, + { + "id": "6:1619262703215131232", + "name": "uniqueId", + "type": 6, + "flags": 1 + } + ], + "relations": [ + { + "id": "1:4232741166114493489", + "name": "meals", + "targetId": "10:7804684898880346326" + } + ] + }, + { + "id": "10:7804684898880346326", + "lastPropertyId": "10:8226375929086131956", + "name": "Meal", + "properties": [ + { + "id": "1:2217941936139097442", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:4514448927584280007", + "name": "type", + "type": 9 + }, + { + "id": "3:6516233270701917997", + "name": "namePt", + "type": 9 + }, + { + "id": "4:7870030775251025092", + "name": "nameEn", + "type": 9 + }, + { + "id": "5:9026414350313814339", + "name": "date", + "type": 10 + }, + { + "id": "10:8226375929086131956", + "name": "dbDayOfWeek", + "type": 6 + } + ], + "relations": [] } ], - "lastEntityId": "8:4515966536170961118", - "lastIndexId": "0:0", - "lastRelationId": "0:0", + "lastEntityId": "10:7804684898880346326", + "lastIndexId": "2:5553263973871522574", + "lastRelationId": "1:4232741166114493489", "lastSequenceId": "0:0", "modelVersion": 5, "modelVersionParserMinimum": 5, "retiredEntityUids": [], - "retiredIndexUids": [], - "retiredPropertyUids": [], + "retiredIndexUids": [ + 6025187928432397390, + 5553263973871522574 + ], + "retiredPropertyUids": [ + 7614762063660142831, + 1965207517914420813, + 5763920349665887316, + 3569335303354936661 + ], "retiredRelationUids": [], "version": 1 } \ No newline at end of file diff --git a/packages/uni_app/lib/objectbox.g.dart b/packages/uni_app/lib/objectbox.g.dart index 3a87a7677..524d6d2f3 100644 --- a/packages/uni_app/lib/objectbox.g.dart +++ b/packages/uni_app/lib/objectbox.g.dart @@ -20,8 +20,10 @@ import 'model/entities/course_units/course_unit.dart'; import 'model/entities/exam.dart'; import 'model/entities/floor_occupation.dart'; import 'model/entities/lecture.dart'; +import 'model/entities/meal.dart'; import 'model/entities/profile.dart'; import 'model/entities/reference.dart'; +import 'model/entities/restaurant.dart'; export 'package:objectbox/objectbox.dart'; // so that callers only have to import this file @@ -382,6 +384,89 @@ final _entities = [ flags: 0) ], relations: [], + backlinks: []), + obx_int.ModelEntity( + id: const obx_int.IdUid(9, 8593806623526644439), + name: 'Restaurant', + lastPropertyId: const obx_int.IdUid(6, 1619262703215131232), + flags: 0, + properties: [ + obx_int.ModelProperty( + id: const obx_int.IdUid(1, 8412443093606687605), + name: 'id', + type: 6, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(2, 2346213477361850697), + name: 'namePt', + type: 9, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(3, 5477430405475543381), + name: 'nameEn', + type: 9, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(4, 276897755102017976), + name: 'period', + type: 9, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(5, 5740732395773374659), + name: 'reference', + type: 9, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(6, 1619262703215131232), + name: 'uniqueId', + type: 6, + flags: 1) + ], + relations: [ + obx_int.ModelRelation( + id: const obx_int.IdUid(1, 4232741166114493489), + name: 'meals', + targetId: const obx_int.IdUid(10, 7804684898880346326)) + ], + backlinks: []), + obx_int.ModelEntity( + id: const obx_int.IdUid(10, 7804684898880346326), + name: 'Meal', + lastPropertyId: const obx_int.IdUid(10, 8226375929086131956), + flags: 0, + properties: [ + obx_int.ModelProperty( + id: const obx_int.IdUid(1, 2217941936139097442), + name: 'id', + type: 6, + flags: 1), + obx_int.ModelProperty( + id: const obx_int.IdUid(2, 4514448927584280007), + name: 'type', + type: 9, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(3, 6516233270701917997), + name: 'namePt', + type: 9, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(4, 7870030775251025092), + name: 'nameEn', + type: 9, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(5, 9026414350313814339), + name: 'date', + type: 10, + flags: 0), + obx_int.ModelProperty( + id: const obx_int.IdUid(10, 8226375929086131956), + name: 'dbDayOfWeek', + type: 6, + flags: 0) + ], + relations: [], backlinks: []) ]; @@ -420,13 +505,18 @@ Future openStore( obx_int.ModelDefinition getObjectBoxModel() { final model = obx_int.ModelInfo( entities: _entities, - lastEntityId: const obx_int.IdUid(8, 4515966536170961118), - lastIndexId: const obx_int.IdUid(0, 0), - lastRelationId: const obx_int.IdUid(0, 0), + lastEntityId: const obx_int.IdUid(10, 7804684898880346326), + lastIndexId: const obx_int.IdUid(2, 5553263973871522574), + lastRelationId: const obx_int.IdUid(1, 4232741166114493489), lastSequenceId: const obx_int.IdUid(0, 0), retiredEntityUids: const [], - retiredIndexUids: const [], - retiredPropertyUids: const [], + retiredIndexUids: const [6025187928432397390, 5553263973871522574], + retiredPropertyUids: const [ + 7614762063660142831, + 1965207517914420813, + 5763920349665887316, + 3569335303354936661 + ], retiredRelationUids: const [], modelVersion: 5, modelVersionParserMinimum: 5, @@ -881,6 +971,96 @@ obx_int.ModelDefinition getObjectBoxModel() { ..id = const fb.Int64Reader().vTableGetNullable(buffer, rootOffset, 4); + return object; + }), + Restaurant: obx_int.EntityDefinition( + model: _entities[8], + toOneRelations: (Restaurant object) => [], + toManyRelations: (Restaurant object) => { + obx_int.RelInfo.toMany(1, object.uniqueId!): + object.meals + }, + getId: (Restaurant object) => object.uniqueId, + setId: (Restaurant object, int id) { + object.uniqueId = id; + }, + objectToFB: (Restaurant object, fb.Builder fbb) { + final namePtOffset = fbb.writeString(object.namePt); + final nameEnOffset = fbb.writeString(object.nameEn); + final periodOffset = fbb.writeString(object.period); + final referenceOffset = fbb.writeString(object.reference); + fbb.startTable(7); + fbb.addInt64(0, object.id); + fbb.addOffset(1, namePtOffset); + fbb.addOffset(2, nameEnOffset); + fbb.addOffset(3, periodOffset); + fbb.addOffset(4, referenceOffset); + fbb.addInt64(5, object.uniqueId ?? 0); + fbb.finish(fbb.endTable()); + return object.uniqueId ?? 0; + }, + objectFromFB: (obx.Store store, ByteData fbData) { + final buffer = fb.BufferContext(fbData); + final rootOffset = buffer.derefObject(0); + final idParam = + const fb.Int64Reader().vTableGetNullable(buffer, rootOffset, 4); + final namePtParam = const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 6, ''); + final nameEnParam = const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 8, ''); + final periodParam = const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 10, ''); + final referenceParam = const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 12, ''); + final mealsParam = obx.ToMany(); + final object = Restaurant(idParam, namePtParam, nameEnParam, + periodParam, referenceParam, mealsParam) + ..uniqueId = const fb.Int64Reader() + .vTableGetNullable(buffer, rootOffset, 14); + obx_int.InternalToManyAccess.setRelInfo(object.meals, + store, obx_int.RelInfo.toMany(1, object.uniqueId!)); + return object; + }), + Meal: obx_int.EntityDefinition( + model: _entities[9], + toOneRelations: (Meal object) => [], + toManyRelations: (Meal object) => {}, + getId: (Meal object) => object.id, + setId: (Meal object, int id) { + object.id = id; + }, + objectToFB: (Meal object, fb.Builder fbb) { + final typeOffset = fbb.writeString(object.type); + final namePtOffset = fbb.writeString(object.namePt); + final nameEnOffset = fbb.writeString(object.nameEn); + fbb.startTable(11); + fbb.addInt64(0, object.id ?? 0); + fbb.addOffset(1, typeOffset); + fbb.addOffset(2, namePtOffset); + fbb.addOffset(3, nameEnOffset); + fbb.addInt64(4, object.date.millisecondsSinceEpoch); + fbb.addInt64(9, object.dbDayOfWeek); + fbb.finish(fbb.endTable()); + return object.id ?? 0; + }, + objectFromFB: (obx.Store store, ByteData fbData) { + final buffer = fb.BufferContext(fbData); + final rootOffset = buffer.derefObject(0); + final typeParam = const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 6, ''); + final namePtParam = const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 8, ''); + final nameEnParam = const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 10, ''); + final dateParam = DateTime.fromMillisecondsSinceEpoch( + const fb.Int64Reader().vTableGet(buffer, rootOffset, 12, 0)); + final dbDayOfWeekParam = + const fb.Int64Reader().vTableGet(buffer, rootOffset, 22, 0); + final object = Meal(typeParam, namePtParam, nameEnParam, dateParam, + dbDayOfWeek: dbDayOfWeekParam) + ..id = + const fb.Int64Reader().vTableGetNullable(buffer, rootOffset, 4); + return object; }) }; @@ -1136,3 +1316,58 @@ class Profile_ { static final feesLimit = obx.QueryDateProperty(_entities[7].properties[5]); } + +/// [Restaurant] entity fields to define ObjectBox queries. +class Restaurant_ { + /// See [Restaurant.id]. + static final id = + obx.QueryIntegerProperty(_entities[8].properties[0]); + + /// See [Restaurant.namePt]. + static final namePt = + obx.QueryStringProperty(_entities[8].properties[1]); + + /// See [Restaurant.nameEn]. + static final nameEn = + obx.QueryStringProperty(_entities[8].properties[2]); + + /// See [Restaurant.period]. + static final period = + obx.QueryStringProperty(_entities[8].properties[3]); + + /// See [Restaurant.reference]. + static final reference = + obx.QueryStringProperty(_entities[8].properties[4]); + + /// See [Restaurant.uniqueId]. + static final uniqueId = + obx.QueryIntegerProperty(_entities[8].properties[5]); + + /// see [Restaurant.meals] + static final meals = + obx.QueryRelationToMany(_entities[8].relations[0]); +} + +/// [Meal] entity fields to define ObjectBox queries. +class Meal_ { + /// See [Meal.id]. + static final id = obx.QueryIntegerProperty(_entities[9].properties[0]); + + /// See [Meal.type]. + static final type = obx.QueryStringProperty(_entities[9].properties[1]); + + /// See [Meal.namePt]. + static final namePt = + obx.QueryStringProperty(_entities[9].properties[2]); + + /// See [Meal.nameEn]. + static final nameEn = + obx.QueryStringProperty(_entities[9].properties[3]); + + /// See [Meal.date]. + static final date = obx.QueryDateProperty(_entities[9].properties[4]); + + /// See [Meal.dbDayOfWeek]. + static final dbDayOfWeek = + obx.QueryIntegerProperty(_entities[9].properties[5]); +} diff --git a/packages/uni_app/lib/view/home/widgets/restaurant_card.dart b/packages/uni_app/lib/view/home/widgets/restaurant_card.dart index c9ce793c7..1d7012aaa 100644 --- a/packages/uni_app/lib/view/home/widgets/restaurant_card.dart +++ b/packages/uni_app/lib/view/home/widgets/restaurant_card.dart @@ -81,7 +81,7 @@ class RestaurantCard extends GenericCard { final offset = (weekDay - 1) % 7; if (restaurants - .map((e) => e.meals[DayOfWeek.values[offset]]) + .map((e) => e.groupMealsByDayOfWeek()[DayOfWeek.values[offset]]) .every((element) => element?.isEmpty ?? true)) { return Column( children: [ diff --git a/packages/uni_app/lib/view/restaurant/restaurant_page_view.dart b/packages/uni_app/lib/view/restaurant/restaurant_page_view.dart index bc4dd0f43..2091c165e 100644 --- a/packages/uni_app/lib/view/restaurant/restaurant_page_view.dart +++ b/packages/uni_app/lib/view/restaurant/restaurant_page_view.dart @@ -77,7 +77,10 @@ class _RestaurantPageViewState extends GeneralPageViewState ) { final dayContents = DayOfWeek.values.map((dayOfWeek) { final restaurantsWidgets = restaurants - .where((element) => element.meals[dayOfWeek]?.isNotEmpty ?? false) + .where( + (element) => + element.groupMealsByDayOfWeek()[dayOfWeek]?.isNotEmpty ?? false, + ) .map( (restaurant) => createRestaurant(context, restaurant, dayOfWeek), )