content: import 'dart:core';
  
  import 'package:flutter/foundation.dart';
  
  import '../models/models.dart';
  import 'repository.dart';
  
  class MemoryRepository with ChangeNotifier implements Repository {
    final List<Recipe> _currentRecipes = <Recipe>[];
    final List<Ingredient> _currentIngredients = <Ingredient>[];
  
    @override
    List<Recipe> findAllRecipes() {
      return _currentRecipes;
    }
  
    @override
    Recipe findRecipeById(int id) {
      return _currentRecipes.firstWhere((recipe) => recipe.id == id);
    }
  
    @override
    List<Ingredient> findAllIngredients() {
      return _currentIngredients;
    }
  
    @override
    List<Ingredient> findRecipeIngredients(int recipeId) {
      final recipe =
          _currentRecipes.firstWhere((recipe) => recipe.id == recipeId);
      final recipeIngredients = _currentIngredients
          .where((ingredient) => ingredient.recipeId == recipe.id)
          .toList();
      return recipeIngredients;
    }
  
    @override
    int insertRecipe(Recipe recipe) {
      // TODO: Update insertRecipe()
      _currentRecipes.add(recipe);
      insertIngredients(recipe.ingredients);
      notifyListeners();
      return 0;
    }
  
    @override
    List<int> insertIngredients(List<Ingredient> ingredients) {
      if (ingredients.isNotEmpty) {
        // TODO: Update insertIngredients()
        _currentIngredients.addAll(ingredients);
      }
      notifyListeners();
      return <int>[];
    }
  
    @override
    void deleteRecipe(Recipe recipe) {
      // TODO: Update deleteRecipe()
      _currentRecipes.remove(recipe);
      if (recipe.id != null) {
        deleteRecipeIngredients(recipe.id!);
      }
      notifyListeners();
    }
  
    @override
    void deleteIngredient(Ingredient ingredient) {
      _currentIngredients.remove(ingredient);
      notifyListeners();
    }
  
    @override
    void deleteIngredients(List<Ingredient> ingredients) {
      _currentIngredients
          .removeWhere((ingredient) => ingredients.contains(ingredient));
      notifyListeners();
    }
  
    @override
    void deleteRecipeIngredients(int recipeId) {
      _currentIngredients
          .removeWhere((ingredient) => ingredient.recipeId == recipeId);
      notifyListeners();
    }
  
    @override
    Future init() {
      return Future.value(null);
    }
  
    @override
    void close() {}
  }
  