├── .gitignore ├── LICENSE ├── README.md ├── SwiftUITimeTravel.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── SwiftUITimeTravel ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ └── LaunchScreen.storyboard ├── ContentView.swift ├── Info.plist ├── Preview Content │ └── Preview Assets.xcassets │ │ └── Contents.json ├── SceneDelegate.swift ├── TimeTravelView │ ├── StateMachine.swift │ ├── Store.swift │ ├── TimeTravelBarView.swift │ └── TimeTravelView.swift └── TodoList │ ├── Internal Views │ ├── AddItemView.swift │ ├── ModalDimmingView.swift │ └── TodoListItemView.swift │ ├── Model │ ├── TodoItem.swift │ └── TodoState.swift │ └── TodoListView.swift └── image.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | # Package.resolved 41 | .build/ 42 | 43 | # CocoaPods 44 | # 45 | # We recommend against adding the Pods directory to your .gitignore. However 46 | # you should judge for yourself, the pros and cons are mentioned at: 47 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 48 | # 49 | # Pods/ 50 | # 51 | # Add this line if you want to avoid checking in source code from the Xcode workspace 52 | # *.xcworkspace 53 | 54 | # Carthage 55 | # 56 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 57 | # Carthage/Checkouts 58 | 59 | Carthage/Build 60 | 61 | # Accio dependency management 62 | Dependencies/ 63 | .accio/ 64 | 65 | # fastlane 66 | # 67 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 68 | # screenshots whenever they are needed. 69 | # For more information about the recommended setup visit: 70 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 71 | 72 | fastlane/report.xml 73 | fastlane/Preview.html 74 | fastlane/screenshots/**/*.png 75 | fastlane/test_output 76 | 77 | # Code Injection 78 | # 79 | # After new code Injection tools there's a generated folder /iOSInjectionProject 80 | # https://github.com/johnno1962/injectionforxcode 81 | 82 | iOSInjectionProject/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Tim Donnelly 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 | # SwiftUI Time Travel 2 | 3 | A SwiftUI state store and view that allow you to scrub through an application's state. 4 | 5 | This is a super rough prototype: it's only meant to serve as an example of what can be done using SwiftUI. 6 | 7 | ![animated gif](image.gif) 8 | 9 | 10 | ----- 11 | 12 | ### How this works 13 | 14 | Any view hierarchy can use time travel: in the provided todo app, `ContentView` adds time travel like this: 15 | 16 | ```swift 17 | struct ContentView : View { 18 | var body: some View { 19 | TimeTravelView(initialState: TodoState()) { 20 | TodoListView() 21 | } 22 | } 23 | } 24 | ``` 25 | 26 | Then you can use `@EnvironmentObject` to access the state store from views within the hierarchy. 27 | 28 | ```swift 29 | struct MyView: View { 30 | 31 | @EnvironmentObject var store: Store 32 | 33 | var body: some View { 34 | Text("Count: \(store.state.todoItems.count)") 35 | } 36 | } 37 | ``` 38 | 39 | To update your application's state, use the `dispatch` method on the store: 40 | 41 | ```swift 42 | struct MyView: View { 43 | 44 | @EnvironmentObject var store: Store 45 | 46 | var body: some View { 47 | VStack(spacing: 10) { 48 | Text("Count: \(store.state.todoItems.count)") 49 | HStack(spacing: 10) { 50 | Button(action: { self.store.dispatch(event: .someEventName) }) { 51 | Text("Do Something") 52 | } 53 | } 54 | } 55 | } 56 | } 57 | ``` 58 | 59 | For time travel to work, all state must be accessed like this. Any state that is stored outside of the single `Store` will not be controlled by the time travel view. This means that local view state (using `@State`) should be used sparingly (if at all). 60 | 61 | Internally, the store keeps a stack of all states that have occured – the slider controls which of those states is used as the active (current) state. 62 | -------------------------------------------------------------------------------- /SwiftUITimeTravel.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 22316DC622A7B255004124F2 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DC522A7B255004124F2 /* AppDelegate.swift */; }; 11 | 22316DC822A7B255004124F2 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DC722A7B255004124F2 /* SceneDelegate.swift */; }; 12 | 22316DCA22A7B255004124F2 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DC922A7B255004124F2 /* ContentView.swift */; }; 13 | 22316DCC22A7B255004124F2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 22316DCB22A7B255004124F2 /* Assets.xcassets */; }; 14 | 22316DCF22A7B255004124F2 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 22316DCE22A7B255004124F2 /* Preview Assets.xcassets */; }; 15 | 22316DD222A7B255004124F2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 22316DD022A7B255004124F2 /* LaunchScreen.storyboard */; }; 16 | 22316DE722A7B287004124F2 /* TodoListItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DE022A7B263004124F2 /* TodoListItemView.swift */; }; 17 | 22316DE822A7B287004124F2 /* ModalDimmingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DE122A7B263004124F2 /* ModalDimmingView.swift */; }; 18 | 22316DE922A7B287004124F2 /* AddItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DE222A7B263004124F2 /* AddItemView.swift */; }; 19 | 22316DEA22A7B28B004124F2 /* TodoItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DE422A7B263004124F2 /* TodoItem.swift */; }; 20 | 22316DEB22A7B28B004124F2 /* TodoState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DE522A7B263004124F2 /* TodoState.swift */; }; 21 | 22316DEC22A7B28D004124F2 /* TodoListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DE622A7B263004124F2 /* TodoListView.swift */; }; 22 | 22316DED22A7B291004124F2 /* StateMachine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DDA22A7B263004124F2 /* StateMachine.swift */; }; 23 | 22316DEE22A7B291004124F2 /* TimeTravelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DDB22A7B263004124F2 /* TimeTravelView.swift */; }; 24 | 22316DEF22A7B291004124F2 /* Store.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DDC22A7B263004124F2 /* Store.swift */; }; 25 | 22316DF022A7B291004124F2 /* TimeTravelBarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22316DDD22A7B263004124F2 /* TimeTravelBarView.swift */; }; 26 | /* End PBXBuildFile section */ 27 | 28 | /* Begin PBXFileReference section */ 29 | 22316DC222A7B255004124F2 /* SwiftUITimeTravel.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUITimeTravel.app; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | 22316DC522A7B255004124F2 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 31 | 22316DC722A7B255004124F2 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 32 | 22316DC922A7B255004124F2 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 33 | 22316DCB22A7B255004124F2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 34 | 22316DCE22A7B255004124F2 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 35 | 22316DD122A7B255004124F2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 36 | 22316DD322A7B255004124F2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 37 | 22316DDA22A7B263004124F2 /* StateMachine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateMachine.swift; sourceTree = ""; }; 38 | 22316DDB22A7B263004124F2 /* TimeTravelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeTravelView.swift; sourceTree = ""; }; 39 | 22316DDC22A7B263004124F2 /* Store.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Store.swift; sourceTree = ""; }; 40 | 22316DDD22A7B263004124F2 /* TimeTravelBarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeTravelBarView.swift; sourceTree = ""; }; 41 | 22316DE022A7B263004124F2 /* TodoListItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListItemView.swift; sourceTree = ""; }; 42 | 22316DE122A7B263004124F2 /* ModalDimmingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalDimmingView.swift; sourceTree = ""; }; 43 | 22316DE222A7B263004124F2 /* AddItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddItemView.swift; sourceTree = ""; }; 44 | 22316DE422A7B263004124F2 /* TodoItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoItem.swift; sourceTree = ""; }; 45 | 22316DE522A7B263004124F2 /* TodoState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoState.swift; sourceTree = ""; }; 46 | 22316DE622A7B263004124F2 /* TodoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListView.swift; sourceTree = ""; }; 47 | 22316DF122A7B3FB004124F2 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 48 | /* End PBXFileReference section */ 49 | 50 | /* Begin PBXFrameworksBuildPhase section */ 51 | 22316DBF22A7B255004124F2 /* Frameworks */ = { 52 | isa = PBXFrameworksBuildPhase; 53 | buildActionMask = 2147483647; 54 | files = ( 55 | ); 56 | runOnlyForDeploymentPostprocessing = 0; 57 | }; 58 | /* End PBXFrameworksBuildPhase section */ 59 | 60 | /* Begin PBXGroup section */ 61 | 22316DB922A7B255004124F2 = { 62 | isa = PBXGroup; 63 | children = ( 64 | 22316DF122A7B3FB004124F2 /* README.md */, 65 | 22316DC422A7B255004124F2 /* SwiftUITimeTravel */, 66 | 22316DC322A7B255004124F2 /* Products */, 67 | ); 68 | sourceTree = ""; 69 | }; 70 | 22316DC322A7B255004124F2 /* Products */ = { 71 | isa = PBXGroup; 72 | children = ( 73 | 22316DC222A7B255004124F2 /* SwiftUITimeTravel.app */, 74 | ); 75 | name = Products; 76 | sourceTree = ""; 77 | }; 78 | 22316DC422A7B255004124F2 /* SwiftUITimeTravel */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | 22316DD922A7B263004124F2 /* TimeTravelView */, 82 | 22316DDE22A7B263004124F2 /* TodoList */, 83 | 22316DC522A7B255004124F2 /* AppDelegate.swift */, 84 | 22316DC722A7B255004124F2 /* SceneDelegate.swift */, 85 | 22316DC922A7B255004124F2 /* ContentView.swift */, 86 | 22316DCB22A7B255004124F2 /* Assets.xcassets */, 87 | 22316DD022A7B255004124F2 /* LaunchScreen.storyboard */, 88 | 22316DD322A7B255004124F2 /* Info.plist */, 89 | 22316DCD22A7B255004124F2 /* Preview Content */, 90 | ); 91 | path = SwiftUITimeTravel; 92 | sourceTree = ""; 93 | }; 94 | 22316DCD22A7B255004124F2 /* Preview Content */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 22316DCE22A7B255004124F2 /* Preview Assets.xcassets */, 98 | ); 99 | path = "Preview Content"; 100 | sourceTree = ""; 101 | }; 102 | 22316DD922A7B263004124F2 /* TimeTravelView */ = { 103 | isa = PBXGroup; 104 | children = ( 105 | 22316DDA22A7B263004124F2 /* StateMachine.swift */, 106 | 22316DDB22A7B263004124F2 /* TimeTravelView.swift */, 107 | 22316DDC22A7B263004124F2 /* Store.swift */, 108 | 22316DDD22A7B263004124F2 /* TimeTravelBarView.swift */, 109 | ); 110 | path = TimeTravelView; 111 | sourceTree = ""; 112 | }; 113 | 22316DDE22A7B263004124F2 /* TodoList */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | 22316DDF22A7B263004124F2 /* Internal Views */, 117 | 22316DE322A7B263004124F2 /* Model */, 118 | 22316DE622A7B263004124F2 /* TodoListView.swift */, 119 | ); 120 | path = TodoList; 121 | sourceTree = ""; 122 | }; 123 | 22316DDF22A7B263004124F2 /* Internal Views */ = { 124 | isa = PBXGroup; 125 | children = ( 126 | 22316DE022A7B263004124F2 /* TodoListItemView.swift */, 127 | 22316DE122A7B263004124F2 /* ModalDimmingView.swift */, 128 | 22316DE222A7B263004124F2 /* AddItemView.swift */, 129 | ); 130 | path = "Internal Views"; 131 | sourceTree = ""; 132 | }; 133 | 22316DE322A7B263004124F2 /* Model */ = { 134 | isa = PBXGroup; 135 | children = ( 136 | 22316DE422A7B263004124F2 /* TodoItem.swift */, 137 | 22316DE522A7B263004124F2 /* TodoState.swift */, 138 | ); 139 | path = Model; 140 | sourceTree = ""; 141 | }; 142 | /* End PBXGroup section */ 143 | 144 | /* Begin PBXNativeTarget section */ 145 | 22316DC122A7B255004124F2 /* SwiftUITimeTravel */ = { 146 | isa = PBXNativeTarget; 147 | buildConfigurationList = 22316DD622A7B255004124F2 /* Build configuration list for PBXNativeTarget "SwiftUITimeTravel" */; 148 | buildPhases = ( 149 | 22316DBE22A7B255004124F2 /* Sources */, 150 | 22316DBF22A7B255004124F2 /* Frameworks */, 151 | 22316DC022A7B255004124F2 /* Resources */, 152 | ); 153 | buildRules = ( 154 | ); 155 | dependencies = ( 156 | ); 157 | name = SwiftUITimeTravel; 158 | productName = SwiftUITimeTravel; 159 | productReference = 22316DC222A7B255004124F2 /* SwiftUITimeTravel.app */; 160 | productType = "com.apple.product-type.application"; 161 | }; 162 | /* End PBXNativeTarget section */ 163 | 164 | /* Begin PBXProject section */ 165 | 22316DBA22A7B255004124F2 /* Project object */ = { 166 | isa = PBXProject; 167 | attributes = { 168 | LastSwiftUpdateCheck = 1100; 169 | LastUpgradeCheck = 1100; 170 | ORGANIZATIONNAME = "Tim Donnelly"; 171 | TargetAttributes = { 172 | 22316DC122A7B255004124F2 = { 173 | CreatedOnToolsVersion = 11.0; 174 | }; 175 | }; 176 | }; 177 | buildConfigurationList = 22316DBD22A7B255004124F2 /* Build configuration list for PBXProject "SwiftUITimeTravel" */; 178 | compatibilityVersion = "Xcode 9.3"; 179 | developmentRegion = en; 180 | hasScannedForEncodings = 0; 181 | knownRegions = ( 182 | en, 183 | Base, 184 | ); 185 | mainGroup = 22316DB922A7B255004124F2; 186 | productRefGroup = 22316DC322A7B255004124F2 /* Products */; 187 | projectDirPath = ""; 188 | projectRoot = ""; 189 | targets = ( 190 | 22316DC122A7B255004124F2 /* SwiftUITimeTravel */, 191 | ); 192 | }; 193 | /* End PBXProject section */ 194 | 195 | /* Begin PBXResourcesBuildPhase section */ 196 | 22316DC022A7B255004124F2 /* Resources */ = { 197 | isa = PBXResourcesBuildPhase; 198 | buildActionMask = 2147483647; 199 | files = ( 200 | 22316DD222A7B255004124F2 /* LaunchScreen.storyboard in Resources */, 201 | 22316DCF22A7B255004124F2 /* Preview Assets.xcassets in Resources */, 202 | 22316DCC22A7B255004124F2 /* Assets.xcassets in Resources */, 203 | ); 204 | runOnlyForDeploymentPostprocessing = 0; 205 | }; 206 | /* End PBXResourcesBuildPhase section */ 207 | 208 | /* Begin PBXSourcesBuildPhase section */ 209 | 22316DBE22A7B255004124F2 /* Sources */ = { 210 | isa = PBXSourcesBuildPhase; 211 | buildActionMask = 2147483647; 212 | files = ( 213 | 22316DEA22A7B28B004124F2 /* TodoItem.swift in Sources */, 214 | 22316DEE22A7B291004124F2 /* TimeTravelView.swift in Sources */, 215 | 22316DEB22A7B28B004124F2 /* TodoState.swift in Sources */, 216 | 22316DEF22A7B291004124F2 /* Store.swift in Sources */, 217 | 22316DC622A7B255004124F2 /* AppDelegate.swift in Sources */, 218 | 22316DED22A7B291004124F2 /* StateMachine.swift in Sources */, 219 | 22316DE922A7B287004124F2 /* AddItemView.swift in Sources */, 220 | 22316DF022A7B291004124F2 /* TimeTravelBarView.swift in Sources */, 221 | 22316DE722A7B287004124F2 /* TodoListItemView.swift in Sources */, 222 | 22316DEC22A7B28D004124F2 /* TodoListView.swift in Sources */, 223 | 22316DE822A7B287004124F2 /* ModalDimmingView.swift in Sources */, 224 | 22316DC822A7B255004124F2 /* SceneDelegate.swift in Sources */, 225 | 22316DCA22A7B255004124F2 /* ContentView.swift in Sources */, 226 | ); 227 | runOnlyForDeploymentPostprocessing = 0; 228 | }; 229 | /* End PBXSourcesBuildPhase section */ 230 | 231 | /* Begin PBXVariantGroup section */ 232 | 22316DD022A7B255004124F2 /* LaunchScreen.storyboard */ = { 233 | isa = PBXVariantGroup; 234 | children = ( 235 | 22316DD122A7B255004124F2 /* Base */, 236 | ); 237 | name = LaunchScreen.storyboard; 238 | sourceTree = ""; 239 | }; 240 | /* End PBXVariantGroup section */ 241 | 242 | /* Begin XCBuildConfiguration section */ 243 | 22316DD422A7B255004124F2 /* Debug */ = { 244 | isa = XCBuildConfiguration; 245 | buildSettings = { 246 | ALWAYS_SEARCH_USER_PATHS = NO; 247 | CLANG_ANALYZER_NONNULL = YES; 248 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 249 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 250 | CLANG_CXX_LIBRARY = "libc++"; 251 | CLANG_ENABLE_MODULES = YES; 252 | CLANG_ENABLE_OBJC_ARC = YES; 253 | CLANG_ENABLE_OBJC_WEAK = YES; 254 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 255 | CLANG_WARN_BOOL_CONVERSION = YES; 256 | CLANG_WARN_COMMA = YES; 257 | CLANG_WARN_CONSTANT_CONVERSION = YES; 258 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 259 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 260 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 261 | CLANG_WARN_EMPTY_BODY = YES; 262 | CLANG_WARN_ENUM_CONVERSION = YES; 263 | CLANG_WARN_INFINITE_RECURSION = YES; 264 | CLANG_WARN_INT_CONVERSION = YES; 265 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 266 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 267 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 268 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 269 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 270 | CLANG_WARN_STRICT_PROTOTYPES = YES; 271 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 272 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 273 | CLANG_WARN_UNREACHABLE_CODE = YES; 274 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 275 | COPY_PHASE_STRIP = NO; 276 | DEBUG_INFORMATION_FORMAT = dwarf; 277 | ENABLE_STRICT_OBJC_MSGSEND = YES; 278 | ENABLE_TESTABILITY = YES; 279 | GCC_C_LANGUAGE_STANDARD = gnu11; 280 | GCC_DYNAMIC_NO_PIC = NO; 281 | GCC_NO_COMMON_BLOCKS = YES; 282 | GCC_OPTIMIZATION_LEVEL = 0; 283 | GCC_PREPROCESSOR_DEFINITIONS = ( 284 | "DEBUG=1", 285 | "$(inherited)", 286 | ); 287 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 288 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 289 | GCC_WARN_UNDECLARED_SELECTOR = YES; 290 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 291 | GCC_WARN_UNUSED_FUNCTION = YES; 292 | GCC_WARN_UNUSED_VARIABLE = YES; 293 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 294 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 295 | MTL_FAST_MATH = YES; 296 | ONLY_ACTIVE_ARCH = YES; 297 | SDKROOT = iphoneos; 298 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 299 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 300 | }; 301 | name = Debug; 302 | }; 303 | 22316DD522A7B255004124F2 /* Release */ = { 304 | isa = XCBuildConfiguration; 305 | buildSettings = { 306 | ALWAYS_SEARCH_USER_PATHS = NO; 307 | CLANG_ANALYZER_NONNULL = YES; 308 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 309 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 310 | CLANG_CXX_LIBRARY = "libc++"; 311 | CLANG_ENABLE_MODULES = YES; 312 | CLANG_ENABLE_OBJC_ARC = YES; 313 | CLANG_ENABLE_OBJC_WEAK = YES; 314 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 315 | CLANG_WARN_BOOL_CONVERSION = YES; 316 | CLANG_WARN_COMMA = YES; 317 | CLANG_WARN_CONSTANT_CONVERSION = YES; 318 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 319 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 320 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 321 | CLANG_WARN_EMPTY_BODY = YES; 322 | CLANG_WARN_ENUM_CONVERSION = YES; 323 | CLANG_WARN_INFINITE_RECURSION = YES; 324 | CLANG_WARN_INT_CONVERSION = YES; 325 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 326 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 327 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 328 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 329 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 330 | CLANG_WARN_STRICT_PROTOTYPES = YES; 331 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 332 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 333 | CLANG_WARN_UNREACHABLE_CODE = YES; 334 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 335 | COPY_PHASE_STRIP = NO; 336 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 337 | ENABLE_NS_ASSERTIONS = NO; 338 | ENABLE_STRICT_OBJC_MSGSEND = YES; 339 | GCC_C_LANGUAGE_STANDARD = gnu11; 340 | GCC_NO_COMMON_BLOCKS = YES; 341 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 342 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 343 | GCC_WARN_UNDECLARED_SELECTOR = YES; 344 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 345 | GCC_WARN_UNUSED_FUNCTION = YES; 346 | GCC_WARN_UNUSED_VARIABLE = YES; 347 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 348 | MTL_ENABLE_DEBUG_INFO = NO; 349 | MTL_FAST_MATH = YES; 350 | SDKROOT = iphoneos; 351 | SWIFT_COMPILATION_MODE = wholemodule; 352 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 353 | VALIDATE_PRODUCT = YES; 354 | }; 355 | name = Release; 356 | }; 357 | 22316DD722A7B255004124F2 /* Debug */ = { 358 | isa = XCBuildConfiguration; 359 | buildSettings = { 360 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 361 | CODE_SIGN_STYLE = Automatic; 362 | DEVELOPMENT_ASSET_PATHS = "SwiftUITimeTravel/Preview\\ Content"; 363 | DEVELOPMENT_TEAM = MM763NMPEA; 364 | ENABLE_PREVIEWS = YES; 365 | INFOPLIST_FILE = SwiftUITimeTravel/Info.plist; 366 | LD_RUNPATH_SEARCH_PATHS = ( 367 | "$(inherited)", 368 | "@executable_path/Frameworks", 369 | ); 370 | PRODUCT_BUNDLE_IDENTIFIER = com.tdonnelly.SwiftUITimeTravel; 371 | PRODUCT_NAME = "$(TARGET_NAME)"; 372 | SWIFT_VERSION = 5.0; 373 | TARGETED_DEVICE_FAMILY = "1,2"; 374 | }; 375 | name = Debug; 376 | }; 377 | 22316DD822A7B255004124F2 /* Release */ = { 378 | isa = XCBuildConfiguration; 379 | buildSettings = { 380 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 381 | CODE_SIGN_STYLE = Automatic; 382 | DEVELOPMENT_ASSET_PATHS = "SwiftUITimeTravel/Preview\\ Content"; 383 | DEVELOPMENT_TEAM = MM763NMPEA; 384 | ENABLE_PREVIEWS = YES; 385 | INFOPLIST_FILE = SwiftUITimeTravel/Info.plist; 386 | LD_RUNPATH_SEARCH_PATHS = ( 387 | "$(inherited)", 388 | "@executable_path/Frameworks", 389 | ); 390 | PRODUCT_BUNDLE_IDENTIFIER = com.tdonnelly.SwiftUITimeTravel; 391 | PRODUCT_NAME = "$(TARGET_NAME)"; 392 | SWIFT_VERSION = 5.0; 393 | TARGETED_DEVICE_FAMILY = "1,2"; 394 | }; 395 | name = Release; 396 | }; 397 | /* End XCBuildConfiguration section */ 398 | 399 | /* Begin XCConfigurationList section */ 400 | 22316DBD22A7B255004124F2 /* Build configuration list for PBXProject "SwiftUITimeTravel" */ = { 401 | isa = XCConfigurationList; 402 | buildConfigurations = ( 403 | 22316DD422A7B255004124F2 /* Debug */, 404 | 22316DD522A7B255004124F2 /* Release */, 405 | ); 406 | defaultConfigurationIsVisible = 0; 407 | defaultConfigurationName = Release; 408 | }; 409 | 22316DD622A7B255004124F2 /* Build configuration list for PBXNativeTarget "SwiftUITimeTravel" */ = { 410 | isa = XCConfigurationList; 411 | buildConfigurations = ( 412 | 22316DD722A7B255004124F2 /* Debug */, 413 | 22316DD822A7B255004124F2 /* Release */, 414 | ); 415 | defaultConfigurationIsVisible = 0; 416 | defaultConfigurationName = Release; 417 | }; 418 | /* End XCConfigurationList section */ 419 | }; 420 | rootObject = 22316DBA22A7B255004124F2 /* Project object */; 421 | } 422 | -------------------------------------------------------------------------------- /SwiftUITimeTravel.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftUITimeTravel.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | @UIApplicationMain 4 | class AppDelegate: UIResponder, UIApplicationDelegate { 5 | 6 | 7 | 8 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 9 | // Override point for customization after application launch. 10 | return true 11 | } 12 | 13 | func applicationWillTerminate(_ application: UIApplication) { 14 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 15 | } 16 | 17 | // MARK: UISceneSession Lifecycle 18 | 19 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 20 | // Called when a new scene session is being created. 21 | // Use this method to select a configuration to create the new scene with. 22 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 23 | } 24 | 25 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 26 | // Called when the user discards a scene session. 27 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 28 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 29 | } 30 | 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /SwiftUITimeTravel/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SwiftUITimeTravel/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/ContentView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct ContentView : View { 4 | var body: some View { 5 | TimeTravelView(initialState: TodoState()) { 6 | TodoListView() 7 | } 8 | } 9 | } 10 | 11 | #if DEBUG 12 | struct ContentView_Previews : PreviewProvider { 13 | static var previews: some View { 14 | ContentView() 15 | } 16 | } 17 | #endif 18 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/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 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UILaunchStoryboardName 33 | LaunchScreen 34 | UISceneConfigurationName 35 | Default Configuration 36 | UISceneDelegateClassName 37 | $(PRODUCT_MODULE_NAME).SceneDelegate 38 | 39 | 40 | 41 | 42 | UILaunchStoryboardName 43 | LaunchScreen 44 | UIRequiredDeviceCapabilities 45 | 46 | armv7 47 | 48 | UISupportedInterfaceOrientations 49 | 50 | UIInterfaceOrientationPortrait 51 | UIInterfaceOrientationLandscapeLeft 52 | UIInterfaceOrientationLandscapeRight 53 | 54 | UISupportedInterfaceOrientations~ipad 55 | 56 | UIInterfaceOrientationPortrait 57 | UIInterfaceOrientationPortraitUpsideDown 58 | UIInterfaceOrientationLandscapeLeft 59 | UIInterfaceOrientationLandscapeRight 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SwiftUITimeTravel/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import SwiftUI 3 | 4 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 5 | 6 | var window: UIWindow? 7 | 8 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 9 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 10 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 11 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 12 | 13 | if let windowScene = scene as? UIWindowScene { 14 | let window = UIWindow(windowScene: windowScene) 15 | 16 | window.rootViewController = UIHostingController( 17 | rootView: ContentView() 18 | ) 19 | 20 | self.window = window 21 | window.makeKeyAndVisible() 22 | } 23 | } 24 | 25 | func sceneDidDisconnect(_ scene: UIScene) { 26 | // Called as the scene is being released by the system. 27 | // This occurs shortly after the scene enters the background, or when its session is discarded. 28 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 29 | // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). 30 | } 31 | 32 | func sceneDidBecomeActive(_ scene: UIScene) { 33 | // Called when the scene has moved from an inactive state to an active state. 34 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 35 | } 36 | 37 | func sceneWillResignActive(_ scene: UIScene) { 38 | // Called when the scene will move from an active state to an inactive state. 39 | // This may occur due to temporary interruptions (ex. an incoming phone call). 40 | } 41 | 42 | func sceneWillEnterForeground(_ scene: UIScene) { 43 | // Called as the scene transitions from the background to the foreground. 44 | // Use this method to undo the changes made on entering the background. 45 | } 46 | 47 | func sceneDidEnterBackground(_ scene: UIScene) { 48 | // Called as the scene transitions from the foreground to the background. 49 | // Use this method to save data, release shared resources, and store enough scene-specific state information 50 | // to restore the scene back to its current state. 51 | } 52 | 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TimeTravelView/StateMachine.swift: -------------------------------------------------------------------------------- 1 | /// Conforming types serve as the state of a time travelable application 2 | public protocol StateMachine { 3 | 4 | /// Events define things that can happen within your application that change its state. 5 | /// 6 | /// This might include things like text editing, button taps, or network responses. 7 | associatedtype Event 8 | 9 | /// Applies an event to the current state. 10 | mutating func update(with event: Event) 11 | } 12 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TimeTravelView/Store.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | import Combine 3 | 4 | public final class Store: ObservableObject where StateType: StateMachine { 5 | 6 | private let initialState: StateType 7 | private var subsequentStates: [StateType] = [] 8 | 9 | public let objectWillChange = PassthroughSubject() 10 | 11 | public init(state: StateType) { 12 | initialState = state 13 | } 14 | 15 | var allStates: [StateType] { 16 | [[initialState], subsequentStates].flatMap({ $0 }) 17 | } 18 | 19 | var stateCount: Int { 20 | 1 + subsequentStates.count 21 | } 22 | 23 | var currentStateIndex: Int = 0 { 24 | willSet { 25 | withAnimation { 26 | objectWillChange.send(()) 27 | } 28 | } 29 | } 30 | 31 | /// The current state of the store. This will update as time traveling occurs. 32 | public var state: StateType { 33 | allStates[currentStateIndex] 34 | } 35 | 36 | /// Dispatches an event to be applied to the current state. 37 | public func dispatch(event: StateType.Event) { 38 | var newState = state 39 | newState.update(with: event) 40 | subsequentStates.append(newState) 41 | currentStateIndex = stateCount - 1 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TimeTravelView/TimeTravelBarView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct TimeTravelBarView : View { 4 | 5 | @EnvironmentObject var store: Store 6 | 7 | var body: some View { 8 | let indexBinding = Binding( 9 | get: { Double(self.store.currentStateIndex) }, 10 | set: { self.store.currentStateIndex = Int($0) }) 11 | 12 | return Slider(value: indexBinding, in: 0 ... Double(store.stateCount-1)) 13 | .background(Color.white) 14 | .frame(height: 44.0, alignment: .bottom) 15 | .padding() 16 | } 17 | } 18 | 19 | #if DEBUG 20 | struct TimeTravelBarView_Previews : PreviewProvider { 21 | static var previews: some View { 22 | TimeTravelBarView() 23 | } 24 | } 25 | #endif 26 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TimeTravelView/TimeTravelView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public struct TimeTravelView: View where StateType: StateMachine, Content: View { 4 | 5 | let initialState: StateType 6 | 7 | private let content: Content 8 | 9 | @State var store: Store? = nil 10 | 11 | public init(initialState: StateType, content: () -> Content) { 12 | self.initialState = initialState 13 | self.content = content() 14 | } 15 | 16 | public var body: some View { 17 | 18 | let store = self.store ?? Store(state: initialState) 19 | if (self.store == nil) { 20 | self.store = store 21 | } 22 | 23 | return VStack { 24 | content 25 | TimeTravelBarView() 26 | } 27 | .environmentObject(store) 28 | } 29 | } 30 | 31 | #if DEBUG 32 | struct TimeTravelView_Previews : PreviewProvider { 33 | static var previews: some View { 34 | TimeTravelView(initialState: TodoState()) { 35 | TodoListView() 36 | } 37 | } 38 | } 39 | #endif 40 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TodoList/Internal Views/AddItemView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct AddItemView: View { 4 | 5 | @EnvironmentObject var store: Store 6 | 7 | var body: some View { 8 | 9 | let textBinding = Binding( 10 | get: { self.store.state.partialItemName }, 11 | set: { self.store.dispatch(event: .changePartialItemName($0)) }) 12 | 13 | return VStack(spacing: 16) { 14 | TextField("Title", text: textBinding) 15 | Button(action: { 16 | self.store.dispatch(event: .addItem) 17 | }) { 18 | HStack { 19 | Spacer() 20 | Text("Add").padding([.top, .bottom], 8.0) 21 | Spacer() 22 | } 23 | 24 | } 25 | .background(Color.accentColor) 26 | .disabled(store.state.partialItemName.isEmpty) 27 | .foregroundColor(.white) 28 | .cornerRadius(8.0) 29 | } 30 | .padding() 31 | } 32 | } 33 | #if DEBUG 34 | struct AddItemView_Previews : PreviewProvider { 35 | static var previews: some View { 36 | AddItemView() 37 | } 38 | } 39 | #endif 40 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TodoList/Internal Views/ModalDimmingView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct ModalDimmingView : View { 4 | 5 | @EnvironmentObject var store: Store 6 | 7 | var body: some View { 8 | Color 9 | .black 10 | .opacity(0.3) 11 | .edgesIgnoringSafeArea([.bottom, .top]) 12 | .transition(.opacity) 13 | .onTapGesture { 14 | self.store.dispatch(event: .cancelCreatingItem) 15 | } 16 | } 17 | } 18 | 19 | #if DEBUG 20 | struct ModalDimmingView_Previews : PreviewProvider { 21 | static var previews: some View { 22 | ModalDimmingView() 23 | } 24 | } 25 | #endif 26 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TodoList/Internal Views/TodoListItemView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct TodoListItemView : View { 4 | 5 | @EnvironmentObject var store: Store 6 | 7 | let item: TodoItem 8 | 9 | var body: some View { 10 | let binding = Binding( 11 | get: { self.item.isFinished }, 12 | set: { self.store.dispatch(event: .setItemDone(identifier: self.item.id, isDone: $0)) }) 13 | 14 | return Toggle(isOn: binding) { 15 | Text(item.title) 16 | } 17 | } 18 | } 19 | 20 | #if DEBUG 21 | struct TodoListItemView_Previews : PreviewProvider { 22 | static var previews: some View { 23 | TodoListItemView(item: TodoItem(id: UUID(), title: "Test", isFinished: false)) 24 | } 25 | } 26 | #endif 27 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TodoList/Model/TodoItem.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct TodoItem: Identifiable { 4 | var id: UUID 5 | var title: String 6 | var isFinished: Bool 7 | } 8 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TodoList/Model/TodoState.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct TodoState { 4 | var isCreatingItem: Bool = false 5 | var partialItemName: String = "" 6 | var todoItems: [TodoItem] = [] 7 | } 8 | 9 | extension TodoState: StateMachine { 10 | 11 | enum Event { 12 | case startCreatingItem 13 | case cancelCreatingItem 14 | case changePartialItemName(String) 15 | case addItem 16 | case setItemDone(identifier: UUID, isDone: Bool) 17 | } 18 | 19 | mutating func update(with event: TodoState.Event) { 20 | switch event { 21 | case .addItem: 22 | todoItems.append(TodoItem(id: UUID(), title: partialItemName, isFinished: false)) 23 | partialItemName = "" 24 | isCreatingItem = false 25 | case .changePartialItemName(let name): 26 | partialItemName = name 27 | case .cancelCreatingItem: 28 | isCreatingItem = false 29 | case .startCreatingItem: 30 | isCreatingItem = true 31 | partialItemName = "" 32 | case .setItemDone(let identifier, let isDone): 33 | if let index = todoItems.firstIndex(where: { $0.id == identifier }) { 34 | todoItems[index].isFinished = isDone 35 | } 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /SwiftUITimeTravel/TodoList/TodoListView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct TodoListView : View { 4 | 5 | @EnvironmentObject var store: Store 6 | 7 | var body: some View { 8 | ZStack { 9 | NavigationView { 10 | List(store.state.todoItems) { item in TodoListItemView(item: item) } 11 | .navigationBarTitle(Text("Todo List")) 12 | .navigationBarItems(trailing: Button(action: { 13 | withAnimation { 14 | self.store.dispatch(event: .startCreatingItem) 15 | } 16 | }, label: { Image(systemName: "plus.circle") })) } 17 | 18 | if store.state.isCreatingItem { 19 | 20 | ModalDimmingView() 21 | 22 | VStack { 23 | Spacer() 24 | AddItemView() 25 | .background(Color.white) 26 | .cornerRadius(12.0) 27 | .shadow(radius: 16.0) 28 | .padding() 29 | Spacer() 30 | } 31 | .transition(.move(edge: .bottom)) 32 | } 33 | } 34 | } 35 | } 36 | #if DEBUG 37 | struct TodoListView_Previews : PreviewProvider { 38 | static var previews: some View { 39 | TodoListView() 40 | } 41 | } 42 | #endif 43 | -------------------------------------------------------------------------------- /image.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/timdonnelly/SwiftUITimeTravel/337fe5cdfd2390ba3e458a50978847b6853c7e5b/image.gif --------------------------------------------------------------------------------