2024-03-04 11:24:25 +07:00
|
|
|
import 'package:appflowy/shared/appflowy_cache_manager.dart';
|
2024-03-25 22:08:52 +07:00
|
|
|
import 'package:appflowy/startup/tasks/prelude.dart';
|
|
|
|
import 'package:file/file.dart' hide FileSystem;
|
|
|
|
import 'package:file/local.dart';
|
2024-01-20 23:16:18 +08:00
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
2024-03-25 22:08:52 +07:00
|
|
|
import 'package:path/path.dart' as p;
|
2024-01-20 23:16:18 +08:00
|
|
|
|
2024-03-04 11:24:25 +07:00
|
|
|
class CustomImageCacheManager extends CacheManager
|
|
|
|
with ImageCacheManager
|
|
|
|
implements ICache {
|
2024-03-25 22:08:52 +07:00
|
|
|
CustomImageCacheManager._()
|
|
|
|
: super(
|
|
|
|
Config(
|
|
|
|
key,
|
|
|
|
fileSystem: CustomIOFileSystem(key),
|
|
|
|
),
|
|
|
|
);
|
2024-01-20 23:16:18 +08:00
|
|
|
|
2024-01-25 16:37:36 +01:00
|
|
|
factory CustomImageCacheManager() => _instance;
|
2024-01-20 23:16:18 +08:00
|
|
|
|
2024-01-25 16:37:36 +01:00
|
|
|
static final CustomImageCacheManager _instance = CustomImageCacheManager._();
|
2024-01-20 23:16:18 +08:00
|
|
|
|
2024-03-25 22:08:52 +07:00
|
|
|
static const key = 'image_cache';
|
2024-03-04 11:24:25 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<int> cacheSize() async {
|
|
|
|
// https://github.com/Baseflow/flutter_cache_manager/issues/239#issuecomment-719475429
|
|
|
|
// this package does not provide a way to get the cache size
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> clearAll() async {
|
|
|
|
await emptyCache();
|
|
|
|
}
|
2024-01-20 23:16:18 +08:00
|
|
|
}
|
2024-03-25 22:08:52 +07:00
|
|
|
|
|
|
|
class CustomIOFileSystem implements FileSystem {
|
|
|
|
CustomIOFileSystem(this._cacheKey) : _fileDir = createDirectory(_cacheKey);
|
|
|
|
final Future<Directory> _fileDir;
|
|
|
|
final String _cacheKey;
|
|
|
|
|
|
|
|
static Future<Directory> createDirectory(String key) async {
|
|
|
|
final baseDir = await appFlowyApplicationDataDirectory();
|
|
|
|
final path = p.join(baseDir.path, key);
|
|
|
|
|
|
|
|
const fs = LocalFileSystem();
|
|
|
|
final directory = fs.directory(path);
|
|
|
|
await directory.create(recursive: true);
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<File> createFile(String name) async {
|
|
|
|
final directory = await _fileDir;
|
|
|
|
if (!(await directory.exists())) {
|
|
|
|
await createDirectory(_cacheKey);
|
|
|
|
}
|
|
|
|
return directory.childFile(name);
|
|
|
|
}
|
|
|
|
}
|