Line data Source code
1 : import 'package:rxdart/rxdart.dart';
2 : import 'package:stream_feed/stream_feed.dart';
3 :
4 : //TODO: find a better than shift
5 :
6 : // extension AlreadyReacted on Reaction {
7 : // bool alreadyReactedTo(String kind) {
8 : // final ownReactions = ownChildren?[kind];
9 : // return ownReactions != null && ownReactions.isNotEmpty;
10 : // }
11 : // }
12 :
13 : // extension AlreadyReactedEnrichedActivity on EnrichedActivity {
14 : // bool alreadyReactedTo(String kind) {
15 : // final _ownReactions = ownReactions?[kind];
16 : // return _ownReactions != null && _ownReactions.isNotEmpty;
17 : // }
18 : // }
19 :
20 : extension ReactionX on List<Reaction> {
21 : ///Filter reactions by reaction kind
22 0 : List<Reaction?> filterByKind(String kind) =>
23 0 : where((reaction) => reaction.kind == kind).toList();
24 :
25 1 : Reaction getReactionPath(Reaction reaction) =>
26 5 : firstWhere((r) => r.id! == reaction.id!); //TODO; handle doesn't exist
27 : }
28 :
29 : extension EnrichedActivityX<A, Ob, T, Or>
30 : on List<GenericEnrichedActivity<A, Ob, T, Or>> {
31 1 : GenericEnrichedActivity<A, Ob, T, Or> getEnrichedActivityPath(
32 : GenericEnrichedActivity<A, Ob, T, Or> enrichedActivity) =>
33 1 : firstWhere(
34 4 : (e) => e.id! == enrichedActivity.id!); //TODO; handle doesn't exist
35 :
36 : }
37 :
38 : extension UpdateIn<A, Ob, T, Or>
39 : on List<GenericEnrichedActivity<A, Ob, T, Or>> {
40 : // EnrichedActivity getEnrichedActivityPath(EnrichedActivity enrichedActivity) =>
41 : // this.firstWhere(
42 : // (e) => e.id! == enrichedActivity.id!); //TODO; handle doesn't exist
43 :
44 1 : List<GenericEnrichedActivity<A, Ob, T, Or>> updateIn(
45 : GenericEnrichedActivity<A, Ob, T, Or> enrichedActivity, int indexPath) {
46 1 : var result = List<GenericEnrichedActivity<A, Ob, T, Or>>.from(this);
47 1 : result.isNotEmpty
48 1 : ? result.removeAt(indexPath) //removes the item at index 1
49 : : null;
50 1 : result.insert(indexPath, enrichedActivity);
51 : return result;
52 : }
53 : }
54 :
55 : extension UpdateInReaction on List<Reaction> {
56 : // EnrichedActivity getEnrichedActivityPath(EnrichedActivity enrichedActivity) =>
57 : // this.firstWhere(
58 : // (e) => e.id! == enrichedActivity.id!); //TODO; handle doesn't exist
59 :
60 1 : List<Reaction> updateIn(Reaction enrichedActivity, int indexPath) {
61 1 : var result = List<Reaction>.from(this);
62 1 : result.isNotEmpty
63 1 : ? result.removeAt(indexPath) //removes the item at index 1
64 : : null;
65 1 : result.insert(indexPath, enrichedActivity);
66 : return result;
67 : }
68 : }
69 :
70 : extension UnshiftMapList on Map<String, List<Reaction>>? {
71 : //TODO: maybe refactor to an operator maybe [Reaction] + Reaction
72 2 : Map<String, List<Reaction>> unshiftByKind(String kind, Reaction reaction,
73 : [ShiftType type = ShiftType.increment]) {
74 : Map<String, List<Reaction>>? result;
75 : result = this;
76 4 : final latestReactionsByKind = this?[kind] ?? [];
77 : if (result != null) {
78 8 : result = {...result, kind: latestReactionsByKind.unshift(reaction, type)};
79 : } else {
80 1 : result = {
81 : //TODO: handle decrement: should we throw?
82 1 : kind: [reaction]
83 : };
84 : }
85 : return result;
86 : }
87 : }
88 :
89 : extension UnshiftMapController
90 : on Map<String, BehaviorSubject<List<Reaction>>>? {
91 : ///Lookup latest Reactions by Id and inserts the given reaction to the beginning of the list
92 2 : Map<String, BehaviorSubject<List<Reaction>>> unshiftById(
93 : String activityId, Reaction reaction,
94 : [ShiftType type = ShiftType.increment]) {
95 : Map<String, BehaviorSubject<List<Reaction>>>? result;
96 : result = this;
97 5 : final latestReactionsById = this?[activityId]?.valueOrNull ?? [];
98 2 : if (result != null && result[activityId] != null) {
99 6 : result[activityId]!.add(latestReactionsById.unshift(reaction, type));
100 : } else {
101 0 : result = {
102 : //TODO: handle decrement
103 0 : activityId: BehaviorSubject.seeded([reaction])
104 : };
105 : }
106 : return result;
107 : }
108 : }
109 :
110 : //TODO: find a better name
111 7 : enum ShiftType { increment, decrement }
112 :
113 : extension UnshiftMapInt on Map<String, int>? {
114 2 : Map<String, int> unshiftByKind(String kind,
115 : [ShiftType type = ShiftType.increment]) {
116 : Map<String, int>? result;
117 : result = this;
118 2 : final reactionCountsByKind = result?[kind] ?? 0;
119 : if (result != null) {
120 2 : result = {
121 2 : ...result,
122 4 : kind: reactionCountsByKind.unshift(type)
123 : }; //+1 if increment else -1
124 : } else {
125 1 : if (type == ShiftType.increment) {
126 1 : result = {kind: 1};
127 : } else {
128 0 : result = {kind: 0};
129 : }
130 : }
131 : return result;
132 : }
133 : }
134 :
135 : extension Unshift<T> on List<T> {
136 2 : List<T> unshift(T item, [ShiftType type = ShiftType.increment]) {
137 2 : final result = List<T>.from(this);
138 2 : if (type == ShiftType.increment) {
139 4 : return [item, ...result];
140 : } else {
141 2 : return result..remove(item);
142 : }
143 : }
144 : }
145 :
146 : extension UnshiftInt on int {
147 2 : int unshift([ShiftType type = ShiftType.increment]) {
148 2 : if (type == ShiftType.increment) {
149 1 : return this + 1;
150 : } else {
151 2 : return this - 1;
152 : }
153 : }
154 : }
|