Last active
February 2, 2018 04:14
-
-
Save chrislconover/74b42a1ec6fee1048a74c503184eadc6 to your computer and use it in GitHub Desktop.
Single selection logic for a cell, using RxSwift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParentViewModelClass { | |
func singleSelectionCell(_ title: String) -> FormCellBuildable { | |
return FormCheckmarkCellModel( | |
title: title, selected: false, | |
didSelect: { model in | |
// create single use subscription | |
let _ = self.selectedValue.asObservable().withPrevious(startWith: nil).take(1) | |
.filter { $0.1 != nil }.map { ($0.0, $0.1!) } | |
.subscribe(onNext: { from, to in | |
from?.selected.value = false | |
to.selected.value = true }) | |
// signal change to selected, triggering the above | |
self.selectedValue.value = model }).cell | |
} | |
lazy var selectedValue = Variable<FormCheckmarkCellModel?>(nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment