2024-06-16 17:54:15 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2023-06-27 23:00:20 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-01 04:36:40 +02:00
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart';
|
|
|
|
import 'package:immich_mobile/models/memories/memory.model.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/providers/api.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/db.provider.dart';
|
|
|
|
import 'package:immich_mobile/services/api.service.dart';
|
2023-12-26 22:41:51 +01:00
|
|
|
import 'package:isar/isar.dart';
|
2023-06-27 23:00:20 +02:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:openapi/api.dart';
|
|
|
|
|
|
|
|
final memoryServiceProvider = StateProvider<MemoryService>((ref) {
|
|
|
|
return MemoryService(
|
|
|
|
ref.watch(apiServiceProvider),
|
2023-12-26 22:41:51 +01:00
|
|
|
ref.watch(dbProvider),
|
2023-06-27 23:00:20 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
class MemoryService {
|
|
|
|
final log = Logger("MemoryService");
|
|
|
|
|
|
|
|
final ApiService _apiService;
|
2023-12-26 22:41:51 +01:00
|
|
|
final Isar _db;
|
2023-06-27 23:00:20 +02:00
|
|
|
|
2023-12-26 22:41:51 +01:00
|
|
|
MemoryService(this._apiService, this._db);
|
2023-06-27 23:00:20 +02:00
|
|
|
|
|
|
|
Future<List<Memory>?> getMemoryLane() async {
|
|
|
|
try {
|
|
|
|
final now = DateTime.now();
|
2024-05-30 00:26:57 +02:00
|
|
|
final data = await _apiService.assetsApi.getMemoryLane(
|
2023-10-05 00:11:11 +02:00
|
|
|
now.day,
|
|
|
|
now.month,
|
2023-06-27 23:00:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (data == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Memory> memories = [];
|
2024-05-24 16:37:01 +02:00
|
|
|
for (final MemoryLaneResponseDto(:yearsAgo, :assets) in data) {
|
2024-05-14 17:35:37 +02:00
|
|
|
final dbAssets =
|
|
|
|
await _db.assets.getAllByRemoteId(assets.map((e) => e.id));
|
|
|
|
if (dbAssets.isNotEmpty) {
|
2024-06-16 17:54:15 +02:00
|
|
|
final String title = yearsAgo <= 1
|
|
|
|
? 'memories_year_ago'.tr()
|
|
|
|
: 'memories_years_ago'.tr(args: [yearsAgo.toString()]);
|
2024-05-14 17:35:37 +02:00
|
|
|
memories.add(
|
|
|
|
Memory(
|
2024-06-16 17:54:15 +02:00
|
|
|
title: title,
|
2024-05-14 17:35:37 +02:00
|
|
|
assets: dbAssets,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-06-27 23:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return memories.isNotEmpty ? memories : null;
|
|
|
|
} catch (error, stack) {
|
2024-02-24 04:38:57 +01:00
|
|
|
log.severe("Cannot get memories", error, stack);
|
2023-06-27 23:00:20 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|