Files
buzz/mobile/lib/shared/theme/theme_provider.dart
Arthur Garza 4a440b5932 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>
2026-11-23 12:38:16 -06:00

117 lines
4.3 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'buzz_theme.dart';
import 'theme_catalog.dart';
import 'theme_pairs.dart';
/// Buzz ships as the default: the first-party pair, so a fresh install gets the
/// branded top-section gradient without picking a theme first.
const defaultSchemeName = buzzThemeName;
/// Pre-loaded SharedPreferences instance, overridden in main().
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
/// variants. Switching to it from an unpaired or unknown selection therefore
/// falls back to the first paired theme, matching the picker ordering.
String? schemeForAppearanceMode(String? schemeName, ThemeMode mode) {
if (mode != ThemeMode.system) return schemeName;
final selected = findTheme(schemeName ?? defaultSchemeName);
if (selected != null && isPairedTheme(selected.name)) return schemeName;
return themeGroups().paired.firstOrNull?.name ?? schemeName;
}
/// The theme whose colors are actually applied for [schemeName] under [mode].
///
/// In [ThemeMode.system] that is the selected theme itself — its pair covers the
/// other brightness. In the pinned modes it is the theme coerced to the pinned
/// brightness, which is what the picker highlights so the checkmark always
/// tracks what is on screen.
ThemeColors? effectiveTheme(String? schemeName, ThemeMode mode) {
final selected =
findTheme(schemeName ?? defaultSchemeName) ??
findTheme(defaultSchemeName);
if (selected == null) return null;
return switch (mode) {
ThemeMode.system => selected,
ThemeMode.light => _themeForBrightness(selected, wantDark: false),
ThemeMode.dark => _themeForBrightness(selected, wantDark: true),
};
}
/// Coerces [selected] to the requested brightness: itself when it already
/// matches, otherwise its pair, otherwise the first catalog theme of that
/// brightness. Keeps a stored light theme usable after switching to Dark mode
/// without rewriting the user's saved selection.
ThemeColors _themeForBrightness(
ThemeColors selected, {
required bool wantDark,
}) {
if (selected.isDark == wantDark) return selected;
final pairName = themePairFor(selected.name);
final paired = pairName == null ? null : findTheme(pairName);
if (paired != null && paired.isDark == wantDark) return paired;
// Match desktop's paired-first picker ordering: an unpaired selection falls
// back through the first-party default pair, not whichever borrowed theme
// sorts first in the full brightness group.
final defaultTheme = findTheme(defaultSchemeName);
if (defaultTheme != null) {
if (defaultTheme.isDark == wantDark) return defaultTheme;
final defaultPairName = themePairFor(defaultTheme.name);
final defaultPair = defaultPairName == null
? null
: findTheme(defaultPairName);
if (defaultPair != null && defaultPair.isDark == wantDark) {
return defaultPair;
}
}
final groups = themeGroups();
final fallback = wantDark ? groups.dark : groups.light;
return fallback.isEmpty ? selected : fallback.first;
}