mirror of
https://github.com/immich-app/immich
synced 2025-10-17 18:19:27 +00:00
Refactor abstract class to separate file
This commit is contained in:
parent
d77e25425e
commit
3617433858
3 changed files with 51 additions and 50 deletions
49
mobile/lib/shared/services/json_cache.dart
Normal file
49
mobile/lib/shared/services/json_cache.dart
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
abstract class JsonCache<T> {
|
||||
final String cacheFileName;
|
||||
|
||||
JsonCache(this.cacheFileName);
|
||||
|
||||
Future<File> _getCacheFile() async {
|
||||
final basePath = await getTemporaryDirectory();
|
||||
final basePathName = basePath.path;
|
||||
|
||||
final file = File("$basePathName/$cacheFileName.bin");
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
Future<bool> isValid() async {
|
||||
final file = await _getCacheFile();
|
||||
return await file.exists();
|
||||
}
|
||||
|
||||
Future<void> invalidate() async {
|
||||
final file = await _getCacheFile();
|
||||
await file.delete();
|
||||
}
|
||||
|
||||
Future<void> putRawData(dynamic data) async {
|
||||
final jsonString = json.encode(data);
|
||||
final file = await _getCacheFile();
|
||||
|
||||
if (!await file.exists()) {
|
||||
await file.create();
|
||||
}
|
||||
|
||||
await file.writeAsString(jsonString);
|
||||
}
|
||||
|
||||
dynamic readRawData() async {
|
||||
final file = await _getCacheFile();
|
||||
final data = await file.readAsString();
|
||||
return json.decode(data);
|
||||
}
|
||||
|
||||
void put(T data);
|
||||
Future<T> get();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue