2023-11-06 17:46:26 +02:00
|
|
|
import 'package:immich_mobile/modules/activities/models/activity.model.dart';
|
2024-01-05 07:20:55 +02:00
|
|
|
import 'package:immich_mobile/modules/activities/providers/activity_service.provider.dart';
|
|
|
|
import 'package:immich_mobile/modules/activities/providers/activity_statistics.provider.dart';
|
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
2023-11-06 17:46:26 +02:00
|
|
|
|
2024-01-05 07:20:55 +02:00
|
|
|
part 'activity.provider.g.dart';
|
2023-11-06 17:46:26 +02:00
|
|
|
|
2024-01-05 07:20:55 +02:00
|
|
|
/// Maintains the current list of all activities for <share-album-id, asset>
|
|
|
|
@riverpod
|
|
|
|
class AlbumActivity extends _$AlbumActivity {
|
|
|
|
@override
|
|
|
|
Future<List<Activity>> build(String albumId, [String? assetId]) async {
|
|
|
|
return ref
|
|
|
|
.watch(activityServiceProvider)
|
|
|
|
.getAllActivities(albumId, assetId: assetId);
|
2023-11-06 17:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> removeActivity(String id) async {
|
2024-01-05 07:20:55 +02:00
|
|
|
if (await ref.watch(activityServiceProvider).removeActivity(id)) {
|
|
|
|
final activities = state.valueOrNull ?? [];
|
2023-11-06 17:46:26 +02:00
|
|
|
final removedActivity = activities.firstWhere((a) => a.id == id);
|
|
|
|
activities.remove(removedActivity);
|
|
|
|
state = AsyncData(activities);
|
2024-01-05 07:20:55 +02:00
|
|
|
// Decrement activity count only for comments
|
2023-11-06 17:46:26 +02:00
|
|
|
if (removedActivity.type == ActivityType.comment) {
|
2024-01-05 07:20:55 +02:00
|
|
|
ref
|
|
|
|
.watch(activityStatisticsProvider(albumId, assetId).notifier)
|
2023-11-06 17:46:26 +02:00
|
|
|
.removeActivity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addLike() async {
|
2024-01-05 07:20:55 +02:00
|
|
|
final activity = await ref
|
|
|
|
.watch(activityServiceProvider)
|
2023-11-06 17:46:26 +02:00
|
|
|
.addActivity(albumId, ActivityType.like, assetId: assetId);
|
2024-01-05 07:20:55 +02:00
|
|
|
if (activity.hasValue) {
|
2023-11-06 17:46:26 +02:00
|
|
|
final activities = state.asData?.value ?? [];
|
2024-01-05 07:20:55 +02:00
|
|
|
state = AsyncData([...activities, activity.requireValue]);
|
2023-11-06 17:46:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:20:55 +02:00
|
|
|
Future<void> addComment(String comment) async {
|
|
|
|
final activity = await ref.watch(activityServiceProvider).addActivity(
|
|
|
|
albumId,
|
|
|
|
ActivityType.comment,
|
|
|
|
assetId: assetId,
|
|
|
|
comment: comment,
|
|
|
|
);
|
2023-11-06 17:46:26 +02:00
|
|
|
|
2024-01-05 07:20:55 +02:00
|
|
|
if (activity.hasValue) {
|
|
|
|
final activities = state.valueOrNull ?? [];
|
|
|
|
state = AsyncData([...activities, activity.requireValue]);
|
|
|
|
ref
|
|
|
|
.watch(activityStatisticsProvider(albumId, assetId).notifier)
|
|
|
|
.addActivity();
|
|
|
|
// The previous addActivity call would increase the count of an asset if assetId != null
|
|
|
|
// To also increase the activity count of the album, calling it once again with assetId set to null
|
|
|
|
if (assetId != null) {
|
|
|
|
ref.watch(activityStatisticsProvider(albumId).notifier).addActivity();
|
|
|
|
}
|
2023-12-05 21:34:37 +02:00
|
|
|
}
|
2023-11-06 17:46:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:20:55 +02:00
|
|
|
/// Mock class for testing
|
|
|
|
abstract class AlbumActivityInternal extends _$AlbumActivity {}
|