Usage

Read the API reference and the USAGE.md for detailed information.

Basic Usage

Import frameworks to your project

import RxSwift
import RxCocoa
import RxPullToRefresh

Add RxPullToRefresh

Create a RxPullToRefresh object.

// Create a RxPullToRefresh object
self.topPullToRefresh = RxPullToRefresh(position: .top)
// Add a RxPullToRefresh object to UITableView
self.tableView.p2r.addPullToRefresh(self.topPullToRefresh)

Observe RxPullToRefreshDelegate

By observing RxPullToRefreshDelegate, you can watch the state of a RxPullToRefresh object. This delegate is get called by the RxPullToRefresh object every time its state or scrolling rate is changed.

// Observe RxPullToRefreshDelegate
self.topPullToRefresh.rx.action
        .subscribe(onNext: { [weak self] (state: RxPullToRefreshState, progress: CGFloat, scroll: CGFloat) in
            // Send request if RxPullToRefreshState is changed to .loading
            switch state {
            case .loading: self?.prepend()
            default:       break
            }
        })
        .disposed(by: self.disposeBag)

Load and append contents

self.viewModel.prepend()
              .subscribe(onSuccess: { [weak self] in
                  // Successfully loaded, collapse refresh view immediately
                  self?.tableView.p2r.endRefreshing(at: .top)
              }, onError: { [weak self] (_: Error) in
                  // Failed to load, show error
                  self?.tableView.p2r.failRefreshing(at: .top)
              })
              .disposed(by: self.disposeBag)

Disable refreshing by binding Boolean value to canLoadMore property

self.viewModel.canPrepend
        .asDriver()
        .drive(self.topPullToRefresh.rx.canLoadMore)
        .disposed(by: self.disposeBag)

Dispose RxPullToRefresh objects

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    self.tableView.p2r.endAllRefreshing()
    self.tableView.p2r.removeAllPullToRefresh()
}

Advanced Usage

About the example project

RxPullToRefresh allows you flexibly customize a refresh view by inheriting RxPullToRefresh and RxPullToRefreshView classes. Please check example sources for advanced usage.

Build the example app

  1. Update Carthage frameworks bash $ carthage update --platform iOS
  2. Open RxPullToRefresh.xcodeproj
  3. Select the scheme RxPullToRefreshExample from the drop-down menu in the upper left of the Xcode window
  4. Press ⌘R