From be15e04aa31abe3fe60f3ae021e0ff348bb55020 Mon Sep 17 00:00:00 2001 From: Mikolaj Kieres Date: Sun, 11 Jul 2021 12:17:38 +1000 Subject: [PATCH] Fixing filtering by number of players and the number of sent analytics when filtering by number of players --- .../collections/collection_filter_panel.dart | 7 +++++++ .../pages/collections/collections_page.dart | 14 ++++---------- .../lib/stores/board_games_filters_store.dart | 13 +++++++++---- .../lib/stores/board_games_store.dart | 18 ++++++++---------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/board_games_companion/lib/pages/collections/collection_filter_panel.dart b/board_games_companion/lib/pages/collections/collection_filter_panel.dart index 29c50646..67b5036e 100644 --- a/board_games_companion/lib/pages/collections/collection_filter_panel.dart +++ b/board_games_companion/lib/pages/collections/collection_filter_panel.dart @@ -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); @@ -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, ); diff --git a/board_games_companion/lib/pages/collections/collections_page.dart b/board_games_companion/lib/pages/collections/collections_page.dart index 9e95ff35..1397d258 100644 --- a/board_games_companion/lib/pages/collections/collections_page.dart +++ b/board_games_companion/lib/pages/collections/collections_page.dart @@ -80,17 +80,11 @@ class _Collection extends StatelessWidget { child: PageContainer( child: CustomScrollView( slivers: [ - _SearchBar( - boardGamesStore: _boardGamesStore, - ), + _SearchBar(boardGamesStore: _boardGamesStore), if (hasNoSearchResults) - _EmptySearchResult( - boardGamesStore: _boardGamesStore, - ), - if (!hasNoSearchResults) - _Grid( - boardGamesStore: _boardGamesStore, - ), + _EmptySearchResult(boardGamesStore: _boardGamesStore) + else + _Grid(boardGamesStore: _boardGamesStore), ], ), ), diff --git a/board_games_companion/lib/stores/board_games_filters_store.dart b/board_games_companion/lib/stores/board_games_filters_store.dart index 36e6d754..f2213abc 100644 --- a/board_games_companion/lib/stores/board_games_filters_store.dart +++ b/board_games_companion/lib/stores/board_games_filters_store.dart @@ -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 loadFilterPreferences() async { _collectionFilters = @@ -117,13 +120,17 @@ class BoardGamesFiltersStore with ChangeNotifier { notifyListeners(); } - Future updateNumberOfPlayers(int numberOfPlayers) async { + Future changeNumberOfPlayers(int numberOfPlayers) async { if (_collectionFilters == null) { _collectionFilters = CollectionFilters(); } _collectionFilters.numberOfPlayers = numberOfPlayers; + notifyListeners(); + } + + Future updateNumberOfPlayers(int numberOfPlayers) async { await _analyticsService.logEvent( name: Analytics.FilterCollection, parameters: { @@ -135,8 +142,6 @@ class BoardGamesFiltersStore with ChangeNotifier { await _boardGamesFiltersService .addOrUpdateCollectionFilters(_collectionFilters); - - notifyListeners(); } @override diff --git a/board_games_companion/lib/stores/board_games_store.dart b/board_games_companion/lib/stores/board_games_store.dart index 9b27c832..67f25df1 100644 --- a/board_games_companion/lib/stores/board_games_store.dart +++ b/board_games_companion/lib/stores/board_games_store.dart @@ -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) {