Skip to content

Commit 14969e4

Browse files
authored
reusing RequestManager.addDatapoint (#613)
## Summary Commit 3be26e8 introduced an addDatapoint to RequestManager. This MR consolidates the places in code adding datapoints directly by using the method in RequestManager. ## Validation ran app in simulator added datapoint using the timer
1 parent de9809a commit 14969e4

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

BeeKit/Managers/DataPointManager.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public actor DataPointManager {
4646
let _ = try await requestManager.delete(url: "api/v1/users/{username}/goals/\(goal.slug)/datapoints/\(datapoint.id)")
4747
}
4848

49-
private func postDatapoint(goal : Goal, params : [String : String]) async throws {
50-
let _ = try await requestManager.post(url: "api/v1/users/{username}/goals/\(goal.slug)/datapoints.json", parameters: params)
49+
private func postDatapoint(goal : Goal, urText: String, requestId: String) async throws {
50+
let _ = try await requestManager.addDatapoint(urtext: urText, slug: goal.slug, requestId: requestId)
5151
}
5252

5353
private func fetchDatapoints(goal: Goal, sort: String, per: Int, page: Int) async throws -> [DataPoint] {
@@ -118,11 +118,12 @@ public actor DataPointManager {
118118
return
119119
}
120120

121-
let params = ["urtext": "\(newDataPoint.daystamp.day) \(newDataPoint.value) \"\(newDataPoint.comment)\"", "requestid": newDataPoint.requestid]
121+
let urText = "\(newDataPoint.daystamp.day) \(newDataPoint.value) \"\(newDataPoint.comment)\""
122+
let requestId = newDataPoint.requestid
122123

123124
logger.notice("Creating new datapoint for \(goal.id, privacy: .public) on \(newDataPoint.daystamp, privacy: .public): \(newDataPoint.value, privacy: .private)")
124125

125-
try await postDatapoint(goal: goal, params: params)
126+
try await postDatapoint(goal: goal, urText: urText, requestId: requestId)
126127
} else if matchingDatapoints.count >= 1 {
127128
let firstDatapoint = matchingDatapoints.remove(at: 0)
128129
for datapoint in matchingDatapoints {

BeeKit/Managers/RequestManager.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ public class RequestManager {
105105
])
106106
}
107107

108-
public func addDatapoint(urtext: String, slug: String) async throws -> Any? {
109-
let params = ["urtext": urtext, "requestid": UUID().uuidString]
108+
public func addDatapoint(urtext: String, slug: String, requestId: String? = nil) async throws -> Any? {
109+
let params = [
110+
"urtext": urtext,
111+
"requestid": requestId
112+
]
113+
.compactMapValues { $0 }
110114

111-
return try await post(url: "api/v1/users/{username}/goals/\(slug)/datapoints.json", parameters: params)
115+
return try await post(url: "api/v1/users/{username}/goals/\(slug)/datapoints.json",
116+
parameters: params)
112117
}
113118
}
114119

BeeSwift/TimerViewController.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ class TimerViewController: UIViewController {
165165

166166
Task { @MainActor in
167167
do {
168-
let params = ["urtext": self.urtext(), "requestid": UUID().uuidString]
169-
let _ = try await ServiceLocator.requestManager.post(url: "api/v1/users/{username}/goals/\(self.goal.slug)/datapoints.json", parameters: params)
168+
let _ = try await ServiceLocator.requestManager.addDatapoint(urtext: self.urtext(), slug: self.goal.slug)
170169
hud.mode = .text
171170
hud.label.text = "Added!"
172171
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {

0 commit comments

Comments
 (0)