Created
June 11, 2021 09:12
-
-
Save geor-kasapidi/9e5a7f2efffb9b71b24c1678fd7e560b to your computer and use it in GitHub Desktop.
FOOM logger
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
| private final class AppInfo { | |
| private struct Keys { | |
| static let lastKnownAppVersion = "app_data.last_known_app_version" | |
| static let appWasInBackground = "app_data.app_was_in_background" | |
| static let appWasTerminated = "app_data.app_was_terminated" | |
| } | |
| struct State { | |
| let appVersionIsChanged: Bool | |
| let appWasInBackground: Bool | |
| let appWasTerminated: Bool | |
| func wasFOOM(wasCrash: Bool) -> Bool { | |
| return !wasCrash && !appVersionIsChanged && !appWasInBackground && !appWasTerminated | |
| } | |
| } | |
| let savedState: State | |
| init() { | |
| let currentAppVersion = Bundle.main.appShortVersion | |
| savedState = State( | |
| appVersionIsChanged: currentAppVersion != AppInfo.lastKnownAppVersion, | |
| appWasInBackground: AppInfo.appWasInBackground, | |
| appWasTerminated: AppInfo.appWasTerminated | |
| ) | |
| AppInfo.lastKnownAppVersion = currentAppVersion | |
| AppInfo.appWasInBackground = false | |
| AppInfo.appWasTerminated = false | |
| NotificationCenter.default.addObserver( | |
| self, | |
| selector: #selector(applicationWillTerminate), | |
| name: UIApplication.willTerminateNotification, | |
| object: nil | |
| ) | |
| NotificationCenter.default.addObserver( | |
| self, | |
| selector: #selector(applicationDidEnterBackground), | |
| name: UIApplication.didEnterBackgroundNotification, | |
| object: nil | |
| ) | |
| NotificationCenter.default.addObserver( | |
| self, | |
| selector: #selector(applicationWillEnterForeground), | |
| name: UIApplication.willEnterForegroundNotification, | |
| object: nil | |
| ) | |
| } | |
| deinit { | |
| NotificationCenter.default.removeObserver(self) | |
| } | |
| // MARK: - | |
| @objc | |
| private func applicationWillTerminate() { | |
| AppInfo.appWasTerminated = true | |
| } | |
| @objc | |
| private func applicationDidEnterBackground() { | |
| AppInfo.appWasInBackground = true | |
| } | |
| @objc | |
| private func applicationWillEnterForeground() { | |
| AppInfo.appWasInBackground = false | |
| } | |
| // MARK: - | |
| private static var lastKnownAppVersion: String { | |
| get { return UserDefaults.standard.string(forKey: Keys.lastKnownAppVersion) ?? "" } | |
| set { UserDefaults.standard.set(newValue, forKey: Keys.lastKnownAppVersion) } | |
| } | |
| private static var appWasInBackground: Bool { | |
| get { return UserDefaults.standard.bool(forKey: Keys.appWasInBackground) } | |
| set { UserDefaults.standard.set(newValue, forKey: Keys.appWasInBackground) } | |
| } | |
| private static var appWasTerminated: Bool { | |
| get { return UserDefaults.standard.bool(forKey: Keys.appWasTerminated) } | |
| set { UserDefaults.standard.set(newValue, forKey: Keys.appWasTerminated) } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment