This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:collection';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
@@ -6,12 +7,19 @@ import '../entities/photo.dart';
|
||||
|
||||
class PhotoRepository {
|
||||
final String basePath;
|
||||
final Map<int, List<Photo>> _map = HashMap();
|
||||
final Map<int, DateTime> _updatedMap = HashMap();
|
||||
|
||||
PhotoRepository(this.basePath);
|
||||
|
||||
Future<List<Photo>> getPhotosByAlbumId(int id) async {
|
||||
final dir = Directory(basePath);
|
||||
if (_updatedMap.containsKey(id) &&
|
||||
DateTime.now().difference(_updatedMap[id]!).inSeconds <= 60) {
|
||||
return _map[id]!;
|
||||
}
|
||||
_updatedMap[id] = DateTime.now();
|
||||
|
||||
final dir = Directory(basePath);
|
||||
if (!await dir.exists()) {
|
||||
return [];
|
||||
}
|
||||
@@ -57,10 +65,15 @@ class PhotoRepository {
|
||||
);
|
||||
});
|
||||
|
||||
return Future.wait(photoFutures);
|
||||
final photos = await Future.wait(photoFutures);
|
||||
_map[id] = List.from(photos);
|
||||
return photos;
|
||||
}
|
||||
|
||||
Future<Photo?> getPhotoById(int id) async {
|
||||
try {
|
||||
return _map.values.expand((l) => l).firstWhere((p) => p.id == id);
|
||||
} catch (e) {}
|
||||
final dir = Directory(basePath);
|
||||
|
||||
if (!await dir.exists()) {
|
||||
|
||||
Reference in New Issue
Block a user