File size: 3,474 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

import AudioToolbox
import CoreHaptics
import SwiftRs
import Tauri
import UIKit
import WebKit

class ImpactFeedbackOptions: Decodable {
  let style: ImpactFeedbackStyle
}

enum ImpactFeedbackStyle: String, Decodable {
  case light, medium, heavy, soft, rigid

  func into() -> UIImpactFeedbackGenerator.FeedbackStyle {
    switch self {
    case .light:
      return .light
    case .medium:
      return .medium
    case .heavy:
      return .heavy
    case .soft:
      return .soft
    case .rigid:
      return .rigid
    }
  }
}

class NotificationFeedbackOptions: Decodable {
  let type: NotificationFeedbackType
}

enum NotificationFeedbackType: String, Decodable {
  case success, warning, error

  func into() -> UINotificationFeedbackGenerator.FeedbackType {
    switch self {
    case .success:
      return .success
    case .warning:
      return .warning
    case .error:
      return .error
    }
  }
}

class VibrateOptions: Decodable {
  // TODO: Array
  let duration: Double
}

class HapticsPlugin: Plugin {
  //
  // Tauri commands
  //

  @objc public func vibrate(_ invoke: Invoke) throws {
    let args = try invoke.parseArgs(VibrateOptions.self)
    if CHHapticEngine.capabilitiesForHardware().supportsHaptics {
      do {
        let engine = try CHHapticEngine()
        try engine.start()
        engine.resetHandler = { [] in
          do {
            try engine.start()
          } catch {
            AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
          }
        }
        // TODO: Make some of this (or all) configurable?
        let intensity: CHHapticEventParameter = CHHapticEventParameter(
          parameterID: .hapticIntensity, value: 1.0)
        let sharpness: CHHapticEventParameter = CHHapticEventParameter(
          parameterID: .hapticSharpness, value: 1.0)
        let continuousEvent = CHHapticEvent(
          eventType: .hapticContinuous,
          parameters: [intensity, sharpness],
          relativeTime: 0.0,
          duration: args.duration / 1000
        )
        let pattern = try CHHapticPattern(events: [continuousEvent], parameters: [])
        let player = try engine.makePlayer(with: pattern)

        try player.start(atTime: 0)
      } catch {
        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
      }
    } else {
      AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
    }

    invoke.resolve()
  }

  @objc public func impactFeedback(_ invoke: Invoke) throws {
    let args = try invoke.parseArgs(ImpactFeedbackOptions.self)
    let generator = UIImpactFeedbackGenerator(style: args.style.into())
    generator.prepare()
    generator.impactOccurred()

    invoke.resolve()
  }

  @objc public func notificationFeedback(_ invoke: Invoke) throws {
    let args = try invoke.parseArgs(NotificationFeedbackOptions.self)
    let generator = UINotificationFeedbackGenerator()
    generator.prepare()
    generator.notificationOccurred(args.type.into())

    invoke.resolve()
  }

  // TODO: Consider breaking this up into Start,Change,End like capacitor
  @objc public func selectionFeedback(_ invoke: Invoke) throws {
    let generator = UISelectionFeedbackGenerator()
    generator.prepare()
    generator.selectionChanged()

    invoke.resolve()
  }
}

@_cdecl("init_plugin_haptics")
func initPlugin() -> Plugin {
  return HapticsPlugin()
}