Simplify favorites
This commit is contained in:
parent
28541b8246
commit
2bbb27a55c
5 changed files with 41 additions and 45 deletions
|
@ -341,6 +341,18 @@ class FilesDB {
|
|||
}
|
||||
}
|
||||
|
||||
Future<bool> doesFileExistInCollection(
|
||||
int uploadedFileID, int collectionID) async {
|
||||
final db = await instance.database;
|
||||
final rows = await db.query(
|
||||
table,
|
||||
where: '$columnUploadedFileID = ? AND $columnCollectionID = ?',
|
||||
whereArgs: [uploadedFileID, collectionID],
|
||||
limit: 1,
|
||||
);
|
||||
return rows.isNotEmpty;
|
||||
}
|
||||
|
||||
List<File> _convertToFiles(List<Map<String, dynamic>> results) {
|
||||
final files = List<File>();
|
||||
for (final result in results) {
|
||||
|
|
|
@ -6,7 +6,6 @@ import 'package:path_provider/path_provider.dart';
|
|||
import 'package:photos/core/constants.dart';
|
||||
import 'package:photos/core/configuration.dart';
|
||||
import 'package:photos/services/collections_service.dart';
|
||||
import 'package:photos/services/favorites_service.dart';
|
||||
import 'package:photos/services/memories_service.dart';
|
||||
import 'package:photos/services/sync_service.dart';
|
||||
import 'package:photos/ui/home_widget.dart';
|
||||
|
@ -34,7 +33,6 @@ void _main() async {
|
|||
await CollectionsService.instance.init();
|
||||
await SyncService.instance.init();
|
||||
await MemoriesService.instance.init();
|
||||
await FavoritesService.instance.init();
|
||||
_sync();
|
||||
|
||||
final SentryClient sentry = new SentryClient(dsn: SENTRY_DSN);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -128,18 +128,6 @@ class _CollectionsGalleryWidgetState extends State<CollectionsGalleryWidget> {
|
|||
});
|
||||
|
||||
final collectionsWithThumbnail = List<CollectionWithThumbnail>();
|
||||
final favorites = FavoritesService.instance.getFavoriteFiles().toList();
|
||||
favorites.sort((first, second) {
|
||||
return second.creationTime.compareTo(first.creationTime);
|
||||
});
|
||||
if (favorites.length > 0) {
|
||||
final favoritesCollection =
|
||||
await FavoritesService.instance.getFavoritesCollection();
|
||||
final lastUpdatedFile = await FilesDB.instance
|
||||
.getLastModifiedFileInCollection(favoritesCollection.id);
|
||||
collectionsWithThumbnail.add(CollectionWithThumbnail(
|
||||
favoritesCollection, favorites[0], lastUpdatedFile));
|
||||
}
|
||||
final collections = CollectionsService.instance.getCollections();
|
||||
|
||||
for (final c in collections) {
|
||||
|
|
|
@ -178,8 +178,22 @@ class _DetailPageState extends State<DetailPage> {
|
|||
|
||||
Widget _getFavoriteButton() {
|
||||
final file = _files[_selectedIndex];
|
||||
|
||||
return FutureBuilder(
|
||||
future: FavoritesService.instance.isFavorite(file),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return _getLikeButton(file, snapshot.data);
|
||||
} else {
|
||||
return _getLikeButton(file, false);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _getLikeButton(File file, bool isLiked) {
|
||||
return LikeButton(
|
||||
isLiked: FavoritesService.instance.isLiked(file.uploadedFileID),
|
||||
isLiked: isLiked,
|
||||
onTap: (oldValue) async {
|
||||
final isLiked = !oldValue;
|
||||
bool hasError = false;
|
||||
|
|
Loading…
Add table
Reference in a new issue