1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-20 00:38:24 +02:00
immich/mobile/lib/modules/album/services/album_cache.service.dart

47 lines
1.2 KiB
Dart
Raw Normal View History

2022-10-17 14:53:27 +02:00
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/shared/models/album.dart';
import 'package:immich_mobile/shared/services/json_cache.dart';
2022-10-17 14:53:27 +02:00
class BaseAlbumCacheService extends JsonCache<List<Album>> {
2022-10-18 14:06:35 +02:00
BaseAlbumCacheService(super.cacheFileName);
2022-10-17 14:53:27 +02:00
@override
void put(List<Album> data) {
2022-10-17 14:53:27 +02:00
putRawData(data.map((e) => e.toJson()).toList());
}
@override
Future<List<Album>?> get() async {
2022-10-17 14:53:27 +02:00
try {
2022-10-17 16:40:51 +02:00
final mapList = await readRawData() as List<dynamic>;
2022-10-17 14:53:27 +02:00
final responseData =
mapList.map((e) => Album.fromJson(e)).whereNotNull().toList();
2022-10-17 14:53:27 +02:00
return responseData;
} catch (e) {
await invalidate();
2022-10-17 14:53:27 +02:00
debugPrint(e.toString());
return null;
2022-10-17 14:53:27 +02:00
}
}
2022-10-18 14:06:35 +02:00
}
class AlbumCacheService extends BaseAlbumCacheService {
AlbumCacheService() : super("album_cache");
}
2022-10-17 14:53:27 +02:00
2022-10-18 14:06:35 +02:00
class SharedAlbumCacheService extends BaseAlbumCacheService {
SharedAlbumCacheService() : super("shared_album_cache");
2022-10-17 14:53:27 +02:00
}
final albumCacheServiceProvider = Provider(
(ref) => AlbumCacheService(),
2022-10-17 14:53:27 +02:00
);
2022-10-18 14:06:35 +02:00
final sharedAlbumCacheServiceProvider = Provider(
(ref) => SharedAlbumCacheService(),
2022-10-18 14:06:35 +02:00
);