Skip to content

Commit

Permalink
Support RxSwift
Browse files Browse the repository at this point in the history
  • Loading branch information
kawoou committed Jan 9, 2018
1 parent a327b02 commit 188cfbf
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ fastlane/screenshots
fastlane/test_output

*.swp

Pods/
44 changes: 6 additions & 38 deletions DrawerController/DrawerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,12 @@ import UIKit

@objc
public protocol DrawerControllerDelegate {

@objc
optional func drawerDidAnimation(
drawerController: DrawerController,
side: DrawerSide,
percentage: Float
)

@objc
optional func drawerDidBeganAnimation(
drawerController: DrawerController,
side: DrawerSide
)

@objc
optional func drawerWillFinishAnimation(
drawerController: DrawerController,
side: DrawerSide
)

@objc
optional func drawerWillCancelAnimation(
drawerController: DrawerController,
side: DrawerSide
)

@objc
optional func drawerDidFinishAnimation(
drawerController: DrawerController,
side: DrawerSide
)

@objc
optional func drawerDidCancelAnimation(
drawerController: DrawerController,
side: DrawerSide
)

@objc optional func drawerDidAnimation(drawerController: DrawerController, side: DrawerSide, percentage: Float)
@objc optional func drawerDidBeganAnimation(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerWillFinishAnimation(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerWillCancelAnimation(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerDidFinishAnimation(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerDidCancelAnimation(drawerController: DrawerController, side: DrawerSide)
}

open class DrawerController: UIViewController, UIGestureRecognizerDelegate {
Expand Down
129 changes: 129 additions & 0 deletions DrawerController/Rx/DrawerController+Rx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
The MIT License (MIT)
Copyright (c) 2017 Kawoou (Jungwon An)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import RxCocoa
import RxSwift

// Taken from RxCococa until marked as public
func castOrThrow(_ resultType: DrawerSide.Type, _ object: Any) throws -> DrawerSide {
let rawValue = try castOrThrow(Int.self, object)
guard let returnValue = DrawerSide(rawValue: rawValue) else {
throw RxCocoaError.castingError(object: object, targetType: resultType)
}
return returnValue
}
func castOrThrow<T>(_ resultType: T.Type, _ object: Any) throws -> T {
guard let returnValue = object as? T else {
throw RxCocoaError.castingError(object: object, targetType: resultType)
}
return returnValue
}

public extension DrawerController {
public func openSide(_ side: DrawerSide) -> Completable {
return .create { [weak self] completable -> Disposable in
guard let ss = self else {
completable(.error(RxCocoaError.unknown))
return Disposables.create()
}
ss.openSide(side) {
completable(.completed)
}
return Disposables.create()
}
}
public func closeSide() -> Completable {
return .create { [weak self] completable -> Disposable in
guard let ss = self else {
completable(.error(RxCocoaError.unknown))
return Disposables.create()
}
ss.closeSide() {
completable(.completed)
}
return Disposables.create()
}
}
}

extension Reactive where Base: DrawerController {

/**
Reactive wrapper for `delegate`.
For more information take a look at `DelegateProxyType` protocol documentation.
*/
public var delegate: DelegateProxy<DrawerController, DrawerControllerDelegate> {
return RxDrawerControllerDelegateProxy.proxy(for: base)
}

public typealias AnimationPercent = (side: DrawerSide, percent: Float)
public var didAnimation: ControlEvent<AnimationPercent> {
let source = delegate
.methodInvoked(#selector(DrawerControllerDelegate.drawerDidAnimation(drawerController:side:percentage:)))
.map { a in
return (
side: try castOrThrow(DrawerSide.self, a[1]),
percent: try castOrThrow(Float.self, a[2])
)
}

return ControlEvent(events: source)
}
public var didBeganAnimation: ControlEvent<DrawerSide> {
let source = delegate
.methodInvoked(#selector(DrawerControllerDelegate.drawerDidBeganAnimation(drawerController:side:)))
.map { try castOrThrow(DrawerSide.self, $0[1]) }

return ControlEvent(events: source)
}
public var willFinishAnimation: ControlEvent<DrawerSide> {
let source = delegate
.methodInvoked(#selector(DrawerControllerDelegate.drawerWillFinishAnimation(drawerController:side:)))
.map { try castOrThrow(DrawerSide.self, $0[1]) }

return ControlEvent(events: source)
}
public var willCancelAnimation: ControlEvent<DrawerSide> {
let source = delegate
.methodInvoked(#selector(DrawerControllerDelegate.drawerWillCancelAnimation(drawerController:side:)))
.map { try castOrThrow(DrawerSide.self, $0[1]) }

return ControlEvent(events: source)
}
public var didFinishAnimation: ControlEvent<DrawerSide> {
let source = delegate
.methodInvoked(#selector(DrawerControllerDelegate.drawerDidFinishAnimation(drawerController:side:)))
.map { try castOrThrow(DrawerSide.self, $0[1]) }

return ControlEvent(events: source)
}
public var didCancelAnimation: ControlEvent<DrawerSide> {
let source = delegate
.methodInvoked(#selector(DrawerControllerDelegate.drawerDidCancelAnimation(drawerController:side:)))
.map { try castOrThrow(DrawerSide.self, $0[1]) }

return ControlEvent(events: source)
}

}
47 changes: 47 additions & 0 deletions DrawerController/Rx/RxDrawerControllerDelegateProxy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
The MIT License (MIT)
Copyright (c) 2017 Kawoou (Jungwon An)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import RxCocoa
import RxSwift

extension DrawerController: HasDelegate {
public typealias Delegate = DrawerControllerDelegate
}

class RxDrawerControllerDelegateProxy: DelegateProxy<DrawerController, DrawerControllerDelegate>, DelegateProxyType, DrawerControllerDelegate {

/// Typed parent object.
public private(set) weak var drawerController: DrawerController?

/// - parameter mapView: Parent object for delegate proxy.
public init(drawerController: DrawerController) {
self.drawerController = drawerController
super.init(parentObject: drawerController, delegateProxy: RxDrawerControllerDelegateProxy.self)
}

static func registerKnownImplementations() {
self.register { RxDrawerControllerDelegateProxy(drawerController: $0) }
}

}
11 changes: 11 additions & 0 deletions DrawerController/Types/DrawerSide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ public enum DrawerSide: Int {
case left = 0
case right
case none

var stringValue: String {
switch self {
case .left:
return "Left"
case .right:
return "Right"
case .none:
return "None"
}
}
}
48 changes: 47 additions & 1 deletion Example/Conroller/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,51 @@
//

import UIKit
import RxSwift

class MainViewController: UITabBarController {

let disposeBag = DisposeBag()

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
drawerController?.rx.didAnimation
.subscribe(onNext: { (side, percent) in
print("DrawerController.rx.didAnimation: \(side.stringValue), \(percent)")
})
.disposed(by: disposeBag)

drawerController?.rx.didBeganAnimation
.subscribe(onNext: { (side) in
print("DrawerController.rx.didBeganAnimation: \(side.stringValue)")
})
.disposed(by: disposeBag)

drawerController?.rx.willFinishAnimation
.subscribe(onNext: { (side) in
print("DrawerController.rx.willFinishAnimation: \(side.stringValue)")
})
.disposed(by: disposeBag)

drawerController?.rx.willCancelAnimation
.subscribe(onNext: { (side) in
print("DrawerController.rx.willCancelAnimation: \(side.stringValue)")
})
.disposed(by: disposeBag)

drawerController?.rx.didFinishAnimation
.subscribe(onNext: { (side) in
print("DrawerController.rx.didFinishAnimation: \(side.stringValue)")
})
.disposed(by: disposeBag)

drawerController?.rx.didCancelAnimation
.subscribe(onNext: { (side) in
print("DrawerController.rx.didCancelAnimation: \(side.stringValue)")
})
.disposed(by: disposeBag)

}

override func didReceiveMemoryWarning() {
Expand All @@ -24,16 +62,24 @@ class MainViewController: UITabBarController {
@IBAction func leftButtonDidPressed(_ sender: UIBarButtonItem) {
if self.drawerController!.drawerSide == .none {
self.drawerController?.openSide(.left)
.subscribe()
.disposed(by: disposeBag)
} else {
self.drawerController?.closeSide()
.subscribe()
.disposed(by: disposeBag)
}
}

@IBAction func rightButtonDidPressed(_ sender: UIBarButtonItem) {
if self.drawerController!.drawerSide == .none {
self.drawerController?.openSide(.right)
.subscribe()
.disposed(by: disposeBag)
} else {
self.drawerController?.closeSide()
.subscribe()
.disposed(by: disposeBag)
}
}

Expand Down
Loading

0 comments on commit 188cfbf

Please sign in to comment.