Skip to content

Commit

Permalink
Fix hide/drop actions + add "hideAll(animated: Bool)" method on Modal…
Browse files Browse the repository at this point in the history
…Presenter
  • Loading branch information
Elviro Rocca committed Feb 9, 2018
1 parent 397c629 commit fe13a10
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
19 changes: 11 additions & 8 deletions NavigationHelperUIKit/NavigationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ extension UINavigationController: StructuredPresenter {
}

public func dropLast(animated: Bool) -> Future<()> {
return Future<()>.unfold { done in
DispatchQueue.main.async {
guard
self.viewControllers.isEmpty.not,
self.popViewController(animated: animated).isNil.not,
animated else { done(()); return }
self.transitionCoordinator?.animate(alongsideTransition: nil) { _ in done(()) }
guard allStructuredPresented.isEmpty.not else { return .pure(()) }

return Future<()>
.unfold { done in
DispatchQueue.main.async {
guard
self.popViewController(animated: animated).isNil.not,
animated else { done(()); return }
self.transitionCoordinator?.animate(alongsideTransition: nil) { _ in done(()) }
}
}
}.start()
.start()
}
}

Expand Down
18 changes: 11 additions & 7 deletions NavigationHelperUIKit/TabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ extension UITabBarController: StructuredPresenter {
}

public func dropLast(animated: Bool) -> Future<()> {
return Future<()>.unfold { done in
DispatchQueue.main.async {
guard let viewControllers = self.viewControllers, viewControllers.isEmpty.not else { done(()); return }
self.setViewControllers(viewControllers.dropLast() |> Array.init(_:), animated: animated)
guard animated else { done(()); return }
self.transitionCoordinator?.animate(alongsideTransition: nil) { _ in done(()) }
guard allStructuredPresented.isEmpty.not else { return .pure(()) }

return Future<()>
.unfold { done in
DispatchQueue.main.async {
guard let viewControllers = self.viewControllers, viewControllers.isEmpty.not else { done(()); return }
self.setViewControllers(viewControllers.dropLast() |> Array.init(_:), animated: animated)
guard animated else { done(()); return }
self.transitionCoordinator?.animate(alongsideTransition: nil) { _ in done(()) }
}
}
}.start()
.start()
}
}

Expand Down
14 changes: 9 additions & 5 deletions NavigationHelperUIKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ extension UIViewController: ModalPresenter {
}

public func hide(animated: Bool) -> Future<()> {
if let lastModalPresented = self.lastModalPresented, let shownPresenter = lastModalPresented as? ModalPresenter, shownPresenter.isPresenting {
guard let lastModalPresented = self.lastModalPresented else { return .pure(()) }

if let shownPresenter = lastModalPresented as? ModalPresenter, shownPresenter.isPresenting {
return shownPresenter.hide(animated: animated).flatMap {
self.hide(animated: animated)
}
}

return Future<()>.unfold { done in
DispatchQueue.main.async {
self.dismiss(animated: animated, completion: done)
return Future<()>
.unfold { done in
DispatchQueue.main.async {
self.dismiss(animated: animated, completion: done)
}
}
}.start()
.start()
}

public var lastModalPresented: Presentable? {
Expand Down
7 changes: 7 additions & 0 deletions Sources/NavigationHelper/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ extension ModalPresenter {
public var isPresenting: Bool {
return lastModalPresented.isNil.not
}

public func hideAll(animated: Bool) -> Future<()> {
guard isPresenting else { return .pure(()) }
return hide(animated: animated).flatMap {
self.hideAll(animated: true)
}
}
}

public protocol StructuredPresenter {
Expand Down

0 comments on commit fe13a10

Please sign in to comment.