8000 Buat fitur Add/Remove data ke favorite meal di UI List Meal · CoderJava/Food-Recipe@60f2536 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60f2536

Browse files
committed
Buat fitur Add/Remove data ke favorite meal di UI List Meal
1 parent 32477cd commit 60f2536

File tree

15 files changed

+568
-104
lines changed

15 files changed

+568
-104
lines changed

ios/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ PODS:
33
- FMDB (2.7.5):
44
- FMDB/standard (= 2.7.5)
55
- FMDB/standard (2.7.5)
6+
- path_provider (0.0.1):
7+
- Flutter
68
- sqflite (0.0.1):
79
- Flutter
810
- FMDB (~> 2.7.2)
@@ -11,6 +13,7 @@ PODS:
1113

1214
DEPENDENCIES:
1315
- Flutter (from `.symlinks/flutter/ios`)
16+
- path_provider (from `.symlinks/plugins/path_provider/ios`)
1417
- sqflite (from `.symlinks/plugins/sqflite/ios`)
1518
- url_launcher (from `.symlinks/plugins/url_launcher/ios`)
1619

@@ -21,6 +24,8 @@ SPEC REPOS:
2124
EXTERNAL SOURCES:
2225
Flutter:
2326
:path: ".symlinks/flutter/ios"
27+
path_provider:
28+
:path: ".symlinks/plugins/path_provider/ios"
2429
sqflite:
2530
:path: ".symlinks/plugins/sqflite/ios"
2631
url_launcher:
@@ -29,6 +34,7 @@ EXTERNAL SOURCES:
A93C 2934
SPEC CHECKSUMS:
3035
Flutter: 9d0fac939486c9aba2809b7982dfdbb47a7b0296
3136
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
37+
path_provider: 09407919825bfe3c2deae39453b7a5b44f467873
3238
sqflite: d1612813fa7db7c667bed9f1d1b508deffc56999
3339
url_launcher: 92b89c1029a0373879933c21642958c874539095
3440

ios/Runner.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
225225
"${BUILT_PRODUCTS_DIR}/FMDB/FMDB.framework",
226226
"${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework",
227+
"${BUILT_PRODUCTS_DIR}/path_provider/path_provider.framework",
227228
"${BUILT_PRODUCTS_DIR}/sqflite/sqflite.framework",
228229
"${BUILT_PRODUCTS_DIR}/url_launcher/url_launcher.framework",
229230
);
@@ -233,6 +234,7 @@
233234
outputPaths = (
234235
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FMDB.framework",
235236
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
237+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/path_provider.framework",
236238
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/sqflite.framework",
237239
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/url_launcher.framework",
238240
);

lib/src/blocs/detailmeals/detail_meals_bloc.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import 'package:food_recipe/src/models/lookupmealsbyid/lookup_meals_by_id.dart';
2-
import 'package:food_recipe/src/resources/repository.dart';
2+
import 'package:food_recipe/src/resources/food_api_repository.dart';
33

44
class DetailsMealsBloc {
5-
final _repository = Repository();
5+
final _foodApiRepository = FoodApiRepository();
66

77
dispose() {
88
// TODO: do something in here
99
}
1010

1111
Future<LookupMealsById> getDetailsMealsById(String idMeal) async {
12-
return await _repository.getLookupMealsById(idMeal);
12+
return await _foodApiRepository.getLookupMealsById(idMeal);
1313
}
1414

1515
}

lib/src/blocs/home/home_bloc.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:food_recipe/src/models/categories/categories.dart';
22
import 'package:food_recipe/src/models/latest/latest_meals.dart';
3-
import 'package:food_recipe/src/resources/repository.dart';
3+
import 'package:food_recipe/src/resources/food_api_repository.dart';
44

