Skip to content

Commit 0b29458

Browse files
committed
refactor: move sortOrder out of requests
1 parent 89a4541 commit 0b29458

File tree

14 files changed

+169
-151
lines changed

14 files changed

+169
-151
lines changed

packages/stream_chat/lib/src/client/client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:stream_chat/src/client/retry_policy.dart';
99
import 'package:stream_chat/src/core/api/attachment_file_uploader.dart';
1010
import 'package:stream_chat/src/core/api/requests.dart';
1111
import 'package:stream_chat/src/core/api/responses.dart';
12+
import 'package:stream_chat/src/core/api/sort_order.dart';
1213
import 'package:stream_chat/src/core/api/stream_chat_api.dart';
1314
import 'package:stream_chat/src/core/error/error.dart';
1415
import 'package:stream_chat/src/core/http/connection_id_manager.dart';

packages/stream_chat/lib/src/core/api/channel_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22

33
import 'package:stream_chat/src/core/api/requests.dart';
44
import 'package:stream_chat/src/core/api/responses.dart';
5+
import 'package:stream_chat/src/core/api/sort_order.dart';
56
import 'package:stream_chat/src/core/http/stream_http_client.dart';
67
import 'package:stream_chat/src/core/models/channel_state.dart';
78
import 'package:stream_chat/src/core/models/event.dart';

packages/stream_chat/lib/src/core/api/general_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22

33
import 'package:stream_chat/src/core/api/requests.dart';
44
import 'package:stream_chat/src/core/api/responses.dart';
5+
import 'package:stream_chat/src/core/api/sort_order.dart';
56
import 'package:stream_chat/src/core/http/stream_http_client.dart';
67
import 'package:stream_chat/src/core/models/filter.dart';
78
import 'package:stream_chat/src/core/models/member.dart';

packages/stream_chat/lib/src/core/api/polls_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22

33
import 'package:stream_chat/src/core/api/requests.dart';
44
import 'package:stream_chat/src/core/api/responses.dart';
5+
import 'package:stream_chat/src/core/api/sort_order.dart';
56
import 'package:stream_chat/src/core/http/stream_http_client.dart';
67
import 'package:stream_chat/src/core/models/filter.dart';
78
import 'package:stream_chat/src/core/models/poll.dart';

packages/stream_chat/lib/src/core/api/requests.dart

Lines changed: 0 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,8 @@
1-
// ignore_for_file: constant_identifier_names
2-
31
import 'package:equatable/equatable.dart';
42
import 'package:json_annotation/json_annotation.dart';
5-
import 'package:stream_chat/src/core/models/comparable_field.dart';
63

74
part 'requests.g.dart';
85

