AlarmViewController.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // LoopFollow
  2. // AlarmViewController.swift
  3. import Combine
  4. import SwiftUI
  5. import UIKit
  6. class AlarmViewController: UIViewController {
  7. private var hostingController: UIHostingController<AlarmsContainerView>!
  8. private var cancellables = Set<AnyCancellable>()
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11. let alarmsView = AlarmsContainerView()
  12. hostingController = UIHostingController(rootView: alarmsView)
  13. // Apply initial appearance
  14. hostingController.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
  15. // Listen for appearance setting changes
  16. Storage.shared.appearanceMode.$value
  17. .receive(on: DispatchQueue.main)
  18. .sink { [weak self] mode in
  19. self?.hostingController.overrideUserInterfaceStyle = mode.userInterfaceStyle
  20. }
  21. .store(in: &cancellables)
  22. // Listen for system appearance changes (when in System mode)
  23. NotificationCenter.default.publisher(for: .appearanceDidChange)
  24. .receive(on: DispatchQueue.main)
  25. .sink { [weak self] _ in
  26. self?.hostingController.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
  27. }
  28. .store(in: &cancellables)
  29. addChild(hostingController)
  30. view.addSubview(hostingController.view)
  31. hostingController.view.translatesAutoresizingMaskIntoConstraints = false
  32. NSLayoutConstraint.activate([
  33. hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  34. hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  35. hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
  36. hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  37. ])
  38. hostingController.didMove(toParent: self)
  39. }
  40. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  41. super.traitCollectionDidChange(previousTraitCollection)
  42. if Storage.shared.appearanceMode.value == .system,
  43. previousTraitCollection?.userInterfaceStyle != traitCollection.userInterfaceStyle
  44. {
  45. hostingController.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
  46. }
  47. }
  48. }