Skip to content

Commit

Permalink
Merge pull request #22 from Electrode-iOS/swift3
Browse files Browse the repository at this point in the history
migrate to Swift 3
  • Loading branch information
MahatmaManic authored Dec 12, 2016
2 parents 21a4dc4 + 5e9142d commit 92f7c84
Show file tree
Hide file tree
Showing 50 changed files with 474 additions and 550 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
7 changes: 3 additions & 4 deletions .travis.yml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: objective-c
osx_image: xcode8
osx_image: xcode8.1

install:
- git clone https://github.com/Electrode-iOS/ELFoundation.git ../ELFoundation
- git clone https://github.com/Electrode-iOS/ELDispatch.git ../ELDispatch
- git clone https://github.com/Electrode-iOS/ELFoundation.git ../ELFoundation -b carthage
- git clone https://github.com/Electrode-iOS/ELLog.git ../ELLog
script:
- xcodebuild -project ELRouter.xcodeproj -scheme ELRouter -sdk iphonesimulator test -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.0' CODE_SIGNING_REQUIRED=NO
- xcodebuild -project ELRouter.xcodeproj -scheme ELRouter -sdk iphonesimulator test -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.1' CODE_SIGNING_REQUIRED=NO
Empty file modified CHANGELOG.md
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github "Electrode-iOS/ELLog" ~> 3.0.0
github "Electrode-iOS/ELFoundation" ~> 3.0.0
2 changes: 2 additions & 0 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github "Electrode-iOS/ELFoundation" "v3.0.0"
github "Electrode-iOS/ELLog" "v3.0.0"
234 changes: 88 additions & 146 deletions ELRouter.xcodeproj/project.pbxproj
100644 → 100755

Large diffs are not rendered by default.

Empty file modified ELRouter.xcodeproj/project.xcworkspace/contents.xcworkspacedata
100644 → 100755
Empty file.
Empty file.
Empty file modified ELRouter.xcodeproj/xcshareddata/xcschemes/ELRouter.xcscheme
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion ELRouter/AssociatedData.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ public protocol AssociatedData { }
route chain.
*/
extension NSURL: AssociatedData { }

Empty file modified ELRouter/ELRouter.h
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions ELRouter/ELRouter.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import Foundation
import ELLog

