diff --git a/apps/swift-ios/Features/Root/FeatureRootModel.swift b/apps/swift-ios/Features/Root/FeatureRootModel.swift index 455457f67b..12aeb39ce7 100644 --- a/apps/swift-ios/Features/Root/FeatureRootModel.swift +++ b/apps/swift-ios/Features/Root/FeatureRootModel.swift @@ -552,6 +552,32 @@ public final class FeatureRootModel { } } + /// Applies appearance optimistically so selecting a theme updates every + /// surface immediately, then persists just that preference in the current + /// settings snapshot. Other unsaved Settings edits remain drafts. + @discardableResult + public func saveAppearance(_ appearance: FeatureAppearance) async -> Bool { + let previous = snapshot.settings + guard previous.appearance != appearance else { return true } + + var updated = previous + updated.appearance = appearance + snapshot.settings = updated + + do { + try await client.saveSettings(updated) + return true + } catch { + if snapshot.settings == updated { + snapshot.settings = previous + } + if !Self.isBenignCancellation(error) { + errorMessage = error.localizedDescription + } + return false + } + } + @discardableResult private func perform( reportError: Bool = true, diff --git a/apps/swift-ios/Features/Settings/SettingsView.swift b/apps/swift-ios/Features/Settings/SettingsView.swift index fdbba650b7..6682fcbc59 100644 --- a/apps/swift-ios/Features/Settings/SettingsView.swift +++ b/apps/swift-ios/Features/Settings/SettingsView.swift @@ -124,6 +124,16 @@ public struct SettingsView: View { .onDisappear { model.setConnectionManagementPresented(false) } + .onChange(of: settings.appearance) { _, appearance in + Task { + let didSave = await model.saveAppearance(appearance) + if !didSave { + settings.appearance = model.snapshot.settings.appearance + saveErrorMessage = model.errorMessage + ?? "Theme preference could not be saved." + } + } + } } .presentationDragIndicator(.visible) } diff --git a/apps/swift-ios/T3Code.xcodeproj/project.pbxproj b/apps/swift-ios/T3Code.xcodeproj/project.pbxproj index f5e1d66ca0..23995b680c 100644 --- a/apps/swift-ios/T3Code.xcodeproj/project.pbxproj +++ b/apps/swift-ios/T3Code.xcodeproj/project.pbxproj @@ -617,7 +617,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = Extensions/Shared/T3Code.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 14; + CURRENT_PROJECT_VERSION = 15; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = Resources/Info.plist; @@ -674,7 +674,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = Extensions/Shared/T3Code.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 14; + CURRENT_PROJECT_VERSION = 15; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = Resources/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = "T3 Code SwiftUI"; @@ -764,7 +764,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CODE_SIGN_ENTITLEMENTS = Extensions/Widgets/T3CodeWidgets.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 14; + CURRENT_PROJECT_VERSION = 15; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = Extensions/Widgets/Info.plist; INFOPLIST_KEY_ITSAppUsesNonExemptEncryption = NO; @@ -795,7 +795,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CODE_SIGN_ENTITLEMENTS = Extensions/Widgets/T3CodeWidgets.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 14; + CURRENT_PROJECT_VERSION = 15; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = Extensions/Widgets/Info.plist; INFOPLIST_KEY_ITSAppUsesNonExemptEncryption = NO; @@ -828,7 +828,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CODE_SIGN_ENTITLEMENTS = Extensions/Share/T3CodeShare.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 14; + CURRENT_PROJECT_VERSION = 15; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = Extensions/Share/Info.plist; INFOPLIST_KEY_ITSAppUsesNonExemptEncryption = NO; @@ -859,7 +859,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CODE_SIGN_ENTITLEMENTS = Extensions/Share/T3CodeShare.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 14; + CURRENT_PROJECT_VERSION = 15; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = Extensions/Share/Info.plist; INFOPLIST_KEY_ITSAppUsesNonExemptEncryption = NO; diff --git a/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift b/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift index c93f6c65b1..47560f325b 100644 --- a/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift @@ -5,6 +5,19 @@ import Testing @MainActor @Suite("Feature root model") struct FeatureRootModelTests { + @Test + func appearanceAppliesImmediatelyAndPersistsWithoutSavingTheDraft() async { + let client = FeatureClientStub() + let model = testRootModel(client: client) + + let save = Task { await model.saveAppearance(.light) } + await Task.yield() + + #expect(model.snapshot.settings.appearance == .light) + #expect(await save.value) + #expect(client.savedSettings.last?.appearance == .light) + } + @Test func backgroundRefreshUsesTheBoundedClientPath() async { let client = FeatureClientStub() @@ -1203,6 +1216,7 @@ private final class FeatureClientStub: FeatureClient { var loadEarlierCallCount = 0 var resolvedInputID: String? var resolvedInputAnswers: [String: FeatureInputAnswer]? + var savedSettings: [FeatureSettings] = [] init() { let pair = AsyncStream.makeStream() @@ -1332,5 +1346,7 @@ private final class FeatureClientStub: FeatureClient { resolvedInputID = id resolvedInputAnswers = answers } - func saveSettings(_ settings: FeatureSettings) async throws {} + func saveSettings(_ settings: FeatureSettings) async throws { + savedSettings.append(settings) + } }