Giter Site home page Giter Site logo

mjrefresh-rxswift's Introduction

MJRefresh-RxSwift

对MJRefresh进行RxSwift支持。可以对刷新状态多次订阅多次触发。

另对UIScrollView进行了扩展,方便在使用MVVM时绑定用。

背景

自己对Reactive扩展的Rx属性发现不能多次订阅,只会最后一次订阅会被触发。

找了一些其他人写的,方式大同小异,结果也都是仅有最后一次订阅会被触发。

原因

查看了RxCocoa的源码。发现了问题所在。RxCocoa中关于UIControl的实现,ControlEvent是基于addTarget,ControlProperty是基于valueForState。

UIControl可以多次addTarget和订阅ControlEvent.editChange等状态, 但是MJRefresh的refreshingBlock也好、setRefreshingTarget也好,都是被最后订阅的人所持有。

解决

一对多?使用KVO对MJRefreshState进行观察即可!

注:以下代码中的~>操作符是RxBinding库提供的绑定操作符。

使用方式

  1. 导入MJRefresh+RxSwift.swift文件。
  2. 对状态订阅
header.rx.state.filter({$0 == .refreshing})
.subscribe{
}
  1. 状态绑定到刷新控件
/// 开始刷新。控件进入刷新状态
Observable.just(MJRefreshState.refreshing) ~> header.rx.state
  1. 大多数情况都是仅关注刷新,用以下便捷方式
header.rx.refresh
.subscribe{
}

UIScrolView扩展

如果使用了MVVM架构,可能需要这个扩展

在ViewModel只有一个Refresh或者LoadMore操作传入,直接在viewModel里发射出需要的MJRefreshState刷新状态,在View里订阅或绑定即可。

如果同时有Refresh和LoadMore,需要对不同类型的控件分别发送状态, 则可以导入UIScrollView+Rxswit.swift文件。

提供了常见的操作类型MJRefreshAction:

enum MJRefreshAction {
    /// 开始刷新
    case begainRefresh
    /// 停止刷新
    case stopRefresh
    /// 开始加载更多
    case begainLoadmore
    /// 停止加载更多
    case stopLoadmore
    /// 显示无更多数据
    case showNomoreData
    /// 重置无更多数据
    case resetNomoreData
}

在viewModel里发射所需的刷新操作即可。示例:

/// 刷新
refresh.flatMapLatest{
    requestDatas()
}
.subscrip{
    self.refreshSubject.onNext(.stopRefresh)//停止刷新
    self.refreshSubject.onNext(.resetNomoreData)//重设无更多数据
}
/// 加载更多
loadMore.flatMapLatest{
    requestDatas()
}
.subscrip{
    self.refreshSubject.onNext(.stopLoadmore)//停止加载更多
    self.refreshSubject.onNext(.showNomoreData)//无更多数据
}

然后在View层对操作进行监听(绑定)。

viewModel.output.refreshAction ~> self.collectionView.rx.refreshAction

特色

可以多处监听刷新状态。

header.rx.state.filter({$0 == .refreshing})
.subscribe{
print"state1"
}
header.rx.state.filter({$0 == .refreshing})
.subscribe{
print"state2"
}
header.rx.refresh
.subscribe{
print"refresh1"
}
header.rx.refresh
.subscribe{
print"refresh2"
}

结果

state1
state2
refresh1
refresh2

mjrefresh-rxswift's People

Contributors

xueyongwei avatar

Watchers

James Cloos avatar

Forkers

assassinmt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.