-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 5400 - flagging stores as favorite (#5533)
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
1 parent
be819cb
commit e7154a6
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/smooth_app/lib/pages/locations/favorite_location_helper.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters