Skip to content

Commit 922b0c3

Browse files
author
Franco Bugnano
committed
Implemented subject feedFilter
1 parent 26be463 commit 922b0c3

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

lib/src/predicate.dart

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ import 'package:flutter/foundation.dart';
55
class FieldPredicate<T> {
66
final String _operand;
77
String? _value;
8-
List<String>? _values;
8+
List<String?>? _values;
9+
bool _useValue;
910

10-
FieldPredicate.equals(T value) : _operand = '==', _value = value.toString();
11-
FieldPredicate.notEquals(T value) : _operand = '!=', _value = value.toString();
12-
FieldPredicate.oneOf(List<T> values) : _operand = 'oneOf', _values = values.map((value) => value.toString()).toList();
13-
FieldPredicate.notOneOf(List<T> values) : _operand = '!oneOf', _values = values.map((value) => value.toString()).toList();
11+
FieldPredicate.equals(T value) : _operand = '==', _value = (value != null ? value.toString() : null), _useValue = true;
12+
FieldPredicate.notEquals(T value) : _operand = '!=', _value = (value != null ? value.toString() : null), _useValue = true;
13+
FieldPredicate.oneOf(List<T> values) : _operand = 'oneOf', _values = values.map((value) => (value != null ? value.toString() : null)).toList(), _useValue = false;
14+
FieldPredicate.notOneOf(List<T> values) : _operand = '!oneOf', _values = values.map((value) => (value != null ? value.toString() : null)).toList(), _useValue = false;
1415

1516
FieldPredicate.of(FieldPredicate<T> other)
1617
: _operand = other._operand,
1718
_value = other._value,
18-
_values = (other._values != null ? List<String>.of(other._values!) : null);
19+
_values = (other._values != null ? List<String?>.of(other._values!) : null),
20+
_useValue = other._useValue;
1921

2022
@override
2123
String toString() {
@@ -27,7 +29,7 @@ class FieldPredicate<T> {
2729

2830
result.add(_operand);
2931

30-
if (_value != null) {
32+
if (_useValue) {
3133
result.add(_value);
3234
}
3335

@@ -59,13 +61,18 @@ class FieldPredicate<T> {
5961
return false;
6062
}
6163

64+
if (_useValue != other._useValue) {
65+
return false;
66+
}
67+
6268
return true;
6369
}
6470

6571
int get hashCode => Object.hash(
6672
_operand,
6773
_value,
6874
(_values != null ? Object.hashAll(_values!) : _values),
75+
_useValue,
6976
);
7077
}
7178

@@ -113,6 +120,10 @@ class CustomFieldPredicate extends FieldPredicate<String> {
113120
return false;
114121
}
115122

123+
if (_useValue != other._useValue) {
124+
return false;
125+
}
126+
116127
if (_exists != other._exists) {
117128
return false;
118129
}
@@ -124,6 +135,7 @@ class CustomFieldPredicate extends FieldPredicate<String> {
124135
_operand,
125136
_value,
126137
(_values != null ? Object.hashAll(_values!) : _values),
138+
_useValue,
127139
_exists,
128140
);
129141
}
@@ -223,13 +235,17 @@ class ConversationPredicate {
223235
/// Only select conversations that have the last message sent in a particular time interval.
224236
final NumberPredicate? lastMessageTs;
225237

226-
const ConversationPredicate({this.access, this.custom, this.hasUnreadMessages, this.lastMessageTs});
238+
/// Only select conversations that have the subject set to particular values.
239+
final FieldPredicate<String?>? subject;
240+
241+
const ConversationPredicate({this.access, this.custom, this.hasUnreadMessages, this.lastMessageTs, this.subject});
227242

228243
ConversationPredicate.of(ConversationPredicate other)
229244
: access = (other.access != null ? FieldPredicate<ConversationAccessLevel>.of(other.access!) : null),
230245
custom = (other.custom != null ? Map<String, CustomFieldPredicate>.of(other.custom!) : null),
231246
hasUnreadMessages = other.hasUnreadMessages,
232-
lastMessageTs = (other.lastMessageTs != null ? NumberPredicate.of(other.lastMessageTs!) : null);
247+
lastMessageTs = (other.lastMessageTs != null ? NumberPredicate.of(other.lastMessageTs!) : null),
248+
subject = (other.subject != null ? FieldPredicate<String?>.of(other.subject!) : null);
233249

234250
@override
235251
String toString() {
@@ -255,6 +271,10 @@ class ConversationPredicate {
255271
result['lastMessageTs'] = lastMessageTs;
256272
}
257273

274+
if (subject != null) {
275+
result['subject'] = subject;
276+
}
277+
258278
return result;
259279
}
260280

@@ -283,6 +303,10 @@ class ConversationPredicate {
283303
return false;
284304
}
285305

306+
if (subject != other.subject) {
307+
return false;
308+
}
309+
286310
return true;
287311
}
288312

@@ -292,6 +316,7 @@ class ConversationPredicate {
292316
(custom != null ? Object.hashAll(custom!.values) : custom),
293317
hasUnreadMessages,
294318
lastMessageTs,
319+
subject,
295320
);
296321
}
297322

test/talkjs_test.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void main() {
3232
},
3333
hasUnreadMessages: false,
3434
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
35+
subject: FieldPredicate.equals(null),
3536
) == ConversationPredicate(
3637
access: FieldPredicate.notEquals(ConversationAccessLevel.none),
3738
custom: {
@@ -41,6 +42,7 @@ void main() {
4142
},
4243
hasUnreadMessages: false,
4344
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
45+
subject: FieldPredicate.equals(null),
4446
)
4547
, true);
4648
});
@@ -137,6 +139,7 @@ void main() {
137139
},
138140
hasUnreadMessages: false,
139141
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
142+
subject: FieldPredicate.notEquals('Pink shoes'),
140143
)) == ConversationPredicate(
141144
access: FieldPredicate.notEquals(ConversationAccessLevel.none),
142145
custom: {
@@ -146,6 +149,7 @@ void main() {
146149
},
147150
hasUnreadMessages: false,
148151
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
152+
subject: FieldPredicate.notEquals('Pink shoes'),
149153
)
150154
, true);
151155
});
@@ -227,8 +231,9 @@ void main() {
227231
},
228232
hasUnreadMessages: false,
229233
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
234+
subject: FieldPredicate.oneOf(['Pink shoes', null]),
230235
)),
231-
'{"access":["!=","None"],"custom":{"seller":"exists","category":["oneOf",["shoes","sandals"]],"visibility":["==","visible"]},"hasUnreadMessages":false,"lastMessageTs":[">",1679298371586.0]}'
236+
'{"access":["!=","None"],"custom":{"seller":"exists","category":["oneOf",["shoes","sandals"]],"visibility":["==","visible"]},"hasUnreadMessages":false,"lastMessageTs":[">",1679298371586.0],"subject":["oneOf",["Pink shoes",null]]}'
232237
);
233238
});
234239

0 commit comments

Comments
 (0)