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 6, 2024
1 parent 5be8685 commit 8cb1085
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion BeeSwift/TimerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@
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
var timingSince: Date?
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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -125,6 +147,8 @@ class TimerViewController: UIViewController {
self.timer = nil
self.timingSince = nil
}

resetIdleTimer()
}

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

UIApplication.shared.isIdleTimerDisabled = false
resetIdleTimer()
}

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


}

0 comments on commit 8cb1085

Please sign in to comment.