User.swift 4.45 KB
Newer Older
domenicw's avatar
domenicw committed
1
2
3
4
5
6
7
8
9
10
11
12
//
//  User.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 13.10.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation

public struct User {
    
13
14
15
16
17
18
19
20
21
22
23
24
25
    public var nethz: String?
    public var firstname: String
    public var lastname: String
    public var membership: AMIVMembership
    public var legi: String?
    public var department: ETHDepartment?
    public var gender: Gender
    public var email: String
    public var rfid: String?
    public var phone: String?
    public var isPasswordSet: Bool
    public var id: String
    public var etag: String
domenicw's avatar
domenicw committed
26
27
28
    
}

domenicw's avatar
domenicw committed
29
extension User: Codable {
domenicw's avatar
domenicw committed
30
31
32
    
    private enum UserCodingKeys: String, CodingKey {
        case nethz = "nethz"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        case firstname = "firstname"
        case lastname = "lastname"
        case membership = "membership"
        case legi = "legi"
        case department = "department"
        case gender = "gender"
        case password = "password"
        case email = "email"
        case rfid = "rfid"
        case phone = "phone"
        case wantsNewsletter = "send_newsletter"
        case isPasswordSet = "password_set"
        case id = "_id"
        case etag = "_etag"
domenicw's avatar
domenicw committed
47
48
49
50
51
    }
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: UserCodingKeys.self)
        
52
53
54
55
56
57
58
59
60
61
62
63
64
        self.nethz = try? container.decode(String.self, forKey: .nethz)
        self.firstname = try container.decode(String.self, forKey: .firstname)
        self.lastname = try container.decode(String.self, forKey: .lastname)
        self.membership = try container.decode(AMIVMembership.self, forKey: .membership)
        self.legi = try? container.decode(String.self, forKey: .legi)
        self.department = try? container.decode(ETHDepartment.self, forKey: .department)
        self.gender = try container.decode(Gender.self, forKey: .gender)
        self.email = try container.decode(String.self, forKey: .email)
        self.rfid = try? container.decode(String.self, forKey: .rfid)
        self.phone = try? container.decode(String.self, forKey: .phone)
        self.isPasswordSet = try container.decode(Bool.self, forKey: .isPasswordSet)
        self.id = try container.decode(String.self, forKey: .id)
        self.etag = try container.decode(String.self, forKey: .etag)
domenicw's avatar
domenicw committed
65
66
    }
    
domenicw's avatar
domenicw committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    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
        }
    }
    
domenicw's avatar
domenicw committed
124
}