RemoteViewController.swift 3.4 KB

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