RemoteViewController.swift 3.4 KB

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