99import UIKit
1010import SnapKit
1111import MBProgressHUD
12+ import OSLog
1213
1314import BeeKit
1415
1516class TimerViewController : UIViewController {
17+ let logger = Logger ( subsystem: " com.beeminder.beeminder " , category: " TimerViewController " )
18+
1619 private enum TimerUnit {
1720 case hours, minutes
1821 }
@@ -26,6 +29,11 @@ class TimerViewController: UIViewController {
2629
2730 var accumulatedSeconds = 0
2831
32+ private var idleTimer : Timer ?
33+ private var defaultBrightnessLevel : CGFloat = UIScreen . main. brightness
34+ private let dimmedBrightnessLevel : CGFloat = 0.2
35+ private let idleTimeout : TimeInterval = 10
36+
2937 init ( goal: Goal ) {
3038 self . goal = goal
3139 self . units = Self . timerUnit ( goal: goal) ?? . hours
@@ -96,6 +104,16 @@ class TimerViewController: UIViewController {
96104 resetButton. setTitle ( " Reset " , for: . normal)
97105 }
98106
107+ override func viewWillDisappear( _ animated: Bool ) {
108+ super. viewWillDisappear ( animated)
109+
110+ logger. info ( " (re-)enabling app idle timer, restoring screen brightness " )
111+ UIApplication . shared. isIdleTimerDisabled = false
112+ UIScreen . main. brightness = defaultBrightnessLevel
113+ idleTimer? . invalidate ( )
114+ idleTimer = nil
115+ }
116+
99117 @objc func exitButtonPressed( ) {
100118 self . presentingViewController? . dismiss ( animated: true , completion: nil )
101119 }
@@ -121,6 +139,10 @@ class TimerViewController: UIViewController {
121139
122140 @objc func startStopButtonPressed( ) {
123141 if self . timingSince == nil {
142+ if accumulatedSeconds == 0 {
143+ logger. info ( " disabling app idle timer, keeping screen awake while timer running " )
144+ UIApplication . shared. isIdleTimerDisabled = true
145+ }
124146 self . timingSince = Date ( )
125147 self . startStopButton. setTitle ( " Stop " , for: . normal)
126148 self . timer = Timer . scheduledTimer ( timeInterval: 1 , target: self , selector: #selector( self . updateTimerLabel) , userInfo: nil , repeats: true )
@@ -131,6 +153,8 @@ class TimerViewController: UIViewController {
131153 self . timer = nil
132154 self . timingSince = nil
133155 }
156+
157+ resetIdleTimer ( )
134158 }
135159
136160 @objc func resetButtonPressed( ) {
@@ -140,6 +164,9 @@ class TimerViewController: UIViewController {
140164 self . timingSince = nil
141165 self . accumulatedSeconds = 0
142166 self . updateTimerLabel ( )
167+
168+ UIApplication . shared. isIdleTimerDisabled = false
169+ resetIdleTimer ( )
143170 }
144171
145172 func urtext( ) -> String {
@@ -184,6 +211,38 @@ class TimerViewController: UIViewController {
184211 }
185212 }
186213 }
214+
215+
216+ fileprivate func restoreBrightness( ) {
217+ guard UIScreen . main. brightness < defaultBrightnessLevel else { return }
218+
219+ UIScreen . main. brightness = defaultBrightnessLevel
220+ }
221+
222+ // Reset idle timer, call whenever the user interacts
223+ private func resetIdleTimer( ) {
224+ logger. info ( " restoring screen brightness and starting a new idle timer for screen dimming " )
225+
226+ idleTimer? . invalidate ( )
227+
228+ restoreBrightness ( )
229+
230+ idleTimer = Timer . scheduledTimer ( timeInterval: idleTimeout, target: self , selector: #selector( dimScreen) , userInfo: nil , repeats: false )
231+ }
232+
233+ @objc private func dimScreen( ) {
234+ logger. info ( " dimming screen " )
235+ UIScreen . main. brightness = dimmedBrightnessLevel
236+ }
237+
238+ // Detect touches on the VC to reset idle timer and brighten screen
239+ override func touchesBegan( _ touches: Set < UITouch > , with event: UIEvent ? ) {
240+ super. touchesBegan ( touches, with: event)
241+ logger. info ( " touch detected " )
242+ resetIdleTimer ( )
243+ }
244+
245+
187246}
188247
189248private extension TimerViewController {
0 commit comments