Skip to content

Commit

Permalink
improved startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKutschera committed Nov 6, 2023
1 parent 8d7b5f8 commit 6493abb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions app/lib/view_model/logic/meal/CombinedMealPlanAccess.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class CombinedMealPlanAccess extends ChangeNotifier implements IMealAccess {
final canteenString = _preferences.getCanteen();
Canteen? canteen;
// get default canteen from server if canteen id not saved in local storage
if (canteenString == null || canteenString.isEmpty || await _database.getCanteenById(canteenString) == null) {
if (canteenString == null ||
canteenString.isEmpty ||
await _database.getCanteenById(canteenString) == null) {
canteen = await _api.getDefaultCanteen();

// save canteen id in local storage
Expand Down Expand Up @@ -88,7 +90,10 @@ class CombinedMealPlanAccess extends ChangeNotifier implements IMealAccess {

// get meal plans form server
if (_mealPlans.isEmpty) {
await refreshAll();
print("loading initial day");
await refreshMealplan();
print("initial day loaded");
refreshAll();
} else {
await _setNewMealPlan();
refreshAll();
Expand All @@ -99,14 +104,15 @@ class CombinedMealPlanAccess extends ChangeNotifier implements IMealAccess {
List<MealPlan> mealPlans = switch (await _api.updateAll()) {
Success(value: final mealplan) => mealplan,
Failure(exception: final exception) =>
_convertMealPlanExceptionToMealPlan(exception)
_convertMealPlanExceptionToMealPlan(exception)
};

// update all if connection to server is successful
if (mealPlans.isNotEmpty) {
await _database.updateAll(mealPlans);
}
_mealPlans = switch (await _database.getMealPlan(_displayedDate, _activeCanteen)) {
_mealPlans = switch (
await _database.getMealPlan(_displayedDate, _activeCanteen)) {
Success(value: final mealplan) => mealplan,
Failure() => []
};
Expand Down Expand Up @@ -215,7 +221,6 @@ class CombinedMealPlanAccess extends ChangeNotifier implements IMealAccess {
@override
Future<String?> refreshMealplan() async {
DateTime requestingDate = _displayedDate.copyWith();
await _doneInitialization;

final mealPlan = await _getMealPlanFromServer();

Expand Down Expand Up @@ -363,11 +368,15 @@ class CombinedMealPlanAccess extends ChangeNotifier implements IMealAccess {
numberOfRatings += 1;

// change average rating
newRating = (changedMeal.averageRating * changedMeal.numberOfRatings +
rating) / numberOfRatings;
newRating =
(changedMeal.averageRating * changedMeal.numberOfRatings + rating) /
numberOfRatings;
} else {
// change average rating
newRating = (changedMeal.averageRating * changedMeal.numberOfRatings - changedMeal.individualRating + rating) / numberOfRatings;
newRating = (changedMeal.averageRating * changedMeal.numberOfRatings -
changedMeal.individualRating +
rating) /
numberOfRatings;
}

Meal newMeal = Meal.copy(
Expand Down Expand Up @@ -548,7 +557,8 @@ class CombinedMealPlanAccess extends ChangeNotifier implements IMealAccess {
}
}

final price = meal.price.getPrice(_preferences.getPriceCategory() ?? PriceCategory.student);
final price = meal.price
.getPrice(_preferences.getPriceCategory() ?? PriceCategory.student);

// check price
if (_filter.price < price) {
Expand Down
2 changes: 1 addition & 1 deletion app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.1+12
version: 1.0.2+13

environment:
sdk: '>=3.1.4'
Expand Down

0 comments on commit 6493abb

Please sign in to comment.