From 94194218317001b213f61a933c5fdca1a7d7abd3 Mon Sep 17 00:00:00 2001 From: Enes Karaosman Date: Mon, 1 Apr 2024 17:09:17 +0300 Subject: [PATCH] initial commit --- .gitignore | 8 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + Example/Example.xcodeproj/project.pbxproj | 371 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../AccentColor.colorset/Contents.json | 11 + .../AppIcon.appiconset/Contents.json | 13 + Example/Example/Assets.xcassets/Contents.json | 6 + Example/Example/ContentView.swift | 51 +++ Example/Example/ExampleApp.swift | 17 + .../Preview Assets.xcassets/Contents.json | 6 + Package.swift | 24 ++ Readme.md | 50 +++ Sources/KeyboardActions/KeyboardActions.swift | 107 +++++ .../KeyboardActionsTests.swift | 12 + 15 files changed, 699 insertions(+) create mode 100644 .gitignore create mode 100644 .swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 Example/Example.xcodeproj/project.pbxproj create mode 100644 Example/Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Example/Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 Example/Example/Assets.xcassets/AccentColor.colorset/Contents.json create mode 100644 Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 Example/Example/Assets.xcassets/Contents.json create mode 100644 Example/Example/ContentView.swift create mode 100644 Example/Example/ExampleApp.swift create mode 100644 Example/Example/Preview Content/Preview Assets.xcassets/Contents.json create mode 100644 Package.swift create mode 100644 Readme.md create mode 100644 Sources/KeyboardActions/KeyboardActions.swift create mode 100644 Tests/KeyboardActionsTests/KeyboardActionsTests.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj new file mode 100644 index 0000000..af4643c --- /dev/null +++ b/Example/Example.xcodeproj/project.pbxproj @@ -0,0 +1,371 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + 94D1AE872BBAFAA3006E7AF6 /* ExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94D1AE862BBAFAA3006E7AF6 /* ExampleApp.swift */; }; + 94D1AE892BBAFAA3006E7AF6 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94D1AE882BBAFAA3006E7AF6 /* ContentView.swift */; }; + 94D1AE8B2BBAFAA5006E7AF6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 94D1AE8A2BBAFAA5006E7AF6 /* Assets.xcassets */; }; + 94D1AE8E2BBAFAA5006E7AF6 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 94D1AE8D2BBAFAA5006E7AF6 /* Preview Assets.xcassets */; }; + 94D1AE972BBAFAC1006E7AF6 /* KeyboardActions in Frameworks */ = {isa = PBXBuildFile; productRef = 94D1AE962BBAFAC1006E7AF6 /* KeyboardActions */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 94D1AE832BBAFAA3006E7AF6 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 94D1AE862BBAFAA3006E7AF6 /* ExampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleApp.swift; sourceTree = ""; }; + 94D1AE882BBAFAA3006E7AF6 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 94D1AE8A2BBAFAA5006E7AF6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 94D1AE8D2BBAFAA5006E7AF6 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; + 94D1AE942BBAFAB9006E7AF6 /* KeyboardActions */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = KeyboardActions; path = ..; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 94D1AE802BBAFAA3006E7AF6 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 94D1AE972BBAFAC1006E7AF6 /* KeyboardActions in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 94D1AE7A2BBAFAA3006E7AF6 = { + isa = PBXGroup; + children = ( + 94D1AE942BBAFAB9006E7AF6 /* KeyboardActions */, + 94D1AE852BBAFAA3006E7AF6 /* Example */, + 94D1AE842BBAFAA3006E7AF6 /* Products */, + 94D1AE952BBAFAC1006E7AF6 /* Frameworks */, + ); + sourceTree = ""; + }; + 94D1AE842BBAFAA3006E7AF6 /* Products */ = { + isa = PBXGroup; + children = ( + 94D1AE832BBAFAA3006E7AF6 /* Example.app */, + ); + name = Products; + sourceTree = ""; + }; + 94D1AE852BBAFAA3006E7AF6 /* Example */ = { + isa = PBXGroup; + children = ( + 94D1AE862BBAFAA3006E7AF6 /* ExampleApp.swift */, + 94D1AE882BBAFAA3006E7AF6 /* ContentView.swift */, + 94D1AE8A2BBAFAA5006E7AF6 /* Assets.xcassets */, + 94D1AE8C2BBAFAA5006E7AF6 /* Preview Content */, + ); + path = Example; + sourceTree = ""; + }; + 94D1AE8C2BBAFAA5006E7AF6 /* Preview Content */ = { + isa = PBXGroup; + children = ( + 94D1AE8D2BBAFAA5006E7AF6 /* Preview Assets.xcassets */, + ); + path = "Preview Content"; + sourceTree = ""; + }; + 94D1AE952BBAFAC1006E7AF6 /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 94D1AE822BBAFAA3006E7AF6 /* Example */ = { + isa = PBXNativeTarget; + buildConfigurationList = 94D1AE912BBAFAA5006E7AF6 /* Build configuration list for PBXNativeTarget "Example" */; + buildPhases = ( + 94D1AE7F2BBAFAA3006E7AF6 /* Sources */, + 94D1AE802BBAFAA3006E7AF6 /* Frameworks */, + 94D1AE812BBAFAA3006E7AF6 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Example; + packageProductDependencies = ( + 94D1AE962BBAFAC1006E7AF6 /* KeyboardActions */, + ); + productName = Example; + productReference = 94D1AE832BBAFAA3006E7AF6 /* Example.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 94D1AE7B2BBAFAA3006E7AF6 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1520; + LastUpgradeCheck = 1520; + TargetAttributes = { + 94D1AE822BBAFAA3006E7AF6 = { + CreatedOnToolsVersion = 15.2; + }; + }; + }; + buildConfigurationList = 94D1AE7E2BBAFAA3006E7AF6 /* Build configuration list for PBXProject "Example" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 94D1AE7A2BBAFAA3006E7AF6; + productRefGroup = 94D1AE842BBAFAA3006E7AF6 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 94D1AE822BBAFAA3006E7AF6 /* Example */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 94D1AE812BBAFAA3006E7AF6 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 94D1AE8E2BBAFAA5006E7AF6 /* Preview Assets.xcassets in Resources */, + 94D1AE8B2BBAFAA5006E7AF6 /* Assets.xcassets in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 94D1AE7F2BBAFAA3006E7AF6 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 94D1AE892BBAFAA3006E7AF6 /* ContentView.swift in Sources */, + 94D1AE872BBAFAA3006E7AF6 /* ExampleApp.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 94D1AE8F2BBAFAA5006E7AF6 /* 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.2; + 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; + }; + 94D1AE902BBAFAA5006E7AF6 /* 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.2; + 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; + }; + 94D1AE922BBAFAA5006E7AF6 /* 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 = "\"Example/Preview Content\""; + DEVELOPMENT_TEAM = L9DND36YTU; + 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.eneskaraosman.Example; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 94D1AE932BBAFAA5006E7AF6 /* 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 = "\"Example/Preview Content\""; + DEVELOPMENT_TEAM = L9DND36YTU; + 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.eneskaraosman.Example; + 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 */ + 94D1AE7E2BBAFAA3006E7AF6 /* Build configuration list for PBXProject "Example" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 94D1AE8F2BBAFAA5006E7AF6 /* Debug */, + 94D1AE902BBAFAA5006E7AF6 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 94D1AE912BBAFAA5006E7AF6 /* Build configuration list for PBXNativeTarget "Example" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 94D1AE922BBAFAA5006E7AF6 /* Debug */, + 94D1AE932BBAFAA5006E7AF6 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCSwiftPackageProductDependency section */ + 94D1AE962BBAFAC1006E7AF6 /* KeyboardActions */ = { + isa = XCSwiftPackageProductDependency; + productName = KeyboardActions; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = 94D1AE7B2BBAFAA3006E7AF6 /* Project object */; +} diff --git a/Example/Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Example/Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Example/Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Example/Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Example/Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Example/Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Example/Example/Assets.xcassets/AccentColor.colorset/Contents.json b/Example/Example/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/Example/Example/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json b/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..13613e3 --- /dev/null +++ b/Example/Example/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/Example/Example/Assets.xcassets/Contents.json b/Example/Example/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Example/Example/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Example/Example/ContentView.swift b/Example/Example/ContentView.swift new file mode 100644 index 0000000..bf178b9 --- /dev/null +++ b/Example/Example/ContentView.swift @@ -0,0 +1,51 @@ +// +// ContentView.swift +// Example +// +// Created by Enes Karaosman on 1.04.2024. +// + +import KeyboardActions +import SwiftUI + +struct ContentView: View { + @State private var mail: String = "" + @State private var password: String = "" + @State private var confirmPassword: String = "" + + enum Field: CaseIterable, FieldContract { + case mail + case password + case confirmPassword + } + + @FocusState var focusedField: Field? + + var body: some View { + VStack { + TextField("Mail", text: $mail) + .focused($focusedField, equals: Field.mail) + .padding(4) + .border(.gray) + + TextField("Password", text: $password) + .focused($focusedField, equals: Field.password) + .padding(4) + .border(.gray) + + TextField("Confirm Password", text: $confirmPassword) + .focused($focusedField, equals: Field.confirmPassword) + .padding(4) + .border(.gray) + } + .padding() + .keyboardActions(focusedField: $focusedField, fields: Field.allCases) + .onAppear { + focusedField = Field.mail + } + } +} + +#Preview { + ContentView() +} diff --git a/Example/Example/ExampleApp.swift b/Example/Example/ExampleApp.swift new file mode 100644 index 0000000..097cc61 --- /dev/null +++ b/Example/Example/ExampleApp.swift @@ -0,0 +1,17 @@ +// +// ExampleApp.swift +// Example +// +// Created by Enes Karaosman on 1.04.2024. +// + +import SwiftUI + +@main +struct ExampleApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +} diff --git a/Example/Example/Preview Content/Preview Assets.xcassets/Contents.json b/Example/Example/Preview Content/Preview Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Example/Example/Preview Content/Preview Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..271ab1d --- /dev/null +++ b/Package.swift @@ -0,0 +1,24 @@ +// swift-tools-version: 5.9 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "KeyboardActions", + platforms: [.iOS(.v15)], + products: [ + // Products define the executables and libraries a package produces, making them visible to other packages. + .library( + name: "KeyboardActions", + targets: ["KeyboardActions"]), + ], + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .target( + name: "KeyboardActions"), + .testTarget( + name: "KeyboardActionsTests", + dependencies: ["KeyboardActions"]), + ] +) diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..5820f15 --- /dev/null +++ b/Readme.md @@ -0,0 +1,50 @@ +# KeyboardActions + +KeyboardActions is a SwiftUI package that allows you to manage the focus of the keyboard in your app. Adds toolbar item above keyboard to easily switch focus between textfields or close keyboard + +You can create an enumeration that represents your fields in a form. Confirm `FieldContract` which is basically a `Hashable` protocol currently + +And add `.keyboardActions` modifier to your view to enable the toolbar above the keyboard + +```swift +import KeyboardActions +import SwiftUI + +struct ContentView: View { + @State private var mail: String = "" + @State private var password: String = "" + @State private var confirmPassword: String = "" + + enum Field: CaseIterable, FieldContract { + case mail + case password + case confirmPassword + } + + @FocusState var focusedField: Field? + + var body: some View { + VStack { + TextField("Mail", text: $mail) + .focused($focusedField, equals: Field.mail) + .padding(4) + .border(.gray) + + TextField("Password", text: $password) + .focused($focusedField, equals: Field.password) + .padding(4) + .border(.gray) + + TextField("Confirm Password", text: $confirmPassword) + .focused($focusedField, equals: Field.confirmPassword) + .padding(4) + .border(.gray) + } + .padding() + .keyboardActions(focusedField: $focusedField, fields: Field.allCases) + .onAppear { + focusedField = Field.mail + } + } +} +``` diff --git a/Sources/KeyboardActions/KeyboardActions.swift b/Sources/KeyboardActions/KeyboardActions.swift new file mode 100644 index 0000000..786e0d9 --- /dev/null +++ b/Sources/KeyboardActions/KeyboardActions.swift @@ -0,0 +1,107 @@ +// +// KeyboardActions.swift +// KeyboardActions +// +// Created by Enes Karaosman on 1.04.2024. +// + +import SwiftUI + +public protocol FieldContract: Hashable {} + +public struct KeyboardActions: View { + private var focusedField: FocusState.Binding? + private let fields: [Field] + private let content: Content + + public init( + focusedField: FocusState.Binding? = nil, + fields: [Field], + @ViewBuilder content: () -> Content + ) { + self.focusedField = focusedField + self.fields = fields + self.content = content() + } + + public var body: some View { + content + .keyboardActions(focusedField: focusedField, fields: fields) + } +} + +public extension View { + func keyboardActions( + focusedField: FocusState.Binding? = nil, + fields: [Field] + ) -> some View { + modifier(KeyboardActionsModifier(focusedField: focusedField, fields: fields)) + } +} + +struct KeyboardActionsModifier: ViewModifier { + private var focusedField: FocusState.Binding? + private let fields: [Field] + + init( + focusedField: FocusState.Binding? = nil, + fields: [Field] + ) { + self.focusedField = focusedField + self.fields = fields + } + + func body(content: Content) -> some View { + content + .toolbar { + ToolbarItemGroup(placement: .keyboard) { + Button(action: focusPreviousField) { + Image(systemName: "chevron.up") + } + .disabled(!canFocusPreviousField()) + + Button(action: focusNextField) { + Image(systemName: "chevron.down") + } + .disabled(!canFocusNextField()) + + Spacer() + + Button(action: { + focusedField?.wrappedValue = nil + }) { + Image(systemName: "xmark") + } + .buttonStyle(.bordered) + } + } + } + + private var selectedIndex: Int? { + fields.firstIndex(where: { $0.hashValue == focusedField?.wrappedValue?.hashValue }) + } + + private func focusPreviousField() { + if canFocusPreviousField() { + focusedField?.wrappedValue = fields[selectedIndex! - 1] + } + } + + private func focusNextField() { + if canFocusNextField() { + focusedField?.wrappedValue = fields[selectedIndex! + 1] + } + } + + private func canFocusPreviousField() -> Bool { + guard let selectedIndex, selectedIndex > 0 else { return false } + + return true + } + + private func canFocusNextField() -> Bool { + guard let selectedIndex, selectedIndex < fields.endIndex - 1 else { return false } + + return true + } +} diff --git a/Tests/KeyboardActionsTests/KeyboardActionsTests.swift b/Tests/KeyboardActionsTests/KeyboardActionsTests.swift new file mode 100644 index 0000000..6d35362 --- /dev/null +++ b/Tests/KeyboardActionsTests/KeyboardActionsTests.swift @@ -0,0 +1,12 @@ +import XCTest +@testable import KeyboardActions + +final class KeyboardActionsTests: XCTestCase { + func testExample() throws { + // XCTest Documentation + // https://developer.apple.com/documentation/xctest + + // Defining Test Cases and Test Methods + // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods + } +}