fix(mobile): keep Light/Dark/System on each device (#9894)

## 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 <arthur.garzajr@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Arthur Garza
2026-11-23 12:38:16 -06:00
committed by GitHub
parent 8c7f1c9e78
commit 4a440b5932
5 changed files with 139 additions and 15 deletions

View File

@@ -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

View File

@@ -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();

View File

@@ -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<SharedPreferences>(
(_) => 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<ThemeMode> {
@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, ThemeMode>(
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

View File

@@ -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) {

View File

@@ -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<ProviderContainer> _container(Map<String, Object> 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);
});
}