Skip to content

Commit

Permalink
Wish List feature (#2)
Browse files Browse the repository at this point in the history
* Implement WishList domain layer

* UI WishList base implementation

* UI WishList base implementation patch 2

* WishList implementation complete
  • Loading branch information
ShiftHackZ authored Feb 16, 2023
1 parent 7e4b21b commit af2af54
Show file tree
Hide file tree
Showing 49 changed files with 772 additions and 260 deletions.
2 changes: 1 addition & 1 deletion assets/translations/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"rate_app": "Оцініть додаток",
"in_stock": "В наявності",
"out_of_stock": "Немає в наявності",
"back_order": "Передзамовлення",
"back_order": "Під замовлення",
"sort_date_asc": "Спочатку старі",
"sort_date_desc": "Спочатку нові",
"sort_alphabet_asc": "За алфавітом (А-Я)",
Expand Down
4 changes: 3 additions & 1 deletion lib/api/interceptor_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PrinterInterceptor extends Interceptor {
print('''------------------- START REQUEST
Method: ${options.method}
Headers: ${options.headers}
Path: ${options.path}
Path: ${options.baseUrl}${options.path}
Query parameters: ${options.queryParameters}
Data: ${options.data}
Content type: ${options.contentType}
Expand All @@ -20,6 +20,7 @@ Content type: ${options.contentType}
print('''------------------- START RESPONSE
Status code: ${response.statusCode}
Response data: ${response.data}
Path: ${response.realUri}
------------------- END RESPONSE''');
super.onResponse(response, handler);//Headers: ${response.headers}
}
Expand All @@ -30,6 +31,7 @@ Response data: ${response.data}
Response: ${err.response}
Error type: ${err.type}
Error message: ${err.message}
Path: ${err.response?.realUri}
------------------- END ERROR''');
super.onError(err, handler);
}
Expand Down
16 changes: 16 additions & 0 deletions lib/constants/translations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';

class Language {
final Locale locale;
final String title;

const Language(this.locale, this.title);
}

class Translations {
static const languages = [
Language(Locale('en'), 'English'),
Language(Locale('uk'), 'Українська'),
Language(Locale('ru'), 'Русский'),
];
}
132 changes: 96 additions & 36 deletions lib/database/database.dart
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
import 'package:hive_flutter/hive_flutter.dart';
import 'package:wooapp/database/cart_cache_item.dart';
import 'package:wooapp/database/filter.dart';
import 'package:wooapp/database/filter_active.dart';
import 'package:wooapp/database/filter_value.dart';
import 'package:wooapp/database/product.dart';
import 'package:wooapp/database/user.dart';
import 'package:wooapp/core/pair.dart';
import 'package:wooapp/database/entity/cart_cache_item.dart';
import 'package:wooapp/database/entity/filter.dart';
import 'package:wooapp/database/entity/filter_active.dart';
import 'package:wooapp/database/entity/filter_value.dart';
import 'package:wooapp/database/entity/product.dart';
import 'package:wooapp/database/entity/user.dart';
import 'package:wooapp/database/entity/wish_list_cache_item.dart';
import 'package:wooapp/model/attribute.dart';
import 'package:wooapp/model/product.dart';

/// WooApp local database orchestrator.
/// Docs: https://docs.hivedb.dev/
///
/// To generate adapters, execute:
/// flutter packages pub run build_runner build
class AppDb {

static const String boxUser = 'box_user';
static const String boxViewedProduct = 'box_viewed';
static const String boxFilters = 'box_filters';
static const String boxApplied = 'box_applied';
static const String boxCartCache = 'box_cart_cache';
static const String boxWishListCache = 'box_wish_list_cache';

static const String keyUser = 'key_user';
static const String keyViewedProduct = 'key_viewed';
static const String keyFilter = 'key_viewed';
static const String keyApplied = 'key_applied';
static const String keyCart = 'key_cart';
static const String keyWishList = 'key_wish_list';

Future<void> clear() async {
var box1 = await Hive.openBox(boxUser);
var box2 = await Hive.openBox(boxViewedProduct);
await box1.clear();
await box2.clear();
box1.close();
return box2.close();
// var box1 = await Hive.openBox(boxUser);
// var box2 = await Hive.openBox(boxViewedProduct);
// await box1.clear();
// await box2.clear();
// box1.close();
// return box2.close();
await Hive.deleteFromDisk();
}

Future<bool> isActiveFilter(int filterId, int valueId) async {
var box = await Hive.openBox(boxApplied);
if (box.containsKey('$keyApplied$filterId')) {
var applied = box.get('$keyApplied$filterId') as ActiveFilter;
if (applied.termIds.contains(valueId)) {
return true;
} else {
return false;
}
} else {
return false;
if (applied.termIds.contains(valueId)) return true;
//return false;
}
return false;
}

Future<List<ActiveFilter>> getActiveFilters() async {
Expand Down Expand Up @@ -80,14 +85,16 @@ class AppDb {
var box = await Hive.openBox(boxFilters);
// if (box.containsKey('$keyFilter${attribute.id}')) return
box.put(
'$keyFilter${attribute.id}',
Filter(
attribute.id,
attribute.name,
attribute.slug,
attribute.type,
terms.map((term) => FilterValue(term.id, term.name, term.slug)).toList()
)
'$keyFilter${attribute.id}',
Filter(
attribute.id,
attribute.name,
attribute.slug,
attribute.type,
terms
.map((term) => FilterValue(term.id, term.name, term.slug))
.toList(),
),
);
}

Expand All @@ -99,7 +106,7 @@ class AppDb {

Future<void> addToCart(int id, {String name = ''}) async {
var box = await Hive.openBox(boxCartCache);
if (box.containsKey('$keyCart$id}')) return box.close();
if (box.containsKey('$keyCart$id')) return box.close();
box.put('$keyCart$id', CartCacheItem(id, name));
return box.close();
}
Expand All @@ -123,17 +130,70 @@ class AppDb {
return box.close();
}

Future<void> insertWishList(List<Pair<String, int>> raw) async {
var box = await Hive.openBox(boxWishListCache);
await box.clear();
for (var pair in raw) {
var key = '$keyWishList${pair.first}';
box.put(key, WishListCacheItem(pair.first, pair.second));
}
await box.close();
return;
}

Future<void> addToWishList(String itemId, int productId) async {
var box = await Hive.openBox(boxWishListCache);
var key = '$keyWishList$itemId';
if (box.containsKey(key)) {
//await box.close();
}
box.put(key, WishListCacheItem(itemId, productId));
//await box.close();
return;
}

Future<String> getWishListItemIdByProductId(int productId) async {
var box = await Hive.openBox(boxWishListCache);
for (WishListCacheItem item in box.values) {
if (item.productId == productId) return item.itemId;
}
return '';
}

Future<void> deleteFromWishList(String itemId) async {
var box = await Hive.openBox(boxWishListCache);
var key = '$keyWishList$itemId';
if (box.containsKey(key)) {
await box.delete(key);
}
//await box.close();
return;
}

Future<bool> isInWishList(String itemId) async {
var box = await Hive.openBox(boxWishListCache);
return box.containsKey('$keyWishList$itemId');
}

Future<bool> isInWishListByProductId(int productId) async {
var box = await Hive.openBox(boxWishListCache);
for (WishListCacheItem item in box.values) {
if (item.productId == productId) return true;
}
return false;
}

Future<void> saveProductView(Product product) async {
var box = await Hive.openBox(boxViewedProduct);
if (box.containsKey('$keyViewedProduct${product.id}')) return box.close();
box.put(
'$keyViewedProduct${product.id}',
ViewedProduct(
product.id,
product.name,
product.price,
product.images[0].src
)
'$keyViewedProduct${product.id}',
ViewedProduct(
product.id,
product.name,
product.price,
product.images[0].src,
),
);
return box.close();
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:hive/hive.dart';
import 'package:wooapp/database/filter_value.dart';
import 'package:wooapp/database/entity/filter_value.dart';

part 'filter.g.dart';

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion lib/database/user.dart → lib/database/entity/user.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:hive/hive.dart';

part 'user.g.dart';
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions lib/database/entity/wish_list_cache_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:hive/hive.dart';

part 'wish_list_cache_item.g.dart';

@HiveType(typeId: 7)
class WishListCacheItem {
@HiveField(0)
String itemId;
@HiveField(1)
int productId;

WishListCacheItem(this.itemId, this.productId);
}
44 changes: 44 additions & 0 deletions lib/database/entity/wish_list_cache_item.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/datasource/cart_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ abstract class CartDataSource {

Map<String, String> mapVariations(List<Variation> variations, Map<String, String> input) {
Map<String, String> result = {};
print('DBG0 - $input');
for (var key in input.keys) {
Variation? variation = variations.firstWhere((v) => v.name == key, orElse: null);
if (variation == null) continue;
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/category_attribute_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:wooapp/api/woo_api_client.dart';
import 'package:wooapp/api/wp_api_client.dart';
import 'package:wooapp/constants/config.dart';
import 'package:wooapp/database/database.dart';
import 'package:wooapp/database/filter_active.dart';
import 'package:wooapp/database/entity/filter_active.dart';
import 'package:wooapp/locator.dart';
import 'package:wooapp/model/attribute.dart';
import 'package:wooapp/model/product.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/customer_auth_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:wooapp/api/cocart_api_client.dart';
import 'package:wooapp/api/woo_api_client.dart';
import 'package:wooapp/api/wp_api_client.dart';
import 'package:wooapp/database/database.dart';
import 'package:wooapp/database/user.dart';
import 'package:wooapp/database/entity/user.dart';
import 'package:wooapp/locator.dart';
import 'package:wooapp/model/auth_register_response.dart';
import 'package:wooapp/model/customer_profile.dart';
Expand Down
Loading

0 comments on commit af2af54

Please # to comment.