From 8cb108541d56ed9c8507d36f79a12e09b98bc371 Mon Sep 17 00:00:00 2001 From: krugerk <4656811+krugerk@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:13:37 +0100 Subject: [PATCH] keep screen on, albeit dimmed, with active timer on timer screen fixes: #268 --- BeeSwift/TimerViewController.swift | 61 +++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/BeeSwift/TimerViewController.swift b/BeeSwift/TimerViewController.swift index 90375833..71368080 100644 --- a/BeeSwift/TimerViewController.swift +++ b/BeeSwift/TimerViewController.swift @@ -9,10 +9,13 @@ import UIKit import SnapKit import MBProgressHUD +import OSLog import BeeKit class TimerViewController: UIViewController { + let logger = Logger(subsystem: "com.beeminder.beeminder", category: "TimerViewController") + let timerLabel = BSLabel() let startStopButton = BSButton(type: .system) let goal: Goal @@ -20,7 +23,12 @@ class TimerViewController: UIViewController { var timer: Timer? var units: String? var accumulatedSeconds = 0 - + + private var idleTimer: Timer? + private var defaultBrightnessLevel: CGFloat = UIScreen.main.brightness + private let dimmedBrightnessLevel: CGFloat = 0.2 + private let idleTimeout: TimeInterval = 10 + init(goal: Goal) { self.goal = goal super.init(nibName: nil, bundle: nil) @@ -90,6 +98,16 @@ class TimerViewController: UIViewController { resetButton.setTitle("Reset", for: .normal) } + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + logger.info("(re-)enabling app idle timer, restoring screen brightness") + UIApplication.shared.isIdleTimerDisabled = false + UIScreen.main.brightness = defaultBrightnessLevel + idleTimer?.invalidate() + idleTimer = nil + } + @objc func exitButtonPressed() { self.presentingViewController?.dismiss(animated: true, completion: nil) } @@ -115,6 +133,10 @@ class TimerViewController: UIViewController { @objc func startStopButtonPressed() { if self.timingSince == nil { + if accumulatedSeconds == 0 { + logger.info("disabling app idle timer, keeping screen awake while timer running") + UIApplication.shared.isIdleTimerDisabled = true + } self.timingSince = Date() self.startStopButton.setTitle("Stop", for: .normal) self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTimerLabel), userInfo: nil, repeats: true) @@ -125,6 +147,8 @@ class TimerViewController: UIViewController { self.timer = nil self.timingSince = nil } + + resetIdleTimer() } @objc func resetButtonPressed() { @@ -134,6 +158,9 @@ class TimerViewController: UIViewController { self.timingSince = nil self.accumulatedSeconds = 0 self.updateTimerLabel() + + UIApplication.shared.isIdleTimerDisabled = false + resetIdleTimer() } func urtext() -> String { @@ -198,4 +225,36 @@ class TimerViewController: UIViewController { } } } + + + fileprivate func restoreBrightness() { + guard UIScreen.main.brightness < defaultBrightnessLevel else { return } + + UIScreen.main.brightness = defaultBrightnessLevel + } + + // Reset idle timer, call whenever the user interacts + private func resetIdleTimer() { + logger.info("restoring screen brightness and starting a new idle timer for screen dimming") + + idleTimer?.invalidate() + + restoreBrightness() + + idleTimer = Timer.scheduledTimer(timeInterval: idleTimeout, target: self, selector: #selector(dimScreen), userInfo: nil, repeats: false) + } + + @objc private func dimScreen() { + logger.info("dimming screen") + UIScreen.main.brightness = dimmedBrightnessLevel + } + + // Detect touches on the VC to reset idle timer and brighten screen + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + super.touchesBegan(touches, with: event) + logger.info("touch detected") + resetIdleTimer() + } + + }