mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
feat(mobile): Cache assets and albums for faster loading speed
feat(mobile): Cache assets and albums for faster loading speed
This commit is contained in:
commit
061b229e12
7 changed files with 220 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/modules/home/services/asset.service.dart';
|
||||
import 'package:immich_mobile/modules/home/services/asset_cache.service.dart';
|
||||
import 'package:immich_mobile/shared/services/device_info.service.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
|
@ -9,24 +10,50 @@ import 'package:photo_manager/photo_manager.dart';
|
|||
|
||||
class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
||||
final AssetService _assetService;
|
||||
final AssetCacheService _assetCacheService;
|
||||
|
||||
final DeviceInfoService _deviceInfoService = DeviceInfoService();
|
||||
|
||||
AssetNotifier(this._assetService) : super([]);
|
||||
AssetNotifier(this._assetService, this._assetCacheService) : super([]);
|
||||
|
||||
_cacheState() {
|
||||
_assetCacheService.put(state);
|
||||
}
|
||||
|
||||
getAllAsset() async {
|
||||
final stopwatch = Stopwatch();
|
||||
|
||||
|
||||
if (await _assetCacheService.isValid() && state.isEmpty) {
|
||||
stopwatch.start();
|
||||
state = await _assetCacheService.get();
|
||||
debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms");
|
||||
stopwatch.reset();
|
||||
}
|
||||
|
||||
stopwatch.start();
|
||||
var allAssets = await _assetService.getAllAsset();
|
||||
debugPrint("Query assets from API: ${stopwatch.elapsedMilliseconds}ms");
|
||||
stopwatch.reset();
|
||||
|
||||
if (allAssets != null) {
|
||||
state = allAssets;
|
||||
|
||||
stopwatch.start();
|
||||
_cacheState();
|
||||
debugPrint("Store assets in cache: ${stopwatch.elapsedMilliseconds}ms");
|
||||
stopwatch.reset();
|
||||
}
|
||||
}
|
||||
|
||||
clearAllAsset() {
|
||||
state = [];
|
||||
_cacheState();
|
||||
}
|
||||
|
||||
onNewAssetUploaded(AssetResponseDto newAsset) {
|
||||
state = [...state, newAsset];
|
||||
_cacheState();
|
||||
}
|
||||
|
||||
deleteAssets(Set<AssetResponseDto> deleteAssets) async {
|
||||
|
|
@ -65,12 +92,15 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
|||
state.where((immichAsset) => immichAsset.id != asset.id).toList();
|
||||
}
|
||||
}
|
||||
|
||||
_cacheState();
|
||||
}
|
||||
}
|
||||
|
||||
final assetProvider =
|
||||
StateNotifierProvider<AssetNotifier, List<AssetResponseDto>>((ref) {
|
||||
return AssetNotifier(ref.watch(assetServiceProvider));
|
||||
return AssetNotifier(
|
||||
ref.watch(assetServiceProvider), ref.watch(assetCacheServiceProvider));
|
||||
});
|
||||
|
||||
final assetGroupByDateTimeProvider = StateProvider((ref) {
|
||||
|
|
|
|||
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