Skip to content

Commit

Permalink
[#25] feat: add cached repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Soogyo-In committed Jan 3, 2023
1 parent 983dc0c commit dd20279
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
10 changes: 10 additions & 0 deletions data/lib/provider/provider.dart
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()),
);
56 changes: 56 additions & 0 deletions data/lib/repository/cached_ingredient_repository.dart
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;
}
}
51 changes: 51 additions & 0 deletions data/lib/repository/cached_recipe_repository.dart
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;
}
}
8 changes: 8 additions & 0 deletions data/lib/repository/repository.dart
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';
1 change: 1 addition & 0 deletions domain/lib/domain.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'model/model.dart';
export 'repository/repository.dart';
13 changes: 13 additions & 0 deletions domain/lib/repository/ingredient_repository.dart
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);
}
13 changes: 13 additions & 0 deletions domain/lib/repository/recipe_repository.dart
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);
}
4 changes: 4 additions & 0 deletions domain/lib/repository/repository.dart
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';

0 comments on commit dd20279

Please sign in to comment.