ente/lib/favorite_photos_repository.dart

48 lines
1.3 KiB
Dart
Raw Normal View History

import 'package:photos/core/event_bus.dart';
import 'package:photos/events/local_photos_updated_event.dart';
2020-05-06 16:43:03 +00:00
import 'package:photos/models/photo.dart';
import 'package:shared_preferences/shared_preferences.dart';
class FavoritePhotosRepository {
static final _favoritePhotoIdsKey = "favorite_photo_ids";
FavoritePhotosRepository._privateConstructor();
static FavoritePhotosRepository instance =
FavoritePhotosRepository._privateConstructor();
SharedPreferences _preferences;
Future<void> init() async {
_preferences = await SharedPreferences.getInstance();
}
bool isLiked(Photo photo) {
2020-05-06 18:13:24 +00:00
return getLiked().contains(photo.generatedId.toString());
}
bool hasFavorites() {
return getLiked().isNotEmpty;
2020-05-06 16:43:03 +00:00
}
Future<bool> setLiked(Photo photo, bool isLiked) {
2020-05-06 18:13:24 +00:00
final liked = getLiked();
2020-05-06 16:43:03 +00:00
if (isLiked) {
liked.add(photo.generatedId.toString());
} else {
liked.remove(photo.generatedId.toString());
}
Bus.instance.fire(LocalPhotosUpdatedEvent());
2020-05-06 16:43:03 +00:00
return _preferences
.setStringList(_favoritePhotoIdsKey, liked.toList())
.then((_) => isLiked);
}
2020-05-06 18:13:24 +00:00
Set<String> getLiked() {
2020-05-06 16:43:03 +00:00
final value = _preferences.getStringList(_favoritePhotoIdsKey);
if (value == null) {
return Set<String>();
} else {
return value.toSet();
}
}
}