Skip to content

Instantly share code, notes, and snippets.

@satishbabariya
Created July 2, 2018 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satishbabariya/87ccf88a421a5c01843c5fe303ddcb28 to your computer and use it in GitHub Desktop.
Save satishbabariya/87ccf88a421a5c01843c5fe303ddcb28 to your computer and use it in GitHub Desktop.
Application
struct Application {
struct Auth {
static func isUserLoggedin() -> Bool {
return Defaults[.authToken] != ""
}
static func signOut() {
Defaults.removeAll()
Application.appDelegate()?.isAuthorized()
}
static func signOut(in time: Double) {
Async.main(after: time) {
Defaults.removeAll()
Application.appDelegate()?.isAuthorized()
}
}
static func authTokenKey() -> String {
return DefaultsKeys.authToken._key
}
static func bearerToken() -> String {
return "Bearer \(Defaults[.authToken])"
}
static func saveToken(_ token: String) {
Defaults[.authToken] = token
}
/// Saves Users Information to User Defaults in JSON String
///
/// - Parameter data: Information Dictionary
static func save(_ data: [String: Any]) {
Defaults[.userInfo] = data.JSONString()
}
static func currentUser() -> User {
guard let data: [String: Any] = Defaults[.userInfo].toAny() as? [String: Any] else {
return User()
}
return User(from: data)
}
/// By using a listener, you ensure that the Auth object isn't in an intermediate state—such as initialization—when you get the current user.
static func addStateDidChangeListener() -> Observable<String?> {
return UserDefaults.standard.rx.observe(String.self, self.authTokenKey())
}
}
struct Location {
static func isEmpty() -> Bool{
return Defaults[.latitude] == 0.0 && Defaults[.longitude] == 0.0
}
static func current() -> CLLocation {
return CLLocation(latitude: CLLocationDegrees(Defaults[.latitude]), longitude: CLLocationDegrees(Defaults[.longitude]))
}
static func save(_ location : CLLocation) {
Defaults[.latitude] = location.coordinate.latitude
Defaults[.longitude] = location.coordinate.longitude
}
}
struct Filter {
static func distance() -> Double{
return Defaults[.distance] == 0.0 ? 50.0 : Defaults[.distance]
}
static func save(distance : Double) {
Defaults[.distance] = distance
}
}
struct Environment {
private static let production: Bool = {
#if DEBUG
print("DEBUG")
return false
#else
print("PRODUCTION")
return true
#endif
}()
static func isProduction() -> Bool {
return self.production
}
static func isDebug() -> Bool {
return !self.production
}
static func isSimulator() -> Bool{
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
static func appDelegate() -> AppDelegate? {
return UIApplication.shared.delegate as? AppDelegate
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment