diff --git a/packages/smooth_app/lib/database/dao_string_list.dart b/packages/smooth_app/lib/database/dao_string_list.dart index ff42487464f..57be9377a3f 100644 --- a/packages/smooth_app/lib/database/dao_string_list.dart +++ b/packages/smooth_app/lib/database/dao_string_list.dart @@ -20,6 +20,9 @@ class DaoStringList extends AbstractDao { /// Key for the list of latest languages used in the app. static const String keyLanguages = 'languages'; + /// Key for the list of favorite stores (for price additions). + static const String keyPriceStores = 'priceStores'; + /// Max lengths of each key (null means no limit). static const Map _maxLengths = { keySearchProductHistory: 10, diff --git a/packages/smooth_app/lib/pages/locations/favorite_location_helper.dart b/packages/smooth_app/lib/pages/locations/favorite_location_helper.dart new file mode 100644 index 00000000000..6ba2dc09fae --- /dev/null +++ b/packages/smooth_app/lib/pages/locations/favorite_location_helper.dart @@ -0,0 +1,48 @@ +import 'package:smooth_app/database/dao_string_list.dart'; +import 'package:smooth_app/database/local_database.dart'; +import 'package:smooth_app/pages/locations/osm_location.dart'; + +/// Helper used to set/unset/sort stores as user favorites. +class FavoriteLocationHelper { + static const String _key = DaoStringList.keyPriceStores; + + /// Sets a store as a favorite store (or not, depending on [isFavorite]). + Future setFavorite( + final LocalDatabase localDatabase, + final OsmLocation location, + final bool isFavorite, + ) async { + final DaoStringList daoStringList = DaoStringList(localDatabase); + final String locationKey = _locationToString(location); + await daoStringList.remove(_key, locationKey); + if (isFavorite) { + await daoStringList.add(_key, locationKey); + } + } + + /// Returns true if a store was flagged as favorite. + bool isFavorite( + final LocalDatabase localDatabase, + final OsmLocation location, + ) { + final DaoStringList daoStringList = DaoStringList(localDatabase); + final List favorites = daoStringList.getAll(_key); + return _isFavorite(favorites, location); + } + + /// Returns true if a store was flagged as favorite, from stored keys. + bool _isFavorite( + final List favorites, + final OsmLocation location, + ) { + for (final String favorite in favorites) { + if (favorite == _locationToString(location)) { + return true; + } + } + return false; + } + + String _locationToString(final OsmLocation location) => + '${location.osmType}-${location.osmId}'; +} diff --git a/packages/smooth_app/lib/pages/locations/search_location_preloaded_item.dart b/packages/smooth_app/lib/pages/locations/search_location_preloaded_item.dart index a616b1dd51e..5f231769aaa 100644 --- a/packages/smooth_app/lib/pages/locations/search_location_preloaded_item.dart +++ b/packages/smooth_app/lib/pages/locations/search_location_preloaded_item.dart @@ -4,6 +4,7 @@ import 'package:smooth_app/database/dao_osm_location.dart'; import 'package:smooth_app/database/local_database.dart'; import 'package:smooth_app/generic_lib/design_constants.dart'; import 'package:smooth_app/generic_lib/widgets/smooth_card.dart'; +import 'package:smooth_app/pages/locations/favorite_location_helper.dart'; import 'package:smooth_app/pages/locations/location_map_page.dart'; import 'package:smooth_app/pages/locations/osm_location.dart'; import 'package:smooth_app/pages/product/common/search_preloaded_item.dart'; @@ -20,10 +21,23 @@ class SearchLocationPreloadedItem extends SearchPreloadedItem { final BuildContext context, { final VoidCallback? onDismissItem, }) { + final LocalDatabase localDatabase = context.read(); + final bool isFavorite = FavoriteLocationHelper().isFavorite( + localDatabase, + osmLocation, + ); final String? title = osmLocation.getTitle(); final String? subtitle = osmLocation.getSubtitle(); final Widget child = SmoothCard( child: ListTile( + leading: IconButton( + icon: Icon(isFavorite ? Icons.favorite : Icons.favorite_border), + onPressed: () async => FavoriteLocationHelper().setFavorite( + localDatabase, + osmLocation, + !isFavorite, + ), + ), onTap: () { if (popFirst) { // pops this result page