mirror of
https://github.com/immich-app/immich.git
synced 2024-12-26 10:50:29 +02:00
Add album list response caching
This commit is contained in:
parent
75d8ca1306
commit
d310c77fc8
@ -29,3 +29,7 @@ const String backupRequireCharging = "immichBackupRequireCharging"; // Key 3
|
||||
// Asset cache
|
||||
const String assetListCacheBox = "assetListCacheBox";
|
||||
const String assetListCachedAssets = "assetListCachedAssets";
|
||||
|
||||
// Album cache
|
||||
const String albumListCacheBox = "albumListCacheBox";
|
||||
const String albumListCachedAssets = "albumListCachedAssets";
|
||||
|
@ -38,6 +38,7 @@ void main() async {
|
||||
await Hive.openBox(hiveGithubReleaseInfoBox);
|
||||
await Hive.openBox(userSettingInfoBox);
|
||||
await Hive.openBox(assetListCacheBox);
|
||||
await Hive.openBox(albumListCacheBox);
|
||||
|
||||
SystemChrome.setSystemUIOverlayStyle(
|
||||
const SystemUiOverlayStyle(
|
||||
|
@ -1,22 +1,35 @@
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/modules/album/services/album.service.dart';
|
||||
import 'package:immich_mobile/modules/album/services/album_cache.service.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
|
||||
AlbumNotifier(this._albumService) : super([]);
|
||||
AlbumNotifier(this._albumService, this._albumCacheService) : super([]);
|
||||
final AlbumService _albumService;
|
||||
final AlbumCacheService _albumCacheService;
|
||||
|
||||
_cacheState() {
|
||||
_albumCacheService.put(state);
|
||||
}
|
||||
|
||||
getAllAlbums() async {
|
||||
|
||||
if (_albumCacheService.isValid() && state.isEmpty) {
|
||||
state = await _albumCacheService.getAsync();
|
||||
}
|
||||
|
||||
List<AlbumResponseDto>? albums =
|
||||
await _albumService.getAlbums(isShared: false);
|
||||
|
||||
if (albums != null) {
|
||||
state = albums;
|
||||
_cacheState();
|
||||
}
|
||||
}
|
||||
|
||||
deleteAlbum(String albumId) {
|
||||
state = state.where((album) => album.id != albumId).toList();
|
||||
_cacheState();
|
||||
}
|
||||
|
||||
Future<AlbumResponseDto?> createAlbum(
|
||||
@ -28,6 +41,8 @@ class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
|
||||
|
||||
if (album != null) {
|
||||
state = [...state, album];
|
||||
_cacheState();
|
||||
|
||||
return album;
|
||||
}
|
||||
return null;
|
||||
@ -36,5 +51,8 @@ class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
|
||||
|
||||
final albumProvider =
|
||||
StateNotifierProvider<AlbumNotifier, List<AlbumResponseDto>>((ref) {
|
||||
return AlbumNotifier(ref.watch(albumServiceProvider));
|
||||
return AlbumNotifier(
|
||||
ref.watch(albumServiceProvider),
|
||||
ref.watch(albumCacheServiceProvider),
|
||||
);
|
||||
});
|
||||
|
38
mobile/lib/modules/album/services/album_cache.service.dart
Normal file
38
mobile/lib/modules/album/services/album_cache.service.dart
Normal file
@ -0,0 +1,38 @@
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:immich_mobile/modules/home/services/asset_cache.service.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class AlbumCacheService extends JsonCache<List<AlbumResponseDto>> {
|
||||
AlbumCacheService() : super(albumListCacheBox, albumListCachedAssets);
|
||||
|
||||
@override
|
||||
void put(List<AlbumResponseDto> data) {
|
||||
putRawData(data.map((e) => e.toJson()).toList());
|
||||
}
|
||||
|
||||
@override
|
||||
List<AlbumResponseDto> get() {
|
||||
try {
|
||||
final mapList = readRawData() as List<dynamic>;
|
||||
|
||||
final responseData = mapList
|
||||
.map((e) => AlbumResponseDto.fromJson(e))
|
||||
.whereNotNull()
|
||||
.toList();
|
||||
|
||||
return responseData;
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final albumCacheServiceProvider = Provider(
|
||||
(ref) => AlbumCacheService(),
|
||||
);
|
@ -4,36 +4,55 @@ import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
final assetCacheServiceProvider = Provider(
|
||||
(ref) => AssetCacheService(),
|
||||
);
|
||||
abstract class JsonCache<T> {
|
||||
final String boxName;
|
||||
final String valueKey;
|
||||
final Box _cacheBox;
|
||||
|
||||
class AssetCacheService {
|
||||
final _cacheBox = Hive.box(assetListCacheBox);
|
||||
JsonCache(this.boxName, this.valueKey) : _cacheBox = Hive.box(boxName);
|
||||
|
||||
bool isValid() {
|
||||
return _cacheBox.containsKey(assetListCachedAssets) &&
|
||||
_cacheBox.get(assetListCachedAssets) is String;
|
||||
return _cacheBox.containsKey(valueKey) && _cacheBox.get(valueKey) is String;
|
||||
}
|
||||
|
||||
void invalidate() {
|
||||
_cacheBox.clear();
|
||||
}
|
||||
|
||||
void putAssets(List<AssetResponseDto> assets) {
|
||||
final mapList = assets.map((e) => e.toJson()).toList();
|
||||
final jsonString = json.encode(mapList);
|
||||
|
||||
_cacheBox.put(assetListCachedAssets, jsonString);
|
||||
void putRawData(dynamic data) {
|
||||
final jsonString = json.encode(data);
|
||||
_cacheBox.put(valueKey, jsonString);
|
||||
}
|
||||
|
||||
List<AssetResponseDto> getAssets() {
|
||||
dynamic readRawData() {
|
||||
return json.decode(_cacheBox.get(valueKey));
|
||||
}
|
||||
|
||||
void put(T data);
|
||||
|
||||
T get();
|
||||
|
||||
Future<T> getAsync() async {
|
||||
return Future.microtask(() => get());
|
||||
}
|
||||
}
|
||||
|
||||
class AssetCacheService extends JsonCache<List<AssetResponseDto>> {
|
||||
AssetCacheService() : super(assetListCacheBox, assetListCachedAssets);
|
||||
|
||||
@override
|
||||
void put(List<AssetResponseDto> data) {
|
||||
putRawData(data.map((e) => e.toJson()).toList());
|
||||
}
|
||||
|
||||
@override
|
||||
List<AssetResponseDto> get() {
|
||||
try {
|
||||
final jsonString = _cacheBox.get(assetListCachedAssets);
|
||||
final mapList = json.decode(jsonString) as List<dynamic>;
|
||||
final mapList = readRawData() as List<dynamic>;
|
||||
|
||||
final responseData = mapList
|
||||
.map((e) => AssetResponseDto.fromJson(e))
|
||||
@ -48,7 +67,8 @@ class AssetCacheService {
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<AssetResponseDto>> getAssetsAsync() async {
|
||||
return Future.microtask(() => getAssets());
|
||||
}
|
||||
}
|
||||
|
||||
final assetCacheServiceProvider = Provider(
|
||||
(ref) => AssetCacheService(),
|
||||
);
|
||||
|
@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:immich_mobile/modules/album/services/album_cache.service.dart';
|
||||
import 'package:immich_mobile/modules/home/services/asset_cache.service.dart';
|
||||
import 'package:immich_mobile/modules/login/models/authentication_state.model.dart';
|
||||
import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
|
||||
@ -17,6 +18,7 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
||||
this._backupService,
|
||||
this._apiService,
|
||||
this._assetCacheService,
|
||||
this._albumCacheService,
|
||||
) : super(
|
||||
AuthenticationState(
|
||||
deviceId: "",
|
||||
@ -44,6 +46,7 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
||||
final BackupService _backupService;
|
||||
final ApiService _apiService;
|
||||
final AssetCacheService _assetCacheService;
|
||||
final AlbumCacheService _albumCacheService;
|
||||
|
||||
Future<bool> login(
|
||||
String email,
|
||||
@ -155,6 +158,7 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
||||
Hive.box(userInfoBox).delete(accessTokenKey);
|
||||
state = state.copyWith(isAuthenticated: false);
|
||||
_assetCacheService.invalidate();
|
||||
_albumCacheService.invalidate();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -201,5 +205,6 @@ final authenticationProvider =
|
||||
ref.watch(backupServiceProvider),
|
||||
ref.watch(apiServiceProvider),
|
||||
ref.watch(assetCacheServiceProvider),
|
||||
ref.watch(albumCacheServiceProvider),
|
||||
);
|
||||
});
|
||||
|
@ -17,7 +17,7 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
||||
AssetNotifier(this._assetService, this._assetCacheService) : super([]);
|
||||
|
||||
_cacheState() {
|
||||
_assetCacheService.putAssets(state);
|
||||
_assetCacheService.put(state);
|
||||
}
|
||||
|
||||
getAllAsset() async {
|
||||
@ -26,7 +26,7 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
||||
|
||||
if (_assetCacheService.isValid() && state.isEmpty) {
|
||||
stopwatch.start();
|
||||
state = await _assetCacheService.getAssetsAsync();
|
||||
state = await _assetCacheService.getAsync();
|
||||
debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms");
|
||||
stopwatch.reset();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user