├── LICENSE ├── README.md ├── SwiftUIListExample.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcuserdata │ └── andersforssell.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── SwiftUIListExample ├── AppDelegate.swift ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj └── LaunchScreen.storyboard ├── DynamicListView.swift ├── Info.plist ├── Preview Content └── Preview Assets.xcassets │ └── Contents.json └── SceneDelegate.swift /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Callista Enterprise AB 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 | # SwiftUIListExample 2 | Sample code to complement an article about how to create Dynamic Lists With Asynchronous Data Loading in SwiftUI. 3 | You find the article here: [Callista Blog - Dynamic Lists With Asynchronous Data Loading](https://callistaenterprise.se/blogg/teknik/2020/04/20/swiftui-lists/) 4 | 5 | It demonstrates how to create a list that meets the following requirements. 6 | 7 | 8 | 9 | 1. The list should grow dynamically and batch-wise as the user scrolls 10 | 2. The data on each row is fetched from a (possibly) slow data source - must be possible to be performed asynchronously in the background 11 | 3. Some row data should be animated in two different situations: a) when data has been fetched and b) whenever a row becomes visible 12 | 4. Possibility to reset the list and reload data 13 | 5. Smooth scrolling throughout 14 | 15 | Below is a simple video that shows an example of how the list should work. 16 | 17 | [![Video](https://callistaenterprise.se/assets/blogg/swiftui/RPReplay_Final1587392731.png)](https://callistaenterprise.se/assets/blogg/swiftui/RPReplay_Final1587392731.mov " ") 18 | -------------------------------------------------------------------------------- /SwiftUIListExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 14D31288244E212100558B62 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14D31287244E212100558B62 /* AppDelegate.swift */; }; 11 | 14D3128A244E212100558B62 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14D31289244E212100558B62 /* SceneDelegate.swift */; }; 12 | 14D3128C244E212100558B62 /* DynamicListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14D3128B244E212100558B62 /* DynamicListView.swift */; }; 13 | 14D3128E244E212400558B62 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 14D3128D244E212400558B62 /* Assets.xcassets */; }; 14 | 14D31291244E212400558B62 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 14D31290244E212400558B62 /* Preview Assets.xcassets */; }; 15 | 14D31294244E212400558B62 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 14D31292244E212400558B62 /* LaunchScreen.storyboard */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 14D31284244E212100558B62 /* SwiftUIListExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUIListExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 14D31287244E212100558B62 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | 14D31289244E212100558B62 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 22 | 14D3128B244E212100558B62 /* DynamicListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicListView.swift; sourceTree = ""; }; 23 | 14D3128D244E212400558B62 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | 14D31290244E212400558B62 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 25 | 14D31293244E212400558B62 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 26 | 14D31295244E212400558B62 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 14D31281244E212100558B62 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | 14D3127B244E212100558B62 = { 41 | isa = PBXGroup; 42 | children = ( 43 | 14D31286244E212100558B62 /* SwiftUIListExample */, 44 | 14D31285244E212100558B62 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | 14D31285244E212100558B62 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | 14D31284244E212100558B62 /* SwiftUIListExample.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | 14D31286244E212100558B62 /* SwiftUIListExample */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 14D31287244E212100558B62 /* AppDelegate.swift */, 60 | 14D31289244E212100558B62 /* SceneDelegate.swift */, 61 | 14D3128B244E212100558B62 /* DynamicListView.swift */, 62 | 14D3128D244E212400558B62 /* Assets.xcassets */, 63 | 14D31292244E212400558B62 /* LaunchScreen.storyboard */, 64 | 14D31295244E212400558B62 /* Info.plist */, 65 | 14D3128F244E212400558B62 /* Preview Content */, 66 | ); 67 | path = SwiftUIListExample; 68 | sourceTree = ""; 69 | }; 70 | 14D3128F244E212400558B62 /* Preview Content */ = { 71 | isa = PBXGroup; 72 | children = ( 73 | 14D31290244E212400558B62 /* Preview Assets.xcassets */, 74 | ); 75 | path = "Preview Content"; 76 | sourceTree = ""; 77 | }; 78 | /* End PBXGroup section */ 79 | 80 | /* Begin PBXNativeTarget section */ 81 | 14D31283244E212100558B62 /* SwiftUIListExample */ = { 82 | isa = PBXNativeTarget; 83 | buildConfigurationList = 14D31298244E212400558B62 /* Build configuration list for PBXNativeTarget "SwiftUIListExample" */; 84 | buildPhases = ( 85 | 14D31280244E212100558B62 /* Sources */, 86 | 14D31281244E212100558B62 /* Frameworks */, 87 | 14D31282244E212100558B62 /* Resources */, 88 | ); 89 | buildRules = ( 90 | ); 91 | dependencies = ( 92 | ); 93 | name = SwiftUIListExample; 94 | productName = SwiftUIListExample; 95 | productReference = 14D31284244E212100558B62 /* SwiftUIListExample.app */; 96 | productType = "com.apple.product-type.application"; 97 | }; 98 | /* End PBXNativeTarget section */ 99 | 100 | /* Begin PBXProject section */ 101 | 14D3127C244E212100558B62 /* Project object */ = { 102 | isa = PBXProject; 103 | attributes = { 104 | LastSwiftUpdateCheck = 1140; 105 | LastUpgradeCheck = 1140; 106 | ORGANIZATIONNAME = Forssell.Anders; 107 | TargetAttributes = { 108 | 14D31283244E212100558B62 = { 109 | CreatedOnToolsVersion = 11.4.1; 110 | }; 111 | }; 112 | }; 113 | buildConfigurationList = 14D3127F244E212100558B62 /* Build configuration list for PBXProject "SwiftUIListExample" */; 114 | compatibilityVersion = "Xcode 9.3"; 115 | developmentRegion = en; 116 | hasScannedForEncodings = 0; 117 | knownRegions = ( 118 | en, 119 | Base, 120 | ); 121 | mainGroup = 14D3127B244E212100558B62; 122 | productRefGroup = 14D31285244E212100558B62 /* Products */; 123 | projectDirPath = ""; 124 | projectRoot = ""; 125 | targets = ( 126 | 14D31283244E212100558B62 /* SwiftUIListExample */, 127 | ); 128 | }; 129 | /* End PBXProject section */ 130 | 131 | /* Begin PBXResourcesBuildPhase section */ 132 | 14D31282244E212100558B62 /* Resources */ = { 133 | isa = PBXResourcesBuildPhase; 134 | buildActionMask = 2147483647; 135 | files = ( 136 | 14D31294244E212400558B62 /* LaunchScreen.storyboard in Resources */, 137 | 14D31291244E212400558B62 /* Preview Assets.xcassets in Resources */, 138 | 14D3128E244E212400558B62 /* Assets.xcassets in Resources */, 139 | ); 140 | runOnlyForDeploymentPostprocessing = 0; 141 | }; 142 | /* End PBXResourcesBuildPhase section */ 143 | 144 | /* Begin PBXSourcesBuildPhase section */ 145 | 14D31280244E212100558B62 /* Sources */ = { 146 | isa = PBXSourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 14D31288244E212100558B62 /* AppDelegate.swift in Sources */, 150 | 14D3128A244E212100558B62 /* SceneDelegate.swift in Sources */, 151 | 14D3128C244E212100558B62 /* DynamicListView.swift in Sources */, 152 | ); 153 | runOnlyForDeploymentPostprocessing = 0; 154 | }; 155 | /* End PBXSourcesBuildPhase section */ 156 | 157 | /* Begin PBXVariantGroup section */ 158 | 14D31292244E212400558B62 /* LaunchScreen.storyboard */ = { 159 | isa = PBXVariantGroup; 160 | children = ( 161 | 14D31293244E212400558B62 /* Base */, 162 | ); 163 | name = LaunchScreen.storyboard; 164 | sourceTree = ""; 165 | }; 166 | /* End PBXVariantGroup section */ 167 | 168 | /* Begin XCBuildConfiguration section */ 169 | 14D31296244E212400558B62 /* Debug */ = { 170 | isa = XCBuildConfiguration; 171 | buildSettings = { 172 | ALWAYS_SEARCH_USER_PATHS = NO; 173 | CLANG_ANALYZER_NONNULL = YES; 174 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 175 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 176 | CLANG_CXX_LIBRARY = "libc++"; 177 | CLANG_ENABLE_MODULES = YES; 178 | CLANG_ENABLE_OBJC_ARC = YES; 179 | CLANG_ENABLE_OBJC_WEAK = YES; 180 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 181 | CLANG_WARN_BOOL_CONVERSION = YES; 182 | CLANG_WARN_COMMA = YES; 183 | CLANG_WARN_CONSTANT_CONVERSION = YES; 184 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 186 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 187 | CLANG_WARN_EMPTY_BODY = YES; 188 | CLANG_WARN_ENUM_CONVERSION = YES; 189 | CLANG_WARN_INFINITE_RECURSION = YES; 190 | CLANG_WARN_INT_CONVERSION = YES; 191 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 192 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 193 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 194 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 195 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 196 | CLANG_WARN_STRICT_PROTOTYPES = YES; 197 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 198 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 199 | CLANG_WARN_UNREACHABLE_CODE = YES; 200 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 201 | COPY_PHASE_STRIP = NO; 202 | DEBUG_INFORMATION_FORMAT = dwarf; 203 | ENABLE_STRICT_OBJC_MSGSEND = YES; 204 | ENABLE_TESTABILITY = YES; 205 | GCC_C_LANGUAGE_STANDARD = gnu11; 206 | GCC_DYNAMIC_NO_PIC = NO; 207 | GCC_NO_COMMON_BLOCKS = YES; 208 | GCC_OPTIMIZATION_LEVEL = 0; 209 | GCC_PREPROCESSOR_DEFINITIONS = ( 210 | "DEBUG=1", 211 | "$(inherited)", 212 | ); 213 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 214 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 215 | GCC_WARN_UNDECLARED_SELECTOR = YES; 216 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 217 | GCC_WARN_UNUSED_FUNCTION = YES; 218 | GCC_WARN_UNUSED_VARIABLE = YES; 219 | IPHONEOS_DEPLOYMENT_TARGET = 13.4; 220 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 221 | MTL_FAST_MATH = YES; 222 | ONLY_ACTIVE_ARCH = YES; 223 | SDKROOT = iphoneos; 224 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 225 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 226 | }; 227 | name = Debug; 228 | }; 229 | 14D31297244E212400558B62 /* Release */ = { 230 | isa = XCBuildConfiguration; 231 | buildSettings = { 232 | ALWAYS_SEARCH_USER_PATHS = NO; 233 | CLANG_ANALYZER_NONNULL = YES; 234 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 235 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 236 | CLANG_CXX_LIBRARY = "libc++"; 237 | CLANG_ENABLE_MODULES = YES; 238 | CLANG_ENABLE_OBJC_ARC = YES; 239 | CLANG_ENABLE_OBJC_WEAK = YES; 240 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 241 | CLANG_WARN_BOOL_CONVERSION = YES; 242 | CLANG_WARN_COMMA = YES; 243 | CLANG_WARN_CONSTANT_CONVERSION = YES; 244 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 245 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 246 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 247 | CLANG_WARN_EMPTY_BODY = YES; 248 | CLANG_WARN_ENUM_CONVERSION = YES; 249 | CLANG_WARN_INFINITE_RECURSION = YES; 250 | CLANG_WARN_INT_CONVERSION = YES; 251 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 252 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 253 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 254 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 255 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 256 | CLANG_WARN_STRICT_PROTOTYPES = YES; 257 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 258 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 259 | CLANG_WARN_UNREACHABLE_CODE = YES; 260 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 261 | COPY_PHASE_STRIP = NO; 262 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 263 | ENABLE_NS_ASSERTIONS = NO; 264 | ENABLE_STRICT_OBJC_MSGSEND = YES; 265 | GCC_C_LANGUAGE_STANDARD = gnu11; 266 | GCC_NO_COMMON_BLOCKS = YES; 267 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 268 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 269 | GCC_WARN_UNDECLARED_SELECTOR = YES; 270 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 271 | GCC_WARN_UNUSED_FUNCTION = YES; 272 | GCC_WARN_UNUSED_VARIABLE = YES; 273 | IPHONEOS_DEPLOYMENT_TARGET = 13.4; 274 | MTL_ENABLE_DEBUG_INFO = NO; 275 | MTL_FAST_MATH = YES; 276 | SDKROOT = iphoneos; 277 | SWIFT_COMPILATION_MODE = wholemodule; 278 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 279 | VALIDATE_PRODUCT = YES; 280 | }; 281 | name = Release; 282 | }; 283 | 14D31299244E212400558B62 /* Debug */ = { 284 | isa = XCBuildConfiguration; 285 | buildSettings = { 286 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 287 | CODE_SIGN_STYLE = Automatic; 288 | DEVELOPMENT_ASSET_PATHS = "\"SwiftUIListExample/Preview Content\""; 289 | DEVELOPMENT_TEAM = 4NWBPWGYVJ; 290 | ENABLE_PREVIEWS = YES; 291 | INFOPLIST_FILE = SwiftUIListExample/Info.plist; 292 | LD_RUNPATH_SEARCH_PATHS = ( 293 | "$(inherited)", 294 | "@executable_path/Frameworks", 295 | ); 296 | PRODUCT_BUNDLE_IDENTIFIER = Forssell.Anders.SwiftUIListExample; 297 | PRODUCT_NAME = "$(TARGET_NAME)"; 298 | SWIFT_VERSION = 5.0; 299 | TARGETED_DEVICE_FAMILY = "1,2"; 300 | }; 301 | name = Debug; 302 | }; 303 | 14D3129A244E212400558B62 /* Release */ = { 304 | isa = XCBuildConfiguration; 305 | buildSettings = { 306 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 307 | CODE_SIGN_STYLE = Automatic; 308 | DEVELOPMENT_ASSET_PATHS = "\"SwiftUIListExample/Preview Content\""; 309 | DEVELOPMENT_TEAM = 4NWBPWGYVJ; 310 | ENABLE_PREVIEWS = YES; 311 | INFOPLIST_FILE = SwiftUIListExample/Info.plist; 312 | LD_RUNPATH_SEARCH_PATHS = ( 313 | "$(inherited)", 314 | "@executable_path/Frameworks", 315 | ); 316 | PRODUCT_BUNDLE_IDENTIFIER = Forssell.Anders.SwiftUIListExample; 317 | PRODUCT_NAME = "$(TARGET_NAME)"; 318 | SWIFT_VERSION = 5.0; 319 | TARGETED_DEVICE_FAMILY = "1,2"; 320 | }; 321 | name = Release; 322 | }; 323 | /* End XCBuildConfiguration section */ 324 | 325 | /* Begin XCConfigurationList section */ 326 | 14D3127F244E212100558B62 /* Build configuration list for PBXProject "SwiftUIListExample" */ = { 327 | isa = XCConfigurationList; 328 | buildConfigurations = ( 329 | 14D31296244E212400558B62 /* Debug */, 330 | 14D31297244E212400558B62 /* Release */, 331 | ); 332 | defaultConfigurationIsVisible = 0; 333 | defaultConfigurationName = Release; 334 | }; 335 | 14D31298244E212400558B62 /* Build configuration list for PBXNativeTarget "SwiftUIListExample" */ = { 336 | isa = XCConfigurationList; 337 | buildConfigurations = ( 338 | 14D31299244E212400558B62 /* Debug */, 339 | 14D3129A244E212400558B62 /* Release */, 340 | ); 341 | defaultConfigurationIsVisible = 0; 342 | defaultConfigurationName = Release; 343 | }; 344 | /* End XCConfigurationList section */ 345 | }; 346 | rootObject = 14D3127C244E212100558B62 /* Project object */; 347 | } 348 | -------------------------------------------------------------------------------- /SwiftUIListExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftUIListExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SwiftUIListExample.xcodeproj/xcuserdata/andersforssell.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SwiftUIListExample.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SwiftUIListExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SwiftUIListExample 4 | // 5 | // Created by Anders Forssell on 2020-04-20. 6 | // Copyright © 2020 Forssell.Anders. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | // MARK: UISceneSession Lifecycle 22 | 23 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 24 | // Called when a new scene session is being created. 25 | // Use this method to select a configuration to create the new scene with. 26 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 27 | } 28 | 29 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 30 | // Called when the user discards a scene session. 31 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 32 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 33 | } 34 | 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /SwiftUIListExample/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 | -------------------------------------------------------------------------------- /SwiftUIListExample/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /SwiftUIListExample/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 | -------------------------------------------------------------------------------- /SwiftUIListExample/DynamicListView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DynamicListView.swift 3 | // 4 | // 5 | 6 | import Combine 7 | import SwiftUI 8 | 9 | // MARK: - General Components 10 | 11 | /// The data items of the list. Must contain index (row number) as a stored property 12 | protocol ListDataItem { 13 | var index: Int { get set } 14 | init(index: Int) 15 | 16 | /// Fetch additional data of the item, possibly asynchronously 17 | func fetchData() 18 | 19 | /// Has the data been fetched? 20 | var dataIsFetched: Bool { get } 21 | } 22 | 23 | /// Generic data provider for the list 24 | class ListDataProvider: ObservableObject { 25 | /// - Parameters: 26 | /// - itemBatchCount: Number of items to fetch in each batch. It is recommended to be greater than number of rows displayed. 27 | /// - prefetchMargin: How far in advance should the next batch be fetched? Greater number means more eager. 28 | /// Sholuld be less than temBatchSize. 29 | init(itemBatchCount: Int = 20, prefetchMargin: Int = 3) { 30 | itemBatchSize = itemBatchCount 31 | self.prefetchMargin = prefetchMargin 32 | reset() 33 | } 34 | 35 | private let itemBatchSize: Int 36 | private let prefetchMargin: Int 37 | 38 | private(set) var listID: UUID = UUID() 39 | 40 | func reset() { 41 | list = [] 42 | listID = UUID() 43 | fetchMoreItemsIfNeeded(currentIndex: -1) 44 | } 45 | 46 | @Published var list: [Item] = [] 47 | 48 | /// Extend the list if we are close to the end, based on the specified index 49 | func fetchMoreItemsIfNeeded(currentIndex: Int) { 50 | guard currentIndex >= list.count - prefetchMargin else { return } 51 | let startIndex = list.count 52 | for currentIndex in startIndex ..< max(startIndex + itemBatchSize, currentIndex) { 53 | list.append(Item(index: currentIndex)) 54 | list[currentIndex].fetchData() 55 | } 56 | } 57 | } 58 | 59 | /// The view for the list row 60 | protocol DynamicListRow: View { 61 | associatedtype Item: ListDataItem 62 | var item: Item { get } 63 | init(item: Item) 64 | } 65 | 66 | /// The view for the dynamic list 67 | struct DynamicList: View { 68 | @ObservedObject var listProvider: ListDataProvider 69 | var body: some View { 70 | return 71 | List(0 ..< listProvider.list.count, id: \.self) { index in 72 | Row(item: self.listProvider.list[index]) 73 | .onAppear { 74 | self.listProvider.fetchMoreItemsIfNeeded(currentIndex: index) 75 | } 76 | } 77 | .id(self.listProvider.listID) 78 | } 79 | } 80 | 81 | // MARK: - Dynamic List Example 82 | 83 | struct SlowDataStore { 84 | static func getAmount(forIndex _: Int) -> AnyPublisher { 85 | Just(Double.random(in: 0 ..< 1)) 86 | .subscribe(on: DispatchQueue.global(qos: .background)) 87 | .map { val in usleep(UInt32.random(in: 500_000 ..< 2_000_000)); return val } 88 | .eraseToAnyPublisher() 89 | } 90 | } 91 | 92 | final class MyDataItem: ListDataItem, ObservableObject { 93 | init(index: Int) { 94 | self.index = index 95 | } 96 | 97 | var dataIsFetched: Bool { 98 | amount != nil 99 | } 100 | 101 | var index: Int = 0 102 | 103 | @Published var amount: Double? 104 | 105 | var label: String { 106 | "Line \(index)" 107 | } 108 | 109 | private var dataPublisher: AnyCancellable? 110 | 111 | func fetchData() { 112 | if !dataIsFetched { 113 | dataPublisher = SlowDataStore.getAmount(forIndex: index) 114 | .receive(on: DispatchQueue.main) 115 | .sink { amount in 116 | self.amount = amount 117 | } 118 | } 119 | } 120 | } 121 | 122 | struct MyListRow: DynamicListRow { 123 | init(item: MyDataItem) { 124 | self.item = item 125 | } 126 | 127 | @ObservedObject var item: MyDataItem 128 | @State var animatedAmount: Double? 129 | 130 | let graphAnimation = Animation.interpolatingSpring(stiffness: 30, damping: 8) 131 | 132 | var body: some View { 133 | HStack { 134 | Text(self.item.label) 135 | .frame(width: 60, alignment: .leading) 136 | .font(.callout) 137 | Text(self.item.amount == nil ? "Loading..." : 138 | String(format: "Amount: %.1f", self.item.amount!)) 139 | .frame(width: 100, alignment: .leading) 140 | .font(.callout) 141 | GraphBar(amount: self.item.amount, animatedAmount: self.$animatedAmount) 142 | } 143 | .onReceive(self.item.$amount) { amount in 144 | if !self.item.dataIsFetched { 145 | withAnimation(self.graphAnimation) { 146 | self.animatedAmount = amount 147 | } 148 | } 149 | } 150 | .onAppear { 151 | if self.item.dataIsFetched { 152 | withAnimation(self.graphAnimation) { 153 | self.animatedAmount = self.item.amount 154 | } 155 | } 156 | } 157 | } 158 | } 159 | 160 | struct GraphBar: View { 161 | let amount: Double? 162 | @Binding var animatedAmount: Double? 163 | 164 | var color: Color { 165 | guard let theAmount = amount else { return Color.gray } 166 | switch theAmount { 167 | case 0.0 ..< 0.3: return Color.red 168 | case 0.3 ..< 0.7: return Color.yellow 169 | case 0.7 ... 1.0: return Color.green 170 | default: return Color.gray 171 | } 172 | } 173 | 174 | var body: some View { 175 | GeometryReader { geometry in 176 | ZStack { 177 | Capsule() 178 | .frame(maxWidth: CGFloat(geometry.size.width * CGFloat(self.animatedAmount ?? 0)), maxHeight: 20) 179 | .foregroundColor(self.color) 180 | }.frame(width: geometry.size.width, height: geometry.size.height, alignment: .leading) 181 | } 182 | } 183 | } 184 | 185 | struct ContentView: View { 186 | var listProvider = ListDataProvider(itemBatchCount: 20, prefetchMargin: 3) 187 | var body: some View { 188 | VStack { 189 | DynamicList(listProvider: listProvider) 190 | 191 | Button("Reset") { 192 | self.listProvider.reset() 193 | } 194 | } 195 | } 196 | } 197 | 198 | struct DynamicList_Previews: PreviewProvider { 199 | static var previews: some View { 200 | ContentView() 201 | .environment(\.colorScheme, .dark) 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /SwiftUIListExample/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 | UISceneConfigurationName 33 | Default Configuration 34 | UISceneDelegateClassName 35 | $(PRODUCT_MODULE_NAME).SceneDelegate 36 | 37 | 38 | 39 | 40 | UILaunchStoryboardName 41 | LaunchScreen 42 | UIRequiredDeviceCapabilities 43 | 44 | armv7 45 | 46 | UISupportedInterfaceOrientations 47 | 48 | UIInterfaceOrientationPortrait 49 | UIInterfaceOrientationLandscapeLeft 50 | UIInterfaceOrientationLandscapeRight 51 | 52 | UISupportedInterfaceOrientations~ipad 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationPortraitUpsideDown 56 | UIInterfaceOrientationLandscapeLeft 57 | UIInterfaceOrientationLandscapeRight 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /SwiftUIListExample/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /SwiftUIListExample/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.swift 3 | // SwiftUIListExample 4 | // 5 | // Created by Anders Forssell on 2020-04-20. 6 | // Copyright © 2020 Forssell.Anders. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import SwiftUI 11 | 12 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 18 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 19 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 20 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 21 | 22 | // Create the SwiftUI view that provides the window contents. 23 | let contentView = ContentView() 24 | 25 | // Use a UIHostingController as window root view controller. 26 | if let windowScene = scene as? UIWindowScene { 27 | let window = UIWindow(windowScene: windowScene) 28 | window.rootViewController = UIHostingController(rootView: contentView) 29 | self.window = window 30 | window.makeKeyAndVisible() 31 | } 32 | } 33 | 34 | func sceneDidDisconnect(_ scene: UIScene) { 35 | // Called as the scene is being released by the system. 36 | // This occurs shortly after the scene enters the background, or when its session is discarded. 37 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 38 | // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). 39 | } 40 | 41 | func sceneDidBecomeActive(_ scene: UIScene) { 42 | // Called when the scene has moved from an inactive state to an active state. 43 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 44 | } 45 | 46 | func sceneWillResignActive(_ scene: UIScene) { 47 | // Called when the scene will move from an active state to an inactive state. 48 | // This may occur due to temporary interruptions (ex. an incoming phone call). 49 | } 50 | 51 | func sceneWillEnterForeground(_ scene: UIScene) { 52 | // Called as the scene transitions from the background to the foreground. 53 | // Use this method to undo the changes made on entering the background. 54 | } 55 | 56 | func sceneDidEnterBackground(_ scene: UIScene) { 57 | // Called as the scene transitions from the foreground to the background. 58 | // Use this method to save data, release shared resources, and store enough scene-specific state information 59 | // to restore the scene back to its current state. 60 | } 61 | 62 | 63 | } 64 | 65 | --------------------------------------------------------------------------------