@@ -25,9 +25,13 @@ class StreamThreadListController extends PagedValueNotifier<String, Thread> {
25
25
StreamThreadListController ({
26
26
required this .client,
27
27
StreamThreadListEventHandler ? eventHandler,
28
+ this .filter,
29
+ this .sort,
28
30
this .options = const ThreadOptions (),
29
31
this .limit = defaultThreadsPagedLimit,
30
- }) : _activeOptions = options,
32
+ }) : _activeFilter = filter,
33
+ _activeSort = sort,
34
+ _activeOptions = options,
31
35
_eventHandler = eventHandler ?? StreamThreadListEventHandler (),
32
36
super (const PagedValue .loading ());
33
37
@@ -36,9 +40,13 @@ class StreamThreadListController extends PagedValueNotifier<String, Thread> {
36
40
super .value, {
37
41
required this .client,
38
42
StreamThreadListEventHandler ? eventHandler,
43
+ this .filter,
44
+ this .sort,
39
45
this .options = const ThreadOptions (),
40
46
this .limit = defaultThreadsPagedLimit,
41
- }) : _activeOptions = options,
47
+ }) : _activeFilter = filter,
48
+ _activeSort = sort,
49
+ _activeOptions = options,
42
50
_eventHandler = eventHandler ?? StreamThreadListEventHandler ();
43
51
44
52
/// The Stream client used to perform the queries.
@@ -47,6 +55,21 @@ class StreamThreadListController extends PagedValueNotifier<String, Thread> {
47
55
/// The thread event handlers to use for the thread list.
48
56
final StreamThreadListEventHandler _eventHandler;
49
57
58
+ /// The query filters to use.
59
+ ///
60
+ /// You can query on any of the custom fields you've defined on the [Thread] .
61
+ final Filter ? filter;
62
+ Filter ? _activeFilter;
63
+
64
+ /// The sorting used for the threads matching the filters.
65
+ ///
66
+ /// Sorting is based on field and direction, multiple sorting options
67
+ /// can be provided.
68
+ ///
69
+ /// Direction can be ascending or descending.
70
+ final SortOrder <Thread >? sort;
71
+ SortOrder <Thread >? _activeSort;
72
+
50
73
/// The limit to apply to the thread list.
51
74
///
52
75
/// The default is set to [defaultUserPagedLimit] .
@@ -58,10 +81,31 @@ class StreamThreadListController extends PagedValueNotifier<String, Thread> {
58
81
final ThreadOptions options;
59
82
ThreadOptions _activeOptions;
60
83
84
+ /// Allows for the change of filters used for thread queries.
85
+ ///
86
+ /// Use this if you need to support runtime filter changes,
87
+ /// through custom filters UI.
88
+ ///
89
+ /// Note: This will not trigger a new query. make sure to call
90
+ /// [doInitialLoad] after setting a new filter.
91
+ set filter (Filter ? value) => _activeFilter = value;
92
+
93
+ /// Allows for the change of the query sort used for thread queries.
94
+ ///
95
+ /// Use this if you need to support runtime sort changes,
96
+ /// through custom sort UI.
97
+ ///
98
+ /// Note: This will not trigger a new query. make sure to call
99
+ /// [doInitialLoad] after setting a new sort.
100
+ set sort (SortOrder <Thread >? value) => _activeSort = value;
101
+
61
102
/// Allows for the change of the [options] at runtime.
62
103
///
63
104
/// Use this if you need to support runtime option changes,
64
105
/// through custom filters UI.
106
+ ///
107
+ /// Note: This will not trigger a new query. make sure to call
108
+ /// [doInitialLoad] after setting a new option.
65
109
set options (ThreadOptions options) => _activeOptions = options;
66
110
67
111
/// The ids of the threads that have unseen messages.
@@ -80,6 +124,19 @@ class StreamThreadListController extends PagedValueNotifier<String, Thread> {
80
124
/// Clears the set of unseen thread IDs.
81
125
void clearUnseenThreadIds () => _unseenThreadIds.value = const {};
82
126
127
+ @override
128
+ set value (PagedValue <String , Thread > newValue) {
129
+ super .value = switch (_activeSort) {
130
+ null => newValue,
131
+ final threadSort => newValue.maybeMap (
132
+ orElse: () => newValue,
133
+ (success) => success.copyWith (
134
+ items: success.items.sorted (threadSort.compare),
135
+ ),
136
+ ),
137
+ };
138
+ }
139
+
83
140
@override
84
141
Future <void > doInitialLoad () async {
85
142
final limit = min (
@@ -88,6 +145,8 @@ class StreamThreadListController extends PagedValueNotifier<String, Thread> {
88
145
);
89
146
try {
90
147
final response = await client.queryThreads (
148
+ filter: _activeFilter,
149
+ sort: _activeSort,
91
150
options: _activeOptions,
92
151
pagination: PaginationParams (limit: limit),
93
152
);
@@ -114,6 +173,8 @@ class StreamThreadListController extends PagedValueNotifier<String, Thread> {
114
173
115
174
try {
116
175
final response = await client.queryThreads (
176
+ filter: _activeFilter,
177
+ sort: _activeSort,
117
178
options: _activeOptions,
118
179
pagination: PaginationParams (limit: limit, next: nextPageKey),
119
180
);
0 commit comments