fix(swift-ios): apply theme changes immediately

This commit is contained in:
Theo Browne committed 2026-08-09 20:55:48 -07:00
1 parent 5ce53e9e84
commit 5dfc70c016
4 files changed
+59 -7

No files matched your search

@@ -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,
@@ -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)
}
@@ -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;
@@ -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<FeatureEvent>.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)
}
}