Parse the predefined filter with the generated model · GetStream/stream-chat-android@35467d9 · GitHub
Skip to content

Commit 35467d9

Browse files
committed
Parse the predefined filter with the generated model
1 parent 297ba0b commit 35467d9

8 files changed

Lines changed: 71 additions & 35 deletions

File tree

‎stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/MoshiChatApi.kt‎

Lines changed: 4 additions & 1 deletion

‎stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/mapping/DomainMapping.kt‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ import io.getstream.chat.android.network.models.QueryPollsResponse
131131
import io.getstream.chat.android.network.models.ReactionGroupResponse
132132
import io.getstream.chat.android.network.models.ReactionResponse
133133
import io.getstream.chat.android.network.models.ReadStateResponse
134+
import io.getstream.chat.android.network.models.SortParamRequest
134135
import io.getstream.chat.android.network.models.UnreadCountsChannel
135136
import io.getstream.chat.android.network.models.UnreadCountsChannelType
136137
import io.getstream.chat.android.network.models.UnreadCountsThread
@@ -1137,12 +1138,11 @@ internal class DomainMapping(
11371138
defaultPreference = ChatPreferenceToggle.fromValue(defaultPreference),
11381139
)
11391140

1140-
internal fun List<Map<String, Any>>?.toSortDomain(): QuerySorter<Channel>? {
1141+
internal fun List<SortParamRequest>?.toSortDomain(): QuerySorter<Channel>? {
11411142
if (isNullOrEmpty()) return null
1142-
return fold(QuerySortByField()) { sort, sortSpecMap ->
1143-
val fieldName = sortSpecMap[QuerySorter.KEY_FIELD_NAME] as? String ?: return null
1144-
val direction = (sortSpecMap[QuerySorter.KEY_DIRECTION] as? Number)?.toInt() ?: return null
1145-
when (direction) {
1143+
return fold(QuerySortByField()) { sort, sortParam ->
1144+
val fieldName = sortParam.field ?: return null
1145+
when (sortParam.direction) {
11461146
SortDirection.ASC.value -> sort.asc(fieldName)
11471147
SortDirection.DESC.value -> sort.desc(fieldName)
11481148
else -> return null

‎stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/model/response/QueryChannelsResponse.kt‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@
1717
package io.getstream.chat.android.client.api2.model.response
1818

1919
import com.squareup.moshi.JsonClass
20+
import io.getstream.chat.android.network.models.ParsedPredefinedFilterResponse
2021

2122
@JsonClass(generateAdapter = true)
2223
internal data class QueryChannelsResponse(
2324
val channels: List<ChannelResponse>,
2425
val predefined_filter: ParsedPredefinedFilterResponse? = null,
2526
)
26-
27-
@JsonClass(generateAdapter = true)
28-
internal data class ParsedPredefinedFilterResponse(
29-
val name: String,
30-
val filter: Map<String, Any>,
31-
val sort: List<Map<String, Any>>? = null,
32-
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2014-2026 Stream.io Inc. All rights reserved.
3+
*
4+
* Licensed under the Stream License;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress(
18+
"ArrayInDataClass",
19+
"EnumEntryName",
20+
"RemoveRedundantQualifierName",
21+
"UnusedImport",
22+
)
23+
24+
package io.getstream.chat.android.network.models
25+
26+
import com.squareup.moshi.Json
27+
28+
/**
29+
*
30+
*/
31+
@com.squareup.moshi.JsonClass(generateAdapter = true)
32+
internal data class ParsedPredefinedFilterResponse(
33+
@Json(name = "name")
34+
internal val name: String,
35+
36+
@Json(name = "filter")
37+
internal val filter: Map<String, Any?> = emptyMap(),
38+
39+
@Json(name = "sort")
40+
internal val sort: List<io.getstream.chat.android.network.models.SortParamRequest>? = emptyList(),
41+
)

‎stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/MoshiChatApiTest.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import io.getstream.chat.android.client.api2.model.response.FlagResponse
5252
import io.getstream.chat.android.client.api2.model.response.MessageResponse
5353
import io.getstream.chat.android.client.api2.model.response.MessagesResponse
5454
import io.getstream.chat.android.client.api2.model.response.MuteUserResponse
55-
import io.getstream.chat.android.client.api2.model.response.ParsedPredefinedFilterResponse
5655
import io.getstream.chat.android.client.api2.model.response.QueryChannelsResponse
5756
import io.getstream.chat.android.client.api2.model.response.QueryDraftMessagesResponse
5857
import io.getstream.chat.android.client.api2.model.response.QueryGroupedChannelsGroup
@@ -140,6 +139,7 @@ import io.getstream.chat.android.network.models.MembersResponse
140139
import io.getstream.chat.android.network.models.MessageActionRequest
141140
import io.getstream.chat.android.network.models.MessageRequest
142141
import io.getstream.chat.android.network.models.MuteChannelRequest
142+
import io.getstream.chat.android.network.models.ParsedPredefinedFilterResponse
143143
import io.getstream.chat.android.network.models.PollOptionInput
144144
import io.getstream.chat.android.network.models.PollOptionRequest
145145
import io.getstream.chat.android.network.models.PollOptionResponse
@@ -2314,7 +2314,7 @@ internal class MoshiChatApiTest {
23142314
predefinedFilter = ParsedPredefinedFilterResponse(
23152315
name = "my-filter",
23162316
filter = mapOf("type" to "messaging"),
2317-
sort = listOf(mapOf("field" to "created_at", "direction" to 1)),
2317+
sort = listOf(SortParamRequest(field = "created_at", direction = 1)),
23182318
),
23192319
)
23202320
assertEquals(

‎stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/MoshiChatApiTestArguments.kt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import io.getstream.chat.android.client.api2.model.response.FlagResponse
3535
import io.getstream.chat.android.client.api2.model.response.MessageResponse
3636
import io.getstream.chat.android.client.api2.model.response.MessagesResponse
3737
import io.getstream.chat.android.client.api2.model.response.MuteUserResponse
38-
import io.getstream.chat.android.client.api2.model.response.ParsedPredefinedFilterResponse
3938
import io.getstream.chat.android.client.api2.model.response.QueryChannelsResponse
4039
import io.getstream.chat.android.client.api2.model.response.QueryDraftMessagesResponse
4140
import io.getstream.chat.android.client.api2.model.response.QueryGroupedChannelsGroup
@@ -68,6 +67,7 @@ import io.getstream.chat.android.network.models.GetUserGroupResponse
6867
import io.getstream.chat.android.network.models.ListDevicesResponse
6968
import io.getstream.chat.android.network.models.ListUserGroupsResponse
7069
import io.getstream.chat.android.network.models.MembersResponse
70+
import io.getstream.chat.android.network.models.ParsedPredefinedFilterResponse
7171
import io.getstream.chat.android.network.models.PollOptionResponse
7272
import io.getstream.chat.android.network.models.PollResponse
7373
import io.getstream.chat.android.network.models.PollVoteResponse
@@ -80,6 +80,7 @@ import io.getstream.chat.android.network.models.RemoveUserGroupMembersResponse
8080
import io.getstream.chat.android.network.models.Response
8181
import io.getstream.chat.android.network.models.SearchRolesResponse
8282
import io.getstream.chat.android.network.models.SearchUserGroupsResponse
83+
import io.getstream.chat.android.network.models.SortParamRequest
8384
import io.getstream.chat.android.network.models.UnblockUsersResponse
8485
import io.getstream.chat.android.network.models.UpdateLiveLocationRequest
8586
import io.getstream.chat.android.network.models.UpdateMemberPartialResponse
@@ -538,7 +539,7 @@ internal object MoshiChatApiTestArguments {
538539
predefined_filter = ParsedPredefinedFilterResponse(
539540
name = "android_sample_filter",
540541
filter = mapOf("type" to "messaging"),
541-
sort = listOf(mapOf("field" to "last_message_at", "direction" to -1)),
542+
sort = listOf(SortParamRequest(field = "last_message_at", direction = -1)),
542543
),
543544
),
544545
).toRetrofitCall(),

‎stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/mapping/DomainMappingTest.kt‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ import io.getstream.chat.android.network.models.DeliveryReceiptsResponse
131131
import io.getstream.chat.android.network.models.FullUserResponse
132132
import io.getstream.chat.android.network.models.PrivacySettingsResponse
133133
import io.getstream.chat.android.network.models.ReadReceiptsResponse
134+
import io.getstream.chat.android.network.models.SortParamRequest
134135
import io.getstream.chat.android.network.models.TypingIndicatorsResponse
135136
import io.getstream.chat.android.network.models.UserMuteResponse
136137
import io.getstream.chat.android.network.models.UserResponse
@@ -1505,8 +1506,8 @@ internal class DomainMappingTest {
15051506
*/
15061507
@ParameterizedTest
15071508
@MethodSource("toSortDomainArguments")
1508-
fun `List of sort maps is correctly mapped to QuerySorter`(
1509-
input: List<Map<String, Any>>?,
1509+
fun `List of sort params is correctly mapped to QuerySorter`(
1510+
input: List<SortParamRequest>?,
15101511
expected: QuerySorter<Channel>?,
15111512
) {
15121513
val sut = Fixture().get()
@@ -1540,29 +1541,24 @@ internal class DomainMappingTest {
15401541
fun toSortDomainArguments() = listOf(
15411542
// null/error → null
15421543
Arguments.of(null, null),
1543-
Arguments.of(emptyList<Map<String, Any>>(), null),
1544-
Arguments.of(listOf(mapOf("direction" to -1)), null),
1545-
Arguments.of(listOf(mapOf("field" to "created_at")), null),
1546-
Arguments.of(listOf(mapOf("field" to "created_at", "direction" to 0)), null),
1544+
Arguments.of(emptyList<SortParamRequest>(), null),
1545+
Arguments.of(listOf(SortParamRequest(direction = -1)), null),
1546+
Arguments.of(listOf(SortParamRequest(field = "created_at")), null),
1547+
Arguments.of(listOf(SortParamRequest(field = "created_at", direction = 0)), null),
15471548
// valid parsing
15481549
Arguments.of(
1549-
listOf(mapOf("field" to "created_at", "direction" to 1)),
1550+
listOf(SortParamRequest(field = "created_at", direction = 1)),
15501551
ascByName<Channel>("created_at"),
15511552
),
15521553
Arguments.of(
1553-
listOf(mapOf("field" to "last_message_at", "direction" to -1)),
1554+
listOf(SortParamRequest(field = "last_message_at", direction = -1)),
15541555
descByName<Channel>("last_message_at"),
15551556
),
1556-
// Double direction (Moshi edge case)
1557-
Arguments.of(
1558-
listOf(mapOf("field" to "created_at", "direction" to -1.0)),
1559-
descByName<Channel>("created_at"),
1560-
),
15611557
// multiple fields
15621558
Arguments.of(
15631559
listOf(
1564-
mapOf("field" to "created_at", "direction" to -1),
1565-
mapOf("field" to "name", "direction" to 1),
1560+
SortParamRequest(field = "created_at", direction = -1),
1561+
SortParamRequest(field = "name", direction = 1),
15661562
),
15671563
descByName<Channel>("created_at").ascByName("name"),
15681564
),

‎stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/mapping/QuerySortByFieldRoundTripTest.kt‎

Lines changed: 4 additions & 3 deletions

0 commit comments

Comments
 (0)