Commit 38c2efc0 authored by domenicw's avatar domenicw
Browse files

Session User is now stored locally

parent a7e8fd9a
......@@ -4,7 +4,7 @@
<dict>
<key>SchemeUserState</key>
<dict>
<key>Amiv.xcscheme</key>
<key>Amiv.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
......
......@@ -71,6 +71,18 @@ extension OnboardingNavigator: LoginViewControllerDelegate {
// Save token into secure and encrypted keychain
SessionManager.save(response)
let userManager = NetworkManager<AMIVApiUser>()
userManager.getUserInfo({ (user, error) in
guard error == nil, let user = user else {
debugPrint(error)
return
}
if user.saveLocal() {
debugPrint(User.loadLocal())
}
})
DispatchQueue.main.async {
self.delegate?.onboardingFinished()
}
......
......@@ -26,7 +26,7 @@ public struct User {
}
extension User: Decodable {
extension User: Codable {
private enum UserCodingKeys: String, CodingKey {
case nethz = "nethz"
......@@ -64,4 +64,61 @@ extension User: Decodable {
self.etag = try container.decode(String.self, forKey: .etag)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: UserCodingKeys.self)
try container.encode(self.nethz, forKey: .nethz)
try container.encode(self.firstname, forKey: .firstname)
try container.encode(self.lastname, forKey: .lastname)
try container.encode(self.membership, forKey: .membership)
try container.encode(self.legi, forKey: .legi)
try container.encode(self.department, forKey: .department)
try container.encode(self.gender, forKey: .gender)
try container.encode(self.email, forKey: .email)
try container.encode(self.rfid, forKey: .rfid)
try container.encode(self.phone, forKey: .phone)
try container.encode(self.isPasswordSet, forKey: .isPasswordSet)
try container.encode(self.id, forKey: .id)
try container.encode(self.etag, forKey: .etag)
}
}
extension User {
public static func loadLocal() -> User? {
let fileManager = FileManager.default
do {
let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = documentDirectory.appendingPathComponent("user.json")
guard fileManager.fileExists(atPath: fileURL.path) else {
return nil
}
let data = try Data(contentsOf: fileURL)
let json = try JSONDecoder().decode(User.self, from: data)
return json
} catch {
return nil
}
}
public func saveLocal() -> Bool {
return User.saveLocal(self)
}
public static func saveLocal(_ user: User) -> Bool {
let fileManager = FileManager.default
do {
let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = documentDirectory.appendingPathComponent("user.json")
let json = try JSONEncoder().encode(user)
try json.write(to: fileURL)
return true
} catch {
return false
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment