11import 'package:hive_flutter/hive_flutter.dart' ;
2- import 'package:wooapp/database/cart_cache_item.dart' ;
3- import 'package:wooapp/database/filter.dart' ;
4- import 'package:wooapp/database/filter_active.dart' ;
5- import 'package:wooapp/database/filter_value.dart' ;
6- import 'package:wooapp/database/product.dart' ;
7- import 'package:wooapp/database/user.dart' ;
2+ import 'package:wooapp/core/pair.dart' ;
3+ import 'package:wooapp/database/entity/cart_cache_item.dart' ;
4+ import 'package:wooapp/database/entity/filter.dart' ;
5+ import 'package:wooapp/database/entity/filter_active.dart' ;
6+ import 'package:wooapp/database/entity/filter_value.dart' ;
7+ import 'package:wooapp/database/entity/product.dart' ;
8+ import 'package:wooapp/database/entity/user.dart' ;
9+ import 'package:wooapp/database/entity/wish_list_cache_item.dart' ;
810import 'package:wooapp/model/attribute.dart' ;
911import 'package:wooapp/model/product.dart' ;
1012
13+ /// WooApp local database orchestrator.
14+ /// Docs: https://docs.hivedb.dev/
15+ ///
16+ /// To generate adapters, execute:
17+ /// flutter packages pub run build_runner build
1118class AppDb {
12-
1319 static const String boxUser = 'box_user' ;
1420 static const String boxViewedProduct = 'box_viewed' ;
1521 static const String boxFilters = 'box_filters' ;
1622 static const String boxApplied = 'box_applied' ;
1723 static const String boxCartCache = 'box_cart_cache' ;
24+ static const String boxWishListCache = 'box_wish_list_cache' ;
1825
1926 static const String keyUser = 'key_user' ;
2027 static const String keyViewedProduct = 'key_viewed' ;
2128 static const String keyFilter = 'key_viewed' ;
2229 static const String keyApplied = 'key_applied' ;
2330 static const String keyCart = 'key_cart' ;
31+ static const String keyWishList = 'key_wish_list' ;
2432
2533 Future <void > clear () async {
26- var box1 = await Hive .openBox (boxUser);
27- var box2 = await Hive .openBox (boxViewedProduct);
28- await box1.clear ();
29- await box2.clear ();
30- box1.close ();
31- return box2.close ();
34+ // var box1 = await Hive.openBox(boxUser);
35+ // var box2 = await Hive.openBox(boxViewedProduct);
36+ // await box1.clear();
37+ // await box2.clear();
38+ // box1.close();
39+ // return box2.close();
40+ await Hive .deleteFromDisk ();
3241 }
3342
3443 Future <bool > isActiveFilter (int filterId, int valueId) async {
3544 var box = await Hive .openBox (boxApplied);
3645 if (box.containsKey ('$keyApplied $filterId ' )) {
3746 var applied = box.get ('$keyApplied $filterId ' ) as ActiveFilter ;
38- if (applied.termIds.contains (valueId)) {
39- return true ;
40- } else {
41- return false ;
42- }
43- } else {
44- return false ;
47+ if (applied.termIds.contains (valueId)) return true ;
48+ //return false;
4549 }
50+ return false ;
4651 }
4752
4853 Future <List <ActiveFilter >> getActiveFilters () async {
@@ -80,14 +85,16 @@ class AppDb {
8085 var box = await Hive .openBox (boxFilters);
8186 // if (box.containsKey('$keyFilter${attribute.id}')) return
8287 box.put (
83- '$keyFilter ${attribute .id }' ,
84- Filter (
85- attribute.id,
86- attribute.name,
87- attribute.slug,
88- attribute.type,
89- terms.map ((term) => FilterValue (term.id, term.name, term.slug)).toList ()
90- )
88+ '$keyFilter ${attribute .id }' ,
89+ Filter (
90+ attribute.id,
91+ attribute.name,
92+ attribute.slug,
93+ attribute.type,
94+ terms
95+ .map ((term) => FilterValue (term.id, term.name, term.slug))
96+ .toList (),
97+ ),
9198 );
9299 }
93100
@@ -99,7 +106,7 @@ class AppDb {
99106
100107 Future <void > addToCart (int id, {String name = '' }) async {
101108 var box = await Hive .openBox (boxCartCache);
102- if (box.containsKey ('$keyCart $id } ' )) return box.close ();
109+ if (box.containsKey ('$keyCart $id ' )) return box.close ();
103110 box.put ('$keyCart $id ' , CartCacheItem (id, name));
104111 return box.close ();
105112 }
@@ -123,17 +130,70 @@ class AppDb {
123130 return box.close ();
124131 }
125132
133+ Future <void > insertWishList (List <Pair <String , int >> raw) async {
134+ var box = await Hive .openBox (boxWishListCache);
135+ await box.clear ();
136+ for (var pair in raw) {
137+ var key = '$keyWishList ${pair .first }' ;
138+ box.put (key, WishListCacheItem (pair.first, pair.second));
139+ }
140+ await box.close ();
141+ return ;
142+ }
143+
144+ Future <void > addToWishList (String itemId, int productId) async {
145+ var box = await Hive .openBox (boxWishListCache);
146+ var key = '$keyWishList $itemId ' ;
147+ if (box.containsKey (key)) {
148+ //await box.close();
149+ }
150+ box.put (key, WishListCacheItem (itemId, productId));
151+ //await box.close();
152+ return ;
153+ }
154+
155+ Future <String > getWishListItemIdByProductId (int productId) async {
156+ var box = await Hive .openBox (boxWishListCache);
157+ for (WishListCacheItem item in box.values) {
158+ if (item.productId == productId) return item.itemId;
159+ }
160+ return '' ;
161+ }
162+
163+ Future <void > deleteFromWishList (String itemId) async {
164+ var box = await Hive .openBox (boxWishListCache);
165+ var key = '$keyWishList $itemId ' ;
166+ if (box.containsKey (key)) {
167+ await box.delete (key);
168+ }
169+ //await box.close();
170+ return ;
171+ }
172+
173+ Future <bool > isInWishList (String itemId) async {
174+ var box = await Hive .openBox (boxWishListCache);
175+ return box.containsKey ('$keyWishList $itemId ' );
176+ }
177+
178+ Future <bool > isInWishListByProductId (int productId) async {
179+ var box = await Hive .openBox (boxWishListCache);
180+ for (WishListCacheItem item in box.values) {
181+ if (item.productId == productId) return true ;
182+ }
183+ return false ;
184+ }
185+
126186 Future <void > saveProductView (Product product) async {
127187 var box = await Hive .openBox (boxViewedProduct);
128188 if (box.containsKey ('$keyViewedProduct ${product .id }' )) return box.close ();
129189 box.put (
130- '$keyViewedProduct ${product .id }' ,
131- ViewedProduct (
132- product.id,
133- product.name,
134- product.price,
135- product.images[0 ].src
136- )
190+ '$keyViewedProduct ${product .id }' ,
191+ ViewedProduct (
192+ product.id,
193+ product.name,
194+ product.price,
195+ product.images[0 ].src,
196+ ),
137197 );
138198 return box.close ();
139199 }
0 commit comments