From 4bf7994e3103d06aa0a089d436ae7cbc4e1063de Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Sat, 7 Feb 2026 10:35:00 -0600 Subject: [PATCH] fixed warnings Signed-off-by: Matt Bruce --- .../Features/Clock/Models/ClockStyle.swift | 7 ++- .../Clock/Services/AmbientLightService.swift | 44 ++++++++++++------- .../Features/Clock/State/ClockViewModel.swift | 16 ------- .../Features/Clock/Views/ClockView.swift | 1 - .../Views/Components/TimeDisplayView.swift | 3 +- 5 files changed, 31 insertions(+), 40 deletions(-) diff --git a/TheNoiseClock/Features/Clock/Models/ClockStyle.swift b/TheNoiseClock/Features/Clock/Models/ClockStyle.swift index 678605c..d73d0cc 100644 --- a/TheNoiseClock/Features/Clock/Models/ClockStyle.swift +++ b/TheNoiseClock/Features/Clock/Models/ClockStyle.swift @@ -344,9 +344,8 @@ final class ClockStyle: Codable, Equatable { /// Check if ambient light is low enough to trigger night mode private func isAmbientLightLow() -> Bool { - // Use screen brightness as a proxy for ambient light - // In a real implementation, you'd use the ambient light sensor - let currentBrightness = UIScreen.main.brightness + // Use the ambient light service's tracked brightness as a proxy + let currentBrightness = AmbientLightService.shared.currentBrightness return currentBrightness < ambientLightThreshold } @@ -425,7 +424,7 @@ final class ClockStyle: Codable, Equatable { /// Get ambient light factor (0.5 to 1.0) private func getAmbientLightFactor() -> Double { - let currentBrightness = UIScreen.main.brightness + let currentBrightness = AmbientLightService.shared.currentBrightness // Map screen brightness to ambient light factor if currentBrightness < 0.2 { diff --git a/TheNoiseClock/Features/Clock/Services/AmbientLightService.swift b/TheNoiseClock/Features/Clock/Services/AmbientLightService.swift index 5628b61..2a1410d 100644 --- a/TheNoiseClock/Features/Clock/Services/AmbientLightService.swift +++ b/TheNoiseClock/Features/Clock/Services/AmbientLightService.swift @@ -12,7 +12,8 @@ import Bedrock /// Service for monitoring ambient light and managing brightness @Observable -class AmbientLightService { +@MainActor +final class AmbientLightService { // MARK: - Properties private(set) var currentBrightness: Double = 1.0 @@ -31,6 +32,22 @@ class AmbientLightService { // Private initializer for singleton } + // MARK: - Screen Access + + /// Resolve the current UIScreen from the active window scene (iOS 26+ safe) + private var activeScreen: UIScreen? { + UIApplication.shared + .connectedScenes + .compactMap { $0 as? UIWindowScene } + .first(where: { $0.activationState == .foregroundActive })? + .screen + } + + /// Current screen brightness from the active scene, falling back to stored value + private var screenBrightness: CGFloat { + activeScreen?.brightness ?? CGFloat(currentBrightness) + } + // MARK: - Public Interface /// Start monitoring ambient light and brightness @@ -42,7 +59,10 @@ class AmbientLightService { // Check brightness every 5 seconds brightnessTimer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) { [weak self] _ in - self?.updateCurrentBrightness() + guard let self else { return } + Task { @MainActor [weak self] in + self?.updateCurrentBrightness() + } } } @@ -58,23 +78,16 @@ class AmbientLightService { /// Set screen brightness (0.0 to 1.0) func setBrightness(_ brightness: Double) { let clampedBrightness = max(0.0, min(1.0, brightness)) - let previousBrightness = UIScreen.main.brightness -// Design.debugLog("[ambient] AmbientLightService.setBrightness:") -// Design.debugLog("[ambient] - Requested brightness: \(String(format: "%.2f", brightness))") -// Design.debugLog("[ambient] - Clamped brightness: \(String(format: "%.2f", clampedBrightness))") -// Design.debugLog("[ambient] - Previous screen brightness: \(String(format: "%.2f", previousBrightness))") - - UIScreen.main.brightness = clampedBrightness + if let screen = activeScreen { + screen.brightness = clampedBrightness + } currentBrightness = clampedBrightness - -// Design.debugLog("[ambient] - New screen brightness: \(String(format: "%.2f", UIScreen.main.brightness))") -// Design.debugLog("[ambient] - Service currentBrightness: \(String(format: "%.2f", currentBrightness))") } /// Get current screen brightness func getCurrentBrightness() -> Double { - return UIScreen.main.brightness + return Double(screenBrightness) } /// Check if ambient light is below threshold @@ -85,13 +98,10 @@ class AmbientLightService { // MARK: - Private Methods private func updateCurrentBrightness() { - let newBrightness = UIScreen.main.brightness + let newBrightness = Double(screenBrightness) if abs(newBrightness - currentBrightness) > 0.05 { // Only update if significant change - let previousBrightness = currentBrightness currentBrightness = newBrightness - //Design.debugLog("[ambient] AmbientLightService: Brightness changed from \(String(format: "%.2f", previousBrightness)) to \(String(format: "%.2f", newBrightness))") - // Notify that brightness changed onBrightnessChange?() } diff --git a/TheNoiseClock/Features/Clock/State/ClockViewModel.swift b/TheNoiseClock/Features/Clock/State/ClockViewModel.swift index d7a3c46..85083dc 100644 --- a/TheNoiseClock/Features/Clock/State/ClockViewModel.swift +++ b/TheNoiseClock/Features/Clock/State/ClockViewModel.swift @@ -267,24 +267,8 @@ final class ClockViewModel { private func updateBrightness() { if style.autoBrightness { let targetBrightness = style.effectiveBrightness - let currentScreenBrightness = UIScreen.main.brightness - let isNightMode = style.isNightModeActive - -// Design.debugLog("[brightness] Auto Brightness Debug:") -// Design.debugLog("[brightness] - Auto brightness enabled: \(style.autoBrightness)") -// Design.debugLog("[brightness] - Current screen brightness: \(String(format: "%.2f", currentScreenBrightness))") -// Design.debugLog("[brightness] - Target brightness: \(String(format: "%.2f", targetBrightness))") -// Design.debugLog("[brightness] - Night mode active: \(isNightMode)") -// Design.debugLog("[brightness] - Color theme: \(style.selectedColorTheme)") -// Design.debugLog("[brightness] - Ambient light threshold: \(String(format: "%.2f", style.ambientLightThreshold))") ambientLightService.setBrightness(targetBrightness) - -// Design.debugLog("[brightness] - Brightness set to: \(String(format: "%.2f", targetBrightness))") -// Design.debugLog("[brightness] - Actual screen brightness now: \(String(format: "%.2f", UIScreen.main.brightness))") -// Design.debugLog("[brightness] ---") -// } else { -// Design.debugLog("[brightness] Auto Brightness: DISABLED") } } } diff --git a/TheNoiseClock/Features/Clock/Views/ClockView.swift b/TheNoiseClock/Features/Clock/Views/ClockView.swift index 3736779..db4fa76 100644 --- a/TheNoiseClock/Features/Clock/Views/ClockView.swift +++ b/TheNoiseClock/Features/Clock/Views/ClockView.swift @@ -39,7 +39,6 @@ struct ClockView: View { // Get safe area insets from UIWindow since GeometryReader ignores them let windowInsets = Self.getWindowSafeAreaInsets() - let safeInsets = geometry.safeAreaInsets // May be 0 due to ignoresSafeArea // Dynamic Island handling: // In landscape, apply symmetric padding to keep content centered diff --git a/TheNoiseClock/Features/Clock/Views/Components/TimeDisplayView.swift b/TheNoiseClock/Features/Clock/Views/Components/TimeDisplayView.swift index ea8c170..99c76dd 100644 --- a/TheNoiseClock/Features/Clock/Views/Components/TimeDisplayView.swift +++ b/TheNoiseClock/Features/Clock/Views/Components/TimeDisplayView.swift @@ -233,7 +233,7 @@ struct TimeDisplayView: View { Text("Digit Slot: \(Int(digitSlotSize.width))×\(Int(digitSlotSize.height))") Text("Font Size: \(Int(fontSize))") Text("Cols: \(Int(digitColumns)) Rows: \(Int(digitRows))") - Text("Portrait: \(portrait)") + Text("Portrait: \(portrait ? "true" : "false")") Text("Family: \(fontFamily.rawValue)") } .font(.system(size: 10, weight: .bold, design: .monospaced)) @@ -251,7 +251,6 @@ struct TimeDisplayView: View { let digitColumns: CGFloat = portrait ? 2.0 : (showSeconds ? 6.0 : 4.0) let digitRows: CGFloat = portrait ? (showSeconds ? 3.0 : 2.0) : 1.0 let colonCount: CGFloat = showSeconds ? 2.0 : 1.0 - let previousFontSize = fontSize func calculateFontSize(reservingColonSize colonSize: CGFloat) -> CGFloat { let availableWidth = portrait