├── .gitignore ├── LICENSE ├── README.md └── Sources ├── MJRefresh+RxSwift.swift └── UIScrollView+RxSwift.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 薛永伟 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MJRefresh-RxSwift 2 | 对MJRefresh进行RxSwift支持。可以对刷新状态多次订阅多次触发。 3 | 4 | 另对UIScrollView进行了扩展,方便在使用MVVM时绑定用。 5 | 6 | ## 背景 7 | 自己对Reactive扩展的Rx属性发现不能多次订阅,只会最后一次订阅会被触发。 8 | 9 | 找了一些其他人写的,方式大同小异,结果也都是仅有最后一次订阅会被触发。 10 | 11 | ## 原因 12 | 查看了RxCocoa的源码。发现了问题所在。RxCocoa中关于UIControl的实现,ControlEvent是基于addTarget,ControlProperty是基于valueForState。 13 | 14 | UIControl可以多次addTarget和订阅ControlEvent.editChange等状态, 15 | 但是MJRefresh的refreshingBlock也好、setRefreshingTarget也好,都是被最后订阅的人所持有。 16 | 17 | ## 解决 18 | 一对多?使用KVO对MJRefreshState进行观察即可! 19 | 20 | >>> 注:以下代码中的```~>```操作符是RxBinding库提供的绑定操作符。 21 | 22 | ## 使用方式 23 | 24 | 1. 导入MJRefresh+RxSwift.swift文件。 25 | 2. 对状态订阅 26 | 27 | ``` 28 | header.rx.state.filter({$0 == .refreshing}) 29 | .subscribe{ 30 | } 31 | ``` 32 | 33 | 3. 状态绑定到刷新控件 34 | ``` 35 | /// 开始刷新。控件进入刷新状态 36 | Observable.just(MJRefreshState.refreshing) ~> header.rx.state 37 | ``` 38 | 39 | 4. 大多数情况都是仅关注刷新,用以下便捷方式 40 | 41 | ``` 42 | header.rx.refresh 43 | .subscribe{ 44 | } 45 | ``` 46 | 47 | 48 | ## UIScrolView扩展 49 | 50 | **如果使用了MVVM架构,可能需要这个扩展** 51 | 52 | 在ViewModel只有一个Refresh或者LoadMore操作传入,直接在viewModel里发射出需要的MJRefreshState刷新状态,在View里订阅或绑定即可。 53 | 54 | 如果同时有Refresh和LoadMore,需要对不同类型的控件分别发送状态, 则可以导入UIScrollView+Rxswit.swift文件。 55 | 56 | 提供了常见的操作类型MJRefreshAction: 57 | ``` 58 | enum MJRefreshAction { 59 | /// 开始刷新 60 | case begainRefresh 61 | /// 停止刷新 62 | case stopRefresh 63 | /// 开始加载更多 64 | case begainLoadmore 65 | /// 停止加载更多 66 | case stopLoadmore 67 | /// 显示无更多数据 68 | case showNomoreData 69 | /// 重置无更多数据 70 | case resetNomoreData 71 | } 72 | ``` 73 | 在viewModel里发射所需的刷新操作即可。示例: 74 | ``` 75 | /// 刷新 76 | refresh.flatMapLatest{ 77 | requestDatas() 78 | } 79 | .subscrip{ 80 | self.refreshSubject.onNext(.stopRefresh)//停止刷新 81 | self.refreshSubject.onNext(.resetNomoreData)//重设无更多数据 82 | } 83 | /// 加载更多 84 | loadMore.flatMapLatest{ 85 | requestDatas() 86 | } 87 | .subscrip{ 88 | self.refreshSubject.onNext(.stopLoadmore)//停止加载更多 89 | self.refreshSubject.onNext(.showNomoreData)//无更多数据 90 | } 91 | ``` 92 | 93 | 94 | 然后在View层对操作进行监听(绑定)。 95 | 96 | ``` 97 | viewModel.output.refreshAction ~> self.collectionView.rx.refreshAction 98 | ``` 99 | 100 | ## 特色 101 | 102 | 可以多处监听刷新状态。 103 | ``` 104 | header.rx.state.filter({$0 == .refreshing}) 105 | .subscribe{ 106 | print"state1" 107 | } 108 | header.rx.state.filter({$0 == .refreshing}) 109 | .subscribe{ 110 | print"state2" 111 | } 112 | header.rx.refresh 113 | .subscribe{ 114 | print"refresh1" 115 | } 116 | header.rx.refresh 117 | .subscribe{ 118 | print"refresh2" 119 | } 120 | ``` 121 | 122 | 结果 123 | ``` 124 | state1 125 | state2 126 | refresh1 127 | refresh2 128 | ``` 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /Sources/MJRefresh+RxSwift.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MJRefresh+RxSwift.swift 3 | // Demo 4 | // 5 | // Created by Yuri on 2020/6/17. 6 | // Copyright © 2020 WSJC. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import RxSwift 11 | import RxCocoa 12 | import MJRefresh 13 | 14 | class Target: NSObject, Disposable { 15 | private var retainSelf: Target? 16 | override init() { 17 | super.init() 18 | self.retainSelf = self 19 | } 20 | func dispose() { 21 | self.retainSelf = nil 22 | } 23 | } 24 | 25 | private final 26 | class MJRefreshTarget: Target { 27 | typealias CallBack = (MJRefreshState)->() 28 | /// 组件 29 | weak var component: Component? 30 | /// 回调 31 | let callBack:CallBack 32 | 33 | init(_ component: Component , callBack: @escaping CallBack) { 34 | self.callBack = callBack 35 | self.component = component 36 | super.init() 37 | component.addObserver(self, forKeyPath: "state", options: [.new], context: nil) 38 | } 39 | 40 | override func dispose() { 41 | super.dispose() 42 | self.component?.removeObserver(self, forKeyPath: "state") 43 | } 44 | 45 | override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 46 | if keyPath == "state", 47 | let new = change?[NSKeyValueChangeKey.newKey] as? Int , 48 | let state = MJRefreshState.init(rawValue: new) { 49 | self.callBack(state) 50 | } 51 | } 52 | 53 | deinit { 54 | print("deinit") 55 | } 56 | } 57 | 58 | extension Reactive where Base: MJRefreshComponent { 59 | 60 | /// 刷新 61 | var refresh:ControlEvent { 62 | let source = state.filter({$0 == .refreshing}) 63 | .map({_ in ()}).asObservable() 64 | return ControlEvent.init(events: source) 65 | } 66 | 67 | /// 刷新状态 68 | var state: ControlProperty { 69 | let source: Observable = Observable.create { [weak component = self.base] observer in 70 | MainScheduler.ensureExecutingOnScheduler() 71 | guard let component = component else { 72 | observer.on(.completed) 73 | return Disposables.create() 74 | } 75 | observer.on(.next(component.state)) 76 | let target = MJRefreshTarget(component) { (state) in 77 | observer.onNext(state) 78 | } 79 | return target 80 | }.takeUntil(deallocated) 81 | 82 | let bindingObserver = Binder(self.base) { (component, state) in 83 | component.state = state 84 | } 85 | return ControlProperty(values: source, valueSink: bindingObserver) 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Sources/UIScrollView+RxSwift.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIScrollView+RxSwift.swift 3 | // WS-Designated_Driver 4 | // 5 | // Created by Yuri on 2020/6/18. 6 | // Copyright © 2020 WSJC. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import RxSwift 11 | import RxCocoa 12 | import MJRefresh 13 | 14 | /// 供ViewModel使用 15 | enum MJRefreshAction { 16 | /// 开始刷新 17 | case begainRefresh 18 | /// 停止刷新 19 | case stopRefresh 20 | /// 开始加载更多 21 | case begainLoadmore 22 | /// 停止加载更多 23 | case stopLoadmore 24 | /// 显示无更多数据 25 | case showNomoreData 26 | /// 重置无更多数据 27 | case resetNomoreData 28 | } 29 | 30 | //MARK:- Refresh 31 | extension Reactive where Base:UIScrollView { 32 | 33 | /// 执行的操作类型 34 | var refreshAction:Binder { 35 | 36 | return Binder(base) { (target, action) in 37 | 38 | switch action{ 39 | case .begainRefresh: 40 | if let header = target.mj_header { 41 | header.beginRefreshing() 42 | } 43 | case .stopRefresh: 44 | if let header = target.mj_header { 45 | header.endRefreshing() 46 | } 47 | case .begainLoadmore: 48 | if let footer = target.mj_footer { 49 | footer.beginRefreshing() 50 | } 51 | case .stopLoadmore: 52 | if let footer = target.mj_footer { 53 | footer.endRefreshing() 54 | } 55 | case .showNomoreData: 56 | if let footer = target.mj_footer { 57 | footer.endRefreshingWithNoMoreData() 58 | } 59 | case .resetNomoreData: 60 | if let footer = target.mj_footer { 61 | footer.resetNoMoreData() 62 | } 63 | break 64 | } 65 | } 66 | } 67 | 68 | } 69 | --------------------------------------------------------------------------------