Skip to content

Commit

Permalink
new feature: filter
Browse files Browse the repository at this point in the history
  • Loading branch information
saiteki-kai committed Oct 31, 2020
1 parent 59a3d39 commit 5d87a2c
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 269 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,34 @@
### To Do
- [ ] Notifications
- [x] Sorting
- [ ] Filter
- [x] Filter
- [ ] Statistics Page
- [ ] Remove, Update examples
- [ ] Automatic Backups
- [ ] Language localization
- [ ] Redesign Add/Edit Page

### Folder Structure

```
lib
├── components # Common widgets shared between pages
├── database # Database configuration and Dao classes
├── models # Models
├── pages # Page widgets
│ ├─ home
│ │ ├── components # Local widgets used only in the home page
│ │ └── home.dart
│ ├─ details
│ └─ ...
├── redux # Redux folders
│ ├─ actions
│ ├─ reducers
│ ├─ state
│ ├─ thunk
│ └─ store.dart
├── utils # Common functions
├── main.dart
└── routes.dart # Contains the routes and imports all pages.
```

69 changes: 48 additions & 21 deletions lib/database/item_dao.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:jap_vocab/models/item.dart';
import 'package:jap_vocab/database/app_database.dart';
import 'package:jap_vocab/database/review_dao.dart';
import 'package:jap_vocab/redux/state/filter_state.dart';
import 'package:jap_vocab/redux/state/order_state.dart';
import 'package:sembast/sembast.dart';

class ItemDao {
Expand Down Expand Up @@ -34,42 +35,68 @@ class ItemDao {
await _store.delete(await _db, finder: finder);
}

Future<List<Item>> getAllItems(
{@required String type,
String search,
String sortField,
String sortMode}) async {
var finder = Finder(filter: Filter.equals('type', type));
Future<List<Item>> getAllItems({FilterState filter, OrderState order}) async {
var filters = [Filter.equals('type', filter.type)];

if (search != null && search.isNotEmpty) {
finder = Finder(
filter: Filter.and(
if (filter.search != null && filter.search.isNotEmpty) {
filters.add(
Filter.or(
[
Filter.equals('type', type),
Filter.or(
[
Filter.matches('text', search),
Filter.matches('reading', search)
],
),
Filter.matches('text', filter.search),
Filter.matches('reading', filter.search),
],
),
);
}

final recordSnapshot = await _store.find(await _db, finder: finder);
if (filter.jlpt != null && filter.jlpt.isNotEmpty) {
filters.add(Filter.inList('jlpt', filter.jlpt));
}

if (filter.type == 'word' &&
filter.partOfSpeech != null &&
filter.partOfSpeech.isNotEmpty) {
filters.add(Filter.custom((record) {
for (var i = 0; i < filter.partOfSpeech.length; i++) {
if (record.value['part_of_speech'].contains(filter.partOfSpeech[i])) {
return true;
}
}
return false;
}));
}

final recordSnapshot = await _store.find(
await _db,
finder: Finder(filter: Filter.and(filters)),
);

if (recordSnapshot != null) {
final list = recordSnapshot.map((snapshot) async {
final futureList = recordSnapshot.map((snapshot) async {
final item = Item.fromMap(snapshot.value);
item.review1 = await ReviewDao().getReviewById(item.reviewId1);
item.review2 = await ReviewDao().getReviewById(item.reviewId2);

return item;
}).toList();

final sorted = await Future.wait(list);
return sorted..sort(Item.comparator(sortField, sortMode));
var list = await Future.wait(futureList);

if (list == null || list.isEmpty) return [];

if (filter.level != null && filter.level.isNotEmpty) {
list = list.where((Item item) {
if (item.streak >= 6.0 && filter.level.contains('strong')) {
return true;
} else if (item.streak >= 4.0 && filter.level.contains('medium')) {
return true;
} else {
return filter.level.contains('weak');
}
}).toList();
}

return list..sort(Item.comparator(order.field, order.mode));
}
return null;
}
Expand Down
233 changes: 0 additions & 233 deletions lib/pages/home/components/filter_sheet.dart

This file was deleted.

Loading

0 comments on commit 5d87a2c

Please sign in to comment.