Skip to content

Commit

Permalink
fix: filters
Browse files Browse the repository at this point in the history
feat: add saving data to db
  • Loading branch information
p0dyakov committed Dec 19, 2022
1 parent 5c8e68a commit 1e00895
Show file tree
Hide file tree
Showing 31 changed files with 643 additions and 864 deletions.
20 changes: 3 additions & 17 deletions lib/src/core/database/hive/app_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,14 @@ class AppDatabase {

final Map<String, int> _boxCounter = <String, int>{};

Future<Box<List<Shop>>?> openShopsBox() async =>
_openBox<List<Shop>>(name: AppDatabaseKeys.shops, typeId: 0);

Future get({
required Box box,
required String boxKey,
}) async =>
box.get(boxKey);

Future<void> put({
required Box box,
required String boxKey,
required Object model,
}) async {
await box.put(boxKey, model);
}
Future<Box<List>> openShopsBox() async =>
_openBox(name: AppDatabaseKeys.shops, typeId: 0);

Future<void> close() async {
await Hive.close();
}

Future<Box<T>?> _openBox<T>({
Future<Box<T>> _openBox<T>({
required String name,
required int typeId,
}) async {
Expand Down
1 change: 1 addition & 0 deletions lib/src/core/extension/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ library common_extensions;
export 'src/build_context.dart';
export 'src/function.dart';
export 'src/object.dart';
export 'src/sf_range_values.dart';

mixin ImportCommonExtensions {}
14 changes: 14 additions & 0 deletions lib/src/core/extension/src/sf_range_values.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:syncfusion_flutter_sliders/sliders.dart';

extension CustomSfRangeValues on SfRangeValues {
bool get isInitial => start == 0 && end == 1000;
bool isContains(int other) {
// TODO: change end and start typing
final typedStart =
double == start.runtimeType ? (start as double).toInt() : start as int;
final typedEnd =
double == end.runtimeType ? (end as double).toInt() : end as int;

return typedStart <= other && other <= typedEnd;
}
}
47 changes: 3 additions & 44 deletions lib/src/core/widget/search.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_vibrate/flutter_vibrate.dart';
import 'package:shop/src/core/resource/theme/sizes.dart';
import 'package:shop/src/core/utils/show_draggable_bottom_sheet.dart';
import 'package:shop/src/feature/shop/widget/filters.dart';
import 'package:syncfusion_flutter_sliders/sliders.dart';

class SearchWidget extends StatelessWidget {
const SearchWidget({
Key? key,
this.hint = 'Search',
this.disabled = false,
required this.onQueryChanged,
required this.weightValues,
required this.priceValues,
required this.onFiltersChanged,
required this.onFiltersChangeEnd,
required this.onChanged,
}) : super(key: key);

final bool disabled;
final String hint;
final Function(String query) onQueryChanged;
final SfRangeValues weightValues;
final SfRangeValues priceValues;
final FiltersChanges onFiltersChanged;
final FiltersChanges onFiltersChangeEnd;
final Function(String query) onChanged;

@override
Widget build(BuildContext context) => Padding(
Expand Down Expand Up @@ -53,7 +41,7 @@ class SearchWidget extends StatelessWidget {
Expanded(
child: TextField(
readOnly: disabled,
onChanged: onQueryChanged,
onChanged: onChanged,
cursorColor: Colors.grey,
style: const TextStyle(
color: Color.fromARGB(255, 61, 61, 61),
Expand All @@ -72,35 +60,6 @@ class SearchWidget extends StatelessWidget {
),
),
),
const SizedBox(width: 5),
DecoratedBox(
decoration: BoxDecoration(
color: const Color(0xFFCECECE).withOpacity(0.50),
borderRadius: const BorderRadius.all(Radius.circular(50)),
),
child: IconButton(
padding: EdgeInsets.zero,
constraints: const BoxConstraints(minHeight: 36, minWidth: 36),
iconSize: 20,
onPressed: () {
Vibrate.feedback(FeedbackType.light);
showDraggableBottomSheet(
context: context,
child: FiltersWidget(
onFiltersChanged: onFiltersChanged,
weightValues: weightValues,
priceValues: priceValues,
onFiltersChangeEnd: onFiltersChangeEnd,
),
);
},
icon: const Icon(
Icons.filter_list_alt,
size: 20,
color: Colors.grey,
),
),
),
],
),
);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/feature/app/logic/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ mixin Logger {
l.e(_formatError('Top-level', e.toString(), s), s);
}

static void logError(Object e) {
l.e(e);
}

static void logFlutterError(
FlutterErrorDetails details,
) {
Expand Down
15 changes: 7 additions & 8 deletions lib/src/feature/shop/database/shop_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class ShopDao implements IShopDao {
@override
Future<List<Shop>?> get shops async {
final box = await _appDatabase.openShopsBox();
if (box != null) {
final shops = await _appDatabase.get(
box: box,
boxKey: AppDatabaseKeys.shops,
) as List<Shop>?;
if (shops != null) return shops;
}

return null;
return box.get(AppDatabaseKeys.shops)?.cast<Shop>();
}

@override
Future<void> updateShops(List<Shop> shops) async {
final box = await _appDatabase.openShopsBox();
await box.put(AppDatabaseKeys.shops, shops);
}
}
1 change: 1 addition & 0 deletions lib/src/feature/shop/database/shop_dao_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import 'package:shop/src/feature/shop/model/shop/shop.dart';

abstract class IShopDao {
Future<List<Shop>?> get shops;
Future<void> updateShops(List<Shop> shops);
}
2 changes: 1 addition & 1 deletion lib/src/feature/shop/model/shop/shop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Shop with _$Shop {
@HiveField(0) required String id,
@HiveField(1) required String name,
@HiveField(2) required String description,
@HiveField(3) required Duration deliveryTime,
@HiveField(3) required int deliveryTimeInMinutes,
@HiveField(4) required int deliveryPrice,
@HiveField(5) required String photoUrl,
@HiveField(6) required List<Product> products,
Expand Down
42 changes: 21 additions & 21 deletions lib/src/feature/shop/model/shop/shop.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mixin _$Shop {
@HiveField(2)
String get description => throw _privateConstructorUsedError;
@HiveField(3)
Duration get deliveryTime => throw _privateConstructorUsedError;
int get deliveryTimeInMinutes => throw _privateConstructorUsedError;
@HiveField(4)
int get deliveryPrice => throw _privateConstructorUsedError;
@HiveField(5)
Expand All @@ -49,7 +49,7 @@ abstract class $ShopCopyWith<$Res> {
{@HiveField(0) String id,
@HiveField(1) String name,
@HiveField(2) String description,
@HiveField(3) Duration deliveryTime,
@HiveField(3) int deliveryTimeInMinutes,
@HiveField(4) int deliveryPrice,
@HiveField(5) String photoUrl,
@HiveField(6) List<Product> products});
Expand All @@ -71,7 +71,7 @@ class _$ShopCopyWithImpl<$Res, $Val extends Shop>
Object? id = null,
Object? name = null,
Object? description = null,
Object? deliveryTime = null,
Object? deliveryTimeInMinutes = null,
Object? deliveryPrice = null,
Object? photoUrl = null,
Object? products = null,
Expand All @@ -89,10 +89,10 @@ class _$ShopCopyWithImpl<$Res, $Val extends Shop>
? _value.description
: description // ignore: cast_nullable_to_non_nullable
as String,
deliveryTime: null == deliveryTime
? _value.deliveryTime
: deliveryTime // ignore: cast_nullable_to_non_nullable
as Duration,
deliveryTimeInMinutes: null == deliveryTimeInMinutes
? _value.deliveryTimeInMinutes
: deliveryTimeInMinutes // ignore: cast_nullable_to_non_nullable
as int,
deliveryPrice: null == deliveryPrice
? _value.deliveryPrice
: deliveryPrice // ignore: cast_nullable_to_non_nullable
Expand All @@ -119,7 +119,7 @@ abstract class _$$_ShopCopyWith<$Res> implements $ShopCopyWith<$Res> {
{@HiveField(0) String id,
@HiveField(1) String name,
@HiveField(2) String description,
@HiveField(3) Duration deliveryTime,
@HiveField(3) int deliveryTimeInMinutes,
@HiveField(4) int deliveryPrice,
@HiveField(5) String photoUrl,
@HiveField(6) List<Product> products});
Expand All @@ -137,7 +137,7 @@ class __$$_ShopCopyWithImpl<$Res> extends _$ShopCopyWithImpl<$Res, _$_Shop>
Object? id = null,
Object? name = null,
Object? description = null,
Object? deliveryTime = null,
Object? deliveryTimeInMinutes = null,
Object? deliveryPrice = null,
Object? photoUrl = null,
Object? products = null,
Expand All @@ -155,10 +155,10 @@ class __$$_ShopCopyWithImpl<$Res> extends _$ShopCopyWithImpl<$Res, _$_Shop>
? _value.description
: description // ignore: cast_nullable_to_non_nullable
as String,
deliveryTime: null == deliveryTime
? _value.deliveryTime
: deliveryTime // ignore: cast_nullable_to_non_nullable
as Duration,
deliveryTimeInMinutes: null == deliveryTimeInMinutes
? _value.deliveryTimeInMinutes
: deliveryTimeInMinutes // ignore: cast_nullable_to_non_nullable
as int,
deliveryPrice: null == deliveryPrice
? _value.deliveryPrice
: deliveryPrice // ignore: cast_nullable_to_non_nullable
Expand All @@ -183,7 +183,7 @@ class _$_Shop implements _Shop {
{@HiveField(0) required this.id,
@HiveField(1) required this.name,
@HiveField(2) required this.description,
@HiveField(3) required this.deliveryTime,
@HiveField(3) required this.deliveryTimeInMinutes,
@HiveField(4) required this.deliveryPrice,
@HiveField(5) required this.photoUrl,
@HiveField(6) required final List<Product> products})
Expand All @@ -202,7 +202,7 @@ class _$_Shop implements _Shop {
final String description;
@override
@HiveField(3)
final Duration deliveryTime;
final int deliveryTimeInMinutes;
@override
@HiveField(4)
final int deliveryPrice;
Expand All @@ -219,7 +219,7 @@ class _$_Shop implements _Shop {

@override
String toString() {
return 'Shop(id: $id, name: $name, description: $description, deliveryTime: $deliveryTime, deliveryPrice: $deliveryPrice, photoUrl: $photoUrl, products: $products)';
return 'Shop(id: $id, name: $name, description: $description, deliveryTimeInMinutes: $deliveryTimeInMinutes, deliveryPrice: $deliveryPrice, photoUrl: $photoUrl, products: $products)';
}

@override
Expand All @@ -231,8 +231,8 @@ class _$_Shop implements _Shop {
(identical(other.name, name) || other.name == name) &&
(identical(other.description, description) ||
other.description == description) &&
(identical(other.deliveryTime, deliveryTime) ||
other.deliveryTime == deliveryTime) &&
(identical(other.deliveryTimeInMinutes, deliveryTimeInMinutes) ||
other.deliveryTimeInMinutes == deliveryTimeInMinutes) &&
(identical(other.deliveryPrice, deliveryPrice) ||
other.deliveryPrice == deliveryPrice) &&
(identical(other.photoUrl, photoUrl) ||
Expand All @@ -247,7 +247,7 @@ class _$_Shop implements _Shop {
id,
name,
description,
deliveryTime,
deliveryTimeInMinutes,
deliveryPrice,
photoUrl,
const DeepCollectionEquality().hash(_products));
Expand All @@ -271,7 +271,7 @@ abstract class _Shop implements Shop {
{@HiveField(0) required final String id,
@HiveField(1) required final String name,
@HiveField(2) required final String description,
@HiveField(3) required final Duration deliveryTime,
@HiveField(3) required final int deliveryTimeInMinutes,
@HiveField(4) required final int deliveryPrice,
@HiveField(5) required final String photoUrl,
@HiveField(6) required final List<Product> products}) = _$_Shop;
Expand All @@ -289,7 +289,7 @@ abstract class _Shop implements Shop {
String get description;
@override
@HiveField(3)
Duration get deliveryTime;
int get deliveryTimeInMinutes;
@override
@HiveField(4)
int get deliveryPrice;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/feature/shop/model/shop/shop.g.dart

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

4 changes: 2 additions & 2 deletions lib/src/feature/shop/model/shop/shop.hive.dart

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

11 changes: 10 additions & 1 deletion lib/src/feature/shop/model/shop_data/shop_data.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:shop/src/feature/shop/model/shop/shop.dart';
import 'package:syncfusion_flutter_sliders/sliders.dart';

part 'shop_data.freezed.dart';

@freezed
class ShopData with _$ShopData {
factory ShopData({
required List<Shop> shops,
required String query,
required SfRangeValues weightValues,
required SfRangeValues priceValues,
}) = _ShopData;

factory ShopData.initial() => ShopData(shops: []);
factory ShopData.initial() => ShopData(
query: '',
shops: [],
weightValues: const SfRangeValues(0, 1000),
priceValues: const SfRangeValues(0, 1000),
);
}
Loading

0 comments on commit 1e00895

Please sign in to comment.