Skip to content

Commit

Permalink
fix(range): filter range bounds init (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam authored Jul 19, 2022
1 parent 5cf2d13 commit 22cfed2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.algolia.instantsearch.core.subscription

import com.algolia.instantsearch.core.subscription.internal.SubscriptionOnce
import kotlin.properties.Delegates

public class SubscriptionValue<T>(initialValue: T) : Subscription<T>() {
Expand All @@ -13,6 +14,10 @@ public class SubscriptionValue<T>(initialValue: T) : Subscription<T>() {
subscriptions += subscription
}

public fun subscribePastOnce(skipNull: Boolean = true, subscription: (T) -> Unit) {
subscriptions += SubscriptionOnce(this, skipNull, subscription)
}

public fun notifySubscriptions() {
notifyAll(value)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.algolia.instantsearch.core.subscription.internal

import com.algolia.instantsearch.core.subscription.Subscription

internal class SubscriptionOnce<T>(
private val subscription: Subscription<T>,
private val skipNull: Boolean,
private val call: (T) -> Unit
) : (T) -> Unit {

override fun invoke(value: T) {
if (value == null && skipNull) return
execute(value)
}

private fun execute(value: T) {
subscription.unsubscribe(this)
call(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public data class FilterRangeConnector<T>(
) : AbstractConnection() where T : Number, T : Comparable<T> {

/**
* ```
* | Bounds/Range | connectSearcher | Behavior |
* |--------------|-----------------|----------------------------------------------------------------------------------------------------------------|
* | Yes | Yes | The bounds retrieved from the first search operation, the range will match the value set using the constructor |
* | No | Yes | The bounds retrieved from the first search operation, the range will match the bounds |
* | Yes | No | The bounds/range from the constructor are used |
* | No | No | Undefined behavior |
* ```
*
* @param filterState the FilterState that will hold your filters
* @param attribute the attribute to filter
* @param bounds the limits of the acceptable range within which values will be coerced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ internal data class FilterRangeConnectionFilterState<T>(
.filterIsInstance<Filter.Numeric.Value.Range>()
.firstOrNull()

viewModel.range.value = if (filter != null) Range(filter.lowerBound as T, filter.upperBound as T) else null
if (filter != null) { // avoid default range override to null
viewModel.range.value = Range(filter.lowerBound as T, filter.upperBound as T)
}
}
private val updateFilterState: Callback<Range<T>?> = { range ->
filterState.notify {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ internal class FilterRangeConnectionSearcherImpl<T>(
val max = mapper(it.max)
Range(min, max)
}
if (range.value == null) { // if no range is specified, match the bounds.
range.value = bounds.value
}
}

override fun connect() {
super.connect()
searcher.query.updateQueryFacets(attribute)
searcher.response.subscribePast(responseSubscription)
searcher.response.subscribePastOnce(subscription = responseSubscription)
}

private fun CommonSearchParameters.updateQueryFacets(attribute: Attribute) {
val current = facets?.toMutableSet() ?: mutableSetOf()
facets = if (!current.contains(attribute)) current + attribute else setOf(attribute)
facets = (facets?.toMutableSet() ?: mutableSetOf()).apply { add(attribute) }
}

override fun disconnect() {
Expand Down

0 comments on commit 22cfed2

Please sign in to comment.