Created
April 24, 2018 17:58
-
-
Save andrewcampoli/2b1f748fecf42288cc09f10a72485b36 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
enum BackgroundingSource: String { | |
case lock = "lock", homeOrSwitch = "homeOrSwitch" | |
} | |
protocol BackgroundingSourceTrackerDelegate: class { | |
func backgroundingSourceTracker(_ backgroundingSourceTracker: BackgroundingSourceTracker, didTrackBackgroundingWith source: BackgroundingSource) | |
} | |
final class BackgroundingSourceTracker { | |
weak var delegate: BackgroundingSourceTrackerDelegate? | |
private var source: BackgroundingSource? { | |
didSet { | |
guard let source = source else { | |
return | |
} | |
delegate?.backgroundingSourceTracker(self, didTrackBackgroundingWith: source) | |
} | |
} | |
private var brightness: CGFloat = 1 | |
private func didUserPressLockButton() -> Bool { | |
UIScreen.main.brightness = brightness + (brightness <= 0.01 ? (0.01) : (-0.01)) | |
let testBrightness = UIScreen.main.brightness | |
UIScreen.main.brightness = brightness | |
return brightness != testBrightness | |
} | |
func didEnterBackground() { | |
source = didUserPressLockButton() ? .lock : .homeOrSwitch | |
} | |
func didBecomeActive() { | |
brightness = UIScreen.main.brightness | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment