2023-06-27 23:00:20 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
|
|
|
import 'package:immich_mobile/modules/memories/models/memory.dart';
|
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2023-12-26 23:41:51 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
2023-06-27 23:00:20 +02:00
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2023-12-26 23:41:51 +02: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 23:41:51 +02:00
|
|
|
ref.watch(dbProvider),
|
2023-06-27 23:00:20 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
class MemoryService {
|
|
|
|
final log = Logger("MemoryService");
|
|
|
|
|
|
|
|
final ApiService _apiService;
|
2023-12-26 23:41:51 +02:00
|
|
|
final Isar _db;
|
2023-06-27 23:00:20 +02:00
|
|
|
|
2023-12-26 23:41:51 +02:00
|
|
|
MemoryService(this._apiService, this._db);
|
2023-06-27 23:00:20 +02:00
|
|
|
|
|
|
|
Future<List<Memory>?> getMemoryLane() async {
|
|
|
|
try {
|
|
|
|
final now = DateTime.now();
|
|
|
|
final data = await _apiService.assetApi.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 = [];
|
|
|
|
for (final MemoryLaneResponseDto(:title, :assets) in data) {
|
|
|
|
memories.add(
|
|
|
|
Memory(
|
|
|
|
title: title,
|
2023-12-26 23:41:51 +02:00
|
|
|
assets: await _db.assets.getAllByRemoteId(assets.map((e) => e.id)),
|
2023-06-27 23:00:20 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return memories.isNotEmpty ? memories : null;
|
|
|
|
} catch (error, stack) {
|
|
|
|
log.severe("Cannot get memories ${error.toString()}", error, stack);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|