|
| 1 | +// |
| 2 | +// PagingMenuView+Rx.swift |
| 3 | +// RxPagingKit |
| 4 | +// |
| 5 | +// Created by Meng Li on 2019/06/26. |
| 6 | +// Copyright © 2019 Kazuhiro Hayashi. All rights reserved. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in |
| 16 | +// all copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +// THE SOFTWARE. |
| 25 | + |
| 26 | +import UIKit |
| 27 | +import RxSwift |
| 28 | +import RxCocoa |
| 29 | +import PagingKit |
| 30 | + |
| 31 | +extension Reactive where Base: PagingMenuView { |
| 32 | + |
| 33 | + public func items<Item, O: ObservableType> |
| 34 | + (_ source: O) -> |
| 35 | + (_ cellFactory: @escaping (PagingMenuView, Int, Item) -> PagingMenuViewCell) -> Disposable |
| 36 | + where O.Element == [(Item, CGFloat)] { |
| 37 | + return { cellFactory in |
| 38 | + let dataSource = RxPagingMenuViewReactiveArrayDataSource<Item>(cellFactory: cellFactory) |
| 39 | + return self.items(dataSource: dataSource)(source) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public func items<Item, Cell: PagingMenuViewCell, O: ObservableType> |
| 44 | + (cellIdentifier: String, cellType: Cell.Type = Cell.self) -> |
| 45 | + (_ source: O) -> |
| 46 | + (_ configureCell: @escaping (Int, Item, Cell) -> Void) -> Disposable |
| 47 | + where O.Element == [(Item, CGFloat)] { |
| 48 | + |
| 49 | + return { source in |
| 50 | + return { configureCell in |
| 51 | + let dataSource = RxPagingMenuViewReactiveArrayDataSource<Item> { view, index, item in |
| 52 | + let cell = view.dequeue(with: cellIdentifier) as! Cell |
| 53 | + configureCell(index, item, cell) |
| 54 | + return cell |
| 55 | + } |
| 56 | + return self.items(dataSource: dataSource)(source) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public func items<DataSource: PagingMenuViewDataSource & RxPagingMenuViewDataSourceType, O: ObservableType> |
| 62 | + (dataSource: DataSource) |
| 63 | + -> (_ source: O) -> Disposable where O.Element == DataSource.Element { |
| 64 | + RxPagingMenuViewDataSourceProxy.setCurrentDelegate(nil, to: base) |
| 65 | + |
| 66 | + return { [base] source in |
| 67 | + RxPagingMenuViewDataSourceProxy.setCurrentDelegate(dataSource, to: base) |
| 68 | + |
| 69 | + let subscription = source |
| 70 | + .asObservable() |
| 71 | + .observeOn(MainScheduler()) |
| 72 | + .catchError { _ in |
| 73 | + return Observable.empty() |
| 74 | + } |
| 75 | + .concat(Observable.never()) |
| 76 | + .takeUntil(base.rx.deallocated) |
| 77 | + .subscribe { (event) in |
| 78 | + dataSource.pagingView(base, observedEvent: event) |
| 79 | + } |
| 80 | + |
| 81 | + return Disposables.create { [weak base] in |
| 82 | + guard let base = base else { return } |
| 83 | + subscription.dispose() |
| 84 | + base.layoutIfNeeded() |
| 85 | + //TODO: - dispose delegate with unregisterDelegate |
| 86 | + |
| 87 | + RxPagingMenuViewDataSourceProxy.setCurrentDelegate(nil, to: base) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public var itemSelected: ControlEvent<Int> { |
| 93 | + return menuDelegate.didSelect |
| 94 | + } |
| 95 | + |
| 96 | + public var willAnimateFocusView: ControlEvent<(Int, PagingMenuFocusViewAnimationCoordinator)> { |
| 97 | + return menuDelegate.willAnimateFocusView |
| 98 | + } |
| 99 | + |
| 100 | + public var willDisplayCell: ControlEvent<(PagingMenuViewCell, Int)> { |
| 101 | + return menuDelegate.willDisplayCell |
| 102 | + } |
| 103 | + |
| 104 | + public var scroll: Binder<Int> { |
| 105 | + return Binder(self.base) { menuView, index in |
| 106 | + menuView.scroll(index: index) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private var menuDelegate: RxPagingMenuViewDelegateProxy { |
| 111 | + return RxPagingMenuViewDelegateProxy.proxy(for: base) |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments