Skip to content

Instantly share code, notes, and snippets.

@chrislconover
Last active February 2, 2018 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislconover/74b42a1ec6fee1048a74c503184eadc6 to your computer and use it in GitHub Desktop.
Save chrislconover/74b42a1ec6fee1048a74c503184eadc6 to your computer and use it in GitHub Desktop.
Single selection logic for a cell, using RxSwift
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