From ad52ef48e3d802ba1ac993c9eb85ed3bdc9ee42a Mon Sep 17 00:00:00 2001 From: Jerome Pasquier Date: Mon, 28 Jan 2019 10:32:40 +0100 Subject: [PATCH] Add OrderedDictionary for FIFO matching routes New OrderedDictionary to make the list of matching routes FIFO --- Example/Podfile.lock | 8 +- .../project.pbxproj | 16 ---- Sources/URLNavigator/Navigator.swift | 2 +- Sources/URLNavigator/OrderedDictionary.swift | 79 +++++++++++++++++++ 4 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 Sources/URLNavigator/OrderedDictionary.swift diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 70a486e..0f80f43 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,16 +1,16 @@ PODS: - - URLNavigator (2.0.1) + - URLNavigator (2.1.0) DEPENDENCIES: - URLNavigator (from `../`) EXTERNAL SOURCES: URLNavigator: - :path: ../ + :path: "../" SPEC CHECKSUMS: - URLNavigator: 067099d49dd9f40e895e59859ad06dc0618c9768 + URLNavigator: ec645521eda4c0e483390764162738ee689ebd72 PODFILE CHECKSUM: 797c263b229fb2da6a9381964e439b9c78f774e2 -COCOAPODS: 1.3.1 +COCOAPODS: 1.5.3 diff --git a/Example/URLNavigatorExample.xcodeproj/project.pbxproj b/Example/URLNavigatorExample.xcodeproj/project.pbxproj index 65626c5..9247bf9 100644 --- a/Example/URLNavigatorExample.xcodeproj/project.pbxproj +++ b/Example/URLNavigatorExample.xcodeproj/project.pbxproj @@ -188,7 +188,6 @@ 03372AA01FB24051009AE2BA /* Frameworks */, 03372AA11FB24051009AE2BA /* Resources */, C22D19332B156D3E353C7D33 /* [CP] Embed Pods Frameworks */, - 54AC728EDB7C0BAC3D908A25 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -264,21 +263,6 @@ shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - 54AC728EDB7C0BAC3D908A25 /* [CP] Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Copy Pods Resources"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-URLNavigatorExample/Pods-URLNavigatorExample-resources.sh\"\n"; - showEnvVarsInLog = 0; - }; C22D19332B156D3E353C7D33 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; diff --git a/Sources/URLNavigator/Navigator.swift b/Sources/URLNavigator/Navigator.swift index fe6de9e..338caa9 100644 --- a/Sources/URLNavigator/Navigator.swift +++ b/Sources/URLNavigator/Navigator.swift @@ -10,7 +10,7 @@ open class Navigator: NavigatorType { open weak var delegate: NavigatorDelegate? private var viewControllerFactories = [URLPattern: ViewControllerFactory]() - private var handlerFactories = [URLPattern: URLOpenHandlerFactory]() + private var handlerFactories = OrderedDictionary() public init() { // ⛵ I'm a Navigator! diff --git a/Sources/URLNavigator/OrderedDictionary.swift b/Sources/URLNavigator/OrderedDictionary.swift new file mode 100644 index 0000000..1f6d340 --- /dev/null +++ b/Sources/URLNavigator/OrderedDictionary.swift @@ -0,0 +1,79 @@ +// +// OrderedDictionary.swift +// URLNavigator +// +// Created by Jerome Pasquier on 28/01/2019. +// + +import Foundation + +struct OrderedDictionary { + var keys = [Key]() + var values = [Key: Value]() + + var count: Int { + return self.keys.count + } + + subscript(index: Int) -> Value? { + get { + let key = self.keys[index] + return self.values[key] + } + set(newValue) { + let key = self.keys[index] + if newValue != nil { + self.values[key] = newValue + } else { + self.values.removeValue(forKey: key) + self.keys.remove(at: index) + } + } + } + + subscript(key: Key) -> Value? { + get { + return self.values[key] + } + set(newValue) { + if let newVal = newValue { + let oldValue = self.values.updateValue(newVal, forKey: key) + if oldValue == nil { + self.keys.append(key) + } + } else { + self.values.removeValue(forKey: key) + self.keys = self.keys.filter {$0 != key} + } + } + } + +} + +extension OrderedDictionary: CustomStringConvertible { + var description: String { + let isString = type(of: self.keys[0]) == String.self + var result = "[" + for key in keys { + result += isString ? "\"\(key)\"" : "\(key)" + result += ": \(self[key]!), " + } + result = String(result.dropLast(2)) + result += "]" + return result + } +} + +extension OrderedDictionary: Sequence { + func makeIterator() -> AnyIterator { + var counter = 0 + return AnyIterator { + guard counter