Apologies
You must be signed in to watch this lesson.
With many dating applications out there, its useful to filter down the result set that you want to retrieve based on user settings. In this video, we'll implement an additional cell that captures minimum and maximum age ranges for our filter. We'll utilize this in a Firestore query later on.

Comments (6)
st4n
5 years ago
I changed this func a little bit to provide both way functionality :) fileprivate func evaluateMinMax() { guard let ageRangeCell = tableView.cellForRow(at: [5, 0]) as? AgeRangeCell else { return } let minSliderValue = Int(ageRangeCell.minSlider.value) let maxSliderValue = Int(ageRangeCell.maxSlider.value) let minValue = min(minSliderValue, maxSliderValue) let maxValue = max(minSliderValue, maxSliderValue) ageRangeCell.maxSlider.value = Float(maxValue) ageRangeCell.minSlider.value = Float(minValue) ageRangeCell.minLabel.text = "Min \(minValue)" ageRangeCell.maxLabel.text = "Max \(maxValue)" user?.minSeekingAge = minValue user?.maxSeekingAge = maxValue }
Brian Voong
5 years ago
st4n
5 years ago
I'm checking like twice or three times a day for new episodes :)
Brian Voong
5 years ago
ayon
5 years ago
Will you be doing some refactoring regarding the settingsViewController cells into a viewModel in later videos?
Brian Voong
5 years ago
st4n
5 years ago
listen to Nike :) ahahaha
slcott
5 years ago
Setting up the sliders with a ViewModel really helped my understanding of MVVM. This is my VM: ``` class SettingsViewModel { var ageRange: AgeRange = AgeRange(min: 18, max: 36) var minAge: Int { set(newValue) { if newValue > ageRange.max { maxAge = newValue + 1 } ageRange = AgeRange(min: max(18, newValue), max: ageRange.max) bindableAge.value = ageRange } get { return ageRange.min } } var maxAge: Int { set(newValue) { if newValue < ageRange.min { minAge = newValue - 1 } ageRange = AgeRange(min: ageRange.min, max: min(100, newValue)) bindableAge.value = ageRange } get { return ageRange.max } } var bindableAge = Bindable<AgeRange>() } ``` Binding is straightforward: ``` settingsViewModel.bindableAge.bind { [unowned self] (ageRange) in guard let ageRange = ageRange else { return } let indexPath = IndexPath(row: 0, section: 5) guard let cell = self.tableView.cellForRow(at: indexPath) as? AgeRangeCell else { return } let (minAge, maxAge) = (ageRange.min, ageRange.max) cell.minLabel.text = "Min \(minAge)" cell.maxLabel.text = "Max \(maxAge)" cell.minSlider.value = Float(minAge) cell.maxSlider.value = Float(maxAge) } ``` And handler code could not be simpler: ``` @objc fileprivate func handleMinAgeChange(slider: UISlider) { settingsViewModel.minAge = Int(slider.value) } ```
ayon
5 years ago
What is this AgeRange?
slcott
5 years ago
struct AgeRange { let min: Int let max: Int }
ayon
5 years ago
Tx man!
Dennisvm82
5 years ago
Great job, slcott! I also have to do some more stuff with this technique (bindable + MVVM). It's a great way to write better code.
Dennisvm82
5 years ago
By the way, why did you set the age range to 18-36 at the beginning of your model? var ageRange: AgeRange = AgeRange(min: 18, max: 36) Also, when I want to adjust the age, the minimum age slider automatically jumps back to 18 :-) it should stay fixed until the user decides to move it.
slcott
5 years ago
Did you initialize the `settingsViewModel`? Inside of `setupSettingsViewModelObserver()`, you must initialize it by doing this: ``` if let user = user { settingsViewModel.minAge = Float(user.minSeekingAge ?? 18) settingsViewModel.maxAge = Float(user.maxSeekingAge ?? 36) } ```
Tube
5 years ago
OMG It took me 28 lessons to get the "Taco lover" joke! ;-)
Hanisa Mohamed
4 years ago
What does it mean?
Cinquain
5 years ago
That video was fire!
Kenny Ho
5 years ago
for 19:00. Just curious why wouldnt an instance variable work? For the handleMinAgeChange function, why doesn't "let ageRangeCell = AgeRangeCell()" perform the same. Anyone know an explanation? Is what you're doing an alternative to creating a protocol and delegate?
Brian Voong
5 years ago
Kenny Ho
5 years ago
Oh i see. Thank you for your explanation!
David Guarino
4 years ago
Hey quick question on Firebase....when a user saves their settings ...Does Firebase count each change in the field a change OR all changes made = 1 read change for all fields after save is clicked.
HELP & SUPPORT