├── .gitignore ├── .swiftpm └── xcode │ └── package.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── Demo ├── Demo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── Demo │ ├── AccountView.swift │ ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── ContentView.swift │ ├── DemoApp.swift │ ├── Info.plist │ └── Preview Content │ └── Preview Assets.xcassets │ └── Contents.json ├── LICENSE ├── Package.swift ├── README.md ├── Router.xcworkspace ├── contents.xcworkspacedata └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── Sources └── Router │ ├── Link.swift │ ├── Route.swift │ ├── RouteView.swift │ └── Router.swift ├── Tests ├── LinuxMain.swift └── RouterTests │ ├── RouterTests.swift │ └── XCTestManifests.swift └── router.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 52; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 121525F825D7AAE1003BFAE2 /* DemoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 121525F725D7AAE1003BFAE2 /* DemoApp.swift */; }; 11 | 121525FA25D7AAE1003BFAE2 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 121525F925D7AAE1003BFAE2 /* ContentView.swift */; }; 12 | 121525FC25D7AAE2003BFAE2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 121525FB25D7AAE2003BFAE2 /* Assets.xcassets */; }; 13 | 121525FF25D7AAE2003BFAE2 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 121525FE25D7AAE2003BFAE2 /* Preview Assets.xcassets */; }; 14 | 1215260825D7AB42003BFAE2 /* Router in Frameworks */ = {isa = PBXBuildFile; productRef = 1215260725D7AB42003BFAE2 /* Router */; }; 15 | 1215260C25D7B101003BFAE2 /* AccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1215260B25D7B101003BFAE2 /* AccountView.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 121525F425D7AAE1003BFAE2 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 121525F725D7AAE1003BFAE2 /* DemoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoApp.swift; sourceTree = ""; }; 21 | 121525F925D7AAE1003BFAE2 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 22 | 121525FB25D7AAE2003BFAE2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | 121525FE25D7AAE2003BFAE2 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 24 | 1215260025D7AAE2003BFAE2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | 1215260B25D7B101003BFAE2 /* AccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountView.swift; sourceTree = ""; }; 26 | /* End PBXFileReference section */ 27 | 28 | /* Begin PBXFrameworksBuildPhase section */ 29 | 121525F125D7AAE1003BFAE2 /* Frameworks */ = { 30 | isa = PBXFrameworksBuildPhase; 31 | buildActionMask = 2147483647; 32 | files = ( 33 | 1215260825D7AB42003BFAE2 /* Router in Frameworks */, 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | 121525EB25D7AAE1003BFAE2 = { 41 | isa = PBXGroup; 42 | children = ( 43 | 121525F625D7AAE1003BFAE2 /* Demo */, 44 | 121525F525D7AAE1003BFAE2 /* Products */, 45 | 1215260625D7AB42003BFAE2 /* Frameworks */, 46 | ); 47 | sourceTree = ""; 48 | }; 49 | 121525F525D7AAE1003BFAE2 /* Products */ = { 50 | isa = PBXGroup; 51 | children = ( 52 | 121525F425D7AAE1003BFAE2 /* Demo.app */, 53 | ); 54 | name = Products; 55 | sourceTree = ""; 56 | }; 57 | 121525F625D7AAE1003BFAE2 /* Demo */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | 121525F725D7AAE1003BFAE2 /* DemoApp.swift */, 61 | 121525F925D7AAE1003BFAE2 /* ContentView.swift */, 62 | 1215260B25D7B101003BFAE2 /* AccountView.swift */, 63 | 121525FB25D7AAE2003BFAE2 /* Assets.xcassets */, 64 | 1215260025D7AAE2003BFAE2 /* Info.plist */, 65 | 121525FD25D7AAE2003BFAE2 /* Preview Content */, 66 | ); 67 | path = Demo; 68 | sourceTree = ""; 69 | }; 70 | 121525FD25D7AAE2003BFAE2 /* Preview Content */ = { 71 | isa = PBXGroup; 72 | children = ( 73 | 121525FE25D7AAE2003BFAE2 /* Preview Assets.xcassets */, 74 | ); 75 | path = "Preview Content"; 76 | sourceTree = ""; 77 | }; 78 | 1215260625D7AB42003BFAE2 /* Frameworks */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | ); 82 | name = Frameworks; 83 | sourceTree = ""; 84 | }; 85 | /* End PBXGroup section */ 86 | 87 | /* Begin PBXNativeTarget section */ 88 | 121525F325D7AAE1003BFAE2 /* Demo */ = { 89 | isa = PBXNativeTarget; 90 | buildConfigurationList = 1215260325D7AAE2003BFAE2 /* Build configuration list for PBXNativeTarget "Demo" */; 91 | buildPhases = ( 92 | 121525F025D7AAE1003BFAE2 /* Sources */, 93 | 121525F125D7AAE1003BFAE2 /* Frameworks */, 94 | 121525F225D7AAE1003BFAE2 /* Resources */, 95 | ); 96 | buildRules = ( 97 | ); 98 | dependencies = ( 99 | ); 100 | name = Demo; 101 | packageProductDependencies = ( 102 | 1215260725D7AB42003BFAE2 /* Router */, 103 | ); 104 | productName = Demo; 105 | productReference = 121525F425D7AAE1003BFAE2 /* Demo.app */; 106 | productType = "com.apple.product-type.application"; 107 | }; 108 | /* End PBXNativeTarget section */ 109 | 110 | /* Begin PBXProject section */ 111 | 121525EC25D7AAE1003BFAE2 /* Project object */ = { 112 | isa = PBXProject; 113 | attributes = { 114 | LastSwiftUpdateCheck = 1250; 115 | LastUpgradeCheck = 1250; 116 | TargetAttributes = { 117 | 121525F325D7AAE1003BFAE2 = { 118 | CreatedOnToolsVersion = 12.5; 119 | }; 120 | }; 121 | }; 122 | buildConfigurationList = 121525EF25D7AAE1003BFAE2 /* Build configuration list for PBXProject "Demo" */; 123 | compatibilityVersion = "Xcode 9.3"; 124 | developmentRegion = en; 125 | hasScannedForEncodings = 0; 126 | knownRegions = ( 127 | en, 128 | Base, 129 | ); 130 | mainGroup = 121525EB25D7AAE1003BFAE2; 131 | productRefGroup = 121525F525D7AAE1003BFAE2 /* Products */; 132 | projectDirPath = ""; 133 | projectRoot = ""; 134 | targets = ( 135 | 121525F325D7AAE1003BFAE2 /* Demo */, 136 | ); 137 | }; 138 | /* End PBXProject section */ 139 | 140 | /* Begin PBXResourcesBuildPhase section */ 141 | 121525F225D7AAE1003BFAE2 /* Resources */ = { 142 | isa = PBXResourcesBuildPhase; 143 | buildActionMask = 2147483647; 144 | files = ( 145 | 121525FF25D7AAE2003BFAE2 /* Preview Assets.xcassets in Resources */, 146 | 121525FC25D7AAE2003BFAE2 /* Assets.xcassets in Resources */, 147 | ); 148 | runOnlyForDeploymentPostprocessing = 0; 149 | }; 150 | /* End PBXResourcesBuildPhase section */ 151 | 152 | /* Begin PBXSourcesBuildPhase section */ 153 | 121525F025D7AAE1003BFAE2 /* Sources */ = { 154 | isa = PBXSourcesBuildPhase; 155 | buildActionMask = 2147483647; 156 | files = ( 157 | 1215260C25D7B101003BFAE2 /* AccountView.swift in Sources */, 158 | 121525FA25D7AAE1003BFAE2 /* ContentView.swift in Sources */, 159 | 121525F825D7AAE1003BFAE2 /* DemoApp.swift in Sources */, 160 | ); 161 | runOnlyForDeploymentPostprocessing = 0; 162 | }; 163 | /* End PBXSourcesBuildPhase section */ 164 | 165 | /* Begin XCBuildConfiguration section */ 166 | 1215260125D7AAE2003BFAE2 /* Debug */ = { 167 | isa = XCBuildConfiguration; 168 | buildSettings = { 169 | ALWAYS_SEARCH_USER_PATHS = NO; 170 | CLANG_ANALYZER_NONNULL = YES; 171 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 172 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 173 | CLANG_CXX_LIBRARY = "libc++"; 174 | CLANG_ENABLE_MODULES = YES; 175 | CLANG_ENABLE_OBJC_ARC = YES; 176 | CLANG_ENABLE_OBJC_WEAK = YES; 177 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 178 | CLANG_WARN_BOOL_CONVERSION = YES; 179 | CLANG_WARN_COMMA = YES; 180 | CLANG_WARN_CONSTANT_CONVERSION = YES; 181 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 182 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 183 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 184 | CLANG_WARN_EMPTY_BODY = YES; 185 | CLANG_WARN_ENUM_CONVERSION = YES; 186 | CLANG_WARN_INFINITE_RECURSION = YES; 187 | CLANG_WARN_INT_CONVERSION = YES; 188 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 189 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 190 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 191 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 192 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 193 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 194 | CLANG_WARN_STRICT_PROTOTYPES = YES; 195 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 196 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 197 | CLANG_WARN_UNREACHABLE_CODE = YES; 198 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 199 | COPY_PHASE_STRIP = NO; 200 | DEBUG_INFORMATION_FORMAT = dwarf; 201 | ENABLE_STRICT_OBJC_MSGSEND = YES; 202 | ENABLE_TESTABILITY = YES; 203 | GCC_C_LANGUAGE_STANDARD = gnu11; 204 | GCC_DYNAMIC_NO_PIC = NO; 205 | GCC_NO_COMMON_BLOCKS = YES; 206 | GCC_OPTIMIZATION_LEVEL = 0; 207 | GCC_PREPROCESSOR_DEFINITIONS = ( 208 | "DEBUG=1", 209 | "$(inherited)", 210 | ); 211 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 212 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 213 | GCC_WARN_UNDECLARED_SELECTOR = YES; 214 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 215 | GCC_WARN_UNUSED_FUNCTION = YES; 216 | GCC_WARN_UNUSED_VARIABLE = YES; 217 | IPHONEOS_DEPLOYMENT_TARGET = 14.5; 218 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 219 | MTL_FAST_MATH = YES; 220 | ONLY_ACTIVE_ARCH = YES; 221 | SDKROOT = iphoneos; 222 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 223 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 224 | }; 225 | name = Debug; 226 | }; 227 | 1215260225D7AAE2003BFAE2 /* Release */ = { 228 | isa = XCBuildConfiguration; 229 | buildSettings = { 230 | ALWAYS_SEARCH_USER_PATHS = NO; 231 | CLANG_ANALYZER_NONNULL = YES; 232 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 233 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 234 | CLANG_CXX_LIBRARY = "libc++"; 235 | CLANG_ENABLE_MODULES = YES; 236 | CLANG_ENABLE_OBJC_ARC = YES; 237 | CLANG_ENABLE_OBJC_WEAK = YES; 238 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 239 | CLANG_WARN_BOOL_CONVERSION = YES; 240 | CLANG_WARN_COMMA = YES; 241 | CLANG_WARN_CONSTANT_CONVERSION = YES; 242 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 243 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 244 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 245 | CLANG_WARN_EMPTY_BODY = YES; 246 | CLANG_WARN_ENUM_CONVERSION = YES; 247 | CLANG_WARN_INFINITE_RECURSION = YES; 248 | CLANG_WARN_INT_CONVERSION = YES; 249 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 250 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 251 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 252 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 253 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 254 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 255 | CLANG_WARN_STRICT_PROTOTYPES = YES; 256 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 257 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 258 | CLANG_WARN_UNREACHABLE_CODE = YES; 259 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 260 | COPY_PHASE_STRIP = NO; 261 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 262 | ENABLE_NS_ASSERTIONS = NO; 263 | ENABLE_STRICT_OBJC_MSGSEND = YES; 264 | GCC_C_LANGUAGE_STANDARD = gnu11; 265 | GCC_NO_COMMON_BLOCKS = YES; 266 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 267 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 268 | GCC_WARN_UNDECLARED_SELECTOR = YES; 269 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 270 | GCC_WARN_UNUSED_FUNCTION = YES; 271 | GCC_WARN_UNUSED_VARIABLE = YES; 272 | IPHONEOS_DEPLOYMENT_TARGET = 14.5; 273 | MTL_ENABLE_DEBUG_INFO = NO; 274 | MTL_FAST_MATH = YES; 275 | SDKROOT = iphoneos; 276 | SWIFT_COMPILATION_MODE = wholemodule; 277 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 278 | VALIDATE_PRODUCT = YES; 279 | }; 280 | name = Release; 281 | }; 282 | 1215260425D7AAE2003BFAE2 /* Debug */ = { 283 | isa = XCBuildConfiguration; 284 | buildSettings = { 285 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 286 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 287 | CODE_SIGN_STYLE = Automatic; 288 | DEVELOPMENT_ASSET_PATHS = "\"Demo/Preview Content\""; 289 | DEVELOPMENT_TEAM = 88ACA86N96; 290 | ENABLE_PREVIEWS = YES; 291 | INFOPLIST_FILE = Demo/Info.plist; 292 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 293 | LD_RUNPATH_SEARCH_PATHS = ( 294 | "$(inherited)", 295 | "@executable_path/Frameworks", 296 | ); 297 | PRODUCT_BUNDLE_IDENTIFIER = inc.stamp.Demo; 298 | PRODUCT_NAME = "$(TARGET_NAME)"; 299 | SWIFT_VERSION = 5.0; 300 | TARGETED_DEVICE_FAMILY = "1,2"; 301 | }; 302 | name = Debug; 303 | }; 304 | 1215260525D7AAE2003BFAE2 /* Release */ = { 305 | isa = XCBuildConfiguration; 306 | buildSettings = { 307 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 308 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 309 | CODE_SIGN_STYLE = Automatic; 310 | DEVELOPMENT_ASSET_PATHS = "\"Demo/Preview Content\""; 311 | DEVELOPMENT_TEAM = 88ACA86N96; 312 | ENABLE_PREVIEWS = YES; 313 | INFOPLIST_FILE = Demo/Info.plist; 314 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 315 | LD_RUNPATH_SEARCH_PATHS = ( 316 | "$(inherited)", 317 | "@executable_path/Frameworks", 318 | ); 319 | PRODUCT_BUNDLE_IDENTIFIER = inc.stamp.Demo; 320 | PRODUCT_NAME = "$(TARGET_NAME)"; 321 | SWIFT_VERSION = 5.0; 322 | TARGETED_DEVICE_FAMILY = "1,2"; 323 | }; 324 | name = Release; 325 | }; 326 | /* End XCBuildConfiguration section */ 327 | 328 | /* Begin XCConfigurationList section */ 329 | 121525EF25D7AAE1003BFAE2 /* Build configuration list for PBXProject "Demo" */ = { 330 | isa = XCConfigurationList; 331 | buildConfigurations = ( 332 | 1215260125D7AAE2003BFAE2 /* Debug */, 333 | 1215260225D7AAE2003BFAE2 /* Release */, 334 | ); 335 | defaultConfigurationIsVisible = 0; 336 | defaultConfigurationName = Release; 337 | }; 338 | 1215260325D7AAE2003BFAE2 /* Build configuration list for PBXNativeTarget "Demo" */ = { 339 | isa = XCConfigurationList; 340 | buildConfigurations = ( 341 | 1215260425D7AAE2003BFAE2 /* Debug */, 342 | 1215260525D7AAE2003BFAE2 /* Release */, 343 | ); 344 | defaultConfigurationIsVisible = 0; 345 | defaultConfigurationName = Release; 346 | }; 347 | /* End XCConfigurationList section */ 348 | 349 | /* Begin XCSwiftPackageProductDependency section */ 350 | 1215260725D7AB42003BFAE2 /* Router */ = { 351 | isa = XCSwiftPackageProductDependency; 352 | productName = Router; 353 | }; 354 | /* End XCSwiftPackageProductDependency section */ 355 | }; 356 | rootObject = 121525EC25D7AAE1003BFAE2 /* Project object */; 357 | } 358 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Demo/Demo/AccountView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AccountView.swift 3 | // TransitionDemo 4 | // 5 | // Created by nori on 2021/01/16. 6 | // 7 | 8 | import SwiftUI 9 | import Router 10 | 11 | struct AccountView: View { 12 | 13 | @Environment(\.navigator) private var navigator: Binding 14 | 15 | var body: some View { 16 | ZStack { 17 | VStack { 18 | Image(systemName: "person.fill") 19 | .font(.system(size: 80, weight: .bold, design: .rounded)) 20 | } 21 | VStack { 22 | HStack { 23 | Spacer() 24 | Button(action: { 25 | navigator.push { 26 | navigator.wrappedValue.path = "/weather" 27 | } 28 | }) { 29 | Image(systemName: "chevron.right") 30 | .font(.system(size: 20, weight: .bold, design: .rounded)) 31 | } 32 | .buttonStyle(PlainButtonStyle()) 33 | } 34 | .padding() 35 | Spacer() 36 | } 37 | .padding() 38 | } 39 | } 40 | } 41 | 42 | struct AccountView_Previews: PreviewProvider { 43 | static var previews: some View { 44 | AccountView() 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Demo/Demo/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Demo/Demo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Demo/Demo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Demo/Demo/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // TransitionDemo 4 | // 5 | // Created by nori on 2021/01/01. 6 | // 7 | 8 | import SwiftUI 9 | import Router 10 | 11 | struct Weather { 12 | var label: String 13 | var title: String 14 | var systemImage: String 15 | } 16 | 17 | class DataStore: ObservableObject { 18 | var data: [Weather] = [ 19 | Weather(label: "sunny", title: "Sunny", systemImage: "sun.max"), 20 | Weather(label: "cloudy", title: "Cloudy", systemImage: "icloud"), 21 | Weather(label: "rainy", title: "Rainy", systemImage: "cloud.rain"), 22 | Weather(label: "snow", title: "Snow", systemImage: "snow") 23 | ] 24 | } 25 | 26 | struct ListView: View { 27 | 28 | @Environment(\.navigator) private var navigator: Binding 29 | 30 | @EnvironmentObject var dataStore: DataStore 31 | 32 | var body: some View { 33 | List { 34 | Section(header: 35 | Text("Weather") 36 | .font(.system(size: 24, weight: .black, design: .rounded)) 37 | .padding() 38 | ) { 39 | ForEach(dataStore.data, id: \.label) { data in 40 | Button(action: { 41 | navigator.push { 42 | navigator.wrappedValue.path = "/weather/\(data.label)" 43 | } 44 | }) { 45 | Label(data.title, systemImage: data.systemImage) 46 | .font(.system(size: 20, weight: .bold, design: .rounded)) 47 | Spacer() 48 | } 49 | .buttonStyle(PlainButtonStyle()) 50 | } 51 | } 52 | 53 | Section { 54 | Button(action: { 55 | navigator.pop { 56 | navigator.wrappedValue.path = "/account" 57 | } 58 | }) { 59 | Label("Account", systemImage: "person.fill") 60 | .font(.system(size: 20, weight: .bold, design: .rounded)) 61 | Spacer() 62 | } 63 | .buttonStyle(PlainButtonStyle()) 64 | } 65 | } 66 | .listStyle(InsetGroupedListStyle()) 67 | } 68 | } 69 | 70 | struct DetailView: View { 71 | 72 | @Environment(\.navigator) private var navigator: Binding 73 | 74 | @EnvironmentObject var dataStore: DataStore 75 | 76 | @State var isShrink: Bool = false 77 | 78 | var label: String 79 | 80 | var weather: Weather? { 81 | return self.dataStore.data.filter({$0.label == self.label}).first 82 | } 83 | 84 | var body: some View { 85 | ZStack { 86 | VStack(spacing: 10) { 87 | Image(systemName: self.weather!.systemImage) 88 | .font(.system(size: 120, weight: .bold, design: .rounded)) 89 | .scaleEffect(isShrink ? 0.8 : 1.0) 90 | .animation(.spring(response: 0.2, dampingFraction: 0.2, blendDuration: 0.2)) 91 | .onTapGesture { 92 | self.isShrink.toggle() 93 | } 94 | Text(label) 95 | .font(.system(size: 30, weight: .bold, design: .rounded)) 96 | } 97 | VStack { 98 | HStack { 99 | Button(action: { 100 | navigator.pop { 101 | navigator.wrappedValue.path = "/weather" 102 | } 103 | }) { 104 | Image(systemName: "chevron.backward") 105 | .font(.system(size: 20, weight: .bold, design: .rounded)) 106 | } 107 | .buttonStyle(PlainButtonStyle()) 108 | Spacer() 109 | } 110 | .padding() 111 | Spacer() 112 | } 113 | .padding() 114 | } 115 | } 116 | } 117 | 118 | struct ContentView: View { 119 | 120 | @State var isShow: Bool = false 121 | 122 | var body: some View { 123 | Router("/weather") { 124 | Route("/account") { 125 | AccountView() 126 | } 127 | Route("/weather") { 128 | ListView() 129 | } 130 | Route("/weather/{weatherLabel}") { context in 131 | DetailView(label: context.paramaters["weatherLabel"]!) 132 | } 133 | } 134 | .environmentObject(DataStore()) 135 | } 136 | } 137 | 138 | 139 | struct ContentView_Previews: PreviewProvider { 140 | static var previews: some View { 141 | ContentView() 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /Demo/Demo/DemoApp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DemoApp.swift 3 | // Demo 4 | // 5 | // Created by nori on 2021/02/13. 6 | // 7 | 8 | import SwiftUI 9 | 10 | @main 11 | struct DemoApp: App { 12 | var body: some Scene { 13 | WindowGroup { 14 | ContentView() 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Demo/Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | 28 | UIApplicationSupportsIndirectInputEvents 29 | 30 | UILaunchScreen 31 | 32 | UIRequiredDeviceCapabilities 33 | 34 | armv7 35 | 36 | UISupportedInterfaceOrientations 37 | 38 | UIInterfaceOrientationPortrait 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | UISupportedInterfaceOrientations~ipad 43 | 44 | UIInterfaceOrientationPortrait 45 | UIInterfaceOrientationPortraitUpsideDown 46 | UIInterfaceOrientationLandscapeLeft 47 | UIInterfaceOrientationLandscapeRight 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Demo/Demo/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 1amageek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.3 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "Router", 8 | platforms: [.iOS(.v14)], 9 | products: [ 10 | .library( 11 | name: "Router", 12 | targets: ["Router"]), 13 | ], 14 | dependencies: [ 15 | ], 16 | targets: [ 17 | .target( 18 | name: "Router", 19 | dependencies: []), 20 | .testTarget( 21 | name: "RouterTests", 22 | dependencies: ["Router"]), 23 | ] 24 | ) 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ![SwiftUI Router](router.png "SwiftUI Router") 3 | 4 | # Router 5 | 6 | Router is a library that assists with SwiftUI view transitions. 7 | 8 | ## Installation 9 | 10 | ```swift 11 | .package(name: "Router", url: "git@github.com:1amageek/Router.git", .upToNextMajor(from: "0.2.0")), 12 | ``` 13 | 14 | ## Usage 15 | 16 | ### Router 17 | The `Router` specifies the View to be navigated. 18 | The argument of `Router` is the Path of the first View to be displayed. By default, `/` is specified. 19 | 20 | ### Route 21 | `Route` will show the View of the Path specified in the argument. 22 | Path has placeholders and the parameters can be accessed from context. 23 | 24 | ```swift 25 | import SwiftUI 26 | import Router 27 | 28 | struct ContentView: View { 29 | 30 | @State var isShow: Bool = false 31 | 32 | var body: some View { 33 | Router("/weather") { 34 | Route("/weather") { 35 | ListView() 36 | } 37 | Route("/weather/{weatherLabel}") { context in 38 | DetailView(label: context.paramaters["weatherLabel"]!) 39 | } 40 | } 41 | .environmentObject(DataStore()) 42 | } 43 | } 44 | ``` 45 | 46 | ### Navigator 47 | 48 | It transitions between screens by giving `Navigator` a path. 49 | You can specify the transition animation. In the example below, we call the push animation. 50 | 51 | ```swift 52 | struct ListView: View { 53 | 54 | @Environment(\.navigator) private var navigator: Binding 55 | 56 | @EnvironmentObject var dataStore: DataStore 57 | 58 | var body: some View { 59 | 60 | List { 61 | Section(header: 62 | Text("Weather") 63 | .font(.system(size: 24, weight: .black, design: .rounded)) 64 | .padding() 65 | ) { 66 | ForEach(dataStore.data, id: \.label) { data in 67 | Button(action: { 68 | navigator.push { 69 | navigator.wrappedValue.path = "/weather/\(data.label)" 70 | } 71 | }) { 72 | Label(data.title, systemImage: data.systemImage) 73 | .font(.system(size: 20, weight: .bold, design: .rounded)) 74 | Spacer() 75 | } 76 | .buttonStyle(PlainButtonStyle()) 77 | } 78 | } 79 | } 80 | .listStyle(InsetGroupedListStyle()) 81 | } 82 | } 83 | ``` 84 | 85 | Navigator is defined as an environment, so it can be called from anywhere. 86 | 87 | ```swift 88 | struct DetailView: View { 89 | 90 | @Environment(\.navigator) private var navigator: Binding 91 | 92 | @EnvironmentObject var dataStore: DataStore 93 | 94 | var label: String 95 | 96 | var weather: Weather? { 97 | return self.dataStore.data.filter({$0.label == self.label}).first 98 | } 99 | 100 | var body: some View { 101 | ZStack { 102 | VStack(spacing: 10) { 103 | Image(systemName: self.weather!.systemImage) 104 | .font(.system(size: 120, weight: .bold, design: .rounded)) 105 | Text(label) 106 | .font(.system(size: 30, weight: .bold, design: .rounded)) 107 | } 108 | VStack(alignment: .leading) { 109 | HStack { 110 | Button(action: { 111 | navigator.pop { 112 | navigator.wrappedValue.path = "/weather" 113 | } 114 | }) { 115 | Image(systemName: "chevron.backward") 116 | .font(.system(size: 20, weight: .bold, design: .rounded)) 117 | } 118 | .buttonStyle(PlainButtonStyle()) 119 | 120 | Spacer() 121 | } 122 | Spacer() 123 | } 124 | .padding() 125 | } 126 | } 127 | } 128 | ``` 129 | 130 | ## Custom Transition Animation 131 | 132 | To customize the transition animations, you must first extend AnyTransition. 133 | 134 | ```swift 135 | public extension AnyTransition { 136 | 137 | struct NavigationFrontModifier: ViewModifier { 138 | let offset: CGSize 139 | public func body(content: Content) -> some View { 140 | ZStack { 141 | Color(UIColor.systemBackground) 142 | content 143 | } 144 | .offset(offset) 145 | } 146 | } 147 | 148 | static var navigationFront: AnyTransition { 149 | AnyTransition.modifier( 150 | active: NavigationFrontModifier(offset: CGSize(width: UIScreen.main.bounds.width, height: 0)), 151 | identity: NavigationFrontModifier(offset: .zero) 152 | ) 153 | } 154 | 155 | struct NavigationBackModifier: ViewModifier { 156 | let opacity: Double 157 | let offset: CGSize 158 | public func body(content: Content) -> some View { 159 | ZStack { 160 | content 161 | .offset(offset) 162 | Color.black.opacity(opacity) 163 | } 164 | } 165 | } 166 | 167 | static var navigationBack: AnyTransition { 168 | AnyTransition.modifier( 169 | active: NavigationBackModifier(opacity: 0.17, offset: CGSize(width: -UIScreen.main.bounds.width / 3, height: 0)), 170 | identity: NavigationBackModifier(opacity: 0, offset: .zero) 171 | ) 172 | } 173 | } 174 | ``` 175 | 176 | Next, we will extend Binding. 177 | 178 | ```swift 179 | public extension Binding where Value == Navigator { 180 | 181 | func push(_ animation: Animation? = .default, _ body: () throws -> Result) rethrows -> Result { 182 | let insertion: AnyTransition = .navigationFront 183 | let removal: AnyTransition = .navigationBack 184 | let transition: AnyTransition = .asymmetric(insertion: insertion, removal: removal) 185 | self.wrappedValue.zIndex = 0 186 | self.wrappedValue.transition = transition 187 | self.wrappedValue.uuid = UUID() 188 | return try withAnimation(animation, body) 189 | } 190 | 191 | func pop(_ animation: Animation? = .default, _ body: () throws -> Result) rethrows -> Result { 192 | let insertion: AnyTransition = .navigationBack 193 | let removal: AnyTransition = .navigationFront 194 | let transition: AnyTransition = .asymmetric(insertion: insertion, removal: removal) 195 | self.wrappedValue.zIndex = 1 196 | self.wrappedValue.transition = transition 197 | self.wrappedValue.uuid = UUID() 198 | return try withAnimation(animation, body) 199 | } 200 | } 201 | ``` 202 | 203 | It can be called as follows 204 | 205 | ```swift 206 | navigator.push { 207 | navigator.wrappedValue.path = "/weather/\(data.label)" 208 | } 209 | 210 | navigator.pop { 211 | navigator.wrappedValue.path = "/weather" 212 | } 213 | ``` 214 | -------------------------------------------------------------------------------- /Router.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Router.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Sources/Router/Link.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Link.swift 3 | // 4 | // 5 | // Created by nori on 2020/12/28. 6 | // 7 | 8 | import SwiftUI 9 | 10 | public struct Link : View where Content : View { 11 | 12 | @Environment(\.navigator) private var navigator: Binding 13 | 14 | public var to: String 15 | 16 | public var content: () -> Content 17 | 18 | public init(_ to: String, @ViewBuilder content: @escaping () -> Content) { 19 | self.to = to 20 | self.content = content 21 | } 22 | 23 | public var body: some View { 24 | Button(action: { 25 | withAnimation { 26 | self.navigator.wrappedValue = Navigator(to) 27 | } 28 | }) { 29 | self.content() 30 | } 31 | } 32 | } 33 | 34 | struct Link_Previews: PreviewProvider { 35 | static var previews: some View { 36 | Link("") { 37 | Text("foo") 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Sources/Router/Route.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Route.swift 3 | // 4 | // 5 | // Created by nori on 2020/12/28. 6 | // 7 | 8 | import SwiftUI 9 | 10 | public struct RouteContext { 11 | 12 | public var path: String 13 | 14 | public var pattern: String 15 | 16 | init(pattern: String, path: String) { 17 | self.pattern = pattern 18 | self.path = path 19 | } 20 | 21 | public var paramaters: [String: String] { getParamaters(path: path, pattern: pattern) } 22 | 23 | public var isMatch: Bool { 24 | if path == pattern { 25 | return true 26 | } 27 | guard let matchPath: String = self.matchPath(path: path, pattern: pattern) else { 28 | return false 29 | } 30 | if matchPath.split(separator: "/").count == pattern.split(separator: "/").count && 31 | matchPath.split(separator: "/").count == path.split(separator: "/").count { 32 | return true 33 | } 34 | return false 35 | } 36 | 37 | private func matchPath(path: String, pattern: String) -> String? { 38 | let regex: NSRegularExpression = try! NSRegularExpression(pattern: "\\{(\\S+?)\\}", options: []) 39 | let results: [NSTextCheckingResult] = regex.matches(in: pattern, options: [], range: NSRange(0.. String in 42 | let start = pattern.index(pattern.startIndex, offsetBy: result.range(at: 0).location) 43 | let end = pattern.index(start, offsetBy: result.range(at: 0).length) 44 | return String(pattern[start.. String in 47 | return prev.replacingOccurrences(of: current, with: "(.+)") 48 | } 49 | let values: [String] = try! NSRegularExpression(pattern: matchPattern, options: []) 50 | .matches(in: path, options: [], range: NSRange(0.. String in 52 | let start = path.index(path.startIndex, offsetBy: result.range(at: 0).location) 53 | let end = path.index(start, offsetBy: result.range(at: 0).length) 54 | return String(path[start.. [String: String] { 60 | let regex: NSRegularExpression = try! NSRegularExpression(pattern: "\\{(\\S+?)\\}", options: []) 61 | let results: [NSTextCheckingResult] = regex.matches(in: pattern, options: [], range: NSRange(0.. String in 64 | let start = pattern.index(pattern.startIndex, offsetBy: result.range(at: 1).location) 65 | let end = pattern.index(start, offsetBy: result.range(at: 1).length) 66 | return String(pattern[start.. String in 70 | let start = pattern.index(pattern.startIndex, offsetBy: result.range(at: 0).location) 71 | let end = pattern.index(start, offsetBy: result.range(at: 0).length) 72 | return String(pattern[start.. String in 75 | return prev.replacingOccurrences(of: current, with: "(.+)") 76 | } 77 | guard let values: [String] = try! NSRegularExpression(pattern: matchPattern, options: []) 78 | .matches(in: path, options: [], range: NSRange(0.. [String] in 80 | return (0.. String in 82 | let start = path.index(path.startIndex, offsetBy: result.range(at: idx).location) 83 | let end = path.index(start, offsetBy: result.range(at: idx).length) 84 | return String(path[start.. 101 | 102 | let path: String 103 | 104 | func body(content: Self.Content) -> some View { 105 | return content 106 | .transition(navigator.wrappedValue.transition) 107 | .id(navigator.wrappedValue.uuid.uuidString) 108 | .zIndex(RouteContext(pattern: self.path, path: navigator.wrappedValue.path).isMatch ? navigator.wrappedValue.zIndex : 0) 109 | .onDisappear(perform: { 110 | navigator.wrappedValue.zIndex = 0 111 | navigator.wrappedValue.isAnimating = false 112 | }) 113 | } 114 | } 115 | 116 | public struct Route : View where Content : View { 117 | 118 | @Environment(\.navigator) private var navigator: Binding 119 | 120 | private var path: String 121 | 122 | private var contentWithContext: ((RouteContext) -> Content)! 123 | 124 | private var content: (() -> Content)! 125 | 126 | public init(_ path: String, @ViewBuilder content: @escaping (RouteContext) -> Content) { 127 | self.path = path 128 | self.contentWithContext = content 129 | } 130 | 131 | public init(_ path: String, @ViewBuilder content: @escaping () -> Content) { 132 | self.path = path 133 | self.content = content 134 | } 135 | 136 | @ViewBuilder 137 | var _bodyWithContext: some View { 138 | if RouteContext(pattern: self.path, path: navigator.wrappedValue.path).isMatch { 139 | if navigator.wrappedValue.isAnimating { 140 | RouteView(content: self.contentWithContext(RouteContext(pattern: self.path, path: navigator.wrappedValue.path))) 141 | .modifier(RouteModifier(path: self.path)) 142 | } else { 143 | RouteView(content: self.contentWithContext(RouteContext(pattern: self.path, path: navigator.wrappedValue.path))) 144 | } 145 | } else { 146 | EmptyView() 147 | } 148 | } 149 | 150 | @ViewBuilder 151 | var _body: some View { 152 | if RouteContext(pattern: self.path, path: navigator.wrappedValue.path).isMatch { 153 | if navigator.wrappedValue.isAnimating { 154 | RouteView(content: self.content()) 155 | .modifier(RouteModifier(path: self.path)) 156 | } else { 157 | RouteView(content: self.content()) 158 | } 159 | } else { 160 | EmptyView() 161 | } 162 | } 163 | 164 | public var body: some View { 165 | if self.contentWithContext != nil { 166 | _bodyWithContext 167 | } else { 168 | _body 169 | } 170 | } 171 | } 172 | 173 | struct Route_Previews: PreviewProvider { 174 | static var previews: some View { 175 | Route("/foo") { context in 176 | Text("test") 177 | } 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /Sources/Router/RouteView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by nori on 2021/02/13. 6 | // 7 | 8 | import UIKit 9 | import SwiftUI 10 | 11 | struct RouteView: UIViewControllerRepresentable { 12 | 13 | typealias UIViewControllerType = UIHostingController 14 | 15 | let content: Content 16 | 17 | init(content: Content) { 18 | self.content = content 19 | } 20 | 21 | func makeUIViewController(context: Context) -> UIHostingController { 22 | return UIHostingController(rootView: content) 23 | } 24 | 25 | func updateUIViewController(_ uiViewController: UIHostingController, context: Context) { 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Sources/Router/Router.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Router.swift 3 | // 4 | // 5 | // Created by nori on 2020/12/28. 6 | // 7 | 8 | import SwiftUI 9 | 10 | public struct Navigator { 11 | 12 | public var path: String 13 | 14 | public var transition: AnyTransition = .identity 15 | 16 | var isAnimating: Bool = false 17 | 18 | var zIndex: Double = 0 19 | 20 | var uuid: UUID = UUID() 21 | 22 | public init(_ path: String) { 23 | self.path = path 24 | } 25 | } 26 | 27 | extension AnyTransition { 28 | 29 | struct NavigationFrontModifier: ViewModifier { 30 | let offset: CGSize 31 | func body(content: Content) -> some View { 32 | ZStack { 33 | Color(UIColor.systemBackground) 34 | .ignoresSafeArea() 35 | content 36 | } 37 | .offset(offset) 38 | } 39 | } 40 | 41 | static var navigationFront: AnyTransition { 42 | AnyTransition.modifier( 43 | active: NavigationFrontModifier(offset: CGSize(width: UIScreen.main.bounds.width, height: 0)), 44 | identity: NavigationFrontModifier(offset: .zero) 45 | ) 46 | } 47 | 48 | struct NavigationBackModifier: ViewModifier { 49 | let opacity: Double 50 | let offset: CGSize 51 | func body(content: Content) -> some View { 52 | ZStack { 53 | content 54 | .offset(offset) 55 | Color.black 56 | .opacity(opacity) 57 | .ignoresSafeArea() 58 | } 59 | } 60 | } 61 | 62 | static var navigationBack: AnyTransition { 63 | AnyTransition.modifier( 64 | active: NavigationBackModifier(opacity: 0.17, offset: CGSize(width: -UIScreen.main.bounds.width / 3, height: 0)), 65 | identity: NavigationBackModifier(opacity: 0, offset: .zero) 66 | ) 67 | } 68 | } 69 | 70 | public extension Binding where Value == Navigator { 71 | 72 | func push(_ animation: Animation? = .default, _ body: () throws -> Result) rethrows -> Result { 73 | let insertion: AnyTransition = .navigationFront 74 | let removal: AnyTransition = .navigationBack 75 | let transition: AnyTransition = .asymmetric(insertion: insertion, removal: removal) 76 | self.wrappedValue.isAnimating = true 77 | self.wrappedValue.zIndex = 0 78 | self.wrappedValue.transition = transition 79 | self.wrappedValue.uuid = UUID() 80 | return try withAnimation(animation, body) 81 | } 82 | 83 | func pop(_ animation: Animation? = .default, _ body: () throws -> Result) rethrows -> Result { 84 | let insertion: AnyTransition = .navigationBack 85 | let removal: AnyTransition = .navigationFront 86 | let transition: AnyTransition = .asymmetric(insertion: insertion, removal: removal) 87 | self.wrappedValue.isAnimating = true 88 | self.wrappedValue.zIndex = 1 89 | self.wrappedValue.transition = transition 90 | self.wrappedValue.uuid = UUID() 91 | return try withAnimation(animation, body) 92 | } 93 | } 94 | 95 | public struct NavigatorKey: EnvironmentKey { 96 | public static let defaultValue: Binding = .constant(Navigator("/")) 97 | } 98 | 99 | public extension EnvironmentValues { 100 | var navigator: Binding { 101 | get { self[NavigatorKey.self] } 102 | set { self[NavigatorKey.self] = newValue } 103 | } 104 | } 105 | 106 | public struct Router : View where Content : View { 107 | 108 | @State private var navigator: Navigator = Navigator("/") 109 | 110 | public var content: () -> Content 111 | 112 | public init(_ path: String = "/", @ViewBuilder content: @escaping () -> Content) { 113 | self.content = content 114 | self._navigator = State(initialValue: Navigator(path)) 115 | } 116 | 117 | public var body: some View { 118 | self.content() 119 | .environment(\.navigator, self.$navigator) 120 | } 121 | } 122 | 123 | struct Router_Previews: PreviewProvider { 124 | static var previews: some View { 125 | Router { 126 | Route("/") { context in 127 | List { 128 | Link("/foo") { 129 | Text("foo") 130 | } 131 | Link("/bar") { 132 | Text("bar") 133 | } 134 | } 135 | .navigationTitle("/") 136 | } 137 | Route("/foo") { context in 138 | List { 139 | Link("/foo") { 140 | Text("foo") 141 | } 142 | Link("/bar") { 143 | Text("bar") 144 | } 145 | } 146 | .navigationTitle("/foo") 147 | } 148 | Route("/bar") { context in 149 | List { 150 | Link("/foo") { 151 | Text("foo") 152 | } 153 | Link("/bar") { 154 | Text("bar") 155 | } 156 | } 157 | .navigationTitle("/bar") 158 | } 159 | Route("/user/:uid") { context in 160 | List { 161 | Link("/foo") { 162 | Text("foo") 163 | } 164 | Link("/bar") { 165 | Text("bar") 166 | } 167 | } 168 | .navigationTitle("/bar") 169 | } 170 | } 171 | 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import RouterTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += RouterTests.allTests() 7 | XCTMain(tests) 8 | -------------------------------------------------------------------------------- /Tests/RouterTests/RouterTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import Router 3 | 4 | final class RouterTests: XCTestCase { 5 | 6 | func testRouteContextIsTrue() { 7 | XCTAssertEqual(RouteContext(pattern: "/", path: "/").isMatch, true) 8 | XCTAssertEqual(RouteContext(pattern: "/a", path: "/a").isMatch, true) 9 | XCTAssertEqual(RouteContext(pattern: "/1", path: "/1").isMatch, true) 10 | XCTAssertEqual(RouteContext(pattern: "/{a}", path: "/a").isMatch, true) 11 | XCTAssertEqual(RouteContext(pattern: "/{a}", path: "/1").isMatch, true) 12 | XCTAssertEqual(RouteContext(pattern: "/{a}/{b}", path: "/a/b").isMatch, true) 13 | XCTAssertEqual(RouteContext(pattern: "/{a}/{b}", path: "/1/2").isMatch, true) 14 | XCTAssertEqual(RouteContext(pattern: "/a/{a}/b/{b}", path: "/a/a/b/b").isMatch, true) 15 | XCTAssertEqual(RouteContext(pattern: "/a/{a}/b/{b}", path: "/a/1/b/2").isMatch, true) 16 | } 17 | 18 | func testRouteContextIsFalse() { 19 | XCTAssertEqual(RouteContext(pattern: "/", path: "/a").isMatch, false) 20 | XCTAssertEqual(RouteContext(pattern: "/a", path: "/b").isMatch, false) 21 | XCTAssertEqual(RouteContext(pattern: "/1", path: "/2").isMatch, false) 22 | XCTAssertEqual(RouteContext(pattern: "/{a}", path: "/a/a").isMatch, false) 23 | XCTAssertEqual(RouteContext(pattern: "/{a}", path: "/1/a").isMatch, false) 24 | XCTAssertEqual(RouteContext(pattern: "/{a}/{b}", path: "/a/b/c").isMatch, false) 25 | XCTAssertEqual(RouteContext(pattern: "/{a}/{b}", path: "/1/2/3").isMatch, false) 26 | XCTAssertEqual(RouteContext(pattern: "/a/{a}/b/{b}/c", path: "/a/a/b/b/").isMatch, false) 27 | XCTAssertEqual(RouteContext(pattern: "/a/{a}/b/{b}/c", path: "/a/1/b/2/").isMatch, false) 28 | XCTAssertEqual(RouteContext(pattern: "/b/{a}", path: "/a/a").isMatch, false) 29 | XCTAssertEqual(RouteContext(pattern: "/b/{a}", path: "/a/1").isMatch, false) 30 | } 31 | 32 | func testRouteContextParameters() { 33 | XCTAssertEqual(RouteContext(pattern: "/{a}", path: "/a").paramaters["a"], "a") 34 | XCTAssertEqual(RouteContext(pattern: "/{key}", path: "/a").paramaters["key"], "a") 35 | XCTAssertEqual(RouteContext(pattern: "/{key}", path: "/1").paramaters["key"], "1") 36 | XCTAssertEqual(RouteContext(pattern: "/{a}/{b}", path: "/a/b").paramaters["a"], "a") 37 | XCTAssertEqual(RouteContext(pattern: "/{a}/{b}", path: "/a/b").paramaters["b"], "b") 38 | XCTAssertEqual(RouteContext(pattern: "/user/{uid}", path: "/user/b").paramaters["uid"], "b") 39 | XCTAssertEqual(RouteContext(pattern: "/user/{uid}/products/{product_id}", path: "/user/a/products/0").paramaters["uid"], "a") 40 | XCTAssertEqual(RouteContext(pattern: "/user/{uid}/products/{product_id}", path: "/user/a/products/0").paramaters["product_id"], "0") 41 | } 42 | 43 | static var allTests = [ 44 | ("testRouteContextIsTrue", testRouteContextIsTrue), 45 | ("testRouteContextIsFalse", testRouteContextIsFalse), 46 | ("testRouteContextParameters", testRouteContextParameters) 47 | ] 48 | } 49 | -------------------------------------------------------------------------------- /Tests/RouterTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !canImport(ObjectiveC) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(RouterTests.allTests), 7 | ] 8 | } 9 | #endif 10 | -------------------------------------------------------------------------------- /router.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1amageek/Router/999d73129a5565990ab61882fc9f77a43b1ee408/router.png --------------------------------------------------------------------------------