├── .gitignore ├── LICENSE ├── README.md ├── RouterWorkaround.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcuserdata │ └── d.lomaev.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── RouterWorkaround ├── Assets.xcassets ├── AccentColor.colorset │ └── Contents.json ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── ContentView.swift ├── Preview Content └── Preview Assets.xcassets │ └── Contents.json ├── Router ├── NavigationNode.swift ├── Router.swift ├── RouterFactory.swift ├── Routing.swift └── View + Cover.swift ├── RouterWorkaroundApp.swift └── Screens ├── FullScreenView.swift ├── MainView.swift ├── RoutesView.swift ├── SheetView.swift └── StartingView.swift /.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 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Danil Lomaev 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Router-SwiftUIWorkaround 2 | Workaround for https://www.swiftuiseries.com/workarounds 3 | 4 | 5 | https://user-images.githubusercontent.com/59708377/170004683-737cb7ef-7f48-47d7-9fbf-872d0c49bcbd.mov 6 | 7 | ## About 8 | 9 | SwiftUI does not allow programmable screen navigation out of the box, has many bugs and limited screen manipulation capabilities. This repository provides a workaround to use routing programmatically. 10 | 11 | ## Steps to use 12 | 13 | 1. Configure routes 14 | Use enum for routes and dont forget to choose screens with NavigationView! 15 | ```swift 16 | enum ScreenRoute: ScreenProtocol { 17 | case start 18 | case navigator 19 | case fullScreen 20 | case sheetScreen 21 | 22 | var embedInNavView: Bool { 23 | switch self { 24 | case .start, .sheetScreen: 25 | return true 26 | case .navigator, .fullScreen: 27 | return false 28 | } 29 | } 30 | } 31 | ``` 32 | 2. Create router factory. 33 | This class maps routes and creates views. 34 | ```swift 35 | class ScreenRouterFactory: RouterFactory { 36 | 37 | @ViewBuilder func makeBody(for screen: ScreenRoute) -> some View { 38 | switch screen { 39 | case .start: 40 | StartingView() 41 | case .navigator: 42 | RoutesView() 43 | case .fullScreen: 44 | FullScreenView() 45 | case .sheetScreen: 46 | SheetView() 47 | } 48 | } 49 | } 50 | ``` 51 | 3. (? optional) Create typealias for easy naming Routers. 52 | 53 | ```swift 54 | typealias ScreenRouter = Router 55 | ``` 56 | 57 | 4. Create Router instance. 58 | You can create in Views, ViewModels or use in DI. Don't forget to use @StateObject or similar wrappers in views for re-render 59 | ```swift 60 | let screenRouter = ScreenRouter(rootScreen: .start, factory: ScreenRouterFactory()) 61 | ``` 62 | 5. Use it! Start routing with start method. 63 | In this example EnvObject used: 64 | 65 | ```swift 66 | struct MainView: View { 67 | @EnvironmentObject var screenRouter: ScreenRouter 68 | 69 | var body: some View { 70 | screenRouter.start() 71 | } 72 | } 73 | ``` 74 | 6. Available Router's methods: 75 | 76 | ```swift 77 | func start() -> some View 78 | func presentSheet(_ screen: ScreenType) 79 | func dismissLast() 80 | func navigateTo(_ screen: ScreenType) 81 | func presentFullScreen(_ screen: ScreenType) 82 | func popToRoot() 83 | ``` 84 | -------------------------------------------------------------------------------- /RouterWorkaround.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | A2B0DD74283CCF58002C5E66 /* RouterWorkaroundApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD73283CCF58002C5E66 /* RouterWorkaroundApp.swift */; }; 11 | A2B0DD76283CCF58002C5E66 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD75283CCF58002C5E66 /* ContentView.swift */; }; 12 | A2B0DD78283CCF5B002C5E66 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A2B0DD77283CCF5B002C5E66 /* Assets.xcassets */; }; 13 | A2B0DD7B283CCF5B002C5E66 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A2B0DD7A283CCF5B002C5E66 /* Preview Assets.xcassets */; }; 14 | A2B0DD87283CCF83002C5E66 /* NavigationNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD82283CCF83002C5E66 /* NavigationNode.swift */; }; 15 | A2B0DD88283CCF83002C5E66 /* RouterFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD83283CCF83002C5E66 /* RouterFactory.swift */; }; 16 | A2B0DD89283CCF83002C5E66 /* Routing.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD84283CCF83002C5E66 /* Routing.swift */; }; 17 | A2B0DD8A283CCF83002C5E66 /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD85283CCF83002C5E66 /* Router.swift */; }; 18 | A2B0DD8B283CCF83002C5E66 /* View + Cover.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD86283CCF83002C5E66 /* View + Cover.swift */; }; 19 | A2B0DD90283CD079002C5E66 /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD8F283CD079002C5E66 /* MainView.swift */; }; 20 | A2B0DD92283CD499002C5E66 /* StartingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD91283CD499002C5E66 /* StartingView.swift */; }; 21 | A2B0DD94283CD5FF002C5E66 /* RoutesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD93283CD5FF002C5E66 /* RoutesView.swift */; }; 22 | A2B0DD96283CD86C002C5E66 /* FullScreenView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD95283CD86C002C5E66 /* FullScreenView.swift */; }; 23 | A2B0DD98283CD8B0002C5E66 /* SheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2B0DD97283CD8B0002C5E66 /* SheetView.swift */; }; 24 | /* End PBXBuildFile section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | A2B0DD70283CCF58002C5E66 /* RouterWorkaround.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RouterWorkaround.app; sourceTree = BUILT_PRODUCTS_DIR; }; 28 | A2B0DD73283CCF58002C5E66 /* RouterWorkaroundApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouterWorkaroundApp.swift; sourceTree = ""; }; 29 | A2B0DD75283CCF58002C5E66 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 30 | A2B0DD77283CCF5B002C5E66 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 31 | A2B0DD7A283CCF5B002C5E66 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 32 | A2B0DD82283CCF83002C5E66 /* NavigationNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationNode.swift; sourceTree = ""; }; 33 | A2B0DD83283CCF83002C5E66 /* RouterFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouterFactory.swift; sourceTree = ""; }; 34 | A2B0DD84283CCF83002C5E66 /* Routing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Routing.swift; sourceTree = ""; }; 35 | A2B0DD85283CCF83002C5E66 /* Router.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = ""; }; 36 | A2B0DD86283CCF83002C5E66 /* View + Cover.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View + Cover.swift"; sourceTree = ""; }; 37 | A2B0DD8F283CD079002C5E66 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = ""; }; 38 | A2B0DD91283CD499002C5E66 /* StartingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartingView.swift; sourceTree = ""; }; 39 | A2B0DD93283CD5FF002C5E66 /* RoutesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoutesView.swift; sourceTree = ""; }; 40 | A2B0DD95283CD86C002C5E66 /* FullScreenView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullScreenView.swift; sourceTree = ""; }; 41 | A2B0DD97283CD8B0002C5E66 /* SheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SheetView.swift; sourceTree = ""; }; 42 | /* End PBXFileReference section */ 43 | 44 | /* Begin PBXFrameworksBuildPhase section */ 45 | A2B0DD6D283CCF58002C5E66 /* Frameworks */ = { 46 | isa = PBXFrameworksBuildPhase; 47 | buildActionMask = 2147483647; 48 | files = ( 49 | ); 50 | runOnlyForDeploymentPostprocessing = 0; 51 | }; 52 | /* End PBXFrameworksBuildPhase section */ 53 | 54 | /* Begin PBXGroup section */ 55 | A2B0DD67283CCF58002C5E66 = { 56 | isa = PBXGroup; 57 | children = ( 58 | A2B0DD72283CCF58002C5E66 /* RouterWorkaround */, 59 | A2B0DD71283CCF58002C5E66 /* Products */, 60 | ); 61 | sourceTree = ""; 62 | }; 63 | A2B0DD71283CCF58002C5E66 /* Products */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | A2B0DD70283CCF58002C5E66 /* RouterWorkaround.app */, 67 | ); 68 | name = Products; 69 | sourceTree = ""; 70 | }; 71 | A2B0DD72283CCF58002C5E66 /* RouterWorkaround */ = { 72 | isa = PBXGroup; 73 | children = ( 74 | A2B0DD8C283CD042002C5E66 /* Screens */, 75 | A2B0DD81283CCF6B002C5E66 /* Router */, 76 | A2B0DD73283CCF58002C5E66 /* RouterWorkaroundApp.swift */, 77 | A2B0DD75283CCF58002C5E66 /* ContentView.swift */, 78 | A2B0DD77283CCF5B002C5E66 /* Assets.xcassets */, 79 | A2B0DD79283CCF5B002C5E66 /* Preview Content */, 80 | ); 81 | path = RouterWorkaround; 82 | sourceTree = ""; 83 | }; 84 | A2B0DD79283CCF5B002C5E66 /* Preview Content */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | A2B0DD7A283CCF5B002C5E66 /* Preview Assets.xcassets */, 88 | ); 89 | path = "Preview Content"; 90 | sourceTree = ""; 91 | }; 92 | A2B0DD81283CCF6B002C5E66 /* Router */ = { 93 | isa = PBXGroup; 94 | children = ( 95 | A2B0DD82283CCF83002C5E66 /* NavigationNode.swift */, 96 | A2B0DD85283CCF83002C5E66 /* Router.swift */, 97 | A2B0DD83283CCF83002C5E66 /* RouterFactory.swift */, 98 | A2B0DD84283CCF83002C5E66 /* Routing.swift */, 99 | A2B0DD86283CCF83002C5E66 /* View + Cover.swift */, 100 | ); 101 | path = Router; 102 | sourceTree = ""; 103 | }; 104 | A2B0DD8C283CD042002C5E66 /* Screens */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | A2B0DD8F283CD079002C5E66 /* MainView.swift */, 108 | A2B0DD91283CD499002C5E66 /* StartingView.swift */, 109 | A2B0DD93283CD5FF002C5E66 /* RoutesView.swift */, 110 | A2B0DD95283CD86C002C5E66 /* FullScreenView.swift */, 111 | A2B0DD97283CD8B0002C5E66 /* SheetView.swift */, 112 | ); 113 | path = Screens; 114 | sourceTree = ""; 115 | }; 116 | /* End PBXGroup section */ 117 | 118 | /* Begin PBXNativeTarget section */ 119 | A2B0DD6F283CCF58002C5E66 /* RouterWorkaround */ = { 120 | isa = PBXNativeTarget; 121 | buildConfigurationList = A2B0DD7E283CCF5B002C5E66 /* Build configuration list for PBXNativeTarget "RouterWorkaround" */; 122 | buildPhases = ( 123 | A2B0DD6C283CCF58002C5E66 /* Sources */, 124 | A2B0DD6D283CCF58002C5E66 /* Frameworks */, 125 | A2B0DD6E283CCF58002C5E66 /* Resources */, 126 | ); 127 | buildRules = ( 128 | ); 129 | dependencies = ( 130 | ); 131 | name = RouterWorkaround; 132 | productName = RouterWorkaround; 133 | productReference = A2B0DD70283CCF58002C5E66 /* RouterWorkaround.app */; 134 | productType = "com.apple.product-type.application"; 135 | }; 136 | /* End PBXNativeTarget section */ 137 | 138 | /* Begin PBXProject section */ 139 | A2B0DD68283CCF58002C5E66 /* Project object */ = { 140 | isa = PBXProject; 141 | attributes = { 142 | BuildIndependentTargetsInParallel = 1; 143 | LastSwiftUpdateCheck = 1300; 144 | LastUpgradeCheck = 1300; 145 | TargetAttributes = { 146 | A2B0DD6F283CCF58002C5E66 = { 147 | CreatedOnToolsVersion = 13.0; 148 | }; 149 | }; 150 | }; 151 | buildConfigurationList = A2B0DD6B283CCF58002C5E66 /* Build configuration list for PBXProject "RouterWorkaround" */; 152 | compatibilityVersion = "Xcode 13.0"; 153 | developmentRegion = en; 154 | hasScannedForEncodings = 0; 155 | knownRegions = ( 156 | en, 157 | Base, 158 | ); 159 | mainGroup = A2B0DD67283CCF58002C5E66; 160 | productRefGroup = A2B0DD71283CCF58002C5E66 /* Products */; 161 | projectDirPath = ""; 162 | projectRoot = ""; 163 | targets = ( 164 | A2B0DD6F283CCF58002C5E66 /* RouterWorkaround */, 165 | ); 166 | }; 167 | /* End PBXProject section */ 168 | 169 | /* Begin PBXResourcesBuildPhase section */ 170 | A2B0DD6E283CCF58002C5E66 /* Resources */ = { 171 | isa = PBXResourcesBuildPhase; 172 | buildActionMask = 2147483647; 173 | files = ( 174 | A2B0DD7B283CCF5B002C5E66 /* Preview Assets.xcassets in Resources */, 175 | A2B0DD78283CCF5B002C5E66 /* Assets.xcassets in Resources */, 176 | ); 177 | runOnlyForDeploymentPostprocessing = 0; 178 | }; 179 | /* End PBXResourcesBuildPhase section */ 180 | 181 | /* Begin PBXSourcesBuildPhase section */ 182 | A2B0DD6C283CCF58002C5E66 /* Sources */ = { 183 | isa = PBXSourcesBuildPhase; 184 | buildActionMask = 2147483647; 185 | files = ( 186 | A2B0DD92283CD499002C5E66 /* StartingView.swift in Sources */, 187 | A2B0DD89283CCF83002C5E66 /* Routing.swift in Sources */, 188 | A2B0DD96283CD86C002C5E66 /* FullScreenView.swift in Sources */, 189 | A2B0DD76283CCF58002C5E66 /* ContentView.swift in Sources */, 190 | A2B0DD87283CCF83002C5E66 /* NavigationNode.swift in Sources */, 191 | A2B0DD74283CCF58002C5E66 /* RouterWorkaroundApp.swift in Sources */, 192 | A2B0DD8B283CCF83002C5E66 /* View + Cover.swift in Sources */, 193 | A2B0DD94283CD5FF002C5E66 /* RoutesView.swift in Sources */, 194 | A2B0DD8A283CCF83002C5E66 /* Router.swift in Sources */, 195 | A2B0DD90283CD079002C5E66 /* MainView.swift in Sources */, 196 | A2B0DD98283CD8B0002C5E66 /* SheetView.swift in Sources */, 197 | A2B0DD88283CCF83002C5E66 /* RouterFactory.swift in Sources */, 198 | ); 199 | runOnlyForDeploymentPostprocessing = 0; 200 | }; 201 | /* End PBXSourcesBuildPhase section */ 202 | 203 | /* Begin XCBuildConfiguration section */ 204 | A2B0DD7C283CCF5B002C5E66 /* Debug */ = { 205 | isa = XCBuildConfiguration; 206 | buildSettings = { 207 | ALWAYS_SEARCH_USER_PATHS = NO; 208 | CLANG_ANALYZER_NONNULL = YES; 209 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 210 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 211 | CLANG_CXX_LIBRARY = "libc++"; 212 | CLANG_ENABLE_MODULES = YES; 213 | CLANG_ENABLE_OBJC_ARC = YES; 214 | CLANG_ENABLE_OBJC_WEAK = YES; 215 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 216 | CLANG_WARN_BOOL_CONVERSION = YES; 217 | CLANG_WARN_COMMA = YES; 218 | CLANG_WARN_CONSTANT_CONVERSION = YES; 219 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 220 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 221 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 222 | CLANG_WARN_EMPTY_BODY = YES; 223 | CLANG_WARN_ENUM_CONVERSION = YES; 224 | CLANG_WARN_INFINITE_RECURSION = YES; 225 | CLANG_WARN_INT_CONVERSION = YES; 226 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 227 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 228 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 229 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 230 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 231 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 232 | CLANG_WARN_STRICT_PROTOTYPES = YES; 233 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 234 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 235 | CLANG_WARN_UNREACHABLE_CODE = YES; 236 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 237 | COPY_PHASE_STRIP = NO; 238 | DEBUG_INFORMATION_FORMAT = dwarf; 239 | ENABLE_STRICT_OBJC_MSGSEND = YES; 240 | ENABLE_TESTABILITY = YES; 241 | GCC_C_LANGUAGE_STANDARD = gnu11; 242 | GCC_DYNAMIC_NO_PIC = NO; 243 | GCC_NO_COMMON_BLOCKS = YES; 244 | GCC_OPTIMIZATION_LEVEL = 0; 245 | GCC_PREPROCESSOR_DEFINITIONS = ( 246 | "DEBUG=1", 247 | "$(inherited)", 248 | ); 249 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 250 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 251 | GCC_WARN_UNDECLARED_SELECTOR = YES; 252 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 253 | GCC_WARN_UNUSED_FUNCTION = YES; 254 | GCC_WARN_UNUSED_VARIABLE = YES; 255 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 256 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 257 | MTL_FAST_MATH = YES; 258 | ONLY_ACTIVE_ARCH = YES; 259 | SDKROOT = iphoneos; 260 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 261 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 262 | }; 263 | name = Debug; 264 | }; 265 | A2B0DD7D283CCF5B002C5E66 /* Release */ = { 266 | isa = XCBuildConfiguration; 267 | buildSettings = { 268 | ALWAYS_SEARCH_USER_PATHS = NO; 269 | CLANG_ANALYZER_NONNULL = YES; 270 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 271 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 272 | CLANG_CXX_LIBRARY = "libc++"; 273 | CLANG_ENABLE_MODULES = YES; 274 | CLANG_ENABLE_OBJC_ARC = YES; 275 | CLANG_ENABLE_OBJC_WEAK = YES; 276 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 277 | CLANG_WARN_BOOL_CONVERSION = YES; 278 | CLANG_WARN_COMMA = YES; 279 | CLANG_WARN_CONSTANT_CONVERSION = YES; 280 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 281 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 282 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 283 | CLANG_WARN_EMPTY_BODY = YES; 284 | CLANG_WARN_ENUM_CONVERSION = YES; 285 | CLANG_WARN_INFINITE_RECURSION = YES; 286 | CLANG_WARN_INT_CONVERSION = YES; 287 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 288 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 289 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 290 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 291 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 292 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 293 | CLANG_WARN_STRICT_PROTOTYPES = YES; 294 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 295 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 296 | CLANG_WARN_UNREACHABLE_CODE = YES; 297 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 298 | COPY_PHASE_STRIP = NO; 299 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 300 | ENABLE_NS_ASSERTIONS = NO; 301 | ENABLE_STRICT_OBJC_MSGSEND = YES; 302 | GCC_C_LANGUAGE_STANDARD = gnu11; 303 | GCC_NO_COMMON_BLOCKS = YES; 304 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 305 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 306 | GCC_WARN_UNDECLARED_SELECTOR = YES; 307 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 308 | GCC_WARN_UNUSED_FUNCTION = YES; 309 | GCC_WARN_UNUSED_VARIABLE = YES; 310 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 311 | MTL_ENABLE_DEBUG_INFO = NO; 312 | MTL_FAST_MATH = YES; 313 | SDKROOT = iphoneos; 314 | SWIFT_COMPILATION_MODE = wholemodule; 315 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 316 | VALIDATE_PRODUCT = YES; 317 | }; 318 | name = Release; 319 | }; 320 | A2B0DD7F283CCF5B002C5E66 /* Debug */ = { 321 | isa = XCBuildConfiguration; 322 | buildSettings = { 323 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 324 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 325 | CODE_SIGN_STYLE = Automatic; 326 | CURRENT_PROJECT_VERSION = 1; 327 | DEVELOPMENT_ASSET_PATHS = "\"RouterWorkaround/Preview Content\""; 328 | DEVELOPMENT_TEAM = 5XY4M72NS4; 329 | ENABLE_PREVIEWS = YES; 330 | GENERATE_INFOPLIST_FILE = YES; 331 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 332 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 333 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 334 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 335 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 336 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 337 | LD_RUNPATH_SEARCH_PATHS = ( 338 | "$(inherited)", 339 | "@executable_path/Frameworks", 340 | ); 341 | MARKETING_VERSION = 1.0; 342 | PRODUCT_BUNDLE_IDENTIFIER = com.dlom.RouterWorkaround; 343 | PRODUCT_NAME = "$(TARGET_NAME)"; 344 | SWIFT_EMIT_LOC_STRINGS = YES; 345 | SWIFT_VERSION = 5.0; 346 | TARGETED_DEVICE_FAMILY = "1,2"; 347 | }; 348 | name = Debug; 349 | }; 350 | A2B0DD80283CCF5B002C5E66 /* Release */ = { 351 | isa = XCBuildConfiguration; 352 | buildSettings = { 353 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 354 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 355 | CODE_SIGN_STYLE = Automatic; 356 | CURRENT_PROJECT_VERSION = 1; 357 | DEVELOPMENT_ASSET_PATHS = "\"RouterWorkaround/Preview Content\""; 358 | DEVELOPMENT_TEAM = 5XY4M72NS4; 359 | ENABLE_PREVIEWS = YES; 360 | GENERATE_INFOPLIST_FILE = YES; 361 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 362 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 363 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 364 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 365 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 366 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 367 | LD_RUNPATH_SEARCH_PATHS = ( 368 | "$(inherited)", 369 | "@executable_path/Frameworks", 370 | ); 371 | MARKETING_VERSION = 1.0; 372 | PRODUCT_BUNDLE_IDENTIFIER = com.dlom.RouterWorkaround; 373 | PRODUCT_NAME = "$(TARGET_NAME)"; 374 | SWIFT_EMIT_LOC_STRINGS = YES; 375 | SWIFT_VERSION = 5.0; 376 | TARGETED_DEVICE_FAMILY = "1,2"; 377 | }; 378 | name = Release; 379 | }; 380 | /* End XCBuildConfiguration section */ 381 | 382 | /* Begin XCConfigurationList section */ 383 | A2B0DD6B283CCF58002C5E66 /* Build configuration list for PBXProject "RouterWorkaround" */ = { 384 | isa = XCConfigurationList; 385 | buildConfigurations = ( 386 | A2B0DD7C283CCF5B002C5E66 /* Debug */, 387 | A2B0DD7D283CCF5B002C5E66 /* Release */, 388 | ); 389 | defaultConfigurationIsVisible = 0; 390 | defaultConfigurationName = Release; 391 | }; 392 | A2B0DD7E283CCF5B002C5E66 /* Build configuration list for PBXNativeTarget "RouterWorkaround" */ = { 393 | isa = XCConfigurationList; 394 | buildConfigurations = ( 395 | A2B0DD7F283CCF5B002C5E66 /* Debug */, 396 | A2B0DD80283CCF5B002C5E66 /* Release */, 397 | ); 398 | defaultConfigurationIsVisible = 0; 399 | defaultConfigurationName = Release; 400 | }; 401 | /* End XCConfigurationList section */ 402 | }; 403 | rootObject = A2B0DD68283CCF58002C5E66 /* Project object */; 404 | } 405 | -------------------------------------------------------------------------------- /RouterWorkaround.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /RouterWorkaround.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /RouterWorkaround.xcodeproj/xcuserdata/d.lomaev.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | RouterWorkaround.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /RouterWorkaround/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 | -------------------------------------------------------------------------------- /RouterWorkaround/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 | -------------------------------------------------------------------------------- /RouterWorkaround/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /RouterWorkaround/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // RouterWorkaround 4 | // 5 | // Created by Данил Ломаев on 24.05.2022. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct ContentView: View { 11 | let screenRouter = ScreenRouter(rootScreen: .start, factory: ScreenRouterFactory()) 12 | 13 | var body: some View { 14 | MainView() 15 | .environmentObject(screenRouter) 16 | } 17 | } 18 | 19 | struct ContentView_Previews: PreviewProvider { 20 | static var previews: some View { 21 | ContentView() 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /RouterWorkaround/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /RouterWorkaround/Router/NavigationNode.swift: -------------------------------------------------------------------------------- 1 | // 2 | // NavigationNode.swift 3 | // vkdoc 4 | // 5 | // Created by Данил Ломаев on 14.04.2022. 6 | // 7 | 8 | import SwiftUI 9 | 10 | indirect enum NavigationNode: View { 11 | case view(ScreenView, context: RouterContext, pushing: NavigationNode, stack: Binding<[RouterContext]>, index: Int) 12 | case end 13 | 14 | private var isActiveBinding: Binding { 15 | switch self { 16 | case .end, .view(_, _, pushing: .end, _, _): 17 | return .constant(false) 18 | case .view(_, _, .view, let allRoutes, let index): 19 | return Binding( 20 | get: { 21 | allRoutes.wrappedValue.count > index + 1 22 | }, 23 | set: { isShowing in 24 | guard !isShowing else { return } 25 | guard allRoutes.wrappedValue.count > index + 1 else { return } 26 | allRoutes.wrappedValue = Array(allRoutes.wrappedValue.prefix(index + 1)) 27 | } 28 | ) 29 | } 30 | } 31 | 32 | private var pushedNode: NavigationNode? { 33 | switch self { 34 | case .end: 35 | return nil 36 | case .view(_, _, let pushedNode, _, _): 37 | return pushedNode 38 | } 39 | } 40 | 41 | private var routerContext: RouterContext? { 42 | switch self { 43 | case .view(_, let context, _, _, _): 44 | return context 45 | case .end: 46 | return nil 47 | } 48 | } 49 | 50 | private var sheetBinding: Binding { 51 | switch pushedNode { 52 | case .view(_, let context, _, _, _): 53 | return context.presentationType == .modal ? isActiveBinding : .constant(false) 54 | default: 55 | return .constant(false) 56 | } 57 | } 58 | 59 | private var fullCoverBinding: Binding { 60 | switch pushedNode { 61 | case .view(_, let context, _, _, _): 62 | return context.presentationType == .full ? isActiveBinding : .constant(false) 63 | default: 64 | return .constant(false) 65 | } 66 | } 67 | 68 | private var pushBinding: Binding { 69 | switch pushedNode { 70 | case .view(_, let context, _, _, _): 71 | return context.presentationType == .push ? isActiveBinding : .constant(false) 72 | default: 73 | return .constant(false) 74 | } 75 | } 76 | 77 | @ViewBuilder var viewBody: some View { 78 | let asSheet = pushedNode?.routerContext?.presentationType == .modal 79 | if case .view(let view, _, let pushedNode, _, _) = self { 80 | view 81 | .background( 82 | NavigationLink( 83 | destination: pushedNode, 84 | isActive: pushBinding, 85 | label: EmptyView.init 86 | ) 87 | .hidden() 88 | ) 89 | .present( 90 | asSheet: asSheet, 91 | isPresented: asSheet ? sheetBinding : fullCoverBinding, 92 | content: { pushedNode } 93 | ) 94 | } else { 95 | EmptyView() 96 | } 97 | } 98 | 99 | var body: some View { 100 | if routerContext?.screen.embedInNavView ?? false { 101 | NavigationView { 102 | viewBody 103 | } 104 | .navigationViewStyle(.stack) 105 | } else { 106 | viewBody 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /RouterWorkaround/Router/Router.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RouterObject.swift 3 | // vkdoc 4 | // 5 | // Created by Данил Ломаев on 15.04.2022. 6 | // 7 | 8 | import Foundation 9 | import SwiftUI 10 | 11 | enum PresentationType { 12 | case push 13 | case full 14 | case modal 15 | } 16 | 17 | protocol ScreenProtocol { 18 | var embedInNavView: Bool { get } 19 | } 20 | 21 | protocol RouterObject: AnyObject { 22 | associatedtype Screen = ScreenProtocol 23 | associatedtype Body = View 24 | 25 | func start() -> Body 26 | func navigateTo(_ screen: Screen) 27 | func presentSheet(_ screen: Screen) 28 | func presentFullScreen(_ screen: Screen) 29 | func dismissLast() 30 | func popToRoot() 31 | } 32 | 33 | struct RouterContext { 34 | let screen: ScreenType 35 | let presentationType: PresentationType 36 | } 37 | 38 | class Router: ObservableObject, RouterObject where Factory.Screen == ScreenType { 39 | 40 | @Published private var stack: [RouterContext] = [] 41 | var factory: Factory 42 | 43 | init(rootScreen: ScreenType, presentationType: PresentationType = .push, factory: Factory) { 44 | self.stack = [RouterContext(screen: rootScreen, presentationType: presentationType)] 45 | self.factory = factory 46 | } 47 | 48 | @ViewBuilder func start() -> some View { 49 | let bindingScreens = Binding(get: { 50 | return self.stack 51 | }, set: { 52 | self.stack = $0 53 | }) 54 | 55 | Routing(stack: bindingScreens) { screen in 56 | self.factory.makeBody(for: screen) 57 | } 58 | } 59 | } 60 | 61 | extension Router { 62 | func presentSheet(_ screen: ScreenType) { 63 | self.stack.append(RouterContext(screen: screen, presentationType: .modal)) 64 | } 65 | 66 | func dismissLast() { 67 | self.stack.removeLast() 68 | } 69 | 70 | func navigateTo(_ screen: ScreenType) { 71 | self.stack.append(RouterContext(screen: screen, presentationType: .push)) 72 | } 73 | 74 | func presentFullScreen(_ screen: ScreenType) { 75 | self.stack.append(RouterContext(screen: screen, presentationType: .full)) 76 | } 77 | 78 | func popToRoot() { 79 | self.stack.removeLast(self.stack.count - 1) 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /RouterWorkaround/Router/RouterFactory.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RouterFactory.swift 3 | // vkdoc 4 | // 5 | // Created by Данил Ломаев on 18.04.2022. 6 | // 7 | 8 | import Foundation 9 | import SwiftUI 10 | 11 | protocol RouterFactory { 12 | associatedtype Body: View 13 | associatedtype Screen: ScreenProtocol 14 | 15 | @ViewBuilder func makeBody(for screen: Screen) -> Self.Body 16 | } 17 | -------------------------------------------------------------------------------- /RouterWorkaround/Router/Routing.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Routing.swift 3 | // vkdoc 4 | // 5 | // Created by Данил Ломаев on 14.04.2022. 6 | // 7 | 8 | import Foundation 9 | import SwiftUI 10 | 11 | struct Routing: View { 12 | @Binding var stack: [RouterContext] 13 | @ViewBuilder var buildView: (Screen) -> ScreenView 14 | 15 | public init(stack: Binding<[RouterContext]>, @ViewBuilder buildView: @escaping (Screen) -> ScreenView) { 16 | self._stack = stack 17 | self.buildView = buildView 18 | } 19 | 20 | var body: some View { 21 | stack 22 | .enumerated() 23 | .reversed() 24 | .reduce(NavigationNode.end) { pushedNode, new in 25 | let (index, screenContext) = new 26 | 27 | return NavigationNode.view( 28 | buildView(screenContext.screen), 29 | context: screenContext, 30 | pushing: pushedNode, 31 | stack: $stack, 32 | index: index 33 | ) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /RouterWorkaround/Router/View + Cover.swift: -------------------------------------------------------------------------------- 1 | // 2 | // View + Cover.swift 3 | // vkdoc 4 | // 5 | // Created by Данил Ломаев on 18.04.2022. 6 | // 7 | 8 | import Foundation 9 | import SwiftUI 10 | 11 | extension View { 12 | @ViewBuilder func present(asSheet: Bool, isPresented: Binding, @ViewBuilder content: @escaping () -> Content) -> some View { 13 | if asSheet { 14 | self.sheet( 15 | isPresented: isPresented, 16 | onDismiss: nil, 17 | content: content 18 | ) 19 | } else { 20 | self.fullScreenCover( 21 | isPresented: isPresented, 22 | onDismiss: nil, 23 | content: content 24 | ) 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /RouterWorkaround/RouterWorkaroundApp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RouterWorkaroundApp.swift 3 | // RouterWorkaround 4 | // 5 | // Created by Данил Ломаев on 24.05.2022. 6 | // 7 | 8 | import SwiftUI 9 | 10 | @main 11 | struct RouterWorkaroundApp: App { 12 | var body: some Scene { 13 | WindowGroup { 14 | ContentView() 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /RouterWorkaround/Screens/FullScreenView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // FullScreenView.swift 3 | // RouterWorkaround 4 | // 5 | // Created by Данил Ломаев on 24.05.2022. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct FullScreenView: View { 11 | @EnvironmentObject var screenRouter: ScreenRouter 12 | 13 | var body: some View { 14 | VStack { 15 | Button("Close") { 16 | screenRouter.dismissLast() 17 | } 18 | } 19 | } 20 | } 21 | 22 | struct FullScreenView_Previews: PreviewProvider { 23 | static var previews: some View { 24 | FullScreenView() 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /RouterWorkaround/Screens/MainView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ScreenView.swift 3 | // RouterWorkaround 4 | // 5 | // Created by Данил Ломаев on 24.05.2022. 6 | // 7 | 8 | import SwiftUI 9 | 10 | enum ScreenRoute: ScreenProtocol { 11 | case start 12 | case navigator 13 | case fullScreen 14 | case sheetScreen 15 | 16 | var embedInNavView: Bool { 17 | switch self { 18 | case .start, .sheetScreen: 19 | return true 20 | case .navigator, .fullScreen: 21 | return false 22 | } 23 | } 24 | } 25 | 26 | class ScreenRouterFactory: RouterFactory { 27 | 28 | @ViewBuilder func makeBody(for screen: ScreenRoute) -> some View { 29 | switch screen { 30 | case .start: 31 | StartingView() 32 | case .navigator: 33 | RoutesView() 34 | case .fullScreen: 35 | FullScreenView() 36 | case .sheetScreen: 37 | SheetView() 38 | } 39 | } 40 | } 41 | 42 | typealias ScreenRouter = Router 43 | 44 | struct MainView: View { 45 | @EnvironmentObject var screenRouter: ScreenRouter 46 | 47 | var body: some View { 48 | screenRouter.start() 49 | } 50 | } 51 | 52 | struct ScreenView_Previews: PreviewProvider { 53 | static var previews: some View { 54 | MainView() 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /RouterWorkaround/Screens/RoutesView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RoutesView.swift 3 | // RouterWorkaround 4 | // 5 | // Created by Данил Ломаев on 24.05.2022. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct RoutesView: View { 11 | @EnvironmentObject var screenRouter: ScreenRouter 12 | 13 | var body: some View { 14 | List { 15 | Section { 16 | Button("Push") { 17 | screenRouter.navigateTo(.navigator) 18 | } 19 | } 20 | Section { 21 | Button("Sheet") { 22 | screenRouter.presentSheet(.sheetScreen) 23 | } 24 | } 25 | Section { 26 | Button("Fullscreen") { 27 | screenRouter.presentFullScreen(.fullScreen) 28 | } 29 | } 30 | Section { 31 | Button("Pop to root") { 32 | screenRouter.popToRoot() 33 | } 34 | } 35 | } 36 | } 37 | } 38 | 39 | struct RoutesView_Previews: PreviewProvider { 40 | static var previews: some View { 41 | RoutesView() 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /RouterWorkaround/Screens/SheetView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SheetView.swift 3 | // RouterWorkaround 4 | // 5 | // Created by Данил Ломаев on 24.05.2022. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct SheetView: View { 11 | @EnvironmentObject var screenRouter: ScreenRouter 12 | 13 | var body: some View { 14 | List { 15 | Section { 16 | Button("Close") { 17 | screenRouter.dismissLast() 18 | } 19 | } 20 | } 21 | .navigationTitle("Sheet") 22 | } 23 | } 24 | 25 | struct SheetView_Previews: PreviewProvider { 26 | static var previews: some View { 27 | SheetView() 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /RouterWorkaround/Screens/StartingView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StartingView.swift 3 | // RouterWorkaround 4 | // 5 | // Created by Данил Ломаев on 24.05.2022. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct StartingView: View { 11 | @EnvironmentObject var screenRouter: ScreenRouter 12 | 13 | var body: some View { 14 | RoutesView() 15 | .navigationTitle("Routing") 16 | } 17 | } 18 | 19 | struct StartingView_Previews: PreviewProvider { 20 | static var previews: some View { 21 | StartingView() 22 | } 23 | } 24 | --------------------------------------------------------------------------------