Skip to content

Commit

Permalink
Merge pull request #41 from hackathon-comiko/reset_search
Browse files Browse the repository at this point in the history
Reset Search, Refactor, Fix TextField behaviour.
  • Loading branch information
Pacane committed Oct 2, 2017
2 parents 4589928 + 0225e71 commit 2bde5e4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
27 changes: 24 additions & 3 deletions lib/app_state.dart
Expand Up @@ -3,6 +3,10 @@ import 'package:comiko/services.dart';
import 'package:comiko/widgets/event_card.dart';

class AppState {
static const String defaultCityFilter = "";
static const double defaultPriceFilter = 100.0;
static const int defaultDistanceFilter = 500;

List<Event> events;
Map<Event, bool> eventsFavoriteState = {};
SortingCriteria sortingCriteria;
Expand All @@ -15,9 +19,9 @@ class AppState {
sortingCriteria = SortingCriteria.date;
events = [];
eventsFavoriteState = {};
cityFilter = "";
priceFilter = 100.0;
distanceFilter = 500;
cityFilter = defaultCityFilter;
priceFilter = defaultPriceFilter;
distanceFilter = defaultDistanceFilter;
}

EventCardViewModel createViewModel(Event event) => new EventCardViewModel(
Expand Down Expand Up @@ -52,6 +56,12 @@ class AppState {
events = sorted;
}

void resetFilters() {
cityFilter = defaultCityFilter;
priceFilter = defaultPriceFilter;
distanceFilter = defaultDistanceFilter;
}

AppState._(
this.events,
this.sortingCriteria,
Expand Down Expand Up @@ -172,6 +182,17 @@ class FetchEventsAction extends IsAction {
}
}

class ResetFiltersAction extends IsAction {
@override
AppState handle(AppState state) {
state = state.clone();
state.resetFilters();
state.filterEventsWithActiveFilters();

return state;
}
}

abstract class IsAction {
AppState handle(AppState state);
}
Expand Down
59 changes: 38 additions & 21 deletions lib/widgets/search_popup.dart
Expand Up @@ -7,12 +7,22 @@ import 'package:redux/redux.dart';
class SearchPopup extends StatelessWidget {
final Store<AppState> store;

static const double minPrice = 0.0;
static const double maxPrice = 100.0;

static const double minDistance = 0.0;
static const double maxDistance = 600.0;

SearchPopup({@required this.store});

@override
Widget build(BuildContext context) {
return new AlertDialog(
actions: [
new FlatButton(
onPressed: () => store.dispatch(new ResetFiltersAction()),
child: const Text('Réinitialiser'),
),
new FlatButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
Expand All @@ -23,26 +33,34 @@ class SearchPopup extends StatelessWidget {
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new ListTile(
leading: new Icon(Icons.location_city),
title: new StoreConnector<AppState, String>(
converter: (store) => store.state.cityFilter,
builder: (context, filter) {
return new TextField(
decoration: const InputDecoration(
hintText: 'Entrer le nom de la ville',
),
onSubmitted: (String value) =>
store.dispatch(new UpdateCityFilterAction(value)),
);
},
)),
leading: new Icon(Icons.location_city),
title: new StoreConnector<AppState, String>(
converter: (store) => store.state.cityFilter,
builder: (context, city) {
var cityFilterTextController =
new TextEditingController(text: store.state.cityFilter)
..selection = new TextSelection(
baseOffset: store.state.cityFilter.length,
extentOffset: store.state.cityFilter.length,
);
return new TextField(
controller: cityFilterTextController,
decoration: const InputDecoration(
hintText: 'Entrer le nom de la ville',
),
onSubmitted: (String value) =>
store.dispatch(new UpdateCityFilterAction(value)),
);
},
),
),
new ListTile(
leading: new Icon(Icons.attach_money),
title: new StoreConnector<AppState, double>(
builder: (context, price) => new Slider(
value: price,
min: 0.0,
max: 100.0,
min: minPrice,
max: maxPrice,
label: '${price.toStringAsFixed(0)}\$',
onChanged: (double value) => store.dispatch(
new UpdatePriceFilterAction(value),
Expand All @@ -53,14 +71,13 @@ class SearchPopup extends StatelessWidget {
new ListTile(
leading: new Icon(Icons.directions_car),
title: new StoreConnector<AppState, int>(
builder: (context, value) =>
new Slider(
builder: (context, value) => new Slider(
value: value.roundToDouble(),
max: 500.0,
min: minDistance,
max: maxDistance,
label: '${value}km',
onChanged: (value) =>
store.dispatch(
new UpdateDistanceFilterAction(value.round()))),
onChanged: (value) => store.dispatch(
new UpdateDistanceFilterAction(value.round()))),
converter: (store) => store.state.distanceFilter),
),
],
Expand Down

0 comments on commit 2bde5e4

Please sign in to comment.