Skip to content

Commit 2becf97

Browse files
authored
Merge pull request #462 from loopandlearn/align-remote-errors
Align remote errors
2 parents dd2b848 + 146d318 commit 2becf97

File tree

4 files changed

+272
-195
lines changed

4 files changed

+272
-195
lines changed

LoopFollow/Remote/LoopAPNS/LoopAPNSBolusView.swift

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -352,43 +352,28 @@ struct LoopAPNSBolusView: View {
352352
otp: otpCode
353353
)
354354

355-
Task {
356-
do {
357-
let apnsService = LoopAPNSService()
358-
let success = try await apnsService.sendBolusViaAPNS(payload: payload)
359-
360-
DispatchQueue.main.async {
361-
isLoading = false
362-
if success {
363-
// Mark TOTP code as used
364-
TOTPService.shared.markTOTPAsUsed(qrCodeURL: Storage.shared.loopAPNSQrCodeURL.value)
365-
alertMessage = "Insulin sent successfully!"
366-
alertType = .success
367-
LogManager.shared.log(
368-
category: .apns,
369-
message: "Insulin sent - Amount: \(insulinAmount.doubleValue(for: .internationalUnit()))U"
370-
)
371-
} else {
372-
alertMessage = "Failed to send insulin. Check your Loop APNS configuration."
373-
alertType = .error
374-
LogManager.shared.log(
375-
category: .apns,
376-
message: "Failed to send insulin"
377-
)
378-
}
379-
showAlert = true
380-
}
381-
} catch {
382-
DispatchQueue.main.async {
383-
isLoading = false
384-
alertMessage = "Error sending insulin: \(error.localizedDescription)"
385-
alertType = .error
355+
let apnsService = LoopAPNSService()
356+
apnsService.sendBolusViaAPNS(payload: payload) { success, errorMessage in
357+
DispatchQueue.main.async {
358+
self.isLoading = false
359+
if success {
360+
// Mark TOTP code as used
361+
TOTPService.shared.markTOTPAsUsed(qrCodeURL: Storage.shared.loopAPNSQrCodeURL.value)
362+
self.alertMessage = "Insulin sent successfully!"
363+
self.alertType = .success
386364
LogManager.shared.log(
387365
category: .apns,
388-
message: "APNS insulin error: \(error.localizedDescription)"
366+
message: "Insulin sent - Amount: \(insulinAmount.doubleValue(for: .internationalUnit()))U"
367+
)
368+
} else {
369+
self.alertMessage = errorMessage ?? "Failed to send insulin. Check your Loop APNS configuration."
370+
self.alertType = .error
371+
LogManager.shared.log(
372+
category: .apns,
373+
message: "Failed to send insulin: \(errorMessage ?? "unknown error")"
389374
)
390-
showAlert = true
391375
}
376+
self.showAlert = true
392377
}
393378
}
394379
}

LoopFollow/Remote/LoopAPNS/LoopAPNSCarbsView.swift

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -382,45 +382,30 @@ struct LoopAPNSCarbsView: View {
382382
otp: otpCode
383383
)
384384

385-
Task {
386-
do {
387-
let apnsService = LoopAPNSService()
388-
let success = try await apnsService.sendCarbsViaAPNS(payload: payload)
389-
390-
DispatchQueue.main.async {
391-
isLoading = false
392-
if success {
393-
// Mark TOTP code as used
394-
TOTPService.shared.markTOTPAsUsed(qrCodeURL: Storage.shared.loopAPNSQrCodeURL.value)
395-
let timeFormatter = DateFormatter()
396-
timeFormatter.timeStyle = .short
397-
alertMessage = "Carbs sent successfully for \(timeFormatter.string(from: adjustedConsumedDate))!"
398-
alertType = .success
399-
LogManager.shared.log(
400-
category: .apns,
401-
message: "Carbs sent - Amount: \(carbsAmount.doubleValue(for: .gram()))g, Absorption: \(absorptionTimeString)h, Time: \(adjustedConsumedDate)"
402-
)
403-
} else {
404-
alertMessage = "Failed to send carbs. Check your Loop APNS configuration."
405-
alertType = .error
406-
LogManager.shared.log(
407-
category: .apns,
408-
message: "Failed to send carbs"
409-
)
410-
}
411-
showAlert = true
412-
}
413-
} catch {
414-
DispatchQueue.main.async {
415-
isLoading = false
416-
alertMessage = "Error sending carbs: \(error.localizedDescription)"
417-
alertType = .error
385+
let apnsService = LoopAPNSService()
386+
apnsService.sendCarbsViaAPNS(payload: payload) { success, errorMessage in
387+
DispatchQueue.main.async {
388+
self.isLoading = false
389+
if success {
390+
// Mark TOTP code as used
391+
TOTPService.shared.markTOTPAsUsed(qrCodeURL: Storage.shared.loopAPNSQrCodeURL.value)
392+
let timeFormatter = DateFormatter()
393+
timeFormatter.timeStyle = .short
394+
self.alertMessage = "Carbs sent successfully for \(timeFormatter.string(from: adjustedConsumedDate))!"
395+
self.alertType = .success
418396
LogManager.shared.log(
419397
category: .apns,
420-
message: "APNS carbs error: \(error.localizedDescription)"
398+
message: "Carbs sent - Amount: \(carbsAmount.doubleValue(for: .gram()))g, Absorption: \(absorptionTimeString)h, Time: \(adjustedConsumedDate)"
399+
)
400+
} else {
401+
self.alertMessage = errorMessage ?? "Failed to send carbs. Check your Loop APNS configuration."
402+
self.alertType = .error
403+
LogManager.shared.log(
404+
category: .apns,
405+
message: "Failed to send carbs: \(errorMessage ?? "unknown error")"
421406
)
422-
showAlert = true
423407
}
408+
self.showAlert = true
424409
}
425410
}
426411
}

0 commit comments

Comments
 (0)