-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
8 changed files
with
156 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:data/data.dart'; | ||
import 'package:riverpod/riverpod.dart'; | ||
|
||
final cachedRecipeRepositoryProvider = StateNotifierProvider( | ||
(ref) => CachedRecipeRepository(RecipeLocalDatasource()), | ||
); | ||
|
||
final cachedIngredientRepositoryProvider = StateNotifierProvider( | ||
(ref) => CachedIngredientRepository(RecipeLocalDatasource()), | ||
); |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
part of 'repository.dart'; | ||
|
||
class CachedIngredientRepository | ||
extends StateNotifier<Map<int, IndexedIngredient>> | ||
implements IngredientRepository { | ||
final RecipeDatasource _recipeDataSource; | ||
|
||
CachedIngredientRepository(RecipeDatasource recipeDataSource) | ||
: _recipeDataSource = recipeDataSource, | ||
super({}); | ||
|
||
@override | ||
Future<IndexedIngredient> addIngredient(Ingredient ingredient) async { | ||
final indexedIngredient = await _recipeDataSource.addIngredient(ingredient); | ||
state[indexedIngredient.id] = indexedIngredient; | ||
|
||
return indexedIngredient; | ||
} | ||
|
||
@override | ||
Future<void> deleteIngredient(int id) async { | ||
await _recipeDataSource.deleteIngredient(id); | ||
state.remove(id); | ||
} | ||
|
||
@override | ||
Future<List<IndexedIngredient>> getAllIngredients() async { | ||
if (state.isNotEmpty) return state.values.toList(); | ||
|
||
final ingredients = await _recipeDataSource.getAllIngredients(); | ||
state = {for (final ingredient in ingredients) ingredient.id: ingredient}; | ||
|
||
return ingredients; | ||
} | ||
|
||
@override | ||
Future<IndexedIngredient> getIngredient(int id) async { | ||
final ingredient = state[id] ?? await _recipeDataSource.getIngredient(id); | ||
|
||
state.putIfAbsent(id, () => ingredient); | ||
|
||
return ingredient; | ||
} | ||
|
||
@override | ||
Future<IndexedIngredient> updateIngredient( | ||
IndexedIngredient ingredient, | ||
) async { | ||
final updatedIngredient = await _recipeDataSource.updateIngredient( | ||
ingredient, | ||
); | ||
state[updatedIngredient.id] = updatedIngredient; | ||
|
||
return updatedIngredient; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
part of 'repository.dart'; | ||
|
||
class CachedRecipeRepository extends StateNotifier<Map<int, IndexedRecipe>> | ||
implements RecipeRepository { | ||
final RecipeDatasource _recipeDataSource; | ||
|
||
CachedRecipeRepository(RecipeDatasource recipeDataSource) | ||
: _recipeDataSource = recipeDataSource, | ||
super({}); | ||
|
||
@override | ||
Future<IndexedRecipe> addRecipe(Recipe recipe) async { | ||
final indexedRecipe = await _recipeDataSource.addRecipe(recipe); | ||
state[indexedRecipe.id] = indexedRecipe; | ||
|
||
return indexedRecipe; | ||
} | ||
|
||
@override | ||
Future<void> deleteRecipe(int id) async { | ||
await _recipeDataSource.deleteRecipe(id); | ||
state.remove(id); | ||
} | ||
|
||
@override | ||
Future<List<IndexedRecipe>> getAllRecipes() async { | ||
if (state.isNotEmpty) return state.values.toList(); | ||
|
||
final recipes = await _recipeDataSource.getAllRecipes(); | ||
state = {for (final recipe in recipes) recipe.id: recipe}; | ||
|
||
return recipes; | ||
} | ||
|
||
@override | ||
Future<IndexedRecipe> getRecipe(int id) async { | ||
final recipe = state[id] ?? await _recipeDataSource.getRecipe(id); | ||
|
||
state.putIfAbsent(id, () => recipe); | ||
|
||
return recipe; | ||
} | ||
|
||
@override | ||
Future<IndexedRecipe> updateRecipe(IndexedRecipe recipe) async { | ||
final updatedRecipe = await _recipeDataSource.updateRecipe(recipe); | ||
state[updatedRecipe.id] = updatedRecipe; | ||
|
||
return updatedRecipe; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'dart:isolate'; | ||
|
||
import 'package:data/data.dart'; | ||
import 'package:domain/domain.dart'; | ||
import 'package:riverpod/riverpod.dart'; | ||
|
||
part 'cached_ingredient_repository.dart'; | ||
part 'cached_recipe_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 +1,2 @@ | ||
export 'model/model.dart'; | ||
export 'repository/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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
part of 'repository.dart'; | ||
|
||
abstract class IngredientRepository { | ||
Future<IndexedIngredient> addIngredient(Ingredient ingredient); | ||
|
||
Future<IndexedIngredient> getIngredient(int id); | ||
|
||
Future<List<IndexedIngredient>> getAllIngredients(); | ||
|
||
Future<IndexedIngredient> updateIngredient(IndexedIngredient ingredient); | ||
|
||
Future<void> deleteIngredient(int id); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
part of 'repository.dart'; | ||
|
||
abstract class RecipeRepository { | ||
Future<IndexedRecipe> addRecipe(Recipe recipe); | ||
|
||
Future<IndexedRecipe> getRecipe(int id); | ||
|
||
Future<List<IndexedRecipe>> getAllRecipes(); | ||
|
||
Future<IndexedRecipe> updateRecipe(IndexedRecipe recipe); | ||
|
||
Future<void> deleteRecipe(int id); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import 'package:domain/domain.dart'; | ||
|
||
part 'ingredient_repository.dart'; | ||
part 'recipe_repository.dart'; |