Skip to content

Commit a372b84

Browse files
feat: Rating range controller (#147)
* Add `NumericRatingRangeController` implementation * Fix missing computation in `NumericRatingController`
1 parent 8b50dec commit a372b84

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

Sources/InstantSearch/Numeric/NumericRatingController.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ public class NumericRatingController {
1515

1616
public let ratingControl: RatingControl
1717

18+
var computation: Computation<Double>?
19+
1820
public init(ratingControl: RatingControl = .init()) {
1921
self.ratingControl = ratingControl
2022
}
2123

24+
@objc private func ratingValueChanged(_ ratingControl: RatingControl) {
25+
computation?.just(value: ratingControl.value)
26+
}
27+
2228
}
2329

2430
extension NumericRatingController: NumberController {
@@ -28,7 +34,8 @@ extension NumericRatingController: NumberController {
2834
}
2935

3036
public func setComputation(computation: Computation<Double>) {
31-
37+
self.computation = computation
38+
ratingControl.addTarget(self, action: #selector(ratingValueChanged), for: .valueChanged)
3239
}
3340

3441
public func setBounds(bounds: ClosedRange<Double>?) {
@@ -37,6 +44,10 @@ extension NumericRatingController: NumberController {
3744
}
3845
}
3946

47+
public func invalidate() {
48+
ratingControl.value = 0
49+
}
50+
4051
}
4152

4253
#endif
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// NumericRatingRangeController.swift
3+
//
4+
//
5+
// Created by Vladislav Fitc on 04/11/2020.
6+
//
7+
8+
#if !InstantSearchCocoaPods
9+
import InstantSearchCore
10+
#endif
11+
#if canImport(UIKit) && (os(iOS) || os(macOS))
12+
import UIKit
13+
14+
public class NumericRatingRangeController {
15+
16+
public let ratingControl: RatingControl
17+
18+
public var onRangeChanged: ((ClosedRange<Double>) -> Void)?
19+
20+
public init(ratingControl: RatingControl = .init()) {
21+
self.ratingControl = ratingControl
22+
ratingControl.addTarget(self, action: #selector(ratingValueChanged), for: .valueChanged)
23+
}
24+
25+
@objc private func ratingValueChanged(_ ratingControl: RatingControl) {
26+
onRangeChanged?(ratingControl.value...Double(ratingControl.maximumValue))
27+
}
28+
29+
}
30+
31+
extension NumericRatingRangeController: NumberRangeController {
32+
33+
public func setBounds(_ bounds: ClosedRange<Double>) {
34+
ratingControl.maximumValue = Int(bounds.upperBound)
35+
}
36+
37+
public func setItem(_ item: ClosedRange<Double>) {
38+
ratingControl.value = item.lowerBound
39+
}
40+
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)