2024-04-30 19:36:40 -07:00
|
|
|
import 'package:immich_mobile/models/activities/activity.model.dart';
|
2024-05-02 13:59:14 -07:00
|
|
|
import 'package:immich_mobile/providers/activity_service.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/activity_statistics.provider.dart';
|
2024-01-04 22:20:55 -07:00
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
2023-11-06 08:46:26 -07:00
|
|
|
|
2024-01-04 22:20:55 -07:00
|
|
|
part 'activity.provider.g.dart';
|
2023-11-06 08:46:26 -07:00
|
|
|
|
2024-01-04 22:20:55 -07: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 08:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> removeActivity(String id) async {
|
2024-01-04 22:20:55 -07:00
|
|
|
if (await ref.watch(activityServiceProvider).removeActivity(id)) {
|
|
|
|
final activities = state.valueOrNull ?? [];
|
2023-11-06 08:46:26 -07:00
|
|
|
final removedActivity = activities.firstWhere((a) => a.id == id);
|
|
|
|
activities.remove(removedActivity);
|
|
|
|
state = AsyncData(activities);
|
2024-01-04 22:20:55 -07:00
|
|
|
// Decrement activity count only for comments
|
2023-11-06 08:46:26 -07:00
|
|
|
if (removedActivity.type == ActivityType.comment) {
|
2024-01-04 22:20:55 -07:00
|
|
|
ref
|
|
|
|
.watch(activityStatisticsProvider(albumId, assetId).notifier)
|
2023-11-06 08:46:26 -07:00
|
|
|
.removeActivity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addLike() async {
|
2024-01-04 22:20:55 -07:00
|
|
|
final activity = await ref
|
|
|
|
.watch(activityServiceProvider)
|
2023-11-06 08:46:26 -07:00
|
|
|
.addActivity(albumId, ActivityType.like, assetId: assetId);
|
2024-01-04 22:20:55 -07:00
|
|
|
if (activity.hasValue) {
|
2023-11-06 08:46:26 -07:00
|
|
|
final activities = state.asData?.value ?? [];
|
2024-01-04 22:20:55 -07:00
|
|
|
state = AsyncData([...activities, activity.requireValue]);
|
2023-11-06 08:46:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 22:20:55 -07:00
|
|
|
Future<void> addComment(String comment) async {
|
|
|
|
final activity = await ref.watch(activityServiceProvider).addActivity(
|
|
|
|
albumId,
|
|
|
|
ActivityType.comment,
|
|
|
|
assetId: assetId,
|
|
|
|
comment: comment,
|
|
|
|
);
|
2023-11-06 08:46:26 -07:00
|
|
|
|
2024-01-04 22:20:55 -07: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 12:34:37 -07:00
|
|
|
}
|
2023-11-06 08:46:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 22:20:55 -07:00
|
|
|
/// Mock class for testing
|
|
|
|
abstract class AlbumActivityInternal extends _$AlbumActivity {}
|