API Reference / iOS InstantSearch Widgets / Filter Numeric Range
Apr. 24, 2019

Filter Numeric Range

About this widget

Filter Numeric Range is a filtering view made to filter between two numeric values. The most common interface for this is a slider.

To add a filter numeric range to your search experience, use these components:

  • Searcher: The Searcher that handles your searches.
  • FilterState: The current state of the filters.
  • NumberRangeInteractor: The logic applied to the numeric ranges.
  • NumberRangeController: The controller that interfaces with a concrete numeric range view.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let searcher: SingleIndexSearcher = .init(...)
let filterState: FilterState = .init()

let numericRangeInteractor: NumberRangeInteractor<Double> = .init()
let numericRangeController: NumericRangeController = .init(rangeSlider: RangeSlider())

let priceAttribute: Attribute = Attribute("price")

override func viewDidLoad() {
  super.viewDidLoad()
  
  searcher.connectFilterState(filterState)

  numericRangeInteractor.connectFilterState(filterState, attribute: priceAttribute)
  numericRangeInteractor.connectController(numericRangeController)
  numericRangeInteractor.connectSearcher(searcher, attribute: priceAttribute)

  // In case we want to do a search when on empty query
  searcher.search()
}

Parameters

attribute
type: Attribute
Required

The attribute to filter.

1
2
3
let attribute = Attribute("price")
numericRangeInteractor.connectFilterState(filterState, attribute: attribute)
numericRangeInteractor.connectSearcher(searcher, attribute: attribute)

Customize your view

The controllers provided by default, like the NumericRangeController work well when you want to use native UIKit with their default behavior.

If you want to use another component, a third-party input view, or you want to introduce some custom behavior to the already provided UIKit component, you can create your own controller conforming to the NumberRangeController protocol.

Protocol

func setBounds(_ bounds: ClosedRange<Double>):

Function called when the minimum and maximum of numeric filter view have been defined.

var onRangeChanged: ((ClosedRange<Double>) -> Void)?

Closure to call when new bounds is set for the numeric range filter.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import Foundation
import InstantSearch
import UIKit
import WARangeSlider

public class NumericRangeController: NumberRangeController {
  public var onRangeChanged: ((ClosedRange<Double>) -> Void)?

  public typealias Number = Double

  public func setItem(_ item: ClosedRange<Double>) {
    rangerSlider.lowerValue = item.lowerBound
    rangerSlider.upperValue = item.upperBound
  }

  @objc func onValueChanged(sender: RangeSlider) {
    onRangeChanged?(sender.lowerValue.rounded(toPlaces: 2)...sender.upperValue.rounded(toPlaces: 2))
  }

  public func setBounds(_ bounds: ClosedRange<Double>) {
    rangerSlider.minimumValue = bounds.lowerBound
    rangerSlider.maximumValue = bounds.upperBound
    setItem(bounds)
  }

  public let rangerSlider: RangeSlider

  public init(rangeSlider: RangeSlider) {
    self.rangerSlider = rangeSlider
    rangeSlider.addTarget(self, action: #selector(onValueChanged(sender:)), for: [.touchUpInside, .touchUpOutside])
  }

}

Did you find this page helpful?