diff --git a/mobile/lib/modules/album/providers/shared_album.provider.dart b/mobile/lib/modules/album/providers/shared_album.provider.dart index 202e8241f0..c759f3263b 100644 --- a/mobile/lib/modules/album/providers/shared_album.provider.dart +++ b/mobile/lib/modules/album/providers/shared_album.provider.dart @@ -1,12 +1,18 @@ import 'package:flutter/material.dart'; 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 SharedAlbumNotifier extends StateNotifier> { - SharedAlbumNotifier(this._sharedAlbumService) : super([]); + SharedAlbumNotifier(this._sharedAlbumService, this._sharedAlbumCacheService) : super([]); final AlbumService _sharedAlbumService; + final SharedAlbumCacheService _sharedAlbumCacheService; + + _cacheState() { + _sharedAlbumCacheService.put(state); + } Future createSharedAlbum( String albumName, @@ -22,6 +28,7 @@ class SharedAlbumNotifier extends StateNotifier> { if (newAlbum != null) { state = [...state, newAlbum]; + _cacheState(); } return newAlbum; @@ -33,16 +40,22 @@ class SharedAlbumNotifier extends StateNotifier> { } getAllSharedAlbums() async { + if (await _sharedAlbumCacheService.isValid() && state.isEmpty) { + state = await _sharedAlbumCacheService.get(); + } + List? sharedAlbums = await _sharedAlbumService.getAlbums(isShared: true); if (sharedAlbums != null) { state = sharedAlbums; + _cacheState(); } } deleteAlbum(String albumId) async { state = state.where((album) => album.id != albumId).toList(); + _cacheState(); } Future leaveAlbum(String albumId) async { @@ -50,6 +63,7 @@ class SharedAlbumNotifier extends StateNotifier> { if (res) { state = state.where((album) => album.id != albumId).toList(); + _cacheState(); return true; } else { return false; @@ -72,7 +86,10 @@ class SharedAlbumNotifier extends StateNotifier> { final sharedAlbumProvider = StateNotifierProvider>((ref) { - return SharedAlbumNotifier(ref.watch(albumServiceProvider)); + return SharedAlbumNotifier( + ref.watch(albumServiceProvider), + ref.watch(sharedAlbumCacheServiceProvider), + ); }); final sharedAlbumDetailProvider = FutureProvider.autoDispose diff --git a/mobile/lib/modules/album/services/album_cache.service.dart b/mobile/lib/modules/album/services/album_cache.service.dart index d7ed5b7334..4db41a3775 100644 --- a/mobile/lib/modules/album/services/album_cache.service.dart +++ b/mobile/lib/modules/album/services/album_cache.service.dart @@ -5,8 +5,8 @@ import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:immich_mobile/modules/home/services/asset_cache.service.dart'; import 'package:openapi/api.dart'; -class AlbumCacheService extends JsonCache> { - AlbumCacheService() : super("album_cache"); +class BaseAlbumCacheService extends JsonCache> { + BaseAlbumCacheService(super.cacheFileName); @override void put(List data) { @@ -29,9 +29,21 @@ class AlbumCacheService extends JsonCache> { return []; } } +} +class AlbumCacheService extends BaseAlbumCacheService { + AlbumCacheService() : super("album_cache"); +} + +class SharedAlbumCacheService extends BaseAlbumCacheService { + SharedAlbumCacheService() : super("shared_album_cache"); } final albumCacheServiceProvider = Provider( (ref) => AlbumCacheService(), ); + +final sharedAlbumCacheServiceProvider = Provider( + (ref) => SharedAlbumCacheService(), +); + diff --git a/mobile/lib/modules/login/providers/authentication.provider.dart b/mobile/lib/modules/login/providers/authentication.provider.dart index 8b8f039853..4778ad3fcc 100644 --- a/mobile/lib/modules/login/providers/authentication.provider.dart +++ b/mobile/lib/modules/login/providers/authentication.provider.dart @@ -19,6 +19,7 @@ class AuthenticationNotifier extends StateNotifier { this._apiService, this._assetCacheService, this._albumCacheService, + this._sharedAlbumCacheService, ) : super( AuthenticationState( deviceId: "", @@ -47,6 +48,7 @@ class AuthenticationNotifier extends StateNotifier { final ApiService _apiService; final AssetCacheService _assetCacheService; final AlbumCacheService _albumCacheService; + final SharedAlbumCacheService _sharedAlbumCacheService; Future login( String email, @@ -159,6 +161,7 @@ class AuthenticationNotifier extends StateNotifier { state = state.copyWith(isAuthenticated: false); _assetCacheService.invalidate(); _albumCacheService.invalidate(); + _sharedAlbumCacheService.invalidate(); return true; } @@ -206,5 +209,6 @@ final authenticationProvider = ref.watch(apiServiceProvider), ref.watch(assetCacheServiceProvider), ref.watch(albumCacheServiceProvider), + ref.watch(sharedAlbumCacheServiceProvider), ); });