-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38a712e
commit 316c35d
Showing
6 changed files
with
98 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 40 additions & 35 deletions
75
lib/features/sks-menu/data/repository/sks_menu_repository.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,52 @@ | ||
import "package:flutter_riverpod/flutter_riverpod.dart"; | ||
import "package:fast_immutable_collections/fast_immutable_collections.dart"; | ||
import "package:riverpod_annotation/riverpod_annotation.dart"; | ||
|
||
import "../../../../api_base_rest/cache/cache.dart"; | ||
import "../../../../config/env.dart"; | ||
import "../../../../utils/context_extensions.dart"; | ||
import "../../../../utils/datetime_utils.dart"; | ||
import "../../presentation/sks_menu_screen.dart"; | ||
import "../models/dish_category_enum.dart"; | ||
import "../models/sks_menu_response.dart"; | ||
|
||
part "sks_menu_repository.g.dart"; | ||
|
||
@riverpod | ||
Future<ExtendedSksMenuResponse> getSksMenuData(Ref ref) async { | ||
final mealsUrl = "${Env.sksUrl}/meals/current"; | ||
const ttlDays = 1; | ||
|
||
final sksMenuResponse = await ref.getAndCacheData( | ||
mealsUrl, | ||
ttlDays, | ||
SksMenuResponse.fromJson, | ||
extraValidityCheck: (data) => | ||
data.isMenuOnline && | ||
DateTime.now().date.isSameDay(data.lastUpdate.date), | ||
localizedOfflineMessage: (context) => | ||
context.localize.my_offline_error_message( | ||
context.localize.sks_menu, | ||
), | ||
onRetry: () => ref.invalidateSelf(), | ||
); | ||
|
||
final trueMeals = sksMenuResponse.meals | ||
.where((e) => e.category != DishCategory.technicalInfo) | ||
.toList(); | ||
|
||
final technicalInfos = sksMenuResponse.meals | ||
.where((e) => e.category == DishCategory.technicalInfo) | ||
.map((e) => e.name) | ||
.toList(); | ||
|
||
return ExtendedSksMenuResponse( | ||
isMenuOnline: sksMenuResponse.isMenuOnline, | ||
lastUpdate: sksMenuResponse.lastUpdate, | ||
meals: trueMeals, | ||
technicalInfos: technicalInfos, | ||
); | ||
class SksMenuRepository extends _$SksMenuRepository { | ||
static final _mealsUrl = "${Env.sksUrl}/meals/current"; | ||
static const _ttlDays = 1; | ||
|
||
Future<void> clearCache() async { | ||
return ref.clearCache(_mealsUrl, _ttlDays); | ||
} | ||
|
||
@override | ||
Future<ExtendedSksMenuResponse> build() async { | ||
final sksMenuResponse = await ref.getAndCacheData( | ||
_mealsUrl, | ||
_ttlDays, | ||
SksMenuResponse.fromJson, | ||
extraValidityCheck: (data) { | ||
return data.isMenuOnline && | ||
DateTime.now().date.isSameDay(data.lastUpdate.date); | ||
}, | ||
localizedOfflineMessage: SksMenuView.localizedOfflineMessage, | ||
onRetry: () => ref.invalidateSelf(), | ||
); | ||
|
||
final trueMeals = sksMenuResponse.meals | ||
.where((e) => e.category != DishCategory.technicalInfo) | ||
.toIList(); | ||
|
||
final technicalInfos = sksMenuResponse.meals | ||
.where((e) => e.category == DishCategory.technicalInfo) | ||
.map((e) => e.name) | ||
.toIList(); | ||
|
||
return ExtendedSksMenuResponse( | ||
isMenuOnline: sksMenuResponse.isMenuOnline, | ||
lastUpdate: sksMenuResponse.lastUpdate, | ||
meals: trueMeals, | ||
technicalInfos: technicalInfos, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters