diff --git a/Package.swift b/Package.swift index 0913d88..d3cb6b7 100644 --- a/Package.swift +++ b/Package.swift @@ -15,10 +15,12 @@ let package = Package( products: [ .library( name: "TaskTrigger", - targets: ["TaskTrigger"]), + targets: ["TaskTrigger"] + ), ], targets: [ .target( - name: "TaskTrigger"), + name: "TaskTrigger" + ), ] ) diff --git a/README.md b/README.md index 1190ca3..cacfa59 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Attach async tasks to SwiftUI views using a trigger mechanism. See [this](#the-s ## Table Of Contents - [Usage](#usage) +- [Documentation](#documentation) - [Overview](#overview) - [The Problem](#the-problem) - [The Solution](#the-solution) @@ -23,6 +24,12 @@ dependencies: [ Make sure to `import TaskTrigger` in any location you'd want to use it. +## Documentation + +The documentation is available [here](https://lukepistrol.github.io/TaskTrigger/documentation/tasktrigger). + +You can also have a look at the [sample project](https://github.com/lukepistrol/TaskTrigger/tree/main/TaskTriggerDemo/TaskTriggerDemo) + ## Overview When using Swift's structured concurrency in SwiftUI it is good practice to diff --git a/Sources/TaskTrigger/TaskTrigger.swift b/Sources/TaskTrigger/TaskTrigger.swift index e3060ee..3179a98 100644 --- a/Sources/TaskTrigger/TaskTrigger.swift +++ b/Sources/TaskTrigger/TaskTrigger.swift @@ -1,6 +1,6 @@ import SwiftUI -public struct TaskTrigger: Equatable { +public struct TaskTrigger: Equatable where Value: Sendable { internal enum TaskState: Equatable { case none diff --git a/Sources/TaskTrigger/TaskTriggerViewModifier.swift b/Sources/TaskTrigger/TaskTriggerViewModifier.swift index ff0244b..ce97c91 100644 --- a/Sources/TaskTrigger/TaskTriggerViewModifier.swift +++ b/Sources/TaskTrigger/TaskTriggerViewModifier.swift @@ -7,7 +7,7 @@ import SwiftUI -struct TaskTriggerViewModifier: ViewModifier { +struct TaskTriggerViewModifier: ViewModifier where Value: Sendable { typealias Action = @Sendable (_ value: Value) async -> Void diff --git a/Sources/TaskTrigger/View+Task.swift b/Sources/TaskTrigger/View+Task.swift index f68fdaf..e44f725 100644 --- a/Sources/TaskTrigger/View+Task.swift +++ b/Sources/TaskTrigger/View+Task.swift @@ -16,7 +16,7 @@ public extension View { func task( _ trigger: Binding>, _ action: @escaping @Sendable @MainActor (_ value: Value) async -> Void - ) -> some View { + ) -> some View where Value: Sendable { modifier(TaskTriggerViewModifier(trigger: trigger, action: action)) } diff --git a/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.pbxproj b/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.pbxproj new file mode 100644 index 0000000..0df3119 --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.pbxproj @@ -0,0 +1,371 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + 28C862282ABB4EEE002EA26C /* TaskTriggerDemoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28C862272ABB4EEE002EA26C /* TaskTriggerDemoApp.swift */; }; + 28C8622A2ABB4EEE002EA26C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28C862292ABB4EEE002EA26C /* ContentView.swift */; }; + 28C8622C2ABB4EF0002EA26C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 28C8622B2ABB4EF0002EA26C /* Assets.xcassets */; }; + 28C8622F2ABB4EF0002EA26C /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 28C8622E2ABB4EF0002EA26C /* Preview Assets.xcassets */; }; + 28C862362ABB4F1A002EA26C /* TaskTrigger in Resources */ = {isa = PBXBuildFile; fileRef = 28C862352ABB4F1A002EA26C /* TaskTrigger */; }; + 28C862392ABB4F25002EA26C /* TaskTrigger in Frameworks */ = {isa = PBXBuildFile; productRef = 28C862382ABB4F25002EA26C /* TaskTrigger */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 28C862242ABB4EEE002EA26C /* TaskTriggerDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TaskTriggerDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 28C862272ABB4EEE002EA26C /* TaskTriggerDemoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskTriggerDemoApp.swift; sourceTree = ""; }; + 28C862292ABB4EEE002EA26C /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 28C8622B2ABB4EF0002EA26C /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 28C8622E2ABB4EF0002EA26C /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; + 28C862352ABB4F1A002EA26C /* TaskTrigger */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = TaskTrigger; path = ..; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 28C862212ABB4EEE002EA26C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 28C862392ABB4F25002EA26C /* TaskTrigger in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 28C8621B2ABB4EEE002EA26C = { + isa = PBXGroup; + children = ( + 28C862352ABB4F1A002EA26C /* TaskTrigger */, + 28C862262ABB4EEE002EA26C /* TaskTriggerDemo */, + 28C862252ABB4EEE002EA26C /* Products */, + 28C862372ABB4F25002EA26C /* Frameworks */, + ); + sourceTree = ""; + }; + 28C862252ABB4EEE002EA26C /* Products */ = { + isa = PBXGroup; + children = ( + 28C862242ABB4EEE002EA26C /* TaskTriggerDemo.app */, + ); + name = Products; + sourceTree = ""; + }; + 28C862262ABB4EEE002EA26C /* TaskTriggerDemo */ = { + isa = PBXGroup; + children = ( + 28C862272ABB4EEE002EA26C /* TaskTriggerDemoApp.swift */, + 28C862292ABB4EEE002EA26C /* ContentView.swift */, + 28C8622B2ABB4EF0002EA26C /* Assets.xcassets */, + 28C8622D2ABB4EF0002EA26C /* Preview Content */, + ); + path = TaskTriggerDemo; + sourceTree = ""; + }; + 28C8622D2ABB4EF0002EA26C /* Preview Content */ = { + isa = PBXGroup; + children = ( + 28C8622E2ABB4EF0002EA26C /* Preview Assets.xcassets */, + ); + path = "Preview Content"; + sourceTree = ""; + }; + 28C862372ABB4F25002EA26C /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 28C862232ABB4EEE002EA26C /* TaskTriggerDemo */ = { + isa = PBXNativeTarget; + buildConfigurationList = 28C862322ABB4EF0002EA26C /* Build configuration list for PBXNativeTarget "TaskTriggerDemo" */; + buildPhases = ( + 28C862202ABB4EEE002EA26C /* Sources */, + 28C862212ABB4EEE002EA26C /* Frameworks */, + 28C862222ABB4EEE002EA26C /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = TaskTriggerDemo; + packageProductDependencies = ( + 28C862382ABB4F25002EA26C /* TaskTrigger */, + ); + productName = TaskTriggerDemo; + productReference = 28C862242ABB4EEE002EA26C /* TaskTriggerDemo.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 28C8621C2ABB4EEE002EA26C /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1500; + LastUpgradeCheck = 1500; + TargetAttributes = { + 28C862232ABB4EEE002EA26C = { + CreatedOnToolsVersion = 15.0; + }; + }; + }; + buildConfigurationList = 28C8621F2ABB4EEE002EA26C /* Build configuration list for PBXProject "TaskTriggerDemo" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 28C8621B2ABB4EEE002EA26C; + productRefGroup = 28C862252ABB4EEE002EA26C /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 28C862232ABB4EEE002EA26C /* TaskTriggerDemo */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 28C862222ABB4EEE002EA26C /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 28C8622F2ABB4EF0002EA26C /* Preview Assets.xcassets in Resources */, + 28C8622C2ABB4EF0002EA26C /* Assets.xcassets in Resources */, + 28C862362ABB4F1A002EA26C /* TaskTrigger in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 28C862202ABB4EEE002EA26C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 28C8622A2ABB4EEE002EA26C /* ContentView.swift in Sources */, + 28C862282ABB4EEE002EA26C /* TaskTriggerDemoApp.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 28C862302ABB4EF0002EA26C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 28C862312ABB4EF0002EA26C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 28C862332ABB4EF0002EA26C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_ASSET_PATHS = "\"TaskTriggerDemo/Preview Content\""; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.lukaspistrol.TaskTriggerDemo; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 28C862342ABB4EF0002EA26C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_ASSET_PATHS = "\"TaskTriggerDemo/Preview Content\""; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.lukaspistrol.TaskTriggerDemo; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 28C8621F2ABB4EEE002EA26C /* Build configuration list for PBXProject "TaskTriggerDemo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 28C862302ABB4EF0002EA26C /* Debug */, + 28C862312ABB4EF0002EA26C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 28C862322ABB4EF0002EA26C /* Build configuration list for PBXNativeTarget "TaskTriggerDemo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 28C862332ABB4EF0002EA26C /* Debug */, + 28C862342ABB4EF0002EA26C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCSwiftPackageProductDependency section */ + 28C862382ABB4F25002EA26C /* TaskTrigger */ = { + isa = XCSwiftPackageProductDependency; + productName = TaskTrigger; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = 28C8621C2ABB4EEE002EA26C /* Project object */; +} diff --git a/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/AccentColor.colorset/Contents.json b/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/AppIcon.appiconset/Contents.json b/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..13613e3 --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,13 @@ +{ + "images" : [ + { + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/Contents.json b/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/TaskTriggerDemo/TaskTriggerDemo/ContentView.swift b/TaskTriggerDemo/TaskTriggerDemo/ContentView.swift new file mode 100644 index 0000000..96501dd --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo/ContentView.swift @@ -0,0 +1,115 @@ +// +// ContentView.swift +// TaskTriggerDemo +// +// Created by Lukas Pistrol on 2023-09-20. +// + +import SwiftUI +import TaskTrigger + +/* + This is a very simple example showing how to use TaskTrigger in a SwiftUI View. + + The view contains a Text showing a random number and two buttons. The first button + triggers a task that sets the random number to a new value. The second button + triggers a task that resets the random number to zero. + + Both tasks simulate an async operation by using Task.sleep. + */ + +struct ContentView: View { + + @State private var randomNumber: Int = 0 + + /* + TaskTrigger is generic and accepts any value that conforms to Equatable and Sendable. + + For convenience there is also PlainTaskTrigger which is basically `TaskTrigger`. + PlainTaskTrigger is useful if you don't need to pass a value to the task and therefore + can be triggered with the trigger() method instead of trigger(value:). + */ + + @State private var randomTrigger = TaskTrigger() + @State private var resetTrigger = PlainTaskTrigger() + + var body: some View { + VStack { + Text(randomNumber, format: .number) + .font(.system(size: 60, weight: .semibold, design: .rounded)) + .monospacedDigit() + .contentTransition(.numericText()) + + VStack { + HStack { + Button("Random Number") { + randomTrigger.trigger(value: Int.random(in: 0...100)) + } + Button("Reset", role: .destructive) { + resetTrigger.trigger() + } + } + Button("Cancel All") { + randomTrigger.cancel() + resetTrigger.cancel() + } + } + .buttonStyle(.borderedProminent) + } + .animation(.snappy, value: randomNumber) + + /* + The task modifiers below look pretty similar to the ones provided by SwiftUI. + + It takes a binding to a TaskTrigger and the closure gets executed whenever the + trigger fires. The closure also gets passed the value that was attached to the + trigger. The PlainTaskTrigger variant doesn't need a value since its boolean + value is always true in the triggered state. + + The task only lives as long as the view is alive. If the view gets destroyed + the task is cancelled automatically. You can also cancel the task manually + by calling the cancel() method on the trigger. + + Firing a trigger multiple times in a row will cancel the previous task and + start a new one. + */ + + .task($randomTrigger) { value in + await setRandomNumber(value) + } + .task($resetTrigger) { + await resetToZero() + } + } + + /* + The two functions below simulate an asynchroneous task wich also checks for cancellation. + + You can test this easily by pressing a button twice in a row. The first task will be + cancelled and the second one will be executed. + + This also happens if the "random number" should be the same each time since TaskTrigger + also creates a new UUID each time the trigger method gets called. + + Typically those functions would live in a ViewModel or similar and perform a real async + operation like a network call or some other (possibly) expensive operation. + */ + + @MainActor + private func setRandomNumber(_ value: Int) async { + try? await Task.sleep(for: .seconds(1)) + if Task.isCancelled { return } + self.randomNumber = value + } + + @MainActor + private func resetToZero() async { + try? await Task.sleep(for: .seconds(1)) + if Task.isCancelled { return } + self.randomNumber = 0 + } +} + +#Preview { + ContentView() +} diff --git a/TaskTriggerDemo/TaskTriggerDemo/Preview Content/Preview Assets.xcassets/Contents.json b/TaskTriggerDemo/TaskTriggerDemo/Preview Content/Preview Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo/Preview Content/Preview Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/TaskTriggerDemo/TaskTriggerDemo/TaskTriggerDemoApp.swift b/TaskTriggerDemo/TaskTriggerDemo/TaskTriggerDemoApp.swift new file mode 100644 index 0000000..7ce64d9 --- /dev/null +++ b/TaskTriggerDemo/TaskTriggerDemo/TaskTriggerDemoApp.swift @@ -0,0 +1,17 @@ +// +// TaskTriggerDemoApp.swift +// TaskTriggerDemo +// +// Created by Lukas Pistrol on 2023-09-20. +// + +import SwiftUI + +@main +struct TaskTriggerDemoApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +}