RemoteViewController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // LoopFollow
  2. // RemoteViewController.swift
  3. import Combine
  4. import SwiftUI
  5. import UIKit
  6. class RemoteViewController: UIViewController {
  7. private var cancellables = Set<AnyCancellable>()
  8. private var hostingController: UIHostingController<AnyView>?
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11. // Apply initial appearance
  12. overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
  13. Storage.shared.device.$value
  14. .removeDuplicates()
  15. .sink { [weak self] _ in
  16. DispatchQueue.main.async {
  17. self?.updateView()
  18. }
  19. }
  20. .store(in: &cancellables)
  21. // Listen for appearance setting changes
  22. Storage.shared.appearanceMode.$value
  23. .receive(on: DispatchQueue.main)
  24. .sink { [weak self] mode in
  25. self?.overrideUserInterfaceStyle = mode.userInterfaceStyle
  26. self?.hostingController?.overrideUserInterfaceStyle = mode.userInterfaceStyle
  27. }
  28. .store(in: &cancellables)
  29. // Listen for system appearance changes (when in System mode)
  30. NotificationCenter.default.publisher(for: .appearanceDidChange)
  31. .receive(on: DispatchQueue.main)
  32. .sink { [weak self] _ in
  33. let style = Storage.shared.appearanceMode.value.userInterfaceStyle
  34. self?.overrideUserInterfaceStyle = style
  35. self?.hostingController?.overrideUserInterfaceStyle = style
  36. }
  37. .store(in: &cancellables)
  38. }
  39. private func updateView() {
  40. let remoteType = Storage.shared.remoteType.value
  41. if let existingHostingController = hostingController {
  42. existingHostingController.willMove(toParent: nil)
  43. existingHostingController.view.removeFromSuperview()
  44. existingHostingController.removeFromParent()
  45. }
  46. if remoteType == .nightscout {
  47. var remoteView: AnyView
  48. switch Storage.shared.device.value {
  49. case "Trio":
  50. remoteView = AnyView(TrioNightscoutRemoteView())
  51. default:
  52. remoteView = AnyView(NoRemoteView())
  53. }
  54. hostingController = UIHostingController(rootView: remoteView)
  55. } else if remoteType == .trc {
  56. if Storage.shared.device.value != "Trio" {
  57. hostingController = UIHostingController(
  58. rootView: AnyView(
  59. Text("Trio Remote Control is only supported for 'Trio'")
  60. )
  61. )
  62. } else {
  63. let trioRemoteControlViewModel = TrioRemoteControlViewModel()
  64. let trioRemoteControlView = TrioRemoteControlView(viewModel: trioRemoteControlViewModel)
  65. hostingController = UIHostingController(rootView: AnyView(trioRemoteControlView))
  66. }
  67. } else if remoteType == .loopAPNS {
  68. hostingController = UIHostingController(rootView: AnyView(LoopAPNSRemoteView()))
  69. } else {
  70. hostingController = UIHostingController(rootView: AnyView(Text("Please select a Remote Type in Settings.")))
  71. }
  72. if let hostingController = hostingController {
  73. addChild(hostingController)
  74. view.addSubview(hostingController.view)
  75. hostingController.view.translatesAutoresizingMaskIntoConstraints = false
  76. NSLayoutConstraint.activate([
  77. hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  78. hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  79. hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
  80. hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  81. ])
  82. hostingController.didMove(toParent: self)
  83. }
  84. if remoteType == .nightscout, !Storage.shared.nsWriteAuth.value {
  85. NightscoutUtils.verifyURLAndToken { _, _, nsWriteAuth, nsAdminAuth in
  86. DispatchQueue.main.async {
  87. Storage.shared.nsWriteAuth.value = nsWriteAuth
  88. Storage.shared.nsAdminAuth.value = nsAdminAuth
  89. }
  90. }
  91. }
  92. }
  93. override func viewWillAppear(_ animated: Bool) {
  94. super.viewWillAppear(animated)
  95. updateView()
  96. }
  97. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  98. super.traitCollectionDidChange(previousTraitCollection)
  99. if Storage.shared.appearanceMode.value == .system,
  100. previousTraitCollection?.userInterfaceStyle != traitCollection.userInterfaceStyle
  101. {
  102. let style = Storage.shared.appearanceMode.value.userInterfaceStyle
  103. overrideUserInterfaceStyle = style
  104. hostingController?.overrideUserInterfaceStyle = style
  105. }
  106. }
  107. }