Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

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?

private let dimmedBrightnessLevel: CGFloat = 0.2
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -96,6 +104,16 @@ class TimerViewController: UIViewController {
resetButton.setTitle("Reset", for: .normal)
}

override func viewWillDisappear(_ animated: Bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
}
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()
Copy link
Collaborator

Choose a reason for hiding this comment

The 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() {
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
Loading