@@ -18,23 +18,38 @@ package io.getstream.android.core.api.filter
1818import io.getstream.android.core.annotations.StreamInternalApi
1919import io.getstream.android.core.annotations.StreamPublishedApi
2020import io.getstream.android.core.internal.filter.BinaryOperator
21+ import io.getstream.android.core.internal.filter.BooleanBinaryOperator
2122import io.getstream.android.core.internal.filter.CollectionOperator
2223import io.getstream.android.core.internal.filter.FilterOperations
24+ import io.getstream.android.core.internal.filter.StringBinaryOperator
2325
2426/* *
2527 * Base interface for filters used in Stream API operations.
2628 *
2729 * Filters are used to specify criteria for querying and retrieving data from Stream services. Each
2830 * filter implementation defines specific matching logic for different comparison operations.
2931 */
30- @StreamPublishedApi public sealed interface Filter <M , F : FilterField <M >>
32+ @StreamPublishedApi
33+ public sealed interface Filter <M , F : FilterField <M >>
3134
3235internal data class BinaryOperationFilter <M , F : FilterField <M >>(
3336 val operator : BinaryOperator ,
3437 val field : F ,
3538 val value : Any ,
3639) : Filter<M, F>
3740
41+ internal data class BooleanBinaryOperationFilter <M , F : FilterField <M >>(
42+ val operator : BooleanBinaryOperator ,
43+ val field : F ,
44+ val value : Boolean ,
45+ ) : Filter<M, F>
46+
47+ internal data class StringBinaryOperationFilter <M , F : FilterField <M >>(
48+ val operator : StringBinaryOperator ,
49+ val field : F ,
50+ val value : String ,
51+ ) : Filter<M, F>
52+
3853internal data class CollectionOperationFilter <M , F : FilterField <M >>(
3954 internal val operator : CollectionOperator ,
4055 val filters : Set <Filter <M , F >>,
@@ -45,6 +60,12 @@ internal data class CollectionOperationFilter<M, F : FilterField<M>>(
4560public fun Filter <* , * >.toRequest (): Map <String , Any > =
4661 when (this ) {
4762 is BinaryOperationFilter <* , * > -> mapOf (field.remote to mapOf (operator .remote to value))
63+ is BooleanBinaryOperationFilter <* , * > ->
64+ mapOf (field.remote to mapOf (operator .remote to value))
65+
66+ is StringBinaryOperationFilter <* , * > ->
67+ mapOf (field.remote to mapOf (operator .remote to value))
68+
4869 is CollectionOperationFilter <* , * > ->
4970 mapOf (operator .remote to filters.map(Filter <* , * >::toRequest))
5071 }
@@ -67,15 +88,37 @@ public infix fun <M, F : FilterField<M>> Filter<M, F>.matches(item: M): Boolean
6788 notNull && fieldValue greaterOrEqual filterValue
6889 BinaryOperator .LESS_OR_EQUAL -> notNull && fieldValue lessOrEqual filterValue
6990 BinaryOperator .IN -> notNull && fieldValue `in ` filterValue
70- BinaryOperator .QUERY -> notNull && search(filterValue, where = fieldValue)
71- BinaryOperator .AUTOCOMPLETE -> notNull && fieldValue autocompletes filterValue
72- BinaryOperator .EXISTS -> fieldValue exists filterValue
7391 BinaryOperator .CONTAINS -> notNull && fieldValue contains filterValue
7492 BinaryOperator .PATH_EXISTS -> notNull && fieldValue containsPath filterValue
7593 }
7694 }
7795 }
7896
97+ is StringBinaryOperationFilter <M , F > -> {
98+ val fieldValue = field.localValue(item)
99+ val filterValue = value
100+ val notNull = fieldValue != null
101+
102+ with (FilterOperations ) {
103+ when (operator ) {
104+ StringBinaryOperator .QUERY -> notNull && search(filterValue, where = fieldValue)
105+ StringBinaryOperator .AUTOCOMPLETE ->
106+ notNull && fieldValue autocompletes filterValue
107+ }
108+ }
109+ }
110+
111+ is BooleanBinaryOperationFilter <M , F > -> {
112+ val fieldValue = field.localValue(item)
113+ val filterValue = value
114+
115+ with (FilterOperations ) {
116+ when (operator ) {
117+ BooleanBinaryOperator .EXISTS -> fieldValue exists filterValue
118+ }
119+ }
120+ }
121+
79122 is CollectionOperationFilter <M , F > -> {
80123 when (operator ) {
81124 CollectionOperator .AND -> filters.all { it.matches(item) }
0 commit comments