Skip to content

Commit 4a440b5

Browse files
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>
1 parent 8c7f1c9 commit 4a440b5

5 files changed

Lines changed: 139 additions & 15 deletions

File tree

‎mobile/lib/app.dart‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,13 @@ class App extends HookConsumerWidget {
362362
ref.read(pairingProvider.notifier).reset();
363363
}
364364
});
365-
final communityTheme = ageSignalState != AgeSignalState.restricted
366-
? ref.watch(communityThemeProvider)
367-
: defaultCommunityTheme;
368-
final themeMode = communityTheme.mode;
365+
final restricted = ageSignalState == AgeSignalState.restricted;
366+
final communityTheme = restricted
367+
? defaultCommunityTheme
368+
: ref.watch(communityThemeProvider);
369+
final themeMode = restricted
370+
? defaultCommunityTheme.mode
371+
: ref.watch(appearanceModeProvider);
369372
// The app rebuilds on every lifecycle change. A new ThemeData never
370373
// equals the last one, so MaterialApp would animate between identical
371374
// themes, and each frame of that makes every message re-parse its

‎mobile/lib/features/settings/theme_picker_page/theme_preview_sheet.dart‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class _ThemePreviewExperience extends HookConsumerWidget {
1111
Widget build(BuildContext context, WidgetRef ref) {
1212
final inRail = DuoToolbarScope.maybeOf(context)?.strip != null;
1313
final stored = ref.read(communityThemeProvider);
14-
final draftMode = useState(stored.mode);
14+
final draftMode = useState(ref.read(appearanceModeProvider));
1515
final draftAccent = useState(chatColorIndex(stored.accent));
1616
final platformBrightness = MediaQuery.platformBrightnessOf(context);
1717
final reduceMotion = MediaQuery.disableAnimationsOf(context);
@@ -42,15 +42,17 @@ class _ThemePreviewExperience extends HookConsumerWidget {
4242

4343
void applySelection() {
4444
unawaited(HapticFeedback.mediumImpact());
45+
ref.read(appearanceModeProvider.notifier).setMode(draftMode.value);
46+
// Only the chat color syncs. The synced theme and followSystem stay as
47+
// they are, so this device's mode never changes other devices.
48+
final current = ref.read(communityThemeProvider);
4549
ref
4650
.read(communityThemeProvider.notifier)
4751
.setPreference(
4852
CommunityThemePreference(
49-
theme: draftMode.value == ThemeMode.dark
50-
? buzzDarkThemeName
51-
: buzzThemeName,
53+
theme: current.theme,
5254
accent: chatColorChoices[draftAccent.value].wireValue,
53-
followSystem: draftMode.value == ThemeMode.system,
55+
followSystem: current.followSystem,
5456
),
5557
);
5658
Navigator.of(context).pop();

‎mobile/lib/shared/theme/theme_provider.dart‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:hooks_riverpod/hooks_riverpod.dart';
35
import 'package:shared_preferences/shared_preferences.dart';
@@ -15,6 +17,36 @@ final savedPrefsProvider = Provider<SharedPreferences>(
1517
(_) => throw UnimplementedError('Must be overridden'),
1618
);
1719

20+
/// SharedPreferences key for this device's Light, Dark, or System choice.
21+
const appearanceModeKey = 'buzz-appearance-mode.v1';
22+
23+
/// Light, Dark, or System for this device only.
24+
///
25+
/// Kept out of the synced community preference: brightness follows the device
26+
/// (its OS setting, time of day), so one device's choice must not change
27+
/// another's. Defaults to System.
28+
class AppearanceModeNotifier extends Notifier<ThemeMode> {
29+
@override
30+
ThemeMode build() {
31+
final saved = ref.watch(savedPrefsProvider).getString(appearanceModeKey);
32+
return ThemeMode.values.where((mode) => mode.name == saved).firstOrNull ??
33+
ThemeMode.system;
34+
}
35+
36+
void setMode(ThemeMode mode) {
37+
if (mode == state) return;
38+
state = mode;
39+
unawaited(
40+
ref.read(savedPrefsProvider).setString(appearanceModeKey, mode.name),
41+
);
42+
}
43+
}
44+
45+
final appearanceModeProvider =
46+
NotifierProvider<AppearanceModeNotifier, ThemeMode>(
47+
AppearanceModeNotifier.new,
48+
);
49+
1850
/// Returns a scheme that can honor [mode] without silently pinning brightness.
1951
///
2052
/// System mode only offers paired themes because they have both light and dark

‎mobile/test/features/settings/theme_picker_page_test.dart‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ void main() {
195195
railAction('Set').onPressed!();
196196
await tester.pumpAndSettle();
197197
expect(notifier.preferenceWrites, 1);
198-
expect(notifier.lastPreference?.mode, ThemeMode.light);
198+
expect(instance.getString(appearanceModeKey), ThemeMode.light.name);
199+
expect(notifier.lastPreference?.mode, defaultCommunityTheme.mode);
199200
expect(
200201
notifier.lastPreference?.accent,
201202
chatColorChoices[jade].wireValue,
@@ -222,7 +223,7 @@ void main() {
222223

223224
testWidgets('mode and chat color remain drafts until Set', (tester) async {
224225
final notifier = _RecordingCommunityThemeNotifier();
225-
await _pumpPicker(tester, themeNotifier: () => notifier);
226+
final prefs = await _pumpPicker(tester, themeNotifier: () => notifier);
226227
// System -> Light -> Dark.
227228
for (var i = 0; i < 2; i++) {
228229
await tester.tap(
@@ -249,11 +250,47 @@ void main() {
249250
await tester.tap(find.byKey(const ValueKey('theme-preview-set')));
250251
await tester.pumpAndSettle();
251252
expect(notifier.preferenceWrites, 1);
252-
expect(notifier.lastPreference?.mode, ThemeMode.dark);
253-
expect(notifier.lastPreference?.theme, buzzDarkThemeName);
253+
// Mode stays on this device; the synced preference keeps its own theme
254+
// and followSystem and only takes the new chat color.
255+
expect(prefs.getString(appearanceModeKey), ThemeMode.dark.name);
256+
expect(notifier.lastPreference?.theme, defaultCommunityTheme.theme);
257+
expect(
258+
notifier.lastPreference?.followSystem,
259+
defaultCommunityTheme.followSystem,
260+
);
254261
expect(notifier.lastPreference?.accent, chatColorChoices[green].wireValue);
255262
});
256263

264+
testWidgets('picker uses the device mode and keeps the synced one', (
265+
tester,
266+
) async {
267+
const synced = CommunityThemePreference(
268+
theme: buzzDarkThemeName,
269+
accent: '#3b82f6',
270+
followSystem: false,
271+
);
272+
final notifier = _RecordingCommunityThemeNotifier(synced);
273+
final prefs = await _pumpPicker(
274+
tester,
275+
prefs: {appearanceModeKey: ThemeMode.light.name},
276+
themeNotifier: () => notifier,
277+
);
278+
expect(find.text('Light'), findsOneWidget);
279+
// Light -> Dark -> System.
280+
for (var i = 0; i < 2; i++) {
281+
await tester.tap(
282+
find.byKey(const ValueKey('theme-preview-appearance-action-button')),
283+
);
284+
await tester.pumpAndSettle();
285+
}
286+
await tester.tap(find.byKey(const ValueKey('theme-preview-set')));
287+
await tester.pumpAndSettle();
288+
289+
expect(prefs.getString(appearanceModeKey), ThemeMode.system.name);
290+
// The synced theme and followSystem survive a mode change untouched.
291+
expect(notifier.lastPreference, synced);
292+
});
293+
257294
for (final brightness in Brightness.values) {
258295
testWidgets('${brightness.name} chat preview follows draft colors only', (
259296
tester,
@@ -331,14 +368,15 @@ void main() {
331368

332369
testWidgets('Close discards mode changes', (tester) async {
333370
final notifier = _RecordingCommunityThemeNotifier();
334-
await _pumpPicker(tester, themeNotifier: () => notifier);
371+
final prefs = await _pumpPicker(tester, themeNotifier: () => notifier);
335372
await tester.tap(
336373
find.byKey(const ValueKey('theme-preview-appearance-action-button')),
337374
);
338375
await tester.pumpAndSettle();
339376
await tester.tap(find.byKey(const ValueKey('theme-preview-close')));
340377
await tester.pumpAndSettle();
341378
expect(notifier.preferenceWrites, 0);
379+
expect(prefs.getString(appearanceModeKey), isNull);
342380
});
343381

344382
testWidgets(
@@ -358,11 +396,14 @@ void main() {
358396
}
359397

360398
class _RecordingCommunityThemeNotifier extends CommunityThemeNotifier {
399+
_RecordingCommunityThemeNotifier([this.initial = defaultCommunityTheme]);
400+
401+
final CommunityThemePreference initial;
361402
int preferenceWrites = 0;
362403
CommunityThemePreference? lastPreference;
363404

364405
@override
365-
CommunityThemePreference build() => defaultCommunityTheme;
406+
CommunityThemePreference build() => initial;
366407

367408
@override
368409
void setPreference(CommunityThemePreference preference) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'package:buzz/shared/theme/theme.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:hooks_riverpod/hooks_riverpod.dart';
5+
import 'package:shared_preferences/shared_preferences.dart';
6+
7+
Future<ProviderContainer> _container(Map<String, Object> initial) async {
8+
SharedPreferences.setMockInitialValues(initial);
9+
final prefs = await SharedPreferences.getInstance();
10+
final container = ProviderContainer(
11+
overrides: [savedPrefsProvider.overrideWithValue(prefs)],
12+
);
13+
addTearDown(container.dispose);
14+
return container;
15+
}
16+
17+
void main() {
18+
test('defaults to System, even when the synced theme is pinned', () async {
19+
final container = await _container({
20+
'buzz_theme_mode': ThemeMode.light.name,
21+
'buzz-local-theme.v1':
22+
'{"version":1,"theme":"buzz","accent":"#3b82f6","followSystem":false}',
23+
});
24+
25+
expect(container.read(appearanceModeProvider), ThemeMode.system);
26+
});
27+
28+
test('reads and saves the device mode', () async {
29+
final container = await _container({
30+
appearanceModeKey: ThemeMode.dark.name,
31+
});
32+
expect(container.read(appearanceModeProvider), ThemeMode.dark);
33+
34+
container.read(appearanceModeProvider.notifier).setMode(ThemeMode.light);
35+
36+
expect(container.read(appearanceModeProvider), ThemeMode.light);
37+
final prefs = container.read(savedPrefsProvider);
38+
expect(prefs.getString(appearanceModeKey), ThemeMode.light.name);
39+
});
40+
41+
test('an unknown saved value falls back to System', () async {
42+
final container = await _container({appearanceModeKey: 'sepia'});
43+
44+
expect(container.read(appearanceModeProvider), ThemeMode.system);
45+
});
46+
}

0 commit comments

Comments
 (0)