I'm trying to create a productivity app that utilizes gyroscope data to detect when the phone is picked up. However, in my code, it seems that after I detect the phone flipping over, gyro updates become unavailable and I start receiving the sentinel value instead, which causes the code to not work properly. What happens is that I will receive gyro updates until faceUp changes value, at which point gyroData becomes unavailable.
let dTheta = Double(self.manager.gyroData?.rotationRate.y ?? 0)
if dTheta > 0.1 || dTheta < -0.1 {
totalRotation = totalRotation + dTheta
}
tO = Date.now
if(totalRotation >= 0.9 || totalRotation <= -0.9) {
//Block that detects a phone flip
if faceUp == true {
faceUp = false
}
else if faceUp == false {
faceUp = true
stopText = "Timer paused at \(currentTimeRemaining)"
}
totalRotation = 0
}
guard shouldProceed() else {
return
}
currentTimeRemaining = dispTime(mustRedo: mustRedo)
keepScreenOn()
let hours = job.hours
let mins = job.mins
let secs = job.seconds
if mins == 0 && hours == 0 {
minsDone = true
}
if secs == 0 && mins == 0 {
secsDone = true
}
if mins == 0 && minsDone == false {
job.setHours(new: (hours-1))
job.setMins(new: 59)
}
if job.seconds == 0 && secsDone == false{
job.setMins(new: (mins-1))
job.setSeconds(new: 59)
}
if hours == 0 && minsDone == true && secsDone == true {
workTimeActive = false
//stopTimer()
}
else {
job.setSeconds(new: job.seconds-1)
}
if manager.isGyroActive == false {print("not active tag 3")}
currentTimeRemaining = dispTime(mustRedo: mustRedo)
private func keepScreenOn() {
UIApplication.shared.isIdleTimerDisabled = true
}
private func shouldProceed() -> Bool{
if faceUp == true {
if manager.isGyroActive == false {print("not active tag 4")}
return false
}
return true
}
Post
Replies
Boosts
Views
Activity
I'm trying to create a simple enough countdown timer using properties from a "Job" object, which I created, which holds a name: String, hours: Int, and mins: Int. It seems that on line 20 I receive an index out of range fatal error when I try to run it in the simulator, which is odd considering it doesn't have an issue beside not working quite right in the preview. Not to mention that in the context of running the whole app, I wouldn't access an array for this file. Code:
iimport SwiftUI
import Combine
struct JobTimer: View {
@Binding var job: Job
@State var seconds = 0
@State var startDate = Date.now
@State var timeElapsed: Int = 0
@State private var cancellable: AnyCancellable?
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
let currentTime = Date.now
var body: some View {
Text("\(job.hours) : \(job.mins) : \(seconds)")
.onReceive(timer) { firedDate in
if job.hours == 0 && job.mins == 0 && seconds == 0 {
stopTimer()
}
if job.mins == 0 {
job.setHours(new: (job.hours-1))
job.setMins(new: 59)
}
if seconds == 0 {
job.setMins(new: (job.mins-1))
seconds = 59
}
else {
seconds -= 1
}
}
}
private func stopTimer(){
cancellable?.cancel()
}
}