Skip to content

Commit af2af54

Browse files
authored
Wish List feature (#2)
* Implement WishList domain layer * UI WishList base implementation * UI WishList base implementation patch 2 * WishList implementation complete
1 parent 7e4b21b commit af2af54

49 files changed

Lines changed: 772 additions & 260 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

assets/translations/uk.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"rate_app": "Оцініть додаток",
7171
"in_stock": "В наявності",
7272
"out_of_stock": "Немає в наявності",
73-
"back_order": "Передзамовлення",
73+
"back_order": "Під замовлення",
7474
"sort_date_asc": "Спочатку старі",
7575
"sort_date_desc": "Спочатку нові",
7676
"sort_alphabet_asc": "За алфавітом (А-Я)",

lib/api/interceptor_logger.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class PrinterInterceptor extends Interceptor {
77
print('''------------------- START REQUEST
88
Method: ${options.method}
99
Headers: ${options.headers}
10-
Path: ${options.path}
10+
Path: ${options.baseUrl}${options.path}
1111
Query parameters: ${options.queryParameters}
1212
Data: ${options.data}
1313
Content type: ${options.contentType}
@@ -20,6 +20,7 @@ Content type: ${options.contentType}
2020
print('''------------------- START RESPONSE
2121
Status code: ${response.statusCode}
2222
Response data: ${response.data}
23+
Path: ${response.realUri}
2324
------------------- END RESPONSE''');
2425
super.onResponse(response, handler);//Headers: ${response.headers}
2526
}
@@ -30,6 +31,7 @@ Response data: ${response.data}
3031
Response: ${err.response}
3132
Error type: ${err.type}
3233
Error message: ${err.message}
34+
Path: ${err.response?.realUri}
3335
------------------- END ERROR''');
3436
super.onError(err, handler);
3537
}

lib/constants/translations.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Language {
4+
final Locale locale;
5+
final String title;
6+
7+
const Language(this.locale, this.title);
8+
}
9+
10+
class Translations {
11+
static const languages = [
12+
Language(Locale('en'), 'English'),
13+
Language(Locale('uk'), 'Українська'),
14+
Language(Locale('ru'), 'Русский'),
15+
];
16+
}

lib/database/database.dart

Lines changed: 96 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
11
import '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';
810
import 'package:wooapp/model/attribute.dart';
911
import '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
1118
class 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
}
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:hive/hive.dart';
2-
import 'package:wooapp/database/filter_value.dart';
2+
import 'package:wooapp/database/entity/filter_value.dart';
33

44
part 'filter.g.dart';
55

0 commit comments

Comments
 (0)