nc-photos/app/lib/cache_manager_util.dart

79 lines
2 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
// ignore: implementation_imports
import 'package:flutter_cache_manager/src/cache_store.dart';
class CancelableGetFile {
CancelableGetFile(this.store);
Future<FileInfo> getFileUntil(String key,
{bool ignoreMemCache = false}) async {
2021-07-23 22:05:57 +02:00
FileInfo? product;
2021-04-10 06:28:12 +02:00
while (product == null && _shouldRun) {
product = await store.getFile(key, ignoreMemCache: ignoreMemCache);
2021-09-15 08:58:06 +02:00
await Future.delayed(const Duration(milliseconds: 500));
2021-04-10 06:28:12 +02:00
}
2021-07-23 22:05:57 +02:00
if (product == null) {
return Future.error("Interrupted");
} else {
return product;
}
2021-04-10 06:28:12 +02:00
}
void cancel() {
_shouldRun = false;
}
bool get isGood => _shouldRun;
final CacheStore store;
bool _shouldRun = true;
}
2021-09-16 12:10:50 +02:00
/// Cache manager for thumbnails
///
/// Thumbnails are pretty small in file size and also critical to the scrolling
/// performance, thus a large number of them will be kept
class ThumbnailCacheManager {
static const key = "thumbnailCache";
static CacheManager inst = CacheManager(
Config(
key,
stalePeriod: const Duration(days: 30),
2023-03-19 16:13:06 +01:00
maxNrOfCacheObjects: 50000,
2021-09-16 12:10:50 +02:00
),
);
}
/// Cache manager for large images
///
/// Large images are only loaded when explicitly opening the photos, they are
/// very large in size. Since large images are only viewed one by one (unlike
/// thumbnails), they are less critical to the overall app responsiveness
class LargeImageCacheManager {
// used in file_paths.xml, must not change
2021-09-16 12:10:50 +02:00
static const key = "largeImageCache";
static CacheManager inst = CacheManager(
Config(
key,
stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 1000,
),
);
}
/// Cache manager for covers
///
/// Covers are larger than thumbnails but smaller than full sized photos. They
/// are used to represent a collection
class CoverCacheManager {
static const key = "coverCache";
static CacheManager inst = CacheManager(
Config(
key,
stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 300,
),
);
}