I’ve this iOS app in Swift that lets the consumer take an image, ship it to a server with an API and retrieve a response. The letters within the response are transformed into vibrations that the consumer can really feel utilizing Apple’s CHHapticEngine
. I am additionally utilizing a github library known as JPSVolumeButtonHandler
that listens for quantity button presses and does one thing when it detects one. I need to cancel the vibrations when the consumer presses one of many quantity buttons if the response occurs to be too lengthy. The issue I am going through is that when the app is taking part in the vibrations no quantity button presses are being detected and so I am unable to cancel the vibrations. It is just after the vibrations are carried out that the occasions are registered.
I have not carried out any cancelation but and have solely tried to get the amount buttons to be detected.
I can provide the extra capabilities that creates the haptics as nicely.
Code within the ImagePicker will get executed after a response is retrieved.
import SwiftUI
import JPSVolumeButtonHandler
import AVFoundation
import Corehaptics
struct ContentView: View {
@State personal var hapticEngine: CHHapticEngine?
@State personal var volumeHandler: JPSVolumeButtonHandler?
@State personal var ohwow: String = ""
//different @State personal variables
//different code
.sheet(isPresented: $isImagePickerPresented, onDismiss: {
}) {
ImagePicker(picture: $capturedImage, onImageCapture: { imageData in
if let imageData = imageData{
uploadImage(imageData: imageData, serverURL: userInput) { end in
print("inside closure")
swap outcome {
case .success(let responseString):
print("success")
ohwow = extractedText(responseString) ?? "dangerous response"
ohwow = ohwow.lowercased()
print(ohwow.rely)
vibro = true
viewModel.isConditionTrue = true
finishedstring = morseOhwow(ohwow) //interprets the string into morse code which is performed by means of vibrations.
print("response:"(ohwow)"")
case .failure(let error):
print("Error: (error)")
}
}
}
})
}
The Quantity Button Handler
VStack{
Textual content()
//Different stuff
}
.onAppear {
print("onappear")
setupHaptics()
//setupHaptics()
/*
public func setupHaptics() {
print("setupHaptics name")
do {
hapticEngine = attempt CHHapticEngine()
attempt hapticEngine?.begin()
} catch {
print("Error initializing haptic engine: (error)")
}
}
*/
observeCondition()
volumeHandler = JPSVolumeButtonHandler(up: {
DispatchQueue.essential.async{ //I do not assume that is vital however I added it for testing
if self.vibro{
print("vibro is true")
}
else{
isImagePickerPresented.toggle()
}
print("DETECTED")
}
},
downBlock: {
//Identical because the upblock
}
})
volumeHandler?.begin(true)
}
I attempted making a boolean within the ImagePicker like within the code above in order that as a substitute of operating a perform inside imagepicker, it will get known as by a distinct supply when it detects a change within the boolean. This does not work. I attempted loads with Dispatch.world().async and Dispatch.essential.async to see if it might work as a result of CHHapticEngine must be run in the principle thread and I feel within the jps library supply file it additionally runs on the principle thread. So possibly it is a drawback to do with concurrency?
May be an extended query however any assist can be appreciated!