@objc
public class ELRouter: NSObject {
public static let logging = Logger()
open class ELRouter: NSObject {
open static let logging = Logger()
}

internal func log(level: LogLevel, _ message: String) {
internal func log(_ level: LogLevel, _ message: String) {
ELRouter.logging.log(level, message: "\(ELRouter.self): " + message)
}
Empty file modified ELRouter/Info.plist
100644 → 100755
Empty file.
11 changes: 4 additions & 7 deletions ELRouter/NSURL+DeepLink.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

import Foundation

public extension NSURL {
public extension URL {
public var deepLinkComponents: [String]? {
guard pathComponents != nil else { return nil }

// a deep link doesn't have the notion of a host, construct it as such
var components = [String]()

Expand All @@ -21,13 +19,12 @@ public extension NSURL {
}

// now add the path components, leaving the encoded parts intact
if let urlComponents = NSURLComponents(URL: self, resolvingAgainstBaseURL: false),
percentEncodedPath = urlComponents.percentEncodedPath
{
if let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: false) {
let percentEncodedPath = urlComponents.percentEncodedPath
// Note that the percentEncodedPath property of NSURLComponents does not add any encoding, it just returns any
// encoding that is already in the path. Unencoded slashes will remain unencoded but escaped slashes will remain
// escaped, which is what we want here.
let pathComponents = percentEncodedPath.componentsSeparatedByString("/")
let pathComponents = percentEncodedPath.components(separatedBy: "/")
// remove any empty strings, such as the one in the first element that results from the initial slash
let pathComponentsAfterSlash = pathComponents.filter() { !$0.isEmpty }
// append to our components
Expand Down
70 changes: 32 additions & 38 deletions ELRouter/NavigationSyncing.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,41 @@ If not, we just need to uncomment the swizzle in Router.swift.
import Foundation
import UIKit
import ELFoundation
import ELDispatch

typealias NavSyncAction = () -> Void

public protocol RouterEventFirehose: class {
func viewControllerAppeared(viewController: UIViewController)
func viewControllerPresented(viewController: UIViewController)
func viewControllerPushed(viewController: UIViewController)
func viewControllerAppeared(_ viewController: UIViewController)
func viewControllerPresented(_ viewController: UIViewController)
func viewControllerPushed(_ viewController: UIViewController)
}

internal class NavSync: NSObject {
internal static var sharedInstance = NavSync()

internal var scheduledControllers = NSHashTable.weakObjectsHashTable()
internal var scheduledControllers = NSHashTable<AnyObject>.weakObjects()

let routerQueue: DispatchQueue
weak var eventFirehose: RouterEventFirehose?

override init() {
routerQueue = DispatchQueue.createSerial("ELRouterSync", targetQueue: .Background)
routerQueue = DispatchQueue(label: "ELRouterSync", qos: DispatchQoS.background)
super.init()
}

internal func appeared(controller: UIViewController, animated: Bool) {
internal func appeared(_ controller: UIViewController, animated: Bool) {
eventFirehose?.viewControllerAppeared(controller)

controller.swizzled_viewDidAppear(animated)
Router.lock.unlock()
}

internal func push(viewController: UIViewController, animated: Bool, navController: UINavigationController, fromRouter: Bool) {
internal func push(_ viewController: UIViewController, animated: Bool, navController: UINavigationController, fromRouter: Bool) {
// add it to the scheduled ones in the case of a double-show to prevent a system blow up.
if scheduledControllers.containsObject(viewController) {
if scheduledControllers.contains(viewController) {
return
} else {
scheduledControllers.addObject(viewController)
scheduledControllers.add(viewController)
}

eventFirehose?.viewControllerPushed(viewController)
Expand All @@ -72,24 +71,24 @@ internal class NavSync: NSObject {
}

if animated {
Dispatch().async(routerQueue) {
Dispatch().async(.Main) {
routerQueue.async {
DispatchQueue.main.sync {
navController.swizzled_pushViewController(viewController, animated: animated)
self.scheduledControllers.removeObject(viewController)
self.scheduledControllers.remove(viewController)
}
}
} else {
navController.swizzled_pushViewController(viewController, animated: animated)
scheduledControllers.removeObject(viewController)
scheduledControllers.remove(viewController)
}
}

internal func present(viewController: UIViewController, animated: Bool, completion: (() -> Void)?, fromController: UIViewController, fromRouter: Bool) {
internal func present(_ viewController: UIViewController, animated: Bool, completion: (() -> Void)?, fromController: UIViewController, fromRouter: Bool) {
// add it to the scheduled ones in the case of a double-show to prevent a system blow up.
if scheduledControllers.containsObject(viewController) {
if scheduledControllers.contains(viewController) {
return
} else {
scheduledControllers.addObject(viewController)
scheduledControllers.add(viewController)
}

eventFirehose?.viewControllerPresented(viewController)
Expand All @@ -103,10 +102,10 @@ internal class NavSync: NSObject {
}

if animated {
Dispatch().async(routerQueue) {
Dispatch().async(.Main) {
routerQueue.async {
DispatchQueue.main.sync {
fromController.swizzled_presentViewController(viewController, animated: animated) {
self.scheduledControllers.removeObject(viewController)
self.scheduledControllers.remove(viewController)
if let closure = completion {
closure()
}
Expand All @@ -115,15 +114,15 @@ internal class NavSync: NSObject {
}
} else {
fromController.swizzled_presentViewController(viewController, animated: animated) {
self.scheduledControllers.removeObject(viewController)
self.scheduledControllers.remove(viewController)
if let closure = completion {
closure()
}
}
}
}

internal func performSegueWithIdentifier(identifier: String, sender: AnyObject?, fromController: UIViewController, fromRouter: Bool) {
internal func performSegueWithIdentifier(_ identifier: String, sender: AnyObject?, fromController: UIViewController, fromRouter: Bool) {
// if routes are in process and a manual nav event was attempted, it's ignore it and continue on.
if !fromRouter && Router.sharedInstance.processing {
if !isInUnitTest() {
Expand All @@ -133,49 +132,44 @@ internal class NavSync: NSObject {
return
}

fromController.performSegueWithIdentifier(identifier, sender: sender)
fromController.performSegue(withIdentifier: identifier, sender: sender)
}
}

extension UINavigationController {
internal func swizzled_pushViewController(viewController: UIViewController, animated: Bool) {
internal func swizzled_pushViewController(_ viewController: UIViewController, animated: Bool) {
NavSync.sharedInstance.push(viewController, animated: animated, navController: self, fromRouter: false)
}

internal func router_pushViewController(viewController: UIViewController, animated: Bool) {
internal func router_pushViewController(_ viewController: UIViewController, animated: Bool) {
NavSync.sharedInstance.push(viewController, animated: animated, navController: self, fromRouter: true)
}
}

extension UIViewController {
internal func swizzled_viewDidAppear(animated: Bool) {
internal func swizzled_viewDidAppear(_ animated: Bool) {
// release whatever lock is present
NavSync.sharedInstance.appeared(self, animated: animated)
}

internal func swizzled_presentViewController(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
internal func swizzled_presentViewController(_ viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
NavSync.sharedInstance.present(viewControllerToPresent, animated: animated, completion: completion, fromController: self, fromRouter: false)
}

internal func router_presentViewController(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
internal func router_presentViewController(_ viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
NavSync.sharedInstance.present(viewControllerToPresent, animated: animated, completion: completion, fromController: self, fromRouter: true)
}

internal func router_performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
internal func router_performSegueWithIdentifier(_ identifier: String, sender: AnyObject?) {
NavSync.sharedInstance.performSegueWithIdentifier(identifier, sender: sender, fromController: self, fromRouter: true)
}
}

// MARK: Swizzle Injection

internal func injectRouterSwizzles() {
struct Static {
static var token: dispatch_once_t = 0
}

dispatch_once(&Static.token) {
UINavigationController.swizzleInstanceMethod(#selector(UINavigationController.pushViewController(_:animated:)), swizzledSelector: #selector(UINavigationController.swizzled_pushViewController(_:animated:)))
UIViewController.swizzleInstanceMethod(#selector(UIViewController.viewDidAppear(_:)), swizzledSelector: #selector(UIViewController.swizzled_viewDidAppear(_:)))
UIViewController.swizzleInstanceMethod(#selector(UIViewController.presentViewController(_:animated:completion:)), swizzledSelector: #selector(UIViewController.swizzled_presentViewController(_:animated:completion:)))
}

UINavigationController.swizzleInstanceMethod(#selector(UINavigationController.pushViewController(_:animated:)), swizzledSelector: #selector(UINavigationController.swizzled_pushViewController(_:animated:)))
UIViewController.swizzleInstanceMethod(#selector(UIViewController.viewDidAppear(_:)), swizzledSelector: #selector(UIViewController.swizzled_viewDidAppear(_:)))
UIViewController.swizzleInstanceMethod(#selector(UIViewController.present), swizzledSelector: #selector(UIViewController.swizzled_presentViewController(_:animated:completion:)))
}
2 changes: 1 addition & 1 deletion ELRouter/Navigator.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public protocol Navigator {
var selectedIndex: Int { get set }

var viewControllers: [UIViewController]? { get set }
func setViewControllers(viewControllers: [UIViewController]?, animated: Bool)
func setViewControllers(_ viewControllers: [UIViewController]?, animated: Bool)
}


Expand Down
Loading

0 comments on commit 92f7c84

Please sign in to comment.