-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
keeping the timer screen on with timer running #508
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this should be a function of the default brightness level? If the user has brightness turned low this could end up increasing it, right? |
||
private let idleTimeout: TimeInterval = 10 | ||
|
||
init(goal: Goal) { | ||
self.goal = goal | ||
self.units = Self.timerUnit(goal: goal) ?? .hours | ||
|
@@ -96,6 +104,16 @@ class TimerViewController: UIViewController { | |
resetButton.setTitle("Reset", for: .normal) | ||
} | ||
|
||
override func viewWillDisappear(_ animated: Bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to when leaving the screen, should we disable the brightness override when the app is moved to background? |
||
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) | ||
} | ||
|
@@ -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) | ||
|
@@ -131,6 +153,8 @@ class TimerViewController: UIViewController { | |
self.timer = nil | ||
self.timingSince = nil | ||
} | ||
|
||
resetIdleTimer() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The lifecycle of "when we disable the screen" is a little surprising to me. In particular, we enter "do not sleep" mode when you start the timer, but do not leave it when you stop the timer. I'd suggest either enabling it as soon as the user enters the screen, or disabling it when the timer is not running. |
||
} | ||
|
||
@objc func resetButtonPressed() { | ||
|
@@ -140,6 +164,9 @@ class TimerViewController: UIViewController { | |
self.timingSince = nil | ||
self.accumulatedSeconds = 0 | ||
self.updateTimerLabel() | ||
|
||
UIApplication.shared.isIdleTimerDisabled = false | ||
resetIdleTimer() | ||
} | ||
|
||
func urtext() -> String { | ||
|
@@ -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 { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably capture the current value as late as possible (i.e. just before changing it), to minimize surprising behavior if the user changes brightness while on this screen?