我正在开发 Pomdoro 函数,唯一的问题是,当计时器运行时,iPhone 或 iPad 会因为您未使用设备而关闭。我该如何在代码中更改此设置,使其在计时器处于活动状态时不会关闭,或者我可以使用开关自行决定是否要这样做?
非常感谢你的帮助
我最初尝试在 XCode 中激活后台处理,并认为它会自动转换,但不幸的是,这没有帮助。
这是我的代码:
struct FocusView: View {
@State var progress: Double = 0
@State var isTimerViewPresented: Bool = false
@AppStorage("focusTime") private var focusTime: Double = 0
@State private var workDuration: Double = 25
@State private var pauseDuration: Double = 5
@AppStorage("modus") private var modus: String = "work"
@State var isRunning: Bool = false
@State var pausenint: Int = 2
@State var wiederholungen_Int: Int = 0
@Binding var repetitions: Double
@AppStorage("totalWorkTime") private var totalWorkTime: Double = 0
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State var isCompleteWorkTimeShow: Bool = false
@State private var showCompletionSheet: Bool = false
@State private var totalFocusTime: TimeInterval = 0
func convertSecondsToTime(timeInSeconds: Int) -> String {
let minutes = timeInSeconds / 60
let seconds = timeInSeconds % 60
return String(format: "%02i:%02i", minutes, seconds)
}
var body: some View {
ZStack {
VStack {
Spacer()
VStack {
ZStack {
CircularProgressView_pomodoro(progress: $progress, modus: modus)
Text(convertSecondsToTime(timeInSeconds: Int(focusTime)))
.font(.custom("ArialRoundedMTBold", size: 70))
.onTapGesture {
withAnimation {
isTimerViewPresented.toggle()
}
}
.sheet(isPresented: $isTimerViewPresented) {
TimerView(
progress: $progress,
isTimerViewPresented: $isTimerViewPresented,
sliderValue: $workDuration,
sliderValue_pause: $pauseDuration,
sliderValue_wiederholungen: $repetitions,
focusTime: $focusTime, modus: $modus, pause: $pausenint, wiederholungen: $wiederholungen_Int
)
.presentationBackground(.clear)
}
}
.frame(width: 250, height: 250)
}
Spacer()
HStack {
Spacer()
Button(action: resetTimer) {
Image(systemName: "backward")
.font(.system(size: 42))
.foregroundColor(.primary)
}
.padding()
Spacer()
Button(action: toggleTimer) {
Image(systemName: isRunning ? "pause" : "play")
.font(.system(size: 42))
.foregroundColor(.primary)
}
.padding()
Spacer()
Button(action: skipTimer) {
Image(systemName: "forward")
.font(.system(size: 42))
.foregroundColor(.primary)
}
.padding()
Spacer()
}
if isCompleteWorkTimeShow {
HStack {
Text("Gesamte Arbeitszeit: \(convertSecondsToTime(timeInSeconds: Int(totalWorkTime)))")
.font(.headline)
.foregroundColor(.primary)
Image(systemName: "arrow.clockwise")
.foregroundColor(.primary)
.onTapGesture {
totalWorkTime = 0
}
.padding()
}
}
Spacer()
}
}
.onReceive(timer) { _ in
updateTimer()
}
.sheet(isPresented: $showCompletionSheet) {
CompletionView(totalFocusTime: totalFocusTime, onDismiss: {
showCompletionSheet = false
})
}
}
private func resetTimer() {
focusTime = modus == "work" ? workDuration * 60 : pauseDuration * 60
progress = 0
isRunning = false
}
private func toggleTimer() {
isRunning.toggle()
}
private func skipTimer() {
focusTime = 0
updateTimer()
}
private func updateTimer() {
if isRunning && repetitions > 0 {
if focusTime > 0 {
focusTime -= 1
if modus == "work" {
totalWorkTime += 1
totalFocusTime += 1
}
progress = 1.0 - (focusTime / (modus == "work" ? workDuration * 60 : pauseDuration * 60))
} else {
switchMode()
}
}
}
private func switchMode() {
if modus == "work" {
modus = "pause"
focusTime = pauseDuration * 60
scheduleNotification(for: modus)
} else {
modus = "work"
focusTime = workDuration * 60
repetitions -= 1
if repetitions > 0 {
scheduleNotification(for: modus)
} else {
completeSession()
}
}
progress = 0
}
private func completeSession() {
isRunning = false
showCompletionSheet = true
}
private func scheduleNotification(for mode: String) {
let content = UNMutableNotificationContent()
content.title = "Pomodoro Timer"
content.body = NSLocalizedString(mode == "work" ? "Zeit, wieder an die Arbeit zu gehen!" : "Zeit für eine Pause!", comment: "Pomodoro timer notification")
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "PomodoroTimer", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}
您可以使用isIdleTimerDisabled。
根据您设置应用的方式:
或者我已将其添加到 SwiftUI 中的 AppDelegate。
false
只要记住在完成后将其设置为即可。