Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 5400 - flagging price stores as favorites #5533

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading