Rxswift hide button inside UITableViewCell after tapped - swift

I have a addButton inside a UITableCieCwell. I want the addButton to be disappear after user click it, so I created a Action and bind it to the addButton.
However, all the addButton is disappeared although I just run my app.
I'm very new to RxSwift, please help me out.
Bind UI
viewModel.courses
.asObservable()
.bind(to: collectionView.rx.items(cellIdentifier: AddableCourseCell.reuseIdentifier, cellType: AddableCourseCell.self)) { (row, element, cell) in
let action = self.viewModel.actions.value[row]
action.enabled.asObservable()
.bind(to: cell.addButton.rx.isHidden)
.disposed(by: self.disposeBag)
cell.addButton.rx
.tap
.debounce(0.3, scheduler: MainScheduler.instance)
.subscribe({ (event) in
action.execute(element)
}).disposed(by: cell.disposeBag)
}.disposed(by: disposeBag)
ViewModel
class ViewModel {
var courses: Variable<[Course]> = Variable([])
var selectedCourses: Variable<[Course]> = Variable([])
var actions = Variable<[Action<Course, Bool>]>([])
private func generateAddActions(courses: [Course]) -> [Action<Course, Bool>] {
var actions: [Action<Course, Bool>] = []
for _ in courses {
let action = Action<Enrollment, Bool>(workFactory: { (input) -> Observable<Bool> in
let isAdded = selectedCourses.value.contains(input)
if !isAdded {
self.selectedCourses.value.append(input)
}
return Observable.just(isAdded)
})
actions.append(action)
}
return actions
}
}

Hi one tip I can give you is to add a .debug() in the action observable so you can see the values emitted. However, I believe what's causing you trouble is that the action observable's initial value is true which is bounded to the isHidden attribute of the addButton

Related

Why UITableViewCell does not bind to cellWiewModel when I use RxTableViewSectionedAnimatedDataSource?

