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

Filter Toggle

About this widget

Filter Toggle is a filtering view that displays any kind of filter, and lets the user refine the search results by toggling it on or off.

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

  • Searcher: The Searcher that handles your searches.
  • FilterState: The current state of the filters.
  • SelectableInteractor: The logic applied to the filter.
  • SelectableController: The controller that interfaces with a concrete toggle view.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ToggleFilterViewController: UIViewController {
  
  let searcher: SingleIndexSearcher = .init(appID: "YourApplicationID",
                                            apiKey: "YourSearchOnlyAPIKey",
                                            indexName: "YourIndexName")
  let filterState: FilterState = .init()
  let filter: Filter.Tag = "on sale"
  lazy var onSaleFilterInteractor: SelectableInteractor<Filter.Tag> = { return .init(item: filter) }()
  let onSaleSwitchController: FilterSwitchController<Filter.Tag> = .init(switch: UISwitch())
  
  override func viewDidLoad() {
    super.viewDidLoad()
    setup()
  }
  
  func setup() {
    searcher.connectFilterState(filterState)
    onSaleFilterInteractor.connectFilterState(filterState)
    onSaleFilterInteractor.connectController(onSaleSwitchController)
  }
  
}

Parameters

filter
type: Filter
Required

The filter to apply.

1
2
let filter = Filter.Facet(attribute: "category", stringValue: "shoes")
SelectableInteractor(item: filter)
operator
type: RefinementOperator
default: .or
Optional

Defines the group type in which filter will be placed in FilterState

1
interactor.connectFilterState(filterState, operator: .or)
groupName
type: String?
default: nil
Optional

Defines the group name in which filter will be placed in FilterState. If not specified, the name of the filter attribute will be used as a group name.

1
viewModel.connectFilterState(filterState, operator: .or, groupName: "myGroup")
isSelected
type: Boolean
default: false
Optional

If true, the filter will be active when created.

1
2
let interactor = SelectableInteractor(item: filter)
interactor.isSelected = true

Did you find this page helpful?