Created
February 1, 2018 07:48
-
-
Save Anatoli-Petrosyants/cbe7d5e6890242ab5dd84e5c7ebd10be to your computer and use it in GitHub Desktop.
This file contains 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
public final class FirstLaunchHelper { | |
let wasLaunchedBefore: Bool | |
var isFirstLaunch: Bool { | |
return !wasLaunchedBefore | |
} | |
init(getWasLaunchedBefore: () -> Bool, | |
setWasLaunchedBefore: (Bool) -> ()) { | |
let wasLaunchedBefore = getWasLaunchedBefore() | |
self.wasLaunchedBefore = wasLaunchedBefore | |
if !wasLaunchedBefore { | |
setWasLaunchedBefore(true) | |
} | |
} | |
convenience init(userDefaults: UserDefaults, key: String) { | |
self.init(getWasLaunchedBefore: { userDefaults.bool(forKey: key) }, | |
setWasLaunchedBefore: { userDefaults.set($0, forKey: key) }) | |
} | |
} | |
extension FirstLaunchHelper { | |
static func alwaysFirst() -> FirstLaunchHelper { | |
return FirstLaunchHelper(getWasLaunchedBefore: { return false }, setWasLaunchedBefore: { _ in }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment