SnoozerViewController.swift 2.4 KB

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