|
| 1 | +import '../models.dart'; |
| 2 | +import '../utils/list_extensions.dart'; |
| 3 | + |
| 4 | +// TODO: incomplete |
| 5 | +class ActivityData implements Identifiable { |
| 6 | + const ActivityData({ |
| 7 | + required this.id, |
| 8 | + required this.user, |
| 9 | + required this.text, |
| 10 | + required this.reactionCount, |
| 11 | + required this.latestReactions, |
| 12 | + required this.ownReactions, |
| 13 | + required this.reactionGroups, |
| 14 | + }); |
| 15 | + |
| 16 | + @override |
| 17 | + final String id; |
| 18 | + final UserData user; |
| 19 | + final String? text; |
| 20 | + final int reactionCount; |
| 21 | + final List<FeedsReactionData> latestReactions; |
| 22 | + final List<FeedsReactionData> ownReactions; |
| 23 | + final Map<String, ReactionGroupData> reactionGroups; |
| 24 | + |
| 25 | + ActivityData copyWith({ |
| 26 | + String? text, |
| 27 | + int? reactionCount, |
| 28 | + List<FeedsReactionData>? latestReactions, |
| 29 | + List<FeedsReactionData>? ownReactions, |
| 30 | + Map<String, ReactionGroupData>? reactionGroups, |
| 31 | + }) { |
| 32 | + return ActivityData( |
| 33 | + id: id, |
| 34 | + user: user, |
| 35 | + text: text ?? this.text, |
| 36 | + reactionCount: reactionCount ?? this.reactionCount, |
| 37 | + latestReactions: latestReactions ?? this.latestReactions, |
| 38 | + ownReactions: ownReactions ?? this.ownReactions, |
| 39 | + reactionGroups: reactionGroups ?? this.reactionGroups, |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + ActivityData addReaction(FeedsReactionData reaction, |
| 44 | + {required String currentUserId}) { |
| 45 | + final newLatestReactions = [...latestReactions]; |
| 46 | + final newReactionGroups = |
| 47 | + Map<String, ReactionGroupData>.from(reactionGroups); |
| 48 | + var newOwnReactions = ownReactions; |
| 49 | + |
| 50 | + reaction.updateByAdding( |
| 51 | + to: [...latestReactions], |
| 52 | + reactionGroups: newReactionGroups, |
| 53 | + ); |
| 54 | + if (reaction.user.id == currentUserId) { |
| 55 | + newOwnReactions = ownReactions.insertById(reaction); |
| 56 | + } |
| 57 | + |
| 58 | + return copyWith( |
| 59 | + reactionCount: reactionCount + 1, |
| 60 | + latestReactions: List.unmodifiable(newLatestReactions), |
| 61 | + ownReactions: newOwnReactions, |
| 62 | + reactionGroups: Map.unmodifiable(newReactionGroups), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + ActivityData removeReaction(FeedsReactionData reaction, |
| 67 | + {required String currentUserId}) { |
| 68 | + final newLatestReactions = [...latestReactions]; |
| 69 | + final newReactionGroups = |
| 70 | + Map<String, ReactionGroupData>.from(reactionGroups); |
| 71 | + var newOwnReactions = ownReactions; |
| 72 | + |
| 73 | + reaction.updateByRemoving( |
| 74 | + to: [...latestReactions], |
| 75 | + reactionGroups: newReactionGroups, |
| 76 | + ); |
| 77 | + if (reaction.user.id == currentUserId) { |
| 78 | + newOwnReactions = ownReactions.removeById(reaction); |
| 79 | + } |
| 80 | + |
| 81 | + return copyWith( |
| 82 | + reactionCount: reactionCount - 1, |
| 83 | + latestReactions: List.unmodifiable(newLatestReactions), |
| 84 | + ownReactions: newOwnReactions, |
| 85 | + reactionGroups: Map.unmodifiable(newReactionGroups), |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +extension on FeedsReactionData { |
| 91 | + void updateByAdding({ |
| 92 | + required List<FeedsReactionData> to, |
| 93 | + required Map<String, ReactionGroupData> reactionGroups, |
| 94 | + }) { |
| 95 | + to.insertById(this, createNewList: false); |
| 96 | + |
| 97 | + final reactionGroup = reactionGroups[type]?.increment(createdAt) ?? |
| 98 | + ReactionGroupData( |
| 99 | + count: 1, |
| 100 | + firstReactionAt: createdAt, |
| 101 | + lastReactionAt: createdAt, |
| 102 | + ); |
| 103 | + reactionGroups[type] = reactionGroup; |
| 104 | + } |
| 105 | + |
| 106 | + void updateByRemoving({ |
| 107 | + required List<FeedsReactionData> to, |
| 108 | + required Map<String, ReactionGroupData> reactionGroups, |
| 109 | + }) { |
| 110 | + to.removeById(this, createNewList: false); |
| 111 | + var reactionGroup = reactionGroups[type]; |
| 112 | + if (reactionGroup != null) { |
| 113 | + reactionGroup = reactionGroup.decrement(createdAt); |
| 114 | + if (reactionGroup.isEmpty) { |
| 115 | + reactionGroups.remove(type); |
| 116 | + } else { |
| 117 | + reactionGroups[type] = reactionGroup; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments