Add album list response caching

This commit is contained in:
Matthias Rupp
2022-10-17 14:53:27 +02:00
parent 75d8ca1306
commit d310c77fc8
7 changed files with 108 additions and 22 deletions

View File

@@ -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),
);
});

View 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(),
);