9-
/// A list of [SortOption]s that define a sorting order for elements of type [T]
10-
///
11-
/// When multiple sort options are provided, they are applied in sequence
12-
/// until a non-equal comparison is found.
13-
///
14-
/// Example: `Sort<ChannelState>([pinnedAtSort, lastMessageAtSort])`
15-
typedef SortOrder<T extends ComparableFieldProvider> = List<SortOption<T>>;
16-
17-
/// Extension that allows a [SortOrder] to be used as a comparator function.
18-
extension CompositeComparator<T extends ComparableFieldProvider>
19-
on SortOrder<T> {
20-
/// Compares two objects using all sort options in sequence.
21-
///
22-
/// Returns the first non-zero comparison result, or 0 if all comparisons
23-
/// result in equality.
24-
///
25-
/// ```dart
26-
/// channels.sort(mySort.compare);
27-
/// ```
28-
int compare(T a, T b) {
29-
for (final sortOption in this) {
30-
final comparison = sortOption.compare(a, b);
31-
if (comparison != 0) return comparison;
32-
}
33-
34-
return 0; // All comparisons were equal
35-
}
36-
}
37-
38-
/// A sort specification for objects that implement [ComparableFieldProvider].
39-
///
40-
/// Defines a field to sort by and a direction (ascending or descending).
41-
/// Can use a custom comparator or create a default one based on the field name.
42-
///
43-
/// Example:
44-
/// ```dart
45-
/// // Sort channels by last message date in descending order
46-
/// final sort = SortOption<ChannelState>("last_message_at");
47-
/// ```
48-
@JsonSerializable(includeIfNull: false)
49-
class SortOption<T extends ComparableFieldProvider> {
50-
/// Creates a new SortOption instance with the specified field and direction.
51-
///
52-
/// ```dart
53-
/// final sorting = SortOption("last_message_at") // Default: descending order
54-
/// ```
55-
const SortOption(
56-
this.field, {
57-
this.direction = SortOption.DESC,
58-
Comparator<T>? comparator,
59-
}) : _comparator = comparator;
60-
61-
/// Creates a SortOption for descending order sorting by the specified field.
62-
///
63-
/// Example:
64-
/// ```dart
65-
/// // Sort channels by last message date in descending order
66-
/// final sort = SortOption.desc("last_message_at");
67-
/// ```
68-
const SortOption.desc(
69-
this.field, {
70-
Comparator<T>? comparator,
71-
}) : direction = SortOption.DESC,
72-
_comparator = comparator;
73-
74-
/// Creates a SortOption for ascending order sorting by the specified field.
75-
///
76-
/// Example:
77-
/// ```dart
78-
/// // Sort channels by name in ascending order
79-
/// final sort = SortOption.asc("name");
80-
/// ```
81-
const SortOption.asc(
82-
this.field, {
83-
Comparator<T>? comparator,
84-
}) : direction = SortOption.ASC,
85-
_comparator = comparator;
86-
87-
/// Create a new instance from JSON.
88-
factory SortOption.fromJson(Map<String, dynamic> json) =>
89-
_$SortOptionFromJson(json);
90-
91-
/// Ascending order (1)
92-
static const ASC = 1;
93-
94-
/// Descending order (-1)
95-
static const DESC = -1;
96-
97-
/// The field name to sort by
98-
final String field;
99-
100-
/// The sort direction (ASC or DESC)
101-
final int direction;
102-
103-
/// Compares two objects of type T using the specified field and direction.
104-
///
105-
/// Returns:
106-
/// - 0 if both objects are equal
107-
/// - 1 if the first object is greater than the second
108-
/// - -1 if the first object is less than the second
109-
///
110-
/// Handles null values by treating null as less than any non-null value.
111-
///
112-
/// ```dart
113-
/// final sortOption = SortOption<ChannelState>("last_message_at");
114-
/// final sortedChannels = channels.sort(sortOption.compare);
115-
/// ```
116-
int compare(T a, T b) => direction * comparator(a, b);
117-
118-
/// Returns a comparator function for sorting objects of type T.
119-
@JsonKey(includeToJson: false, includeFromJson: false)
120-
Comparator<T> get comparator {
121-
if (_comparator case final comparator?) return comparator;
122-
123-
return (T a, T b) {
124-
final aValue = a.getComparableField(field);
125-
final bValue = b.getComparableField(field);
126-
127-
// Handle null values
128-
if (aValue == null && bValue == null) return 0;
129-
if (aValue == null) return -1;
130-
if (bValue == null) return 1;
131-
132-
return aValue.compareTo(bValue);
133-
};
134-
}
135-
136-
final Comparator<T>? _comparator;
137-
138-
/// Converts this option to JSON.
139-
Map<String, dynamic> toJson() => _$SortOptionToJson(this);
140-
}
141-
1426
/// Pagination options.
1437
@JsonSerializable(includeIfNull: false)
1448
class PaginationParams extends Equatable {

packages/stream_chat/lib/src/core/api/requests.g.dart

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// ignore_for_file: constant_identifier_names
2+
3+
import 'package:json_annotation/json_annotation.dart';
4+
import 'package:stream_chat/src/core/models/comparable_field.dart';
5+
6+
part 'sort_order.g.dart';
7+
8+
/// A list of [SortOption]s that define a sorting order for elements of type [T]
9+
///
10+
/// When multiple sort options are provided, they are applied in sequence
11+
/// until a non-equal comparison is found.
12+
///
13+
/// Example: `Sort<ChannelState>([pinnedAtSort, lastMessageAtSort])`
14+
typedef SortOrder<T extends ComparableFieldProvider> = List<SortOption<T>>;
15+
16+
/// A sort specification for objects that implement [ComparableFieldProvider].
17+
///
18+
/// Defines a field to sort by and a direction (ascending or descending).
19+
/// Can use a custom comparator or create a default one based on the field name.
20+
///
21+
/// Example:
22+
/// ```dart
23+
/// // Sort channels by last message date in descending order
24+
/// final sort = SortOption<ChannelState>("last_message_at");
25+
/// ```
26+
@JsonSerializable(includeIfNull: false)
27+
class SortOption<T extends ComparableFieldProvider> {
28+
/// Creates a new SortOption instance with the specified field and direction.
29+
///
30+
/// ```dart
31+
/// final sorting = SortOption("last_message_at") // Default: descending order
32+
/// ```
33+
const SortOption(
34+
this.field, {
35+
this.direction = SortOption.DESC,
36+
Comparator<T>? comparator,
37+
}) : _comparator = comparator;
38+
39+
/// Creates a SortOption for descending order sorting by the specified field.
40+
///
41+
/// Example:
42+
/// ```dart
43+
/// // Sort channels by last message date in descending order
44+
/// final sort = SortOption.desc("last_message_at");
45+
/// ```
46+
const SortOption.desc(
47+
this.field, {
48+
Comparator<T>? comparator,
49+
}) : direction = SortOption.DESC,
50+
_comparator = comparator;
51+
52+
/// Creates a SortOption for ascending order sorting by the specified field.
53+
///
54+
/// Example:
55+
/// ```dart
56+
/// // Sort channels by name in ascending order
57+
/// final sort = SortOption.asc("name");
58+
/// ```
59+
const SortOption.asc(
60+
this.field, {
61+
Comparator<T>? comparator,
62+
}) : direction = SortOption.ASC,
63+
_comparator = comparator;
64+
65+
/// Create a new instance from JSON.
66+
factory SortOption.fromJson(Map<String, dynamic> json) =>
67+
_$SortOptionFromJson(json);
68+
69+
/// Ascending order (1)
70+
static const ASC = 1;
71+
72+
/// Descending order (-1)
73+
static const DESC = -1;
74+
75+
/// The field name to sort by
76+
final String field;
77+
78+
/// The sort direction (ASC or DESC)
79+
final int direction;
80+
81+
/// Compares two objects of type T using the specified field and direction.
82+
///
83+
/// Returns:
84+
/// - 0 if both objects are equal
85+
/// - 1 if the first object is greater than the second
86+
/// - -1 if the first object is less than the second
87+
///
88+
/// Handles null values by treating null as less than any non-null value.
89+
///
90+
/// ```dart
91+
/// final sortOption = SortOption<ChannelState>("last_message_at");
92+
/// final sortedChannels = channels.sort(sortOption.compare);
93+
/// ```
94+
int compare(T a, T b) => direction * comparator(a, b);
95+
96+
/// Returns a comparator function for sorting objects of type T.
97+
@JsonKey(includeToJson: false, includeFromJson: false)
98+
Comparator<T> get comparator {
99+
if (_comparator case final comparator?) return comparator;
100+
101+
return (T a, T b) {
102+
final aValue = a.getComparableField(field);
103+
final bValue = b.getComparableField(field);
104+
105+
// Handle null values
106+
if (aValue == null && bValue == null) return 0;
107+
if (aValue == null) return -1;
108+
if (bValue == null) return 1;
109+
110+
return aValue.compareTo(bValue);
111+
};
112+
}
113+
114+
final Comparator<T>? _comparator;
115+
116+
/// Converts this option to JSON.
117+
Map<String, dynamic> toJson() => _$SortOptionToJson(this);
118+
}
119+
120+
/// Extension that allows a [SortOrder] to be used as a comparator function.
121+
extension CompositeComparator<T extends ComparableFieldProvider>
122+
on SortOrder<T> {
123+
/// Compares two objects using all sort options in sequence.
124+
///
125+
/// Returns the first non-zero comparison result, or 0 if all comparisons
126+
/// result in equality.
127+
///
128+
/// ```dart
129+
/// channels.sort(mySort.compare);
130+
/// ```
131+
int compare(T a, T b) {
132+
for (final sortOption in this) {
133+
final comparison = sortOption.compare(a, b);
134+
if (comparison != 0) return comparison;
135+
}
136+
137+
return 0; // All comparisons were equal
138+
}
139+
}

packages/stream_chat/lib/src/core/api/sort_order.g.dart

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/stream_chat/lib/src/core/api/user_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22

33
import 'package:stream_chat/src/core/api/requests.dart';
44
import 'package:stream_chat/src/core/api/responses.dart';
5+
import 'package:stream_chat/src/core/api/sort_order.dart';
56
import 'package:stream_chat/src/core/http/stream_http_client.dart';
67
import 'package:stream_chat/src/core/models/filter.dart';
78
import 'package:stream_chat/src/core/models/user.dart';

packages/stream_chat/lib/src/db/chat_persistence_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:stream_chat/src/core/api/requests.dart';
2+
import 'package:stream_chat/src/core/api/sort_order.dart';
23
import 'package:stream_chat/src/core/models/attachment_file.dart';
34
import 'package:stream_chat/src/core/models/channel_model.dart';
45
import 'package:stream_chat/src/core/models/channel_state.dart';

0 commit comments

Comments
 (0)