From 4a440b5932eb20a0b38d1674edea33bfa4ad3683 Mon Sep 17 00:00:00 2001 From: Arthur Garza Date: Mon, 23 Nov 2026 12:38:16 -0600 Subject: [PATCH] fix(mobile): keep Light/Dark/System on each device (#9894) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Light/Dark/System was saved in the synced per-community theme preference. A mode picked on one device, or carried over from an old setting, followed the account to every other device. A user could end up on Light without ever choosing it on that phone. Brightness depends on the device (its OS setting, time of day), so mobile now keeps the mode on the device: - New `appearanceModeProvider` saves the mode in SharedPreferences under `buzz-appearance-mode.v1`. It defaults to System. - `App` reads the mode from that provider. The age-restricted path still uses System. - The Appearance picker starts from the device mode. **Set** saves the mode locally and syncs only the chat color. The synced `theme` and `followSystem` are written back unchanged, so the wire format stays the same and other clients are unaffected. The Buzz 1.0 desktop app (block/buzz-app) already keeps the mode per device (`appearance.mode`, default System), so this brings mobile in line. The deprecated 0.X desktop app is unchanged. **Accepted tradeoff:** after updating, every mobile user starts on System, including people who picked Light or Dark on purpose. They need to pick it again once. This one-time reset is what fixes accounts stuck on a carried-over Light setting. Seeding from the synced value would keep them stuck. ### Related issue None found. Came from an internal discussion about mobile defaulting to Light. ### Testing - `dart format`, `flutter analyze`, and the full `flutter test` suite pass. - New `test/shared/theme/appearance_mode_provider_test.dart`: covers the System default even when the old synced or legacy settings are pinned, reading and saving the mode, and unknown values. - Picker tests: the mode is saved locally, the synced `theme` and `followSystem` are kept, and Close discards changes. - Not yet exercised on a device. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Arthur Garza Co-authored-by: Claude Opus 5.5 --- mobile/lib/app.dart | 11 ++-- .../theme_preview_sheet.dart | 12 +++-- mobile/lib/shared/theme/theme_provider.dart | 32 +++++++++++ .../settings/theme_picker_page_test.dart | 53 ++++++++++++++++--- .../theme/appearance_mode_provider_test.dart | 46 ++++++++++++++++ 5 files changed, 139 insertions(+), 15 deletions(-) create mode 100644 mobile/test/shared/theme/appearance_mode_provider_test.dart diff --git a/mobile/lib/app.dart b/mobile/lib/app.dart index e48d02287e..39e49b2c31 100644 --- a/mobile/lib/app.dart +++ b/mobile/lib/app.dart @@ -362,10 +362,13 @@ class App extends HookConsumerWidget { ref.read(pairingProvider.notifier).reset(); } }); - final communityTheme = ageSignalState != AgeSignalState.restricted - ? ref.watch(communityThemeProvider) - : defaultCommunityTheme; - final themeMode = communityTheme.mode; + final restricted = ageSignalState == AgeSignalState.restricted; + final communityTheme = restricted + ? defaultCommunityTheme + : ref.watch(communityThemeProvider); + final themeMode = restricted + ? defaultCommunityTheme.mode + : ref.watch(appearanceModeProvider); // The app rebuilds on every lifecycle change. A new ThemeData never // equals the last one, so MaterialApp would animate between identical // themes, and each frame of that makes every message re-parse its diff --git a/mobile/lib/features/settings/theme_picker_page/theme_preview_sheet.dart b/mobile/lib/features/settings/theme_picker_page/theme_preview_sheet.dart index 0b28889a1a..e13e087513 100644 --- a/mobile/lib/features/settings/theme_picker_page/theme_preview_sheet.dart +++ b/mobile/lib/features/settings/theme_picker_page/theme_preview_sheet.dart @@ -11,7 +11,7 @@ class _ThemePreviewExperience extends HookConsumerWidget { Widget build(BuildContext context, WidgetRef ref) { final inRail = DuoToolbarScope.maybeOf(context)?.strip != null; final stored = ref.read(communityThemeProvider); - final draftMode = useState(stored.mode); + final draftMode = useState(ref.read(appearanceModeProvider)); final draftAccent = useState(chatColorIndex(stored.accent)); final platformBrightness = MediaQuery.platformBrightnessOf(context); final reduceMotion = MediaQuery.disableAnimationsOf(context); @@ -42,15 +42,17 @@ class _ThemePreviewExperience extends HookConsumerWidget { void applySelection() { unawaited(HapticFeedback.mediumImpact()); + ref.read(appearanceModeProvider.notifier).setMode(draftMode.value); + // Only the chat color syncs. The synced theme and followSystem stay as + // they are, so this device's mode never changes other devices. + final current = ref.read(communityThemeProvider); ref .read(communityThemeProvider.notifier) .setPreference( CommunityThemePreference( - theme: draftMode.value == ThemeMode.dark - ? buzzDarkThemeName - : buzzThemeName, + theme: current.theme, accent: chatColorChoices[draftAccent.value].wireValue, - followSystem: draftMode.value == ThemeMode.system, + followSystem: current.followSystem, ), ); Navigator.of(context).pop(); diff --git a/mobile/lib/shared/theme/theme_provider.dart b/mobile/lib/shared/theme/theme_provider.dart index 1afa082742..a57f4f6fe3 100644 --- a/mobile/lib/shared/theme/theme_provider.dart +++ b/mobile/lib/shared/theme/theme_provider.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -15,6 +17,36 @@ final savedPrefsProvider = Provider( (_) => throw UnimplementedError('Must be overridden'), ); +/// SharedPreferences key for this device's Light, Dark, or System choice. +const appearanceModeKey = 'buzz-appearance-mode.v1'; + +/// Light, Dark, or System for this device only. +/// +/// Kept out of the synced community preference: brightness follows the device +/// (its OS setting, time of day), so one device's choice must not change +/// another's. Defaults to System. +class AppearanceModeNotifier extends Notifier { + @override + ThemeMode build() { + final saved = ref.watch(savedPrefsProvider).getString(appearanceModeKey); + return ThemeMode.values.where((mode) => mode.name == saved).firstOrNull ?? + ThemeMode.system; + } + + void setMode(ThemeMode mode) { + if (mode == state) return; + state = mode; + unawaited( + ref.read(savedPrefsProvider).setString(appearanceModeKey, mode.name), + ); + } +} + +final appearanceModeProvider = + NotifierProvider( + AppearanceModeNotifier.new, + ); + /// Returns a scheme that can honor [mode] without silently pinning brightness. /// /// System mode only offers paired themes because they have both light and dark diff --git a/mobile/test/features/settings/theme_picker_page_test.dart b/mobile/test/features/settings/theme_picker_page_test.dart index a8d836d764..67f884531e 100644 --- a/mobile/test/features/settings/theme_picker_page_test.dart +++ b/mobile/test/features/settings/theme_picker_page_test.dart @@ -195,7 +195,8 @@ void main() { railAction('Set').onPressed!(); await tester.pumpAndSettle(); expect(notifier.preferenceWrites, 1); - expect(notifier.lastPreference?.mode, ThemeMode.light); + expect(instance.getString(appearanceModeKey), ThemeMode.light.name); + expect(notifier.lastPreference?.mode, defaultCommunityTheme.mode); expect( notifier.lastPreference?.accent, chatColorChoices[jade].wireValue, @@ -222,7 +223,7 @@ void main() { testWidgets('mode and chat color remain drafts until Set', (tester) async { final notifier = _RecordingCommunityThemeNotifier(); - await _pumpPicker(tester, themeNotifier: () => notifier); + final prefs = await _pumpPicker(tester, themeNotifier: () => notifier); // System -> Light -> Dark. for (var i = 0; i < 2; i++) { await tester.tap( @@ -249,11 +250,47 @@ void main() { await tester.tap(find.byKey(const ValueKey('theme-preview-set'))); await tester.pumpAndSettle(); expect(notifier.preferenceWrites, 1); - expect(notifier.lastPreference?.mode, ThemeMode.dark); - expect(notifier.lastPreference?.theme, buzzDarkThemeName); + // Mode stays on this device; the synced preference keeps its own theme + // and followSystem and only takes the new chat color. + expect(prefs.getString(appearanceModeKey), ThemeMode.dark.name); + expect(notifier.lastPreference?.theme, defaultCommunityTheme.theme); + expect( + notifier.lastPreference?.followSystem, + defaultCommunityTheme.followSystem, + ); expect(notifier.lastPreference?.accent, chatColorChoices[green].wireValue); }); + testWidgets('picker uses the device mode and keeps the synced one', ( + tester, + ) async { + const synced = CommunityThemePreference( + theme: buzzDarkThemeName, + accent: '#3b82f6', + followSystem: false, + ); + final notifier = _RecordingCommunityThemeNotifier(synced); + final prefs = await _pumpPicker( + tester, + prefs: {appearanceModeKey: ThemeMode.light.name}, + themeNotifier: () => notifier, + ); + expect(find.text('Light'), findsOneWidget); + // Light -> Dark -> System. + for (var i = 0; i < 2; i++) { + await tester.tap( + find.byKey(const ValueKey('theme-preview-appearance-action-button')), + ); + await tester.pumpAndSettle(); + } + await tester.tap(find.byKey(const ValueKey('theme-preview-set'))); + await tester.pumpAndSettle(); + + expect(prefs.getString(appearanceModeKey), ThemeMode.system.name); + // The synced theme and followSystem survive a mode change untouched. + expect(notifier.lastPreference, synced); + }); + for (final brightness in Brightness.values) { testWidgets('${brightness.name} chat preview follows draft colors only', ( tester, @@ -331,7 +368,7 @@ void main() { testWidgets('Close discards mode changes', (tester) async { final notifier = _RecordingCommunityThemeNotifier(); - await _pumpPicker(tester, themeNotifier: () => notifier); + final prefs = await _pumpPicker(tester, themeNotifier: () => notifier); await tester.tap( find.byKey(const ValueKey('theme-preview-appearance-action-button')), ); @@ -339,6 +376,7 @@ void main() { await tester.tap(find.byKey(const ValueKey('theme-preview-close'))); await tester.pumpAndSettle(); expect(notifier.preferenceWrites, 0); + expect(prefs.getString(appearanceModeKey), isNull); }); testWidgets( @@ -358,11 +396,14 @@ void main() { } class _RecordingCommunityThemeNotifier extends CommunityThemeNotifier { + _RecordingCommunityThemeNotifier([this.initial = defaultCommunityTheme]); + + final CommunityThemePreference initial; int preferenceWrites = 0; CommunityThemePreference? lastPreference; @override - CommunityThemePreference build() => defaultCommunityTheme; + CommunityThemePreference build() => initial; @override void setPreference(CommunityThemePreference preference) { diff --git a/mobile/test/shared/theme/appearance_mode_provider_test.dart b/mobile/test/shared/theme/appearance_mode_provider_test.dart new file mode 100644 index 0000000000..3d2c61cb93 --- /dev/null +++ b/mobile/test/shared/theme/appearance_mode_provider_test.dart @@ -0,0 +1,46 @@ +import 'package:buzz/shared/theme/theme.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +Future _container(Map initial) async { + SharedPreferences.setMockInitialValues(initial); + final prefs = await SharedPreferences.getInstance(); + final container = ProviderContainer( + overrides: [savedPrefsProvider.overrideWithValue(prefs)], + ); + addTearDown(container.dispose); + return container; +} + +void main() { + test('defaults to System, even when the synced theme is pinned', () async { + final container = await _container({ + 'buzz_theme_mode': ThemeMode.light.name, + 'buzz-local-theme.v1': + '{"version":1,"theme":"buzz","accent":"#3b82f6","followSystem":false}', + }); + + expect(container.read(appearanceModeProvider), ThemeMode.system); + }); + + test('reads and saves the device mode', () async { + final container = await _container({ + appearanceModeKey: ThemeMode.dark.name, + }); + expect(container.read(appearanceModeProvider), ThemeMode.dark); + + container.read(appearanceModeProvider.notifier).setMode(ThemeMode.light); + + expect(container.read(appearanceModeProvider), ThemeMode.light); + final prefs = container.read(savedPrefsProvider); + expect(prefs.getString(appearanceModeKey), ThemeMode.light.name); + }); + + test('an unknown saved value falls back to System', () async { + final container = await _container({appearanceModeKey: 'sepia'}); + + expect(container.read(appearanceModeProvider), ThemeMode.system); + }); +}