Skip to content

Instantly share code, notes, and snippets.

@mspvirajpatel
Created June 15, 2020 13:04
Show Gist options
  • Save mspvirajpatel/ff334fea37061bcf617783095dcefdf5 to your computer and use it in GitHub Desktop.
Save mspvirajpatel/ff334fea37061bcf617783095dcefdf5 to your computer and use it in GitHub Desktop.
public struct LoginUserData {
public var userId: String? = ""
public var userName: String? = ""
public var email: String? = ""
public var gender: Int = 0
public var birthday: String? = ""
public var about: String? = ""
public var photoUrl: String? = ""
public var idToken: String? = ""
public var accessToken: String? = ""
}
@mspvirajpatel
Copy link
Author

class Defaults {

static let shared = Defaults()

let appDefaults = UserDefaults(suiteName: Constant.Application.groupIdentifier)
var currentUser: User? {
    get {
        if let loggedUser = appDefaults?.object(forKey: "loggedUser") as? Data {
            let decoder = JSONDecoder()
            return try? decoder.decode(User.self, from: loggedUser)
        }
        return nil
    }
    set {
        let encoder = JSONEncoder()
        if let encoded = try? encoder.encode(newValue) {
            appDefaults?.set(encoded, forKey: "loggedUser")
            appDefaults?.synchronize()
        } else {
            appDefaults?.set(nil, forKey: "loggedUser")
            appDefaults?.synchronize()
        }
    }
}

}

@mspvirajpatel
Copy link
Author

   public typealias UserDataComplition = (_ userData: LoginUserData?, _ error: Error?) -> Void

var userDataBlock: UserDataComplition?

public func login(controller: UIViewController, complitionBlock: @escaping UserDataComplition, didDisconnectBlock: @escaping UserDataComplition) {
userDataBlock = complitionBlock
userDidDisconnectWithBlock = didDisconnectBlock
googleManager?.presentingViewController = controller
googleManager?.delegate = self
googleManager?.signIn()
}

userData = data
if let block = self.userDataBlock {
block(data, nil)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment