RemoteViewController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. import Combine
  13. class RemoteViewController: UIViewController {
  14. private var cancellable: AnyCancellable?
  15. private var hostingController: UIHostingController<AnyView>?
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. cancellable = Publishers.CombineLatest(
  19. Storage.shared.remoteType.$value,
  20. ObservableUserDefaults.shared.device.$value
  21. )
  22. .sink { [weak self] newRemoteType, newDevice in
  23. DispatchQueue.main.async {
  24. self?.updateView()
  25. }
  26. }
  27. updateView()
  28. }
  29. private func updateView() {
  30. let remoteType = Storage.shared.remoteType.value
  31. if let existingHostingController = hostingController {
  32. existingHostingController.willMove(toParent: nil)
  33. existingHostingController.view.removeFromSuperview()
  34. existingHostingController.removeFromParent()
  35. }
  36. if remoteType == .nightscout {
  37. var remoteView: AnyView
  38. switch ObservableUserDefaults.shared.device.value {
  39. case "Trio":
  40. remoteView = AnyView(TrioNightscoutRemoteView())
  41. case "Loop":
  42. remoteView = AnyView(LoopNightscoutRemoteView())
  43. default:
  44. remoteView = AnyView(NoRemoteView())
  45. }
  46. hostingController = UIHostingController(rootView: remoteView)
  47. } else if remoteType == .trc {
  48. if ObservableUserDefaults.shared.device.value != "Trio" {
  49. hostingController = UIHostingController(
  50. rootView: AnyView(
  51. Text("Trio Remote Control is only supported for 'Trio'")
  52. )
  53. )
  54. } else {
  55. let trioRemoteControlViewModel = TrioRemoteControlViewModel()
  56. let trioRemoteControlView = TrioRemoteControlView(viewModel: trioRemoteControlViewModel)
  57. hostingController = UIHostingController(rootView: AnyView(trioRemoteControlView))
  58. }
  59. } else {
  60. hostingController = UIHostingController(rootView: AnyView(Text("Please select a Remote Type in Settings.")))
  61. }
  62. if let hostingController = hostingController {
  63. addChild(hostingController)
  64. view.addSubview(hostingController.view)
  65. hostingController.view.translatesAutoresizingMaskIntoConstraints = false
  66. NSLayoutConstraint.activate([
  67. hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  68. hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  69. hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
  70. hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
  71. ])
  72. hostingController.didMove(toParent: self)
  73. }
  74. if remoteType == .nightscout, !ObservableUserDefaults.shared.nsWriteAuth.value {
  75. NightscoutUtils.verifyURLAndToken { error, jwtToken, nsWriteAuth, nsAdminAuth in
  76. DispatchQueue.main.async {
  77. ObservableUserDefaults.shared.nsWriteAuth.value = nsWriteAuth
  78. ObservableUserDefaults.shared.nsAdminAuth.value = nsAdminAuth
  79. }
  80. }
  81. }
  82. }
  83. deinit {
  84. cancellable?.cancel()
  85. }
  86. }