Skip to content

Commit

Permalink
Fixing filtering by number of players and the number of sent analytic…
Browse files Browse the repository at this point in the history
…s when filtering by number of players
  • Loading branch information
mkieres committed Jul 11, 2021
1 parent 0159350 commit be15e04
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,11 @@ class _FilterNumberOfPlayersSlider extends StatelessWidget {
@override
Widget build(BuildContext context) {
final minNumberOfPlayers = boardGamesStore.allboardGames
.where((boardGameDetails) => boardGameDetails.minPlayers != null)
.map((boardGameDetails) => boardGameDetails.minPlayers)
?.reduce(min);
final maxNumberOfPlayers = boardGamesStore.allboardGames
.where((boardGameDetails) => boardGameDetails.maxPlayers != null)
.map((boardGameDetails) => boardGameDetails.maxPlayers)
?.reduce(max);

Expand Down Expand Up @@ -351,6 +353,11 @@ class _FilterNumberOfPlayersSlider extends StatelessWidget {
? boardGamesFiltersStore.numberOfPlayers?.toString()
: 'Any',
onChanged: (value) {
boardGamesFiltersStore.changeNumberOfPlayers(
value != 0 ? value.round() : null,
);
},
onChangeEnd: (value) {
boardGamesFiltersStore.updateNumberOfPlayers(
value != 0 ? value.round() : null,
);
Expand Down
14 changes: 4 additions & 10 deletions board_games_companion/lib/pages/collections/collections_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,11 @@ class _Collection extends StatelessWidget {
child: PageContainer(
child: CustomScrollView(
slivers: <Widget>[
_SearchBar(
boardGamesStore: _boardGamesStore,
),
_SearchBar(boardGamesStore: _boardGamesStore),
if (hasNoSearchResults)
_EmptySearchResult(
boardGamesStore: _boardGamesStore,
),
if (!hasNoSearchResults)
_Grid(
boardGamesStore: _boardGamesStore,
),
_EmptySearchResult(boardGamesStore: _boardGamesStore)
else
_Grid(boardGamesStore: _boardGamesStore),
],
),
),
Expand Down
13 changes: 9 additions & 4 deletions board_games_companion/lib/stores/board_games_filters_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class BoardGamesFiltersStore with ChangeNotifier {
double get filterByRating => _collectionFilters?.filterByRating;
int get numberOfPlayers => _collectionFilters?.numberOfPlayers;

BoardGamesFiltersStore(this._boardGamesFiltersService, this._analyticsService);
BoardGamesFiltersStore(
this._boardGamesFiltersService,
this._analyticsService,
);

Future<void> loadFilterPreferences() async {
_collectionFilters =
Expand Down Expand Up @@ -117,13 +120,17 @@ class BoardGamesFiltersStore with ChangeNotifier {
notifyListeners();
}

Future<void> updateNumberOfPlayers(int numberOfPlayers) async {
Future<void> changeNumberOfPlayers(int numberOfPlayers) async {
if (_collectionFilters == null) {
_collectionFilters = CollectionFilters();
}

_collectionFilters.numberOfPlayers = numberOfPlayers;

notifyListeners();
}

Future<void> updateNumberOfPlayers(int numberOfPlayers) async {
await _analyticsService.logEvent(
name: Analytics.FilterCollection,
parameters: {
Expand All @@ -135,8 +142,6 @@ class BoardGamesFiltersStore with ChangeNotifier {

await _boardGamesFiltersService
.addOrUpdateCollectionFilters(_collectionFilters);

notifyListeners();
}

@override
Expand Down
18 changes: 8 additions & 10 deletions board_games_companion/lib/stores/board_games_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,25 +247,23 @@ class BoardGamesStore with ChangeNotifier {
}

void applyFilters() {
if (filteredBoardGames?.isEmpty ?? true) {
return;
}

final selectedSortBy = _boardGamesFiltersStore.sortBy.firstWhere(
(sb) => sb.selected,
orElse: () => null,
);

_filteredBoardGames = _allBoardGames
.where((boardGame) =>
?.where((boardGame) =>
(_boardGamesFiltersStore.filterByRating == null ||
boardGame.rating >= _boardGamesFiltersStore.filterByRating) &&
(_boardGamesFiltersStore.numberOfPlayers == null ||
(boardGame.maxPlayers >=
_boardGamesFiltersStore.numberOfPlayers &&
boardGame.minPlayers <=
_boardGamesFiltersStore.numberOfPlayers)))
.toList();
(boardGame.maxPlayers != null &&
boardGame.minPlayers != null &&
(boardGame.maxPlayers >=
_boardGamesFiltersStore.numberOfPlayers &&
boardGame.minPlayers <=
_boardGamesFiltersStore.numberOfPlayers))))
?.toList();

if (selectedSortBy != null) {
filteredBoardGames.sort((a, b) {
Expand Down

0 comments on commit be15e04

Please sign in to comment.