// LoopFollow // RemoteViewController.swift // Created by Jonas Björkert on 2024-07-19. import Combine import Foundation import HealthKit import SwiftUI import UIKit class RemoteViewController: UIViewController { private var cancellable: AnyCancellable? private var hostingController: UIHostingController? override func viewDidLoad() { super.viewDidLoad() cancellable = Publishers.CombineLatest( Storage.shared.remoteType.$value, Storage.shared.device.$value ) .sink { [weak self] _, _ in DispatchQueue.main.async { self?.updateView() } } updateView() } private func updateView() { let remoteType = Storage.shared.remoteType.value if let existingHostingController = hostingController { existingHostingController.willMove(toParent: nil) existingHostingController.view.removeFromSuperview() existingHostingController.removeFromParent() } if remoteType == .nightscout { var remoteView: AnyView switch Storage.shared.device.value { case "Trio": remoteView = AnyView(TrioNightscoutRemoteView()) case "Loop": remoteView = AnyView(LoopNightscoutRemoteView()) default: remoteView = AnyView(NoRemoteView()) } hostingController = UIHostingController(rootView: remoteView) } else if remoteType == .trc { if Storage.shared.device.value != "Trio" { hostingController = UIHostingController( rootView: AnyView( Text("Trio Remote Control is only supported for 'Trio'") ) ) } else { let trioRemoteControlViewModel = TrioRemoteControlViewModel() let trioRemoteControlView = TrioRemoteControlView(viewModel: trioRemoteControlViewModel) hostingController = UIHostingController(rootView: AnyView(trioRemoteControlView)) } } else { hostingController = UIHostingController(rootView: AnyView(Text("Please select a Remote Type in Settings."))) } if let hostingController = hostingController { addChild(hostingController) view.addSubview(hostingController.view) hostingController.view.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor), hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor), hostingController.view.topAnchor.constraint(equalTo: view.topAnchor), hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor), ]) hostingController.didMove(toParent: self) } if remoteType == .nightscout, !Storage.shared.nsWriteAuth.value { NightscoutUtils.verifyURLAndToken { _, _, nsWriteAuth, nsAdminAuth in DispatchQueue.main.async { Storage.shared.nsWriteAuth.value = nsWriteAuth Storage.shared.nsAdminAuth.value = nsAdminAuth } } } } deinit { cancellable?.cancel() } }