I am looking for solution for a problem I am facing. I have TableView which has multiple cell and each cell has a UISwitch and state of that switch (either on/off) is being set like this:
viewModel.permissions
.drive(tblMessageTypes.rx.items(cellIdentifier: "PermissionCellIdentifier", cellType: PermissionTableViewCell.self)) { [switchSubject] _, item, cell in
cell.backgroundColor = .clear
cell.lblTitle.text = item.permissionTitle
cell.lblDetail.text = item.permissionDescirption
cell.selectionStyle = .none
cell.switchPermission.isOn = item.permissionValue
cell.switchPermission.isEnabled = !item.isPermissionReadOnly
cell.switchPermission.rx.controlEvent(.valueChanged)
.withLatestFrom(cell.switchPermission.rx.value)
.map { isOn in
var newPermission = item
newPermission.permissionValue = isOn
return newPermission
}
.bind(to: switchSubject)
.disposed(by: cell.disposeBag)
}
.disposed(by: disposeBag)
So when Switch is toggled, I am passing an current cell value with update Switch state and based on that I am calling api in my VM like this:
let serverReponse = inputs.switchToggle
.map { permission in
let dicto = permission.toDictionary()
let parameters = ["permissions": [dicto]]
return parameters
} .flatMapLatest { parameters in
userService.updateUserPermission(parameters: parameters)
.trackActivity(inputs.indicator)
.materialize()
}
.share()
Now the issue I have is, if api is failed due to any reason, How should that UISwitch should fallback to initial state, i.e if it was Off and user toggled it to On State and Api was failed it should fall back to Off State.