Skip to content

Commit

Permalink
keep screen on, albeit dimmed, with active timer on timer screen
Browse files Browse the repository at this point in the history
fixes: #268
  • Loading branch information
krugerk committed Nov 15, 2024
1 parent 22f47bf commit 91c96c2
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions BeeSwift/TimerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

private enum TimerUnit {
case hours, minutes
}
Expand All @@ -26,6 +29,11 @@ class TimerViewController: UIViewController {

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
self.units = Self.timerUnit(goal: goal) ?? .hours
Expand Down Expand Up @@ -96,6 +104,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)
}
Expand All @@ -121,6 +139,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)
Expand All @@ -131,6 +153,8 @@ class TimerViewController: UIViewController {
self.timer = nil
self.timingSince = nil
}

resetIdleTimer()
}

@objc func resetButtonPressed() {
Expand All @@ -140,6 +164,9 @@ class TimerViewController: UIViewController {
self.timingSince = nil
self.accumulatedSeconds = 0
self.updateTimerLabel()

UIApplication.shared.isIdleTimerDisabled = false
resetIdleTimer()
}

func urtext() -> String {
Expand Down Expand Up @@ -184,6 +211,38 @@ 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<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
logger.info("touch detected")
resetIdleTimer()
}


}

private extension TimerViewController {
Expand Down

0 comments on commit 91c96c2

Please sign in to comment.