From 2ac7f7f3426746bdf5e9c7f9d111b1575317c608 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Sun, 17 Sep 2023 19:00:22 +0200 Subject: [PATCH] Migrate DisplaySettingsLocalValues --- .../Tabs/Settings/DisplaySettingsView.swift | 70 +++++++++---------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/IceCubesApp/App/Tabs/Settings/DisplaySettingsView.swift b/IceCubesApp/App/Tabs/Settings/DisplaySettingsView.swift index eb619b68..77d73067 100644 --- a/IceCubesApp/App/Tabs/Settings/DisplaySettingsView.swift +++ b/IceCubesApp/App/Tabs/Settings/DisplaySettingsView.swift @@ -7,44 +7,16 @@ import Observation import Status import SwiftUI -class DisplaySettingsLocalValues: ObservableObject { - @Published var tintColor = Theme.shared.tintColor - @Published var primaryBackgroundColor = Theme.shared.primaryBackgroundColor - @Published var secondaryBackgroundColor = Theme.shared.secondaryBackgroundColor - @Published var labelColor = Theme.shared.labelColor - @Published var lineSpacing = Theme.shared.lineSpacing - @Published var fontSizeScale = Theme.shared.fontSizeScale +@Observable class DisplaySettingsLocalValues { + var tintColor = Theme.shared.tintColor + var primaryBackgroundColor = Theme.shared.primaryBackgroundColor + var secondaryBackgroundColor = Theme.shared.secondaryBackgroundColor + var labelColor = Theme.shared.labelColor + var lineSpacing = Theme.shared.lineSpacing + var fontSizeScale = Theme.shared.fontSizeScale - private let debouncesDelay: DispatchQueue.SchedulerTimeType.Stride = .seconds(0.5) - private var subscriptions = Set() - - init() { - $tintColor - .debounce(for: debouncesDelay, scheduler: DispatchQueue.main) - .sink(receiveValue: { newColor in Theme.shared.tintColor = newColor }) - .store(in: &subscriptions) - $primaryBackgroundColor - .debounce(for: debouncesDelay, scheduler: DispatchQueue.main) - .sink(receiveValue: { newColor in Theme.shared.primaryBackgroundColor = newColor }) - .store(in: &subscriptions) - $secondaryBackgroundColor - .debounce(for: debouncesDelay, scheduler: DispatchQueue.main) - .sink(receiveValue: { newColor in Theme.shared.secondaryBackgroundColor = newColor }) - .store(in: &subscriptions) - $labelColor - .debounce(for: debouncesDelay, scheduler: DispatchQueue.main) - .sink(receiveValue: { newColor in Theme.shared.labelColor = newColor }) - .store(in: &subscriptions) - $lineSpacing - .debounce(for: debouncesDelay, scheduler: DispatchQueue.main) - .sink(receiveValue: { newSpacing in Theme.shared.lineSpacing = newSpacing }) - .store(in: &subscriptions) - $fontSizeScale - .debounce(for: debouncesDelay, scheduler: DispatchQueue.main) - .sink(receiveValue: { newScale in Theme.shared.fontSizeScale = newScale }) - .store(in: &subscriptions) - } + init() { } } struct DisplaySettingsView: View { @@ -54,7 +26,7 @@ struct DisplaySettingsView: View { @EnvironmentObject private var theme: Theme @EnvironmentObject private var userPreferences: UserPreferences - @StateObject private var localValues = DisplaySettingsLocalValues() + @State private var localValues = DisplaySettingsLocalValues() @State private var isFontSelectorPresented = false @@ -78,6 +50,30 @@ struct DisplaySettingsView: View { .navigationTitle("settings.display.navigation-title") .scrollContentBackground(.hidden) .background(theme.secondaryBackgroundColor) + .task(id: localValues.tintColor) { + do { try await Task.sleep(for: .microseconds(500)) } catch { } + Theme.shared.tintColor = localValues.tintColor + } + .task(id: localValues.primaryBackgroundColor) { + do { try await Task.sleep(for: .microseconds(500)) } catch { } + Theme.shared.primaryBackgroundColor = localValues.primaryBackgroundColor + } + .task(id: localValues.secondaryBackgroundColor) { + do { try await Task.sleep(for: .microseconds(500)) } catch { } + Theme.shared.secondaryBackgroundColor = localValues.secondaryBackgroundColor + } + .task(id: localValues.labelColor) { + do { try await Task.sleep(for: .microseconds(500)) } catch { } + Theme.shared.labelColor = localValues.labelColor + } + .task(id: localValues.lineSpacing) { + do { try await Task.sleep(for: .microseconds(500)) } catch { } + Theme.shared.lineSpacing = localValues.lineSpacing + } + .task(id: localValues.fontSizeScale) { + do { try await Task.sleep(for: .microseconds(500)) } catch { } + Theme.shared.fontSizeScale = localValues.fontSizeScale + } examplePost } }