I'm very new to RXSwift and looking to control my table view swipe action based on a value of an observable.
I have a variable - Observable<[Product]> and based on the if Product.isEnabled I want to show a "Sold Out" or "In Stock" swipe action. Here is the current code:
func tableView(
_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath
) -> UISwipeActionsConfiguration? {
self.interactionService.prod
let soldOutAction = UIContextualAction(
style: .normal,
title: "Sold Out"
) { [weak self] (_, _, completionHandler) in
guard let self = self else { return }
self.interactionService.products.map({
$0[indexPath.row]
})
.take(1)
.do(onNext: { [weak self] in
guard let self = self else { return }
self.analytics.userMarkedAsSoldOut(product: $0.productID)
})
.flatMapLatest({self.interactionService.disableProduct($0)})
.subscribe(onCompleted: {
completionHandler(true)
})
.disposed(by: self.disposeBag)
}
soldOutAction.backgroundColor = UIColor(red: 0.996,
green: 0.09,
blue: 0.478,
alpha: 1)
let config = UISwipeActionsConfiguration(actions: [soldOutAction])
config.performsFirstActionWithFullSwipe = false
return config
}
The above code works fine. But I need to update it so that if products[indexPath.row].isEnabled -> use soldOutAction and if !isEnabled, use a newly created inStockAction.
As stated earlier, I'm very new to RXSwift so I don't know how to change the RXSwift syntax to use either soldOutAction or inStockAction. Any help on this matter would be greatly appreciated.