I have some odd problem with UITableViewCell when I use MVVM pattern with RxSwift. I can't describe it, but I will try to explain.
Let's say we have simple UITableViewCell
class MyTableViewCell: UITableViewCell {
var disposeBag = DisposeBag()
override func prepareForReuse() {
super.prepareForReuse()
disposeBag = DisposeBag()
}
func bind(to viewModel: MyCellViewModel) {
viewModel.randomValue.asDriver()
.drive(onNext: { [weak self] value in
guard let self = self else { return}
print(value) // WHEN TWO CELLS I GET NEW VALUES JUST IN ONE OF THEM
})
.disposed(by: disposeBag)
}
with ViewModel with simple timer.
class MyCellViewModel: NSObject {
let randomValue = PublishSubject<Int>()
init() {
Observable<Int>.timer(.seconds(1), period: .seconds(1), scheduler: SerialDispatchQueueScheduler(qos: .userInteractive))
.map { _ in Int.random()}
.do(onNext: {
print($0) // WORK PERFECT FOR BOTH
})
.bind(to: randomValue)
.disposed(by: rx.disposeBag)
}
}
I also use RxDataSources to fill my tableView.
private func makeDataSource() -> RxTableViewSectionedAnimatedDataSource<Section> {
RxTableViewSectionedAnimatedDataSource<Section>(configureCell: { _, tableView, indexPath, item in
switch item {
case let .myItem(cellViewModel):
let cell = tableView.dequeueReusableCell(with: MyTableViewCell.self, for: indexPath)
cell.bind(to: cellViewModel)
return cell
}
})
}
The problem is that when I have two cells, I get new values in one of them and not in the other.
Pay attention to the method bind(to viewModel
But everything is ok when I change RxTableViewSectionedAnimatedDataSource to RxTableViewSectionedReloadDataSource.
How to make it work with RxTableViewSectionedAnimatedDataSource? I understand the difference between them, but in this case I don't know how does that affect it?
I also checked memory graph and saw just two instances of cells and two instances of cellViewModels. So I hope there are no leaks.
Thanks for any help!
You implemented your ItemMode.== incorrectly. It doesn't actually check for equality, it only checks the identity. Remove the function and let the compiler generate the correct one for you.

Handling button touch based on the Observable value

I wanted to know what is the best practice to handling button tap based on the Observable. I've got enum:
enum ButtonState {
case idle, valid, nonValid
}
and Observable which is combining two Observables:
var buttonStateObservable: Observable<ButtonState> {
return Observable.combineLatest(isAddingVariable.asObservable(), addItemName.asObservable(),
resultSelector: { isAdding, itemName in
if isAdding {
return !itemName.isEmpty ? .valid : .nonValid
}
return .idle
})
}
In my ViewController I'm subscribing to this observable and updating UIButton's UI. I would like to handle tap on this button and do certain action based on this observable. What is the best approach?
RxSwift already has ControlEvent that wrapped TouchUpInside event of UIButton. You can access it by .rx.tap:
button.rx.tap.subscribe(onNext: {
self.button.setTitle("\(arc4random_uniform(100))", for: .normal)
self.button.layer.borderColor = UIColor.random.cgColor // or your special color
})
.disposed(by: bag)
Result of 3 taps:
UPD.
You can check other observables by .withLatestFrom(). Improved top example:
let subject = BehaviorSubject<Bool>(value: true)
button.rx.tap.asDriver()
.withLatestFrom(subject.asDriver(onErrorJustReturn: false))
.drive(onNext: { value in
if value {
self.button.setTitle("\(arc4random_uniform(100))", for: .normal)
self.button.layer.borderColor = UIColor.random().cgColor
}
subject.onNext(!value)
})
.disposed(by: bag)
So, in your case you can put Observable.combineLatest(...) in .withLatestFrom and perform logic with these values.

Structuring a View Model Using RxSwift

My view models are fundamentally flawed because those that use a driver will complete when an error is returned and resubscribing cannot be automated.
An example is my PickerViewModel, the interface of which is:
// MARK: Picker View Modelling
/**
Configures a picker view.
*/
public protocol PickerViewModelling {
/// The titles of the items to be displayed in the picker view.
var titles: Driver<[String]> { get }
/// The currently selected item.
var selectedItem: Driver<String?> { get }
/**
Allows for the fetching of the specific item at the given index.
- Parameter index: The index at which the desired item can be found.
- Returns: The item at the given index. `nil` if the index is invalid.
*/
func item(atIndex index: Int) -> String?
/**
To be called when the user selects an item.
- Parameter index: The index of the selected item.
*/
func selectItem(at index: Int)
}
An example of the Driver issue can be found within my CountryPickerViewModel:
init(client: APIClient, location: LocationService) {
selectedItem = selectedItemVariable.asDriver().map { $0?.name }
let isLoadingVariable = Variable(false)
let countryFetch = location.user
.startWith(nil)
.do(onNext: { _ in isLoadingVariable.value = true })
.flatMap { coordinate -> Observable<ItemsResponse<Country>> in
let url = try client.url(for: RootFetchEndpoint.countries(coordinate))
return Country.fetch(with: url, apiClient: client)
}
.do(onNext: { _ in isLoadingVariable.value = false },
onError: { _ in isLoadingVariable.value = false })
isEmpty = countryFetch.catchError { _ in countryFetch }.map { $0.items.count == 0 }.asDriver(onErrorJustReturn: true)
isLoading = isLoadingVariable.asDriver()
titles = countryFetch
.map { [weak self] response -> [String] in
guard let `self` = self else { return [] }
self.countries = response.items
return response.items.map { $0.name }
}
.asDriver(onErrorJustReturn: [])
}
}
The titles drive the UIPickerView, but when the countryFetch fails with an error, the subscription completes and the fetch cannot be retried manually.
If I attempt to catchError, it is unclear what observable I could return which could be retried later when the user has restored their internet connection.
Any justReturn error handling (asDriver(onErrorJustReturn:), catchError(justReturn:)) will obviously complete as soon as they return a value, and are useless for this issue.
I need to be able to attempt the fetch, fail, and then display a Retry button which will call refresh() on the view model and try again. How do I keep the subscription open?
If the answer requires a restructure of my view model because what I am trying to do is not possible or clean, I would be willing to hear the better solution.
Regarding ViewModel structuring when using RxSwift, during intensive work on a quite big project I've figured out 2 rules that help keeping solution scalable and maintainable:
Avoid any UI-related code in your viewModel. It includes RxCocoa extensions and drivers. ViewModel should focus specifically on business logic. Drivers are meant to be used to drive UI, so leave them for ViewControllers :)
Try to avoid Variables and Subjects if possible. AKA try to make everything "flowing". Function into function, into function and so on and, eventually, in UI. Of course, sometimes you need to convert non-rx events into rx ones (like user input) - for such situations subjects are OK. But be afraid of subjects overuse - otherwise your project will become hard to maintain and scale in no time.
Regarding your particular problem. So it is always a bit tricky when you want retry functionality. Here is a good discussion with RxSwift author on this topic.
First way. In your example, you setup your observables on init, I also like to do so. In this case, you need to accept the fact that you DO NOT expect a sequence that can fail because of error. You DO expect sequence that can emit either result-with-titles or result-with-error. For this, in RxSwift we have .materialize() combinator.
In ViewModel:
// in init
titles = _reloadTitlesSubject.asObservable() // _reloadTitlesSubject is a BehaviorSubject<Void>
.flatMap { _ in
return countryFetch
.map { [weak self] response -> [String] in
guard let `self` = self else { return [] }
self.countries = response.items
return response.items.map { $0.name }
}
.materialize() // it IS important to be inside flatMap
}
// outside init
func reloadTitles() {
_reloadTitlesSubject.onNext(())
}
In ViewController:
viewModel.titles
.asDriver(onErrorDriveWith: .empty())
.drive(onNext: [weak self] { titlesEvent in
if let titles = titlesEvent.element {
// update UI with
}
else if let error = titlesEvent.error {
// handle error
}
})
.disposed(by: bag)
retryButton.rx.tap.asDriver()
.drive(onNext: { [weak self] in
self?.viewModel.reloadTitles()
})
.disposed(by: bag)
Second way is basically what CloackedEddy suggests in his answer. But can be simplified even more to avoid Variables. In this approach you should NOT setup your observable sequence in viewModel's init, but rather return it anew each time:
// in ViewController
yourButton.rx.tap.asDriver()
.startWith(())
.flatMap { [weak self] _ in
guard let `self` = self else { return .empty() }
return self.viewModel.fetchRequest()
.asDriver(onErrorRecover: { error -> Driver<[String]> in
// Handle error.
return .empty()
})
}
.drive(onNext: { [weak self] in
// update UI
})
.disposed(by: disposeBag)
I would shift some responsibilities to the view controller.
One approach would be to have the view model produce an Observable which as a side effect updates the view model properties. In the following code example, the view controller remains in charge of the view bindings, as well as triggering the refresh in viewDidLoad() and via a button tap.
class ViewModel {
let results: Variable<[String]> = Variable([])
let lastFetchError: Variable<Error?> = Variable(nil)
func fetchRequest() -> Observable<[String]> {
return yourNetworkRequest
.do(onNext: { self.results.value = $0 },
onError: { self.lastFetchError.value = $0 })
}
}
class ViewController: UIViewController {
let viewModel = ViewModel()
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
viewModel.results
.asDriver()
.drive(onNext: { yourLabel.text = $0 /* .reduce(...) */ })
.disposed(by: disposeBag)
viewModel.lastFetchError
.asDriver()
.drive(onNext: { yourButton.isHidden = $0 == nil })
.disposed(by: disposeBag)
yourButton.rx.tap
.subscribe(onNext: { [weak self] in
self?.refresh()
})
.disposed(by: disposeBag)
// initial attempt
refresh()
}
func refresh() {
// trigger the request
viewModel.fetchRequest()
.subscribe()
.disposed(by: disposeBag)
}
}
All answers are good, but i want to mentioned about CleanArchitectureRxSwift. This framework really help me to find the way how rx can be applied to my code. The part about "backend" mobile programming (request, parsers, etc) can be omitted, but work with viewModel/viewController has really interesting things.

