-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into contribute-to-your-country-link
- Loading branch information
Showing
5 changed files
with
84 additions
and
2 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
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
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