forked from dashxhq/dashx-demo-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Models.swift
244 lines (205 loc) · 5.57 KB
/
Models.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// Models.swift
// DashX Demo
//
// Created by Ravindar Katkuri on 28/06/22.
//
import Foundation
import DashX
// MARK: - Network Responses
// MARK: - LoginResponse
struct LoginResponse: Codable {
enum CodingKeys: String, CodingKey {
case message, token
case dashXToken = "dashx_token"
}
var message: String?
var token: String?
var dashXToken: String?
var decodedToken: JWTTokenContent? {
if let token = self.token {
let result = try? decode(jwtToken: token, as: JWTTokenContent.self)
return result
}
return nil
}
private func decode<T: Decodable>(jwtToken jwt: String, as type: T.Type) throws -> T? {
func base64Decode(_ base64: String) throws -> Data? {
let base64 = base64
.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")
let padded = base64.padding(toLength: ((base64.count + 3) / 4) * 4, withPad: "=", startingAt: 0)
guard let decoded = Data(base64Encoded: padded, options: .ignoreUnknownCharacters) else {
return nil
}
return decoded
}
func decodeJWTPart(_ value: String) throws -> T? {
if let bodyData = try base64Decode(value) {
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = .base64
return try? decoder.decode(T.self, from: bodyData)
}
return nil
}
let segments = jwt.components(separatedBy: ".")
return try decodeJWTPart(segments[1])
}
}
// MARK: - ProfileResponse
struct ProfileResponse: Codable {
var message: String?
var user: User?
}
// MARK: - UpdateProfileResponse
struct UpdateProfileResponse: Codable {
var message: String?
var user: User?
}
// MARK: - PostsResponse
struct PostsResponse: Codable {
let posts: [Post]
}
// MARK: - AddPostResponse
struct AddPostResponse: Codable {
let message: String
let post: Post
}
// MARK: - Post
struct Post: Codable {
let id, userID: Int
let text: String
// MARK: Type isn't defined, always null
// let image, video: Type Don't know?
let createdAt, updatedAt: String
let bookmarkedAt: String?
let user: User
var isBookmarked: Bool {
bookmarkedAt != nil
}
enum CodingKeys: String, CodingKey {
case id
case userID = "user_id"
case text
// case image, video
case createdAt = "created_at"
case updatedAt = "updated_at"
case bookmarkedAt = "bookmarked_at"
case user
}
}
// MARK: - JWTTokenContent
struct JWTTokenContent: Codable {
// // Define key mapping with JSON
enum CodingKeys: String, CodingKey {
case user, iat, exp
}
var user: User?
var iat: Int?
var exp: Int?
}
// MARK: - ErrorResponse
struct ErrorResponse: Codable {
var message: String?
}
// MARK: - ContactUsResponse
struct ContactUsResponse: Codable {
var message: String
}
// MARK: - BookmarksResponse
struct BookmarksResponse: Codable {
let message: String
let bookmarks: [Bookmark]
}
// MARK: - NoResponse
struct NoResponse: Codable {
init() { }
}
// MARK: - Bookmark
struct Bookmark: Codable {
let id, userID: Int
let text: String
// MARK: Type isn't defined, always null
// let image, video: Type Don't know?
let createdAt, updatedAt: String
let bookmarkID: Int
let bookmarkedAt: String
let user: User
enum CodingKeys: String, CodingKey {
case id
case userID = "user_id"
case text
// case image, video
case createdAt = "created_at"
case updatedAt = "updated_at"
case bookmarkID = "bookmark_id"
case bookmarkedAt = "bookmarked_at"
case user
}
}
// MARK: - MessageResponse
// Used for responses with only "message" field e.g. signup / forgot password
typealias MessageResponse = ErrorResponse
// MARK: Business Data Models
// MARK: - User
struct User: Codable {
enum CodingKeys: String, CodingKey {
case id, email
case firstName = "first_name"
case lastName = "last_name"
case avatar
}
var id: Int?
var firstName: String?
var lastName: String?
var email: String?
var avatar: AssetData?
var name: String {
var temp = firstName ?? ""
if firstName != nil {
temp += " "
}
temp += lastName ?? ""
return temp
}
var idString: String? {
id == nil ? nil : String(id!)
}
}
// MARK: - PreferenceDataResponse
struct PreferenceDataResponse: Codable {
var newBookmark: DashX.Preference
var newPost: DashX.Preference
var newBookmarkNotificationEnabled: Bool {
newBookmark.enabled ?? false
}
var newPostNotificationEnabled: Bool {
newPost.enabled ?? false
}
enum CodingKeys: String, CodingKey {
case newBookmark = "new-bookmark"
case newPost = "new-post"
}
}
// MARK: - AssetData
struct AssetData: Codable {
var status: String?
var url: String?
}
// MARK: - ExternalAssetResponse
struct ExternalAssetResponse: Codable {
let status: String?
let id: String?
struct ExternalAssetData: Codable {
let assetData: AssetData?
enum CodingKeys: String, CodingKey {
case assetData = "asset"
}
}
let data: ExternalAssetData?
enum CodingKeys: String, CodingKey {
case status
case id
case data
}
}