Forked from andrewcampoli/BackgroundingSourceTracker.swift
Created
August 10, 2018 21:25
-
-
Save hyuni/a0fc9f7f3acb7674bb625fcb139a135e 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