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';
|
2023-02-06 09:13:32 +02:00
|
|
|
import 'package:immich_mobile/shared/models/album.dart';
|
2022-10-19 22:03:54 +02:00
|
|
|
import 'package:immich_mobile/shared/services/json_cache.dart';
|
2022-10-17 14:53:27 +02:00
|
|
|
|
2023-02-06 09:13:32 +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
|
2023-02-06 09:13:32 +02:00
|
|
|
void put(List<Album> data) {
|
2022-10-17 14:53:27 +02:00
|
|
|
putRawData(data.map((e) => e.toJson()).toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-02-06 09:13:32 +02:00
|
|
|
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
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
final responseData =
|
|
|
|
mapList.map((e) => Album.fromJson(e)).whereNotNull().toList();
|
2022-10-17 14:53:27 +02:00
|
|
|
|
|
|
|
return responseData;
|
|
|
|
} catch (e) {
|
2023-02-06 09:13:32 +02:00
|
|
|
await invalidate();
|
2022-10-17 14:53:27 +02:00
|
|
|
debugPrint(e.toString());
|
2023-02-06 09:13:32 +02:00
|
|
|
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(
|
2023-02-06 09:13:32 +02:00
|
|
|
(ref) => AlbumCacheService(),
|
2022-10-17 14:53:27 +02:00
|
|
|
);
|
2022-10-18 14:06:35 +02:00
|
|
|
|
|
|
|
final sharedAlbumCacheServiceProvider = Provider(
|
2023-02-06 09:13:32 +02:00
|
|
|
(ref) => SharedAlbumCacheService(),
|
2022-10-18 14:06:35 +02:00
|
|
|
);
|