Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/markbattistella/triggerkit
TriggerKit is a lightweight Swift package designed for building reactive user interface behaviours by responding to state changes through modifiers or standard protocols. It is particularly useful for creating modular, reusable UI components and can be a powerful foundational building block for larger, more feature-rich packages.
https://github.com/markbattistella/triggerkit
ios maccatalyst macos spm swift swift-package-manager swiftui tvos watchos
Last synced: 21 days ago
JSON representation
TriggerKit is a lightweight Swift package designed for building reactive user interface behaviours by responding to state changes through modifiers or standard protocols. It is particularly useful for creating modular, reusable UI components and can be a powerful foundational building block for larger, more feature-rich packages.
- Host: GitHub
- URL: https://github.com/markbattistella/triggerkit
- Owner: markbattistella
- License: mit
- Created: 2024-12-01T22:01:46.000Z (24 days ago)
- Default Branch: main
- Last Pushed: 2024-12-01T22:10:16.000Z (24 days ago)
- Last Synced: 2024-12-01T23:21:22.012Z (24 days ago)
- Topics: ios, maccatalyst, macos, spm, swift, swift-package-manager, swiftui, tvos, watchos
- Language: Swift
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TriggerKit
![Swift Versions](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmarkbattistella%2FTriggerKit%2Fbadge%3Ftype%3Dswift-versions)
![Platforms](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmarkbattistella%2FTriggerKit%2Fbadge%3Ftype%3Dplatforms)
![Licence](https://img.shields.io/badge/Licence-MIT-white?labelColor=blue&style=flat)
`TriggerKit` is a lightweight Swift package designed for building reactive user interface behaviours by responding to state changes through modifiers or standard protocols. It is particularly useful for creating modular, reusable UI components and can be a powerful foundational building block for larger, more feature-rich packages.
## Features
- **Reactive State Handling**: Easily define actions that are triggered when state values change.
- **Extensible Modifier**: Use `StateChangeModifier` to add reactive behaviours to SwiftUI views.
- **Reusable Protocol**: Implement `TriggerActionPerformable` to standardise how triggers and actions are handled across your app.
- **Package Friendly**: Although it can be used directly, it is intended to be integrated into other packages.## Installation
Add `TriggerKit` to your Swift project using Swift Package Manager.
```swift
dependencies: [
.package(url: "https://github.com/markbattistella/TriggerKit", from: "1.0.0")
]
```Alternatively, you can add `TriggerKit` using Xcode by navigating to `File > Add Packages` and entering the package repository URL.
## Recommended Usage
> [!NOTE]
> While you can use this package directly in your code, it is recommended to wrap it in another package or library to integrate it into a larger system. This design pattern promotes loose coupling, making it easier to use and maintain your application. `StateChangeModifier` and `TriggerActionPerformable` are meant to serve as foundational building blocks for more complex state-driven behaviours in other packages, rather than being directly utilised in all instances.## Usage
### Using `TriggerKit` in Another Swift Package
Instead of directly using `TriggerKit` in your application code, consider wrapping it in a custom package that adds additional functionality. Below is an example of how you might use `TriggerKit` to manage haptic feedback through state changes.
#### Wrapping `TriggerKit` for Haptic Feedback
> [!TIP]
> See: [HapticsManager](https://github.com/markbattistella/HapticsManager) Swift PackageLet's create a package that implements haptic feedback using `TriggerKit`:
1. Define Custom User Default Keys for Feedback Settings
```swift
public struct HapticFeedbackSettings {
internal static var isAvailable: Bool {
CHHapticEngine.capabilitiesForHardware().supportsHaptics // system level checks
}
internal static var isEnabled: Bool {
userIsPayingCustomer && !userNeedsHapticFeedback // your custom checks
}
}
```2. Define a Custom Performer for Haptic Feedback
```swift
public struct HapticFeedbackPerformer: TriggerActionPerformable, FeedbackSettingsConfigurable {
public typealias Trigger = Tpublic enum Feedback {
case impact(UIImpactFeedbackGenerator.FeedbackStyle)
}public static var isAvailable: Bool { HapticFeedbackSettings.isAvailable }
public static var isEnabled: Bool { HapticFeedbackSettings.isEnabled }public static func perform(_ feedback: Feedback) {
// your logic here
}public static func canPerform() -> Bool { isAvailable && isEnabled }
}
```3. Define a SwiftUI View Modifier for Haptic Feedback
Next, we use `StateChangeModifier` to create a view modifier that provides haptic feedback based on trigger state changes:
```swift
public extension View {
func hapticFeedback(
_ feedback: HapticFeedbackPerformer.Feedback,
trigger: T
) -> some View {
self.modifier(
StateChangeModifier(
feedback,
trigger: trigger,
actionHandler: { feedback in
guard HapticFeedbackPerformer.canPerform() else { return }
HapticFeedbackPerformer.perform(feedback)
}
)
)
}
}
```### Example Usage in Another Package
```swift
import SwiftUIstruct ContentView: View {
@State private var isButtonTapped = falsevar body: some View {
Button(action: {
isButtonTapped.toggle()
}) {
Text("Tap Me")
}
.hapticFeedback(
.impact(.light),
trigger: isButtonTapped
)
}
}
```In this example, every time the button is tapped, a light haptic impact feedback is provided, but only if haptics are enabled and available.
## Contributing
Contributions are welcome! Please fork the repository and submit a pull request for any features, fixes, or improvements.
## License
`TriggerKit` is available under the MIT license. See the LICENCE file for more information.