Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add state methods to delegate #16

Merged
merged 11 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions DrawerController/DrawerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public protocol DrawerControllerDelegate {
@objc optional func drawerDidFinishAnimation(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerDidCancelAnimation(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerWillOpenSide(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerWillCloseSide(drawerController: DrawerController)
@objc optional func drawerWillCloseSide(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerDidOpenSide(drawerController: DrawerController, side: DrawerSide)
@objc optional func drawerDidCloseSide(drawerController: DrawerController)
@objc optional func drawerDidCloseSide(drawerController: DrawerController, side: DrawerSide)
}

open class DrawerController: UIViewController, UIGestureRecognizerDelegate {
Expand Down Expand Up @@ -315,7 +315,7 @@ open class DrawerController: UIViewController, UIGestureRecognizerDelegate {
guard isEnable() else { return }
guard !isAnimating else { return }

delegate?.drawerWillCloseSide?(drawerController: self)
delegate?.drawerWillCloseSide?(drawerController: self, side: drawerSide)

isAnimating = true

Expand All @@ -332,7 +332,7 @@ open class DrawerController: UIViewController, UIGestureRecognizerDelegate {
didFinishAnimate(side: .none, percent: 0.0)

isAnimating = false
delegate?.drawerDidCloseSide?(drawerController: self)
delegate?.drawerDidCloseSide?(drawerController: self, side: drawerSide)
completion?()
return
}
Expand All @@ -358,7 +358,7 @@ open class DrawerController: UIViewController, UIGestureRecognizerDelegate {
ss.didFinishAnimate(side: .none, percent: 0.0)

ss.isAnimating = false
ss.delegate?.drawerDidCloseSide?(drawerController: ss)
ss.delegate?.drawerDidCloseSide?(drawerController: ss, side: ss.drawerSide)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do not write oldValue, there is likely to be some variation in the middle.

let oldDrawerSide = drawerSide

delegate?.drawerWillCloseSide?(drawerController: self, side: oldDrawerSide)

...

ss.delegate?.drawerDidCloseSide?(drawerController: ss, side: oldDrawerSide)

...

completion?()
}
)
Expand All @@ -374,7 +374,7 @@ open class DrawerController: UIViewController, UIGestureRecognizerDelegate {
ss.didFinishAnimate(side: .none, percent: 0.0)

ss.isAnimating = false
ss.delegate?.drawerDidCloseSide?(drawerController: ss)
ss.delegate?.drawerDidCloseSide?(drawerController: ss, side: ss.drawerSide)
completion?()
}
)
Expand Down
29 changes: 28 additions & 1 deletion DrawerController/Rx/DrawerController+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,32 @@ extension Reactive where Base: DrawerController {

return ControlEvent(events: source)
}

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

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

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

return ControlEvent(events: source)
}
public var didCloseSide: ControlEvent<DrawerSide> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let source = delegate
.methodInvoked(#selector(DrawerControllerDelegate.drawerDidCloseSide(drawerController:side:)))
.map { try castOrThrow(DrawerSide.self, $0[1]) }

return ControlEvent(events: source)
}
}
50 changes: 27 additions & 23 deletions Example/Conroller/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import UIKit
import RxSwift

class MainViewController: UITabBarController, DrawerControllerDelegate {
class MainViewController: UITabBarController {

let disposeBag = DisposeBag()

override func viewDidLoad() {
super.viewDidLoad()

drawerController?.delegate = self


drawerController?.rx.didAnimation
.subscribe(onNext: { (side, percent) in
print("DrawerController.rx.didAnimation: \(side.stringValue), \(percent)")
Expand Down Expand Up @@ -54,6 +52,30 @@ class MainViewController: UITabBarController, DrawerControllerDelegate {
})
.disposed(by: disposeBag)

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

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

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

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

}

override func didReceiveMemoryWarning() {
Expand Down Expand Up @@ -94,23 +116,5 @@ class MainViewController: UITabBarController, DrawerControllerDelegate {
// Pass the selected object to the new view controller.
}
*/


// MARK: - DrawerControllerDelegate

func drawerWillOpenSide(drawerController: DrawerController, side: DrawerSide) {
print("DrawerController.willOpenSide: \(side.stringValue)")
}

func drawerWillCloseSide(drawerController: DrawerController) {
print("DrawerController.willCloseSide")
}

func drawerDidOpenSide(drawerController: DrawerController, side: DrawerSide) {
print("DrawerController.didOpenSide: \(side.stringValue)")
}

func drawerDidCloseSide(drawerController: DrawerController) {
print("DrawerController.didCloseSide")
}

}