RemoteViewController.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // RemoteViewController.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2024-07-19.
  6. // Copyright © 2024 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. import SwiftUI
  11. import HealthKit
  12. class RemoteViewController: UIViewController {
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. let remoteView = RemoteView(
  16. onCancelExistingTarget: cancelExistingTarget,
  17. sendTempTarget: sendTempTarget
  18. )
  19. let hostingController = UIHostingController(rootView: remoteView)
  20. addChild(hostingController)
  21. view.addSubview(hostingController.view)
  22. hostingController.view.translatesAutoresizingMaskIntoConstraints = false
  23. NSLayoutConstraint.activate([
  24. hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  25. hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  26. hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
  27. hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
  28. ])
  29. hostingController.didMove(toParent: self)
  30. if(!ObservableUserDefaults.shared.nsWriteAuth.value) {
  31. NightscoutUtils.verifyURLAndToken { error, jwtToken, nsWriteAuth in
  32. DispatchQueue.main.async {
  33. ObservableUserDefaults.shared.nsWriteAuth.value = nsWriteAuth
  34. }
  35. }
  36. }
  37. }
  38. private func cancelExistingTarget(completion: @escaping (Bool) -> Void) {
  39. Task {
  40. let tempTargetBody: [String: Any] = [
  41. "enteredBy": "LoopFollow",
  42. "eventType": "Temporary Target",
  43. "reason": "Manual",
  44. "duration": 0,
  45. "created_at": ISO8601DateFormatter().string(from: Date())
  46. ]
  47. do {
  48. let response: [TreatmentCancelResponse] = try await NightscoutUtils.executePostRequest(eventType: .treatments, body: tempTargetBody)
  49. Observable.shared.tempTarget.value = nil
  50. NotificationCenter.default.post(name: NSNotification.Name("refresh"), object: nil)
  51. completion(true)
  52. } catch {
  53. completion(false)
  54. }
  55. }
  56. }
  57. private func sendTempTarget(newTarget: HKQuantity, duration: HKQuantity, completion: @escaping (Bool) -> Void) {
  58. let tempTargetBody: [String: Any] = [
  59. "enteredBy": "LoopFollow",
  60. "eventType": "Temporary Target",
  61. "reason": "Manual",
  62. "targetTop": newTarget.doubleValue(for: .milligramsPerDeciliter),
  63. "targetBottom": newTarget.doubleValue(for: .milligramsPerDeciliter),
  64. "duration": Int(duration.doubleValue(for: .minute())),
  65. "created_at": ISO8601DateFormatter().string(from: Date())
  66. ]
  67. Task {
  68. do {
  69. let response: [TreatmentResponse] = try await NightscoutUtils.executePostRequest(eventType: .treatments, body: tempTargetBody)
  70. Observable.shared.tempTarget.value = newTarget
  71. NotificationCenter.default.post(name: NSNotification.Name("refresh"), object: nil)
  72. completion(true)
  73. } catch {
  74. completion(false)
  75. }
  76. }
  77. }
  78. }