Skip to content

Commit

Permalink
Merge pull request #30 from acs-upb-mobile/improve_filter
Browse files Browse the repository at this point in the history
Improve filter
  • Loading branch information
IoanaAlexandru authored Sep 18, 2020
2 parents b4550ac + fe6ea59 commit 4a35268
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 147 deletions.
1 change: 1 addition & 0 deletions lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class MessageLookup extends MessageLookupByLibrary {
"infoPasswordResetEmailSent" : MessageLookupByLibrary.simpleMessage("Please check your inbox for the password reset e-mail."),
"infoRelevance" : MessageLookupByLibrary.simpleMessage("Try to choose the most restrictive category."),
"infoRelevanceExample" : MessageLookupByLibrary.simpleMessage("For instance, if something is only relevant for \"314CB\" and \"315CB\", don\'t just set \"CB\"."),
"infoRelevanceNothingSelected" : MessageLookupByLibrary.simpleMessage("If this is relevant for everyone, don\'t select anything ."),
"labelCategory" : MessageLookupByLibrary.simpleMessage("Category"),
"labelClass" : MessageLookupByLibrary.simpleMessage("Class"),
"labelColor" : MessageLookupByLibrary.simpleMessage("Color"),
Expand Down
1 change: 1 addition & 0 deletions lib/generated/intl/messages_ro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class MessageLookup extends MessageLookupByLibrary {
"infoPasswordResetEmailSent" : MessageLookupByLibrary.simpleMessage("Please check your inbox for the password reset e-mail."),
"infoRelevance" : MessageLookupByLibrary.simpleMessage("Încercați să selectați cea mai restrictivă categorie."),
"infoRelevanceExample" : MessageLookupByLibrary.simpleMessage("De exemplu, dacă ceva este relevant doar pentru \"314CB\" și \"315CB\", nu setați direct \"CB\"."),
"infoRelevanceNothingSelected" : MessageLookupByLibrary.simpleMessage("Nu selectați nimic dacă este relevant pentru toată lumea."),
"labelCategory" : MessageLookupByLibrary.simpleMessage("Categorie"),
"labelClass" : MessageLookupByLibrary.simpleMessage("Materie"),
"labelColor" : MessageLookupByLibrary.simpleMessage("Culoare"),
Expand Down
11 changes: 11 additions & 0 deletions lib/generated/l10n.dart

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

1 change: 1 addition & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@

"infoPasswordResetEmailSent": "Please check your inbox for the password reset e-mail.",
"infoRelevance": "Try to choose the most restrictive category.",
"infoRelevanceNothingSelected": "If this is relevant for everyone, don't select anything .",
"infoRelevanceExample": "For instance, if something is only relevant for \"314CB\" and \"315CB\", don't just set \"CB\".",
"infoPassword": "It must contain lower and uppercase letters, one number and one special character, and have a minimum length of 8.",
"infoAppIsOpenSource": "ACS UPB Mobile is open source.",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/intl_ro.arb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@

"infoPasswordResetEmailSent": "Please check your inbox for the password reset e-mail.",
"infoRelevance": "Încercați să selectați cea mai restrictivă categorie.",
"infoRelevanceNothingSelected": "Nu selectați nimic dacă este relevant pentru toată lumea.",
"infoRelevanceExample": "De exemplu, dacă ceva este relevant doar pentru \"314CB\" și \"315CB\", nu setați direct \"CB\".",
"infoPassword": "Aceasta trebuie să conțină majuscule, minuscule și cel puțin un număr sau un simbol, având minimum 8 caractere.",
"infoAppIsOpenSource": "ACS UPB Mobile este open source.",
Expand Down
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:acs_upb_mobile/navigation/bottom_navigation_bar.dart';
import 'package:acs_upb_mobile/navigation/routes.dart';
import 'package:acs_upb_mobile/pages/classes/service/class_provider.dart';
import 'package:acs_upb_mobile/pages/filter/service/filter_provider.dart';
import 'package:acs_upb_mobile/pages/filter/view/filter_page.dart';
import 'package:acs_upb_mobile/pages/people/service/person_provider.dart';
import 'package:acs_upb_mobile/pages/portal/service/website_provider.dart';
import 'package:acs_upb_mobile/pages/settings/settings_page.dart';
Expand Down Expand Up @@ -68,7 +67,6 @@ class _MyAppState extends State<MyApp> {
Routes.root: (_) => AppLoadingScreen(),
Routes.home: (_) => AppBottomNavigationBar(),
Routes.settings: (_) => SettingsPage(),
Routes.filter: (_) => FilterPage(),
Routes.login: (_) => LoginView(),
Routes.signUp: (_) => SignUpView(),
},
Expand Down
27 changes: 15 additions & 12 deletions lib/pages/filter/model/filter.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/cupertino.dart';

/// Filter represented in the form of a tree with named levels
///
Expand Down Expand Up @@ -27,21 +27,16 @@ class Filter {
/// Name of each level of the tree.
///
/// **Note:** There should be at least as many names as there are levels in the tree.
List<Map<String, String>> localizedLevelNames;
final List<Map<String, String>> localizedLevelNames;

Filter({this.root, this.localizedLevelNames, void Function() listener}) {
Filter({this.root, this.localizedLevelNames}) {
this.root.value = true; // root value is true by default
_addListener(listener ?? () {}, this.root);
}

static _addListener(void Function() listener, FilterNode node) {
node._valueNotifier.addListener(listener);
if (node.children != null) {
for (var child in node.children) {
_addListener(listener, child);
}
}
}
Filter clone() => Filter(
root: this.root.clone(),
localizedLevelNames: this.localizedLevelNames,
);

void _relevantNodesHelper(List<String> list, FilterNode node) {
if (node.value) {
Expand Down Expand Up @@ -167,4 +162,12 @@ class FilterNode {
get value => _valueNotifier.value;

set value(bool value) => _valueNotifier.value = value;

set listener(Function() listener) => _valueNotifier.addListener(listener);

FilterNode clone() => FilterNode(
name: name,
value: value,
children: children?.map((c) => c.clone())?.toList(),
);
}
43 changes: 25 additions & 18 deletions lib/pages/filter/service/filter_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ class FilterProvider with ChangeNotifier {

bool _enabled;
List<String> _relevantNodes;
final List<String> defaultRelevance;

FilterProvider(
{this.global = false,
bool filterEnabled,
this.defaultDegree,
List<String> defaultRelevance})
: _enabled = filterEnabled ?? PrefService.get('relevance_filter') ?? true,
_relevantNodes = defaultRelevance {
if (defaultRelevance != null) {
this.defaultRelevance})
: _enabled =
filterEnabled ?? PrefService.get('relevance_filter') ?? true {
if (defaultRelevance != null && !defaultRelevance.contains('All')) {
if (this.defaultDegree == null) {
throw ArgumentError(
'If the relevance is not null, the degree cannot be null.');
Expand Down Expand Up @@ -76,13 +77,22 @@ class FilterProvider with ChangeNotifier {
notifyListeners();
}

void updateFilter(Filter filter) {
_relevanceFilter = filter;
if (global) {
PrefService.setStringList(
'relevant_nodes', _relevanceFilter.relevantNodes);
}
notifyListeners();
}

bool get filterEnabled => _enabled;

Filter get cachedFilter => _relevanceFilter;
Filter get cachedFilter => _relevanceFilter.clone();

Future<Filter> fetchFilter(BuildContext context) async {
if (_relevanceFilter != null) {
return _relevanceFilter;
return cachedFilter;
}

try {
Expand All @@ -106,19 +116,16 @@ class FilterProvider with ChangeNotifier {

Map<String, dynamic> root = data['root'];
_relevanceFilter = Filter(
localizedLevelNames: levelNames,
root: FilterNodeExtension.fromMap(root, 'All'),
listener: () {
if (global) {
PrefService.setStringList(
'relevant_nodes', _relevanceFilter.relevantNodes);
}
notifyListeners();
});

if (_relevantNodes != null && defaultDegree != null) {
localizedLevelNames: levelNames,
root: FilterNodeExtension.fromMap(root, 'All'),
);

if (_relevantNodes == null && defaultRelevance != null) {
_relevantNodes = defaultRelevance;
_relevantNodes.forEach((node) =>
_relevanceFilter.setRelevantUpToRoot(node, defaultDegree));
} else if (_relevantNodes != null) {
_relevanceFilter.setRelevantNodes(_relevantNodes);
} else {
// No previous setting or defaults => set the user's group
AuthProvider authProvider =
Expand All @@ -138,7 +145,7 @@ class FilterProvider with ChangeNotifier {
}
}

return _relevanceFilter;
return cachedFilter;
} catch (e, stackTrace) {
print(e);
print(stackTrace);
Expand Down
Loading

0 comments on commit 4a35268

Please sign in to comment.