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

Interop with UINavigationController user instantiated popping #74

Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions ReSwiftRouter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1ADD0D9B1F041625004B2FD7 /* NavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ADD0D9A1F041625004B2FD7 /* NavigationController.swift */; };
254B3BAD1D3AD6DD00B1E4F0 /* RouteHashSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 254B3BAC1D3AD6DD00B1E4F0 /* RouteHashSpec.swift */; };
254B3BAE1D3AD6DD00B1E4F0 /* RouteHashSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 254B3BAC1D3AD6DD00B1E4F0 /* RouteHashSpec.swift */; };
254B3BAF1D3AD6DD00B1E4F0 /* RouteHashSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 254B3BAC1D3AD6DD00B1E4F0 /* RouteHashSpec.swift */; };
Expand Down Expand Up @@ -94,6 +95,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
1ADD0D9A1F041625004B2FD7 /* NavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NavigationController.swift; path = ReSwiftRouter/NavigationController.swift; sourceTree = SOURCE_ROOT; };
254B3BAC1D3AD6DD00B1E4F0 /* RouteHashSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RouteHashSpec.swift; path = ReSwiftRouterTests/RouteHashSpec.swift; sourceTree = SOURCE_ROOT; };
25C0A9C51C50AF6400139FA7 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = ReSwiftRouter/Info.plist; sourceTree = SOURCE_ROOT; };
25C0A9C61C50AF6400139FA7 /* NavigationActions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NavigationActions.swift; path = ReSwiftRouter/NavigationActions.swift; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -220,6 +222,7 @@
25C0A9C61C50AF6400139FA7 /* NavigationActions.swift */,
25C0A9C71C50AF6400139FA7 /* NavigationReducer.swift */,
25C0A9C81C50AF6400139FA7 /* NavigationState.swift */,
1ADD0D9A1F041625004B2FD7 /* NavigationController.swift */,
25C0A9C91C50AF6400139FA7 /* ReSwiftRouter.h */,
25C0A9CA1C50AF6400139FA7 /* Routable.swift */,
25C0A9CB1C50AF6400139FA7 /* Router.swift */,
Expand Down Expand Up @@ -774,6 +777,7 @@
buildActionMask = 2147483647;
files = (
25C0A9CE1C50AF6400139FA7 /* NavigationReducer.swift in Sources */,
1ADD0D9B1F041625004B2FD7 /* NavigationController.swift in Sources */,
25C0A9CF1C50AF6400139FA7 /* NavigationState.swift in Sources */,
25C0A9D21C50AF6400139FA7 /* Router.swift in Sources */,
25C0A9CD1C50AF6400139FA7 /* NavigationActions.swift in Sources */,
Expand Down
104 changes: 104 additions & 0 deletions ReSwiftRouter/NavigationController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// NavigationController.swift
// ReSwiftRouter
//
// Created by Taras Vozniuk on 29/06/2017.
// Copyright © 2017 Benjamin Encz. All rights reserved.
//

import UIKit

// dummy view controller returned when popViewController is canceled
public final class PopWasIgnored: UIViewController {}

open class NavigationController: UINavigationController {

fileprivate var isSwipping: Bool = false

// indicates that backButton was pressed when set to false
fileprivate var isPerformingPop: Bool = false

override open func viewDidLoad() {
super.viewDidLoad()
self.interactivePopGestureRecognizer?.addTarget(self, action: #selector(NavigationController.handlePopSwipe))
self.delegate = self
}

override open func popViewController(animated: Bool) -> UIViewController? {

// when swipping we are discarding all subsequent popViewController calls
guard !self.isSwipping else {
return PopWasIgnored()
}

self.isPerformingPop = true
return super.popViewController(animated: animated)
}

// will be called after popViewController call
func handlePopSwipe(){
self.isSwipping = true
}

/// should be overriden
/// normally you should dispatch SetRouteAction here
open func popRoute(){
print("WARNING: \(#function) should to be overriden")
}
}

extension NavigationController: UINavigationControllerDelegate {

public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
self.isSwipping = false
}
}

extension NavigationController: UINavigationBarDelegate {

// if overriden navigationController popViewController won't be called before this method
// on back button press
// isPerformingPop will be false here in this case
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
defer { isPerformingPop = false }

// changeRoute is performed:
// 1. back button was pressed
// 2. pop swipe was triggerred
if !isPerformingPop || self.isSwipping {
self.popRoute()
}

// don't remove the navigationItem if navigationController
// is not going to be popped
return isPerformingPop
}
}

extension UINavigationController {

open func pushViewController(_ viewController: UIViewController, animated: Bool, completion: @escaping () -> Void) {

self.pushViewController(viewController, animated: animated)

guard animated, let coordinator = self.transitionCoordinator else {
completion()
return
}

coordinator.animate(alongsideTransition: nil) { _ in completion() }
}

open func popViewController(animated: Bool, completion: @escaping () -> Void) -> UIViewController? {

let popped = self.popViewController(animated: animated)

guard animated, let coordinator = self.transitionCoordinator else {
completion()
return popped
}

coordinator.animate(alongsideTransition: nil) { _ in completion() }
return popped
}
}