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

44 lines
1.1 KiB
Dart
Raw Normal View History

2022-10-14 23:57:55 +02:00
import 'package:collection/collection.dart';
2022-10-15 23:20:15 +02:00
import 'package:flutter/foundation.dart';
2022-10-14 23:57:55 +02:00
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/shared/models/asset.dart';
import 'package:immich_mobile/shared/services/json_cache.dart';
class AssetCacheService extends JsonCache<List<Asset>> {
AssetCacheService() : super("asset_cache");
2022-10-17 14:53:27 +02:00
static Future<List<Map<String, dynamic>>> _computeSerialize(
List<Asset> assets,
) async {
return assets.map((e) => e.toJson()).toList();
}
2022-10-17 14:53:27 +02:00
@override
void put(List<Asset> data) async {
putRawData(await compute(_computeSerialize, data));
}
static Future<List<Asset>> _computeEncode(List<dynamic> data) async {
return data.map((e) => Asset.fromJson(e)).whereNotNull().toList();
2022-10-17 14:53:27 +02:00
}
@override
Future<List<Asset>> get() async {
2022-10-15 23:20:15 +02:00
try {
2022-10-17 16:40:51 +02:00
final mapList = await readRawData() as List<dynamic>;
2022-10-14 23:57:55 +02:00
final responseData = await compute(_computeEncode, mapList);
2022-10-14 23:57:55 +02:00
2022-10-15 23:20:15 +02:00
return responseData;
} catch (e) {
debugPrint(e.toString());
2022-10-14 23:57:55 +02:00
2022-10-15 23:20:15 +02:00
return [];
}
2022-10-14 23:57:55 +02:00
}
}
2022-10-17 14:53:27 +02:00
final assetCacheServiceProvider = Provider(
(ref) => AssetCacheService(),
2022-10-17 14:53:27 +02:00
);