This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FMChore.swift
192 lines (158 loc) · 5.92 KB
/
FMChore.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
//
// FlatMateChore.swift
// FlatMate
//
// Created by Jamie Walker on 8/08/19.
// Copyright © 2019 Jamie Walker. All rights reserved.
import Foundation
import SwiftyJSON
class FMChore {
// ================================
// Variables
// ================================
var choreId: String?
var choreDescription: String?
var userId: String?
var flatId: String?
var completed: Bool?
var autoRotate: Bool?
var rotateDays: String?
var dateLastRotated: String?
var nextRotateDay: String?
var session: FMSession?
// ================================
// Functions
// ================================
/*
Create a chore without API JSON data
- Parameters:
- session: the parent session
*/
init(session: FMSession?) { self.session = session }
/*
Create a chore with API JSON data
- Parameters:
- fromChoreJSON: the database JSON
- session: the parent session
*/
init(fromChoreJSON: JSON, session: FMSession) {
self.session = session
choreId = fromChoreJSON["choreId"].stringValue
choreDescription = fromChoreJSON["choreDescription"].stringValue
userId = fromChoreJSON["userId"].stringValue
flatId = fromChoreJSON["flatId"].stringValue
autoRotate = fromChoreJSON["autoRotate"].stringValue == "1"
rotateDays = fromChoreJSON["rotateDays"].stringValue
completed = fromChoreJSON["completed"].stringValue == "1"
dateLastRotated = fromChoreJSON["dateLastRotated"].stringValue
nextRotateDay = fromChoreJSON["nextRotateDay"].stringValue
}
/*
Gets the name of the user to do the chore to display
- Returns:
- name: "You" if the session user, firstName if a flatmate user
*/
func userName() -> String? {
if self.userId == session?.user?.userId {
return "You"
} else {
let user = self.session?.flat?.flatMates.first(where: { (u) -> Bool in
return u.userId == self.userId
})
return user?.firstName
}
}
/*
Updates the chore, replaces FMChore in flat with same choreId with new FMChore from database JSON data
- Completion:
- Replaces the chore in the flat with new chore from database JSON
- Parameter:
- success: true if successfully updated, false otherwise
*/
func update(completion: @escaping (Bool)->()) {
let DG = DispatchGroup()
// Setup query
let query = FMHTTPQuery()
let url = "https://www.jamieleewalker.com/flatmate/api/updateChore&token=\(self.session?.token ?? "")"
let body = databaseJSON().rawString()!.data(using: .utf8)!
// Perform query
var data: Data?
var statusCode: Int?
DG.enter()
query.doPut(url: url, body: body) { (d, i) in
// Get the data from the query
data = d
statusCode = i
DG.leave()
}
// Process query response
DG.notify(queue: .main) {
if statusCode != 200 {
completion(false)
} else {
let index = self.session?.flat?.chores.firstIndex(where: { (c) -> Bool in
return c.choreId == self.choreId
})
self.session?.flat?.chores.remove(at: index!)
do {
let choreJSON = try JSON(data: data!)
let newChore = FMChore(fromChoreJSON: choreJSON, session: self.session!)
self.session?.flat?.chores.insert(newChore, at: index!)
self.session?.flat?.categoriseChores()
completion(true)
} catch {
completion(false)
return
}
}
}
}
/*
Updates the chore completed status
- Completion:
- Updates the chore completion status
- Parameter:
- success: true if successfully updated, false otherwise
*/
func updateCompleted(completed: Bool, completion: @escaping (Bool)->()) {
let DG = DispatchGroup()
// Setup query
let query = FMHTTPQuery()
let url = "https://www.jamieleewalker.com/flatmate/api/updateChoreCompleted&token=\(self.session?.token ?? "")"
let body = JSON(["choreId" : self.choreId ?? "",
"flatId" : self.flatId ?? "",
"completed" : completed ? "1" : "0"]).rawString()!.data(using: .utf8)!
// Perform query
var statusCode: Int?
DG.enter()
query.doPut(url: url, body: body) { (_, i) in
// Get the data from the query
statusCode = i
DG.leave()
}
// Process response
DG.notify(queue: .main) {
if statusCode != 200 {
completion(false)
} else {
self.completed = completed
completion(true)
}
}
}
/*
Creates a SwiftyJSON JSON object containing the chore information
- Returns:
- JSON object containing the chore information
*/
func databaseJSON() -> JSON {
return JSON(["choreId": self.choreId ?? "",
"description": self.choreDescription ?? "",
"userId": self.userId ?? "",
"flatId": self.session?.flat?.flatId ?? "",
"completed": (self.completed ?? false) ? "1": "0",
"autoRotate": (self.autoRotate ?? false) ? "1": "0",
"rotateDays": self.rotateDays ?? "",
"timeZone": TimeZone.current.identifier])
}
}