Skip to content

Commit

Permalink
Added an All Category
Browse files Browse the repository at this point in the history
  • Loading branch information
Teifun2 committed Sep 6, 2020
1 parent 49ec7f9 commit 3ac08ed
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/src/models/category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Category extends Equatable {
final int recipeCount;
String imageUrl;

Category(this.name, this.recipeCount);

Category.fromJson(Map<String, dynamic> json)
: name = json["name"],
recipeCount = json["recipe_count"] is int
Expand Down
15 changes: 13 additions & 2 deletions lib/src/services/categories_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ class CategoriesProvider {

if (response.statusCode == 200) {
try {
return Category.parseCategories(response.body)
..sort((a, b) => a.name.compareTo(b.name));
List<Category> categories = Category.parseCategories(response.body);
categories.sort((a, b) => a.name.compareTo(b.name));
categories.insert(
0,
Category(
"All",
categories.fold(
0,
(previousValue, element) =>
previousValue + element.recipeCount),
),
);
return categories;
} catch (e) {
throw Exception(e);
}
Expand Down
14 changes: 10 additions & 4 deletions lib/src/services/data_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class DataRepository {
static Future<List<RecipeShort>> _allRecipesShort;

// Actions
Future<List<RecipeShort>> fetchRecipesShort({String category = 'all'}) {
if (category == 'all') {
Future<List<RecipeShort>> fetchRecipesShort({String category = 'All'}) {
if (category == 'All') {
return recipesShortProvider.fetchRecipesShort();
} else {
return categoryRecipesShortProvider.fetchCategoryRecipesShort(category);
Expand All @@ -52,8 +52,14 @@ class DataRepository {
}

Future<Category> _fetchCategoryImage(Category category) async {
List<RecipeShort> categoryRecipes = await categoryRecipesShortProvider
.fetchCategoryRecipesShort(category.name);
List<RecipeShort> categoryRecipes = await () {
if (category.name == "All") {
return recipesShortProvider.fetchRecipesShort();
} else {
return categoryRecipesShortProvider
.fetchCategoryRecipesShort(category.name);
}
}();

category.imageUrl = categoryRecipes.first.imageUrl;

Expand Down

0 comments on commit 3ac08ed

Please sign in to comment.