RxSwift: How to correctly subscribe to a variable change

I have two screens:
NewControlTableViewController: contains a textfield for selecting a client from the other view
ClientsTableViewController: The second view contains a list clients that can be selected
The two screens share a viewmodel.
So here is my code:
import RxSwift
import RxCocoa
struct NewControlViewModel {
var selectedClient = Variable<Client>(Client())
// other stuff
}
// NewControlTableViewController : viewDidLoad
viewModel.selectedClient.asObservable().subscribe { event in
debugPrint(event)
}
// ClientsTableViewController: viewDidLoad
/*tableView.rx.itemSelected.subscribe(onNext: { indexPath in
let client = self.clients[indexPath.row]
debugPrint(client)
self.viewModel.selectedClient.value = client
self.navigationController?.popToRootViewController(animated: true)
}).disposed(by: self.disposeBag)*/
// new code
tableView.rx
.modelSelected(Client.self)
.debug("client selected", trimOutput: true)
.do(onNext: { _ in
self.navigationController?.popViewController(animated: true)
})
.subscribe(onNext: { client in
debugPrint(client)
self.viewModel.selectedClient.value = client
}, onError: { error in
debugPrint(error)
}).disposed(by: disposeBag)
The event is fired(with empty values) whenever I view the first screen, then, after selecting a client from the second screen, for some reason, no event is being fired.
First of all, Variables are deprecated.
You should use a PublishRelay which doesn't need an initial value, that way it won't be fired upon subscription in your first screen.
The advantage of Relays is they don't error out or complete.
struct NewControlViewModel {
let selectedClient = PublishRelay<Client>()
}
// NewControlTableViewController : viewDidLoad
choisirMandat.rx.tap.subscribe(onNext: { [unowned self] in
let viewController = /* instantiate vc */
// Make sure to use the same viewModel
viewController.viewModel = self.viewModel
self.present(viewController, animated: true)
}).disposed(by: disposeBag)
self.viewModel.selectedClient.debug().subscribe().disposed(by: self.disposeBag)
// ClientsTableViewController: viewDidLoad
tableView.rx.itemSelected.map { [unowned self] indexPath in
return self.clients[indexPath.row]
}
.debug("client selected", trimOutput: true)
.do(onNext: { [unowned self] _ in
self.navigationController?.popToRootViewController(animated: true)
})
.bind(to: self.viewModel.selectedClient)
.disposed(by: self.disposeBag)
On a side note, clients should probably also come from the Rx world if you want to be fully functional.
Second side note, you can use the debug() operator to print events.
bind(to:) was added in RxSwift 4.1 so make sure to be up to date
I think I have found your issue here. You are doing is
// NewControlTableViewController : viewDidLoad
viewModel.selectedClient.asObservable().subscribe { event in
debugPrint(event)
}
// ClientsTableViewController: viewDidLoad
tableView.rx.itemSelected.subscribe(onNext: { indexPath in
let client = self.clients[indexPath.row]
debugPrint(client)
self.viewModel.selectedClient.value = client
self.navigationController?.popToRootViewController(animated: true)
}).disposed(by: self.disposeBag)
You need to change this line "self.viewModel.selectedClient.value = client". You need to use previous controller instance or NewControlTableViewController instance like self.newControlTableVC.selectedClient.value = client
I think you are trying to use viewmodel by sending there value from NewControlTableViewController to ClientsTableViewController.
Moreover, I did the same scenario which you did and it worked very well in my case. Just try my scenario if it could not work there must be some minor issue.
I hope it may help

