2022-10-14 14:57:55 -07:00
|
|
|
import 'package:collection/collection.dart';
|
2022-10-15 14:20:15 -07:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-10-14 14:57:55 -07:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-11-08 10:00:24 -07:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2022-10-19 13:03:54 -07:00
|
|
|
import 'package:immich_mobile/shared/services/json_cache.dart';
|
2022-10-17 09:02:43 -07:00
|
|
|
|
2022-11-08 10:00:24 -07:00
|
|
|
class AssetCacheService extends JsonCache<List<Asset>> {
|
2022-10-17 09:02:43 -07:00
|
|
|
AssetCacheService() : super("asset_cache");
|
2022-10-17 05:53:27 -07:00
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
static Future<List<Map<String, dynamic>>> _computeSerialize(
|
2023-01-26 07:40:19 -07:00
|
|
|
List<Asset> assets,
|
|
|
|
) async {
|
2023-01-18 08:59:23 -07:00
|
|
|
return assets.map((e) => e.toJson()).toList();
|
|
|
|
}
|
|
|
|
|
2022-10-17 05:53:27 -07:00
|
|
|
@override
|
2023-01-18 08:59:23 -07:00
|
|
|
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 05:53:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-02-04 13:42:42 -07:00
|
|
|
Future<List<Asset>?> get() async {
|
2022-10-15 14:20:15 -07:00
|
|
|
try {
|
2022-10-17 07:40:51 -07:00
|
|
|
final mapList = await readRawData() as List<dynamic>;
|
2023-01-18 08:59:23 -07:00
|
|
|
final responseData = await compute(_computeEncode, mapList);
|
2022-10-15 14:20:15 -07:00
|
|
|
return responseData;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint(e.toString());
|
2023-02-04 13:42:42 -07:00
|
|
|
await invalidate();
|
|
|
|
return null;
|
2022-10-15 14:20:15 -07:00
|
|
|
}
|
2022-10-14 14:57:55 -07:00
|
|
|
}
|
|
|
|
}
|
2022-10-17 05:53:27 -07:00
|
|
|
|
|
|
|
final assetCacheServiceProvider = Provider(
|
2022-11-08 10:00:24 -07:00
|
|
|
(ref) => AssetCacheService(),
|
2022-10-17 05:53:27 -07:00
|
|
|
);
|