mirror of
https://github.com/immich-app/immich.git
synced 2025-06-05 06:35:07 -04:00
JSON based caching
This commit is contained in:
parent
1156290377
commit
894eea739e
@ -1,4 +1,7 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:hive_flutter/hive_flutter.dart';
|
import 'package:hive_flutter/hive_flutter.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:immich_mobile/constants/hive_box.dart';
|
import 'package:immich_mobile/constants/hive_box.dart';
|
||||||
@ -8,77 +11,40 @@ final assetCacheServiceProvider = Provider(
|
|||||||
(ref) => AssetCacheService(),
|
(ref) => AssetCacheService(),
|
||||||
);
|
);
|
||||||
|
|
||||||
typedef CacheEntry = Map<dynamic, dynamic>;
|
|
||||||
typedef CacheList = List<CacheEntry>;
|
|
||||||
|
|
||||||
class AssetCacheService {
|
class AssetCacheService {
|
||||||
final _cacheBox = Hive.box(assetListCacheBox);
|
final _cacheBox = Hive.box(assetListCacheBox);
|
||||||
|
|
||||||
bool isValid() {
|
bool isValid() {
|
||||||
return _cacheBox.containsKey(assetListCachedAssets) && _rawGet().isNotEmpty;
|
return _cacheBox.containsKey(assetListCachedAssets) &&
|
||||||
|
_cacheBox.get(assetListCachedAssets) is String;
|
||||||
}
|
}
|
||||||
|
|
||||||
void putAssets(List<AssetResponseDto> assets) {
|
void putAssets(List<AssetResponseDto> assets) {
|
||||||
_rawPut(assets.map((e) => _serialize(e)).toList());
|
final mapList = assets.map((e) => e.toJson()).toList();
|
||||||
|
final jsonString = json.encode(mapList);
|
||||||
|
|
||||||
|
_cacheBox.put(assetListCachedAssets, jsonString);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<AssetResponseDto> getAssets() {
|
List<AssetResponseDto> getAssets() {
|
||||||
return _rawGet().map((e) => _deserialize(e)).whereNotNull().toList();
|
try {
|
||||||
|
final jsonString = _cacheBox.get(assetListCachedAssets);
|
||||||
|
final mapList = json.decode(jsonString) as List<dynamic>;
|
||||||
|
|
||||||
|
final responseData = mapList
|
||||||
|
.map((e) => AssetResponseDto.fromJson(e))
|
||||||
|
.whereNotNull()
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
return responseData;
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint(e.toString());
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<AssetResponseDto>> getAssetsAsync() async {
|
Future<List<AssetResponseDto>> getAssetsAsync() async {
|
||||||
return Future.microtask(() => getAssets());
|
return Future.microtask(() => getAssets());
|
||||||
}
|
}
|
||||||
|
|
||||||
List<dynamic> _rawGet() {
|
|
||||||
return _cacheBox.get(assetListCachedAssets) as List<dynamic>;
|
|
||||||
}
|
|
||||||
|
|
||||||
void _rawPut(CacheList data) {
|
|
||||||
_cacheBox.put(assetListCachedAssets, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
CacheEntry _serialize(AssetResponseDto a) {
|
|
||||||
return {
|
|
||||||
"id": a.id,
|
|
||||||
"cat": a.createdAt,
|
|
||||||
"did": a.deviceAssetId,
|
|
||||||
"oid": a.ownerId,
|
|
||||||
"dev": a.deviceId,
|
|
||||||
"dur": a.duration,
|
|
||||||
"mat": a.modifiedAt,
|
|
||||||
"opa": a.originalPath,
|
|
||||||
"typ": a.type.value,
|
|
||||||
"exif": a.exifInfo?.toJson(),
|
|
||||||
"fav": a.isFavorite,
|
|
||||||
"evp": a.encodedVideoPath,
|
|
||||||
"mim": a.mimeType,
|
|
||||||
"rsp": a.resizePath,
|
|
||||||
"wbp": a.webpPath,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
AssetResponseDto? _deserialize(CacheEntry map) {
|
|
||||||
try {
|
|
||||||
return AssetResponseDto(
|
|
||||||
type: AssetTypeEnum.values
|
|
||||||
.firstWhere((element) => element.value == map["typ"]),
|
|
||||||
id: map["id"],
|
|
||||||
deviceAssetId: map["did"],
|
|
||||||
ownerId: map["oid"],
|
|
||||||
deviceId: map["dev"],
|
|
||||||
originalPath: map["opa"],
|
|
||||||
resizePath: map["rsp"],
|
|
||||||
createdAt: map["cat"],
|
|
||||||
modifiedAt: map["mat"],
|
|
||||||
isFavorite: map["fav"],
|
|
||||||
mimeType: map["mim"],
|
|
||||||
duration: map["dur"],
|
|
||||||
webpPath: map["wbp"],
|
|
||||||
encodedVideoPath: map["evp"],
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,10 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
|||||||
|
|
||||||
AssetNotifier(this._assetService, this._assetCacheService) : super([]);
|
AssetNotifier(this._assetService, this._assetCacheService) : super([]);
|
||||||
|
|
||||||
|
_cacheState() {
|
||||||
|
_assetCacheService.putAssets(state);
|
||||||
|
}
|
||||||
|
|
||||||
getAllAsset() async {
|
getAllAsset() async {
|
||||||
if (_assetCacheService.isValid() && state.isEmpty) {
|
if (_assetCacheService.isValid() && state.isEmpty) {
|
||||||
state = await _assetCacheService.getAssetsAsync();
|
state = await _assetCacheService.getAssetsAsync();
|
||||||
@ -25,16 +29,18 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
|||||||
|
|
||||||
if (allAssets != null) {
|
if (allAssets != null) {
|
||||||
state = allAssets;
|
state = allAssets;
|
||||||
_assetCacheService.putAssets(allAssets);
|
_cacheState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clearAllAsset() {
|
clearAllAsset() {
|
||||||
state = [];
|
state = [];
|
||||||
|
_cacheState();
|
||||||
}
|
}
|
||||||
|
|
||||||
onNewAssetUploaded(AssetResponseDto newAsset) {
|
onNewAssetUploaded(AssetResponseDto newAsset) {
|
||||||
state = [...state, newAsset];
|
state = [...state, newAsset];
|
||||||
|
_cacheState();
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteAssets(Set<AssetResponseDto> deleteAssets) async {
|
deleteAssets(Set<AssetResponseDto> deleteAssets) async {
|
||||||
@ -73,6 +79,8 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
|||||||
state.where((immichAsset) => immichAsset.id != asset.id).toList();
|
state.where((immichAsset) => immichAsset.id != asset.id).toList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_cacheState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user