Contents

kirilovep/hapticclient

`HapticClient` is a lightweight Swift package that provides a simple and unified interface for triggering haptic feedback on Apple platforms. It supports both UIKit and SwiftUI.

πŸ“¦ Installation

Add the following to your Package.swift:

.package(url: "https://github.com/Kirilovep/HapticClient", from: "1.0.0")

Add the dependency to any targets you've declared in your manifest:

.target(
    name: "YourApp",
    dependencies: [
        .product(name: "HapticClient", package: "HapticClient")
    ]
)

Add the HapticClientSwiftUI to support SwiftUI:

.target(
    name: "YourApp",
    dependencies: [
        .product(name: "HapticClientSwiftUI", package: "HapticClient"
    ]
)

Add the HapticClientDependencies to support swift-dependencies:

.target(
    name: "YourApp",
    dependencies: [
        .product(name: "HapticClientDependencies", package: "HapticClient"
    ]
)

✨ Usage

import HapticClient

let haptic = HapticClient()
haptic.vibrate(.feedbackType(.success))
haptic.vibrate(.feedbackStyle(.medium))
haptic.vibrate(.selection)

Swift-dependencies

import HapticClientDependencies

@Dependency(\.hapticClient) var hapticClient

haptic.vibrate(.feedbackType(.success))
haptic.vibrate(.feedbackStyle(.medium))
haptic.vibrate(.selection)

SwiftUI

import SwiftUI
import HapticClientSwiftUI

struct ContentView: View {
    @Environment(\.hapticClient) private var haptic

    var body: some View {
        Button("Tap me") {
            haptic.vibrate(.selection)
        }
    }
}

πŸ” API

Main interface for triggering haptic feedback.

public struct HapticClient: Sendable {
    public init()
    public func vibrate(_ hapticType: HapticType)
}

Enumeration of available haptic feedback types:

public enum HapticType: Sendable {
    case feedbackType(UINotificationFeedbackGenerator.FeedbackType)
    case feedbackStyle(UIImpactFeedbackGenerator.FeedbackStyle)
    case selection
}

πŸ”§ SwiftUI Environment

Seamlessly integrates with SwiftUI via the environment:

extension EnvironmentValues {
    public var hapticClient: HapticClient
}

This makes HapticClient easily accessible with @Environment.

πŸ‘¨β€πŸ’» Author

https://github.com/Kirilovep

Contributing

Contributions are always welcome! Feel free to submit a pull request or open an issue for any suggestions or improvements you have.

License

HapticsClient is licensed under the MIT License. See the LICENCE file for more details.

Package Metadata

Repository: kirilovep/hapticclient

Default branch: master

README: README.md