RemoteViewController.swift 3.4 KB

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