diff --git a/lib/widgets/filter_grids/albums_page.dart b/lib/widgets/filter_grids/albums_page.dart index 86300f3f4..42cc65638 100644 --- a/lib/widgets/filter_grids/albums_page.dart +++ b/lib/widgets/filter_grids/albums_page.dart @@ -6,8 +6,8 @@ import 'package:aves/model/source/collection_source.dart'; import 'package:aves/model/source/enums.dart'; import 'package:aves/utils/android_file_utils.dart'; import 'package:aves/widgets/collection/empty.dart'; -import 'package:aves/widgets/common/aves_selection_dialog.dart'; import 'package:aves/widgets/common/icons.dart'; +import 'package:aves/widgets/filter_grids/chip_action_delegate.dart'; import 'package:aves/widgets/filter_grids/filter_grid_page.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -17,6 +17,8 @@ class AlbumListPage extends StatelessWidget { final CollectionSource source; + static final ChipActionDelegate actionDelegate = AlbumChipActionDelegate(); + const AlbumListPage({@required this.source}); @override @@ -31,7 +33,7 @@ class AlbumListPage extends StatelessWidget { builder: (context, snapshot) => FilterNavigationPage( source: source, title: 'Albums', - onChipActionSelected: _onChipActionSelected, + actionDelegate: actionDelegate, filterEntries: getAlbumEntries(source), filterBuilder: (s) => AlbumFilter(s, source.getUniqueAlbumName(s)), emptyBuilder: () => EmptyContent( @@ -45,27 +47,6 @@ class AlbumListPage extends StatelessWidget { ); } - void _onChipActionSelected(BuildContext context, ChipAction action) async { - switch (action) { - case ChipAction.sort: - final factor = await showDialog( - context: context, - builder: (context) => AvesSelectionDialog( - initialValue: settings.albumSortFactor, - options: { - ChipSortFactor.date: 'By date', - ChipSortFactor.name: 'By name', - }, - title: 'Sort', - ), - ); - if (factor != null) { - settings.albumSortFactor = factor; - } - break; - } - } - // common with album selection page to move/copy entries static Map getAlbumEntries(CollectionSource source) { diff --git a/lib/widgets/filter_grids/chip_action_delegate.dart b/lib/widgets/filter_grids/chip_action_delegate.dart new file mode 100644 index 000000000..7eb741d7b --- /dev/null +++ b/lib/widgets/filter_grids/chip_action_delegate.dart @@ -0,0 +1,65 @@ +import 'package:aves/model/settings/settings.dart'; +import 'package:aves/model/source/enums.dart'; +import 'package:aves/utils/durations.dart'; +import 'package:aves/widgets/common/aves_selection_dialog.dart'; +import 'package:aves/widgets/filter_grids/chip_actions.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; + +abstract class ChipActionDelegate { + ChipSortFactor get sortFactor; + + set sortFactor(ChipSortFactor factor); + + Future onChipActionSelected(BuildContext context, ChipAction action) async { + // wait for the popup menu to hide before proceeding with the action + await Future.delayed(Durations.popupMenuAnimation * timeDilation); + + switch (action) { + case ChipAction.sort: + await _showSortDialog(context); + break; + } + } + + Future _showSortDialog(BuildContext context) async { + final factor = await showDialog( + context: context, + builder: (context) => AvesSelectionDialog( + initialValue: sortFactor, + options: { + ChipSortFactor.date: 'By date', + ChipSortFactor.name: 'By name', + }, + title: 'Sort', + ), + ); + if (factor != null) { + sortFactor = factor; + } + } +} + +class AlbumChipActionDelegate extends ChipActionDelegate { + @override + ChipSortFactor get sortFactor => settings.albumSortFactor; + + @override + set sortFactor(ChipSortFactor factor) => settings.albumSortFactor = factor; +} + +class CountryChipActionDelegate extends ChipActionDelegate { + @override + ChipSortFactor get sortFactor => settings.countrySortFactor; + + @override + set sortFactor(ChipSortFactor factor) => settings.countrySortFactor = factor; +} + +class TagChipActionDelegate extends ChipActionDelegate { + @override + ChipSortFactor get sortFactor => settings.tagSortFactor; + + @override + set sortFactor(ChipSortFactor factor) => settings.tagSortFactor = factor; +} diff --git a/lib/widgets/filter_grids/chip_actions.dart b/lib/widgets/filter_grids/chip_actions.dart new file mode 100644 index 000000000..5e0984543 --- /dev/null +++ b/lib/widgets/filter_grids/chip_actions.dart @@ -0,0 +1,3 @@ +enum ChipAction { + sort, +} diff --git a/lib/widgets/filter_grids/countries_page.dart b/lib/widgets/filter_grids/countries_page.dart index 032c50bbb..8a2362a74 100644 --- a/lib/widgets/filter_grids/countries_page.dart +++ b/lib/widgets/filter_grids/countries_page.dart @@ -5,8 +5,8 @@ import 'package:aves/model/source/collection_source.dart'; import 'package:aves/model/source/enums.dart'; import 'package:aves/model/source/location.dart'; import 'package:aves/widgets/collection/empty.dart'; -import 'package:aves/widgets/common/aves_selection_dialog.dart'; import 'package:aves/widgets/common/icons.dart'; +import 'package:aves/widgets/filter_grids/chip_action_delegate.dart'; import 'package:aves/widgets/filter_grids/filter_grid_page.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -16,6 +16,8 @@ class CountryListPage extends StatelessWidget { final CollectionSource source; + static final ChipActionDelegate actionDelegate = CountryChipActionDelegate(); + const CountryListPage({@required this.source}); @override @@ -28,7 +30,7 @@ class CountryListPage extends StatelessWidget { builder: (context, snapshot) => FilterNavigationPage( source: source, title: 'Countries', - onChipActionSelected: _onChipActionSelected, + actionDelegate: actionDelegate, filterEntries: _getCountryEntries(), filterBuilder: (s) => LocationFilter(LocationLevel.country, s), emptyBuilder: () => EmptyContent( @@ -41,27 +43,6 @@ class CountryListPage extends StatelessWidget { ); } - void _onChipActionSelected(BuildContext context, ChipAction action) async { - switch (action) { - case ChipAction.sort: - final factor = await showDialog( - context: context, - builder: (context) => AvesSelectionDialog( - initialValue: settings.countrySortFactor, - options: { - ChipSortFactor.date: 'By date', - ChipSortFactor.name: 'By name', - }, - title: 'Sort', - ), - ); - if (factor != null) { - settings.countrySortFactor = factor; - } - break; - } - } - Map _getCountryEntries() { final entriesByDate = source.sortedEntriesForFilterList; final locatedEntries = entriesByDate.where((entry) => entry.isLocated); diff --git a/lib/widgets/filter_grids/filter_grid_page.dart b/lib/widgets/filter_grids/filter_grid_page.dart index 4ef83fc00..1d35d2a1e 100644 --- a/lib/widgets/filter_grids/filter_grid_page.dart +++ b/lib/widgets/filter_grids/filter_grid_page.dart @@ -16,18 +16,19 @@ import 'package:aves/widgets/common/double_back_pop.dart'; import 'package:aves/widgets/common/icons.dart'; import 'package:aves/widgets/common/menu_row.dart'; import 'package:aves/widgets/drawer/app_drawer.dart'; +import 'package:aves/widgets/filter_grids/chip_action_delegate.dart'; +import 'package:aves/widgets/filter_grids/chip_actions.dart'; import 'package:aves/widgets/filter_grids/decorated_filter_chip.dart'; import 'package:aves/widgets/filter_grids/search_button.dart'; import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/scheduler.dart'; import 'package:flutter_staggered_animations/flutter_staggered_animations.dart'; import 'package:provider/provider.dart'; class FilterNavigationPage extends StatelessWidget { final CollectionSource source; final String title; - final void Function(BuildContext context, ChipAction action) onChipActionSelected; + final ChipActionDelegate actionDelegate; final Map filterEntries; final CollectionFilter Function(String key) filterBuilder; final Widget Function() emptyBuilder; @@ -35,7 +36,7 @@ class FilterNavigationPage extends StatelessWidget { const FilterNavigationPage({ @required this.source, @required this.title, - @required this.onChipActionSelected, + @required this.actionDelegate, @required this.filterEntries, @required this.filterBuilder, @required this.emptyBuilder, @@ -95,11 +96,7 @@ class FilterNavigationPage extends StatelessWidget { ), ]; }, - onSelected: (action) async { - // wait for the popup menu to hide before proceeding with the action - await Future.delayed(Durations.popupMenuAnimation * timeDilation); - onChipActionSelected(context, action); - }, + onSelected: (action) => actionDelegate.onChipActionSelected(context, action), ), ]; } @@ -219,7 +216,3 @@ class FilterGridPage extends StatelessWidget { ); } } - -enum ChipAction { - sort, -} diff --git a/lib/widgets/filter_grids/tags_page.dart b/lib/widgets/filter_grids/tags_page.dart index d67cb551e..90533f253 100644 --- a/lib/widgets/filter_grids/tags_page.dart +++ b/lib/widgets/filter_grids/tags_page.dart @@ -5,8 +5,8 @@ import 'package:aves/model/source/collection_source.dart'; import 'package:aves/model/source/enums.dart'; import 'package:aves/model/source/tag.dart'; import 'package:aves/widgets/collection/empty.dart'; -import 'package:aves/widgets/common/aves_selection_dialog.dart'; import 'package:aves/widgets/common/icons.dart'; +import 'package:aves/widgets/filter_grids/chip_action_delegate.dart'; import 'package:aves/widgets/filter_grids/filter_grid_page.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -16,6 +16,8 @@ class TagListPage extends StatelessWidget { final CollectionSource source; + static final ChipActionDelegate actionDelegate = TagChipActionDelegate(); + const TagListPage({@required this.source}); @override @@ -28,7 +30,7 @@ class TagListPage extends StatelessWidget { builder: (context, snapshot) => FilterNavigationPage( source: source, title: 'Tags', - onChipActionSelected: _onChipActionSelected, + actionDelegate: actionDelegate, filterEntries: _getTagEntries(), filterBuilder: (s) => TagFilter(s), emptyBuilder: () => EmptyContent( @@ -41,27 +43,6 @@ class TagListPage extends StatelessWidget { ); } - void _onChipActionSelected(BuildContext context, ChipAction action) async { - switch (action) { - case ChipAction.sort: - final factor = await showDialog( - context: context, - builder: (context) => AvesSelectionDialog( - initialValue: settings.tagSortFactor, - options: { - ChipSortFactor.date: 'By date', - ChipSortFactor.name: 'By name', - }, - title: 'Sort', - ), - ); - if (factor != null) { - settings.tagSortFactor = factor; - } - break; - } - } - Map _getTagEntries() { final entriesByDate = source.sortedEntriesForFilterList; final tags = source.sortedTags