Skip to content

Commit

Permalink
feat: 5400 - flagging stores as favorite (#5533)
Browse files Browse the repository at this point in the history
New file:
* `favorite_location_helper.dart`: Helper used to set/unset/sort stores as user favorites.

Impacted files:
* `dao_string_list.dart`: added a key for favorite price stores
* `search_location_preloaded_item.dart`: added a "favorite" `IconButton` for price locations
  • Loading branch information
monsieurtanuki authored Aug 16, 2024
1 parent be819cb commit e7154a6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/smooth_app/lib/database/dao_string_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, int?> _maxLengths = <String, int?>{
keySearchProductHistory: 10,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> 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<String> favorites = daoStringList.getAll(_key);
return _isFavorite(favorites, location);
}

/// Returns true if a store was flagged as favorite, from stored keys.
bool _isFavorite(
final List<String> 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}';
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,10 +21,23 @@ class SearchLocationPreloadedItem extends SearchPreloadedItem {
final BuildContext context, {
final VoidCallback? onDismissItem,
}) {
final LocalDatabase localDatabase = context.read<LocalDatabase>();
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
Expand Down

0 comments on commit e7154a6

Please sign in to comment.