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