55
class HomeBloc {
6-
final _repository = Repository();
6+
final _repository = FoodApiRepository();
77

88
Future<LatestMeals> getLatestMeals() async {
99
return await _repository.getLatestMeals();
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
1+
import 'package:food_recipe/src/database/entity/favorite_meal.dart';
2+
import 'package:food_recipe/src/database/repository/favorite_meal_repository.dart';
13
import 'package:food_recipe/src/models/filtercategories/filter_categories.dart';
2-
import 'package:food_recipe/src/resources/repository.dart';
4+
import 'package:food_recipe/src/models/lookupmealsbyid/lookup_meals_by_id.dart';
5+
import 'package:food_recipe/src/resources/food_api_repository.dart';
36

47
class ListMealsBloc {
5-
final _repository = Repository();
8+
final _foodApiRepository = FoodApiRepository();
9+
final _favoriteMealRepository = FavoriteMealRepository();
610

711
Future<FilterCategories> getFilterCategories(String category) async {
8-
return await _repository.getFilterByCategories(category);
12+
FilterCategories filterCategories =
13+
await _foodApiRepository.getFilterByCategories(category);
14+
List<FavoriteMeal> listFavoriteMeals =
15+
await _favoriteMealRepository.getAllFavoriteMeals();
16+
List<FilterCategoryItem> listFilterCategoryItems =
17+
filterCategories.filterCategoryItems.where((item) {
18+
bool isFavorite = false;
19+
for (FavoriteMeal favoriteMeal in listFavoriteMeals) {
20+
if (item.idMeal == favoriteMeal.idMeal) {
21+
isFavorite = true;
22+
break;
23+
}
24+
}
25+
item.isFavorite = isFavorite;
26+
return true;
27+
}).toList();
28+
filterCategories.filterCategoryItems = listFilterCategoryItems;
29+
return filterCategories;
30+
}
31+
32+
Future<LookupMealsById> getDetailMealById(String id) async {
33+
LookupMealsById lookupMealsById = await _foodApiRepository.getLookupMealsById(id);
34+
return lookupMealsById;
35+
}
36+
37+
Future<int> addFavoriteMeal(FavoriteMeal favoriteMeal) async {
38+
return await _favoriteMealRepository.insertFavoriteMeal(favoriteMeal);
39+
}
40+
41+
Future<int> deleteFavoriteMealById(String id) async {
42+
return await _favoriteMealRepository.deleteFavoriteMealById(id);
943
}
1044

1145
dispose() {
1246
// TODO: do something in here
1347
}
14-
1548
}
1649

17-
final listMealsBloc = ListMealsBloc();
50+
final listMealsBloc = ListMealsBloc();

lib/src/blocs/searchmeals/search_meals_bloc.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:food_recipe/src/models/searchmeals/search_meals.dart';
2-
import 'package:food_recipe/src/resources/repository.dart';
2+
import 'package:food_recipe/src/resources/food_api_repository.dart';
33
import 'package:rxdart/rxdart.dart';
44

55
class SearchMealsBloc {
6-
final _repository = Repository();
6+
final _repository = FoodApiRepository();
77
final _publishSubjectSearchMealsByKeyword = PublishSubject<SearchMeals>();
88

99
dispose() {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'package:food_recipe/src/database/entity/favorite_meal.dart';
2+
3+
import '../database.dart';
4+
5+
class FavoriteMealDao {
6+
final dbProvider = DatabaseProvider.dbProvider;
7+
8+
Future<int> createFavoriteMeal(FavoriteMeal favoriteMeal) async {
9+
final db = await dbProvider.database;
10+
var result = db.insert(favoriteTable, favoriteMeal.toJson());
11+
return result;
12+
}
13+
14+
Future<List<FavoriteMeal>> getAllFavoriteMeals() async {
15+
final db = await dbProvider.database;
16+
List<Map<String, dynamic>> result;
17+
result = await db.query(favoriteTable);
18+
List<FavoriteMeal> listFavoriteMeals = result.isNotEmpty
19+
? result.map((resultMap) {
20+
return FavoriteMeal.fromJson(resultMap);
21+
}).toList()
22+
: [];
23+
return listFavoriteMeals;
24+
}
25+
26+
Future<List<FavoriteMeal>> getFavoriteMealById(String id) async {
27+
final db = await dbProvider.database;
28+
List<Map<String, dynamic>> result;
29+
result = await db.query(favoriteTable, where: "idMeal = ?", whereArgs: [id]);
30+
List<FavoriteMeal> listFavoriteMeals = result.isNotEmpty
31+
? result.map((resultMap) {
32+
return FavoriteMeal.fromJson(resultMap);
33+
}).toList()
34+
: [];
35+
return listFavoriteMeals;
36+
}
37+
38+
Future<int> deleteFavoriteMeal(String id) async {
39+
final db = await dbProvider.database;
40+
var result =
41+
await db.delete(favoriteTable, where: "idMeal = ?", whereArgs: [id]);
42+
return result;
43+
}
44+
}

lib/src/database/database.dart

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import 'dart:io';
2+
3+
import 'package:path/path.dart';
4+
import 'package:sqflite/sqflite.dart';
5+
import 'package:path_provider/path_provider.dart';
6+
7+
final favoriteTable = "favorite";
8+
9+
class DatabaseProvider {
10+
static final DatabaseProvider dbProvider = DatabaseProvider();
11+
12+
Database _database;
13+
14+
Future<Database> get database async {
15+
if (_database != null) return _database;
16+
_database = await createDatabase();
17+
return _database;
18+
}
19+
20+
createDatabase() async {
21+
Directory documentsDirectory = await getApplicationDocumentsDirectory( 10000 );
22+
String path = join(documentsDirectory.path, "FoodRecipe.db");
23+
24+
var database = await openDatabase(
25+
path, version: 1, onCreate: initDb, onUpgrade: onUpgrade);
26+
return database;
27+
}
28+
29+
void onUpgrade(Database database, int oldVersion, int newVersion) {
30+
if (newVersion > oldVersion) {
31+
// TODO: do something in here if needed
32+
}
33+
}
34+
35+
void initDb(Database database, int version) async {
36+
String queryCreateTableFavorite = "CREATE TABLE $favoriteTable ("
37+
"idMeal TEXT PRIMARY KEY, "
38+
"strMeal TEXT, "
39+
"strMealThumb TEXT, "
40+
"strCategory TEXT, "
41+
"strTags TEXT, "
42+
"strYoutube TEXT, "
43+
"strArea TEXT, "
44+
"strIngredient1 TEXT, "
45+
"strIngredient2 TEXT, "
46+
"strIngredient3 TEXT, "
47+
"strIngredient4 TEXT, "
48+
"strIngredient5 TEXT, "
49+
"strIngredient6 TEXT, "
50+
"strIngredient7 TEXT, "
51+
"strIngredient8 TEXT, "
52+
"strIngredient9 TEXT, "
53+
"strIngredient10 TEXT, "
54+
"strIngredient11 TEXT, "
55+
"strIngredient12 TEXT, "
56+
"strIngredient13 TEXT, "
57+
"strIngredient14 TEXT, "
58+
"strIngredient15 TEXT, "
59+
"strIngredient16 TEXT, "
60+
"strIngredient17 TEXT, "
61+
"strIngredient18 TEXT, "
62+
"strIngredient19 TEXT, "
63+
"strIngredient20 TEXT, "
64+
"strMeasure1 TEXT, "
65+
"strMeasure2 TEXT, "
66+
"strMeasure3 TEXT, "
67+
"strMeasure4 TEXT, "
68+
"strMeasure5 TEXT, "
69+
"strMeasure6 TEXT, "
70+
"strMeasure7 TEXT, "
71+
"strMeasure8 TEXT, "
72+
"strMeasure9 TEXT, "
73+
"strMeasure10 TEXT, "
74+
"strMeasure11 TEXT, "
75+
"strMeasure12 TEXT, "
76+
"strMeasure13 TEXT, "
77+
"strMeasure14 TEXT, "
78+
"strMeasure15 TEXT, "
79+
"strMeasure16 TEXT, "
80+
"strMeasure17 TEXT, "
81+
"strMeasure18 TEXT, "
82+
"strMeasure19 TEXT, "
83+
"strMeasure20 TEXT "
84+
")";
85+
await database.execute(queryCreateTableFavorite);
86+
}
87+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'favorite_meal.g.dart';
4+
5+
@JsonSerializable()
6+
class FavoriteMeal {
7+
String idMeal;
8+
String strMeal;
9+
String strMealThumb;
10+
String strCategory;
11+
String strTags;
12+
String strYoutube;
13+
String strArea;
14+
String strIngredient1;
15+
String strIngredient2;
16+
String strIngredient3;
17+
String strIngredient4;
18+
String strIngredient5;
19+
String strIngredient6;
20+
String strIngredient7;
21+
String strIngredient8;
22+
String strIngredient9;
23+
String strIngredient10;
24+
String strIngredient11;
25+
String strIngredient12;
26+
String strIngredient13;
27+
String strIngredient14;
28+
String strIngredient15;
29+
String strIngredient16;
30+
String strIngredient17;
31+
String strIngredient18;
32+
String strIngredient19;
33+
String strIngredient20;
34+
String strMeasure1;
35+
String strMeasure2;
36+
String strMeasure3;
37+
String strMeasure4;
38+
String strMeasure5;
39+
String strMeasure6;
40+
String strMeasure7;
41+
String strMeasure8;
42+
String strMeasure9;
43+
String strMeasure10;
44+
String strMeasure11;
45+
String strMeasure12;
46+
String strMeasure13;
47+
String strMeasure14;
48+
String strMeasure15;
49+
String strMeasure16;
50+
String strMeasure17;
51+
String strMeasure18;
52+
String strMeasure19;
53+
String strMeasure20;
54+
55+
FavoriteMeal(
56+
{this.idMeal,
57+
this.strMeal,
58+
this.strMealThumb,
59+
this.strCategory,
60+
this.strTags,
61+
this.strYoutube,
62+
this.strArea,
63+
this.strIngredient1,
64+
this.strIngredient2,
65+
this.strIngredient3,
66+
this.strIngredient4,
67+
this.strIngredient5,
68+
this.strIngredient6,
69+
this.strIngredient7,
70+
this.strIngredient8,
71+
this.strIngredient9,
72+
this.strIngredient10,
73+
this.strIngredient11,
74+
this.strIngredient12,
75+
this.strIngredient13,
76+
this.strIngredient14,
77+
this.strIngredient15,
78+
this.strIngredient16,
79+
this.strIngredient17,
80+
this.strIngredient18,
81+
this.strIngredient19,
82+
this.strIngredient20,
83+
this.strMeasure1,
84+
this.strMeasure2,
85+
this.strMeasure3,
86+
this.strMeasure4,
87+
this.strMeasure5,
88+
this.strMeasure6,
89+
this.strMeasure7,
90+
this.strMeasure8,
91+
this.strMeasure9,
92+
this.strMeasure10,
93+
this.strMeasure11,
94+
this.strMeasure12,
95+
this.strMeasure13,
96+
this.strMeasure14,
97+
this.strMeasure15,
98+
this.strMeasure16,
99+
this.strMeasure17,
100+
this.strMeasure18,
101+
this.strMeasure19,
102+
this.strMeasure20});
103+
104+
@override
105+
String toString() {
106+
return 'FavoriteMeal{id: $idMeal, strMeal: $strMeal, strMealThumb: $strMealThumb, strCategory: $strCategory, strTags: $strTags, strYoutube: $strYoutube, strArea: $strArea, strIngredient1: $strIngredient1, strIngredient2: $strIngredient2, strIngredient3: $strIngredient3, strIngredient4: $strIngredient4, strIngredient5: $strIngredient5, strIngredient6: $strIngredient6, strIngredient7: $strIngredient7, strIngredient8: $strIngredient8, strIngredient9: $strIngredient9, strIngredient10: $strIngredient10, strIngredient11: $strIngredient11, strIngredient12: $strIngredient12, strIngredient13: $strIngredient13, strIngredient14: $strIngredient14, strIngredient15: $strIngredient15, strIngredient16: $strIngredient16, strIngredient17: $strIngredient17, strIngredient18: $strIngredient18, strIngredient19: $strIngredient19, strIngredient20: $strIngredient20, strMeasure1: $strMeasure1, strMeasure2: $strMeasure2, strMeasure3: $strMeasure3, strMeasure4: $strMeasure4, strMeasure5: $strMeasure5, strMeasure6: $strMeasure6, strMeasure7: $strMeasure7, strMeasure8: $strMeasure8, strMeasure9: $strMeasure9, strMeasure10: $strMeasure10, strMeasure11: $strMeasure11, strMeasure12: $strMeasure12, strMeasure13: $strMeasure13, strMeasure14: $strMeasure14, strMeasure15: $strMeasure15, strMeasure16: $strMeasure16, strMeasure17: $strMeasure17, strMeasure18: $strMeasure18, strMeasure19: $strMeasure19, strMeasure20: $strMeasure20}';
107+
}
108+
109+
factory FavoriteMeal.fromJson(Map<String, dynamic> json) =>
110+
_$FavoriteMealFromJson(json);
111+
112+
Map<String, dynamic> toJson() => _$FavoriteMealToJson(this);
113+
}

0 commit comments

Comments
 (0)
0