ente/lib/favorite_photos_repository.dart
2020-06-07 01:37:23 +05:30

48 lines
1.3 KiB
Dart

import 'package:photos/core/event_bus.dart';
import 'package:photos/events/local_photos_updated_event.dart';
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) {
return getLiked().contains(photo.generatedId.toString());
}
bool hasFavorites() {
return getLiked().isNotEmpty;
}
Future<bool> setLiked(Photo photo, bool isLiked) {
final liked = getLiked();
if (isLiked) {
liked.add(photo.generatedId.toString());
} else {
liked.remove(photo.generatedId.toString());
}
Bus.instance.fire(LocalPhotosUpdatedEvent());
return _preferences
.setStringList(_favoritePhotoIdsKey, liked.toList())
.then((_) => isLiked);
}
Set<String> getLiked() {
final value = _preferences.getStringList(_favoritePhotoIdsKey);
if (value == null) {
return Set<String>();
} else {
return value.toSet();
}
}
}