Skip to content

Commit

Permalink
Improve activity indicator and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sergdort committed Jul 22, 2017
1 parent 6205b4d commit 0aa09a0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 49 deletions.
20 changes: 17 additions & 3 deletions CleanArchitectureRxSwift/Scenes/AllPosts/PostsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ class PostsViewController: UIViewController {

private func bindViewModel() {
assert(viewModel != nil)
let viewWillAppear = rx.sentMessage(#selector(UIViewController.viewWillAppear(_:)))
.mapToVoid()
.asDriverOnErrorJustComplete()
let pull = tableView.refreshControl!.rx
.controlEvent(.valueChanged)
.asDriver()

let input = PostsViewModel.Input(trigger: Driver.just(),
let input = PostsViewModel.Input(trigger: Driver.merge(viewWillAppear, pull),
createPostTrigger: createPostButton.rx.tap.asDriver(),
selection: tableView.rx.itemSelected.asDriver())
let output = viewModel.transform(input: input)
Expand All @@ -35,8 +41,16 @@ class PostsViewController: UIViewController {
cell.detailsLabel.text = item.body
}.addDisposableTo(disposeBag)
//Connect Create Post to UI
output.createPost.drive().addDisposableTo(disposeBag)
output.selectedPost.drive().addDisposableTo(disposeBag)

output.fetching
.drive(tableView.refreshControl!.rx.isRefreshing)
.disposed(by: disposeBag)
output.createPost
.drive()
.disposed(by: disposeBag)
output.selectedPost
.drive()
.disposed(by: disposeBag)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class PostsViewModel: ViewModelType {
return self.useCase.posts()
.trackActivity(activityIndicator)
.trackError(errorTracker)
.asDriver(onErrorJustReturn: [])
.asDriverOnErrorJustComplete()
}

let fetching = activityIndicator.asDriver()
Expand Down
67 changes: 22 additions & 45 deletions CleanArchitectureRxSwift/Utility/ActivityIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,42 @@ import Foundation
import RxSwift
import RxCocoa

private struct ActivityToken<E> : ObservableConvertibleType, Disposable {
private let _source: Observable<E>
private let _dispose: Cancelable

init(source: Observable<E>, disposeAction: @escaping () -> ()) {
_source = source
_dispose = Disposables.create(with: disposeAction)
}

func dispose() {
_dispose.dispose()
}

func asObservable() -> Observable<E> {
return _source
}
}

/**
Enables monitoring of sequence computation.

If there is at least one sequence computation in progress, `true` will be sent.
When all activities complete `false` will be sent.
*/
public class ActivityIndicator : SharedSequenceConvertibleType {
public class ActivityIndicator: SharedSequenceConvertibleType {
public typealias E = Bool
public typealias SharingStrategy = DriverSharingStrategy

private let _lock = NSRecursiveLock()
private let _variable = Variable(0)
private let _variable = Variable(false)
private let _loading: SharedSequence<SharingStrategy, Bool>

public init() {
_loading = _variable.asDriver()
.map { $0 > 0 }
.distinctUntilChanged()
.distinctUntilChanged()
}

fileprivate func trackActivityOfObservable<O: ObservableConvertibleType>(_ source: O) -> Observable<O.E> {
return Observable.using({ () -> ActivityToken<O.E> in
self.increment()
return ActivityToken(source: source.asObservable(), disposeAction: self.decrement)
}) { t in
return t.asObservable()
}
}

private func increment() {
return source.asObservable()
.do(onNext: { _ in
self.sendStopLoading()
}, onError: { _ in
self.sendStopLoading()
}, onCompleted: {
self.sendStopLoading()
}, onSubscribe: subscribed)
}

private func subscribed() {
_lock.lock()
_variable.value = _variable.value + 1
_variable.value = true
_lock.unlock()
}

private func decrement() {
private func sendStopLoading() {
_lock.lock()
_variable.value = _variable.value - 1
_variable.value = false
_lock.unlock()
}

public func asSharedSequence() -> SharedSequence<SharingStrategy, E> {
return _loading
}
Expand Down

0 comments on commit 0aa09a0

Please sign in to comment.