|
@@ -6,16 +6,13 @@ import 'package:photos/models/file.dart';
|
|
|
import 'package:photos/services/collections_service.dart';
|
|
|
import 'package:photos/utils/crypto_util.dart';
|
|
|
import 'package:photos/utils/file_uploader.dart';
|
|
|
-import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
class FavoritesService {
|
|
|
- static final _favoritesCollectionIDKey = "favorites_collection_id";
|
|
|
-
|
|
|
- final _cachedFavoriteFiles = Set<File>();
|
|
|
Configuration _config;
|
|
|
CollectionsService _collectionsService;
|
|
|
FileUploader _fileUploader;
|
|
|
FilesDB _filesDB;
|
|
|
+ int _cachedFavoritesCollectionID;
|
|
|
|
|
|
FavoritesService._privateConstructor() {
|
|
|
_config = Configuration.instance;
|
|
@@ -25,23 +22,13 @@ class FavoritesService {
|
|
|
}
|
|
|
static FavoritesService instance = FavoritesService._privateConstructor();
|
|
|
|
|
|
- SharedPreferences _preferences;
|
|
|
-
|
|
|
- Future<void> init() async {
|
|
|
- _preferences = await SharedPreferences.getInstance();
|
|
|
- if (_preferences.containsKey(_favoritesCollectionIDKey)) {
|
|
|
- final collectionID = _preferences.getInt(_favoritesCollectionIDKey);
|
|
|
- _cachedFavoriteFiles
|
|
|
- .addAll((await _filesDB.getAllInCollection(collectionID)).toSet());
|
|
|
+ Future<bool> isFavorite(File file) async {
|
|
|
+ final collection = await _getFavoritesCollection();
|
|
|
+ if (collection == null) {
|
|
|
+ return false;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- Set<File> getFavoriteFiles() {
|
|
|
- return _cachedFavoriteFiles;
|
|
|
- }
|
|
|
-
|
|
|
- bool isLiked(File file) {
|
|
|
- return _cachedFavoriteFiles.contains(file);
|
|
|
+ return _filesDB.doesFileExistInCollection(
|
|
|
+ file.uploadedFileID, collection.id);
|
|
|
}
|
|
|
|
|
|
Future<void> addToFavorites(File file) async {
|
|
@@ -52,7 +39,6 @@ class FavoritesService {
|
|
|
await _filesDB.update(uploadedFile);
|
|
|
} else {
|
|
|
await _collectionsService.addToCollection(collectionID, [file]);
|
|
|
- _cachedFavoriteFiles.add(file);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -63,28 +49,26 @@ class FavoritesService {
|
|
|
// Do nothing, ignore
|
|
|
} else {
|
|
|
await _collectionsService.removeFromCollection(collectionID, [file]);
|
|
|
- _cachedFavoriteFiles.remove(file);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- Future<Collection> getFavoritesCollection() async {
|
|
|
- if (!_preferences.containsKey(_favoritesCollectionIDKey)) {
|
|
|
+ Future<Collection> _getFavoritesCollection() async {
|
|
|
+ if (_cachedFavoritesCollectionID == null) {
|
|
|
final collections = _collectionsService.getCollections();
|
|
|
for (final collection in collections) {
|
|
|
if (collection.type == CollectionType.favorites) {
|
|
|
- await _preferences.setInt(_favoritesCollectionIDKey, collection.id);
|
|
|
+ _cachedFavoritesCollectionID = collection.id;
|
|
|
return collection;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
- return _collectionsService
|
|
|
- .getCollectionByID(_preferences.getInt(_favoritesCollectionIDKey));
|
|
|
+ return _collectionsService.getCollectionByID(_cachedFavoritesCollectionID);
|
|
|
}
|
|
|
|
|
|
Future<int> _getOrCreateFavoriteCollectionID() async {
|
|
|
- if (_preferences.containsKey(_favoritesCollectionIDKey)) {
|
|
|
- return _preferences.getInt(_favoritesCollectionIDKey);
|
|
|
+ if (_cachedFavoritesCollectionID != null) {
|
|
|
+ return _cachedFavoritesCollectionID;
|
|
|
}
|
|
|
final key = CryptoUtil.generateKey();
|
|
|
final encryptedKeyData = CryptoUtil.encryptSync(key, _config.getKey());
|
|
@@ -100,7 +84,7 @@ class FavoritesService {
|
|
|
CollectionAttributes(),
|
|
|
null,
|
|
|
));
|
|
|
- await _preferences.setInt(_favoritesCollectionIDKey, collection.id);
|
|
|
+ _cachedFavoritesCollectionID = collection.id;
|
|
|
return collection.id;
|
|
|
}
|
|
|
}
|