UIRefreshControl with RxSwift

Hi I am trying to make a UIRefreshControl work with RxSwift. Therefore I am using the Activity Indicator that is in the RxSwift Example.
In my viewModel I have the following function and variable to get my data.
// MARK: - Variables
var data = Variable<[Data]>([])
// MARK: - Public Interface
func getData() {
let request = Data.readAll()
_ = request.rxResult().subscribe(onNext: { response in
self.data.value = response.data
}, onError: { (Error) in
}, onCompleted: {
}, onDisposed: {
})
}
Then in my view controller I try to bind it to the UIRefreshcontrol and the collection view I have.
let refresher: UIRefreshControl = UIRefreshControl()
let indicator = ActivityIndicator()
indicator.asObservable()
.bindTo(refresher.rx.isRefreshing)
.addDisposableTo(disposeBag)
let resultObservable = viewModel.data.asObservable()
.trackActivity(indicator)
.bindTo(self.collectionView.rx.items(cellIdentifier: reuseCell, cellType: DataCollectionViewCell.self)) {
row, data, cell in
cell.configureCell(with: data)
}
resultObservable.addDisposableTo(disposeBag)
My question is, what am I missing to make this work? Right now if I start the app nothing happens except a black activity indicator that doesn't stop spinning.
I think you should prefer subscribing in ViewController and add that subscription in that view controller's dispose bag.
The following way I think is the correct way of using ActivityIndicator from RxExamples. following is a pseudo code.
/// ViewController.swift
import RxSwift
import RxCocoa
…
let refreshControl = UIRefreshControl()
refreshControl.rx.controlEvent(.valueChanged)
.bind(to:self.viewModel.inputs.loadPageTrigger)
.disposed(by: disposeBag)
self.viewModel.indicator
.bind(to:refreshControl.rx.isRefreshing)
.dispose(by:disposeBag)
…
/// ViewModel.swift
…
let loadTrigger = PublishSubject<Void>()
let indicator = ActivityIndicator().asDriver()
…
// Assuming rxResult returns Observable<Response>
let req = indicator.asObservable()
.sample(loadTrigger)
.flatMap { isLoading -> Observable<Response> in
if isLoading { return Observable.empty() }
return Data.readAll().rxResult()
}
.trackActivity(indicator)
.map { $0.data }
.do(onNext: { [unowned self] data in
self.data.value = data
})
…
refresher
.rx.controlEvent(UIControlEvents.valueChanged)
.subscribe(onNext: { [weak self] in
//Put your hide activity code here
self?.refresher.endRefreshing()
}, onCompleted: nil, onDisposed: nil)
.disposed(by: disposeBag)
Subscribe an event of refresher(UIRefreshControl)