2023-06-27 16:00:20 -05:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2025-06-16 22:01:16 +07:00
|
|
|
import 'package:immich_mobile/extensions/translate_extensions.dart';
|
2024-04-30 21:36:40 -05:00
|
|
|
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';
|
2024-09-24 14:50:21 +02:00
|
|
|
import 'package:immich_mobile/repositories/asset.repository.dart';
|
2024-05-02 15:59:14 -05:00
|
|
|
import 'package:immich_mobile/services/api.service.dart';
|
2023-06-27 16:00:20 -05:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
final memoryServiceProvider = StateProvider<MemoryService>((ref) {
|
|
|
|
return MemoryService(
|
|
|
|
ref.watch(apiServiceProvider),
|
2024-09-24 14:50:21 +02:00
|
|
|
ref.watch(assetRepositoryProvider),
|
2023-06-27 16:00:20 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
class MemoryService {
|
|
|
|
final log = Logger("MemoryService");
|
|
|
|
|
|
|
|
final ApiService _apiService;
|
2025-06-21 17:41:02 -05:00
|
|
|
final AssetRepository _assetRepository;
|
2023-06-27 16:00:20 -05:00
|
|
|
|
2024-09-24 14:50:21 +02:00
|
|
|
MemoryService(this._apiService, this._assetRepository);
|
2023-06-27 16:00:20 -05:00
|
|
|
|
|
|
|
Future<List<Memory>?> getMemoryLane() async {
|
|
|
|
try {
|
|
|
|
final now = DateTime.now();
|
2025-02-25 19:10:31 -06:00
|
|
|
final data = await _apiService.memoriesApi.searchMemories(
|
2025-03-04 06:54:54 -06:00
|
|
|
for_: DateTime.utc(now.year, now.month, now.day, 0, 0, 0),
|
2023-06-27 16:00:20 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
if (data == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Memory> memories = [];
|
2025-02-25 19:10:31 -06:00
|
|
|
|
|
|
|
for (final memory in data) {
|
|
|
|
final dbAssets = await _assetRepository
|
|
|
|
.getAllByRemoteId(memory.assets.map((e) => e.id));
|
|
|
|
final yearsAgo = now.year - memory.data.year;
|
2024-05-14 17:35:37 +02:00
|
|
|
if (dbAssets.isNotEmpty) {
|
2025-06-16 22:01:16 +07:00
|
|
|
final String title = 'years_ago'.t(
|
|
|
|
args: {
|
|
|
|
'years': yearsAgo.toString(),
|
|
|
|
},
|
|
|
|
);
|
2024-05-14 17:35:37 +02:00
|
|
|
memories.add(
|
|
|
|
Memory(
|
2024-06-17 01:54:15 +10:00
|
|
|
title: title,
|
2024-05-14 17:35:37 +02:00
|
|
|
assets: dbAssets,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-06-27 16:00:20 -05: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 16:00:20 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2025-06-24 09:20:24 -05:00
|
|
|
|
|
|
|
Future<Memory?> getMemoryById(String id) async {
|
|
|
|
try {
|
|
|
|
final memoryResponse = await _apiService.memoriesApi.getMemory(id);
|
|
|
|
|
|
|
|
if (memoryResponse == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final dbAssets = await _assetRepository
|
|
|
|
.getAllByRemoteId(memoryResponse.assets.map((e) => e.id));
|
|
|
|
if (dbAssets.isEmpty) {
|
|
|
|
log.warning("No assets found for memory with ID: $id");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final yearsAgo = DateTime.now().year - memoryResponse.data.year;
|
|
|
|
final String title = 'years_ago'.t(
|
|
|
|
args: {
|
|
|
|
'years': yearsAgo.toString(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return Memory(
|
|
|
|
title: title,
|
|
|
|
assets: dbAssets,
|
|
|
|
);
|
|
|
|
} catch (error, stack) {
|
|
|
|
log.severe("Cannot get memory with ID: $id", error, stack);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 16:00:20 -05:00
|
|
|
}
|