Files
buzz/mobile/lib/shared/theme/theme_provider.dart
Arthur Garza a8cc77ac34 fix(mobile): keep a legacy chat color when Set changes only the mode (#9898)
Follow-up to #9894, addressing the review finding there.

## Problem
Some desktop chat colors (for example Cyan `#06b6d4`) have no exact
mobile swatch, so the Appearance picker shows the closest one (Jade).
Tapping **Set** after changing only Light/Dark/System saved Jade's value
(`#22c55e`), and that new color synced to every device.

## Fix
On Set, the saved accent is kept unless the user picked a different
color.

Also adds doc comments to `appearanceModeProvider` and
`AppearanceModeNotifier.setMode`.

## Tests
- The existing mode-only picker test now starts from legacy Cyan and
goes through the real `CommunityThemeNotifier.setPreference`, asserting
the synced preference is unchanged. It fails with the fix reverted.
- `just mobile-check` and `just mobile-test` pass.

## Try it
1. On desktop, set the chat color to a legacy color such as Cyan.
2. On mobile, open Settings → Appearance, change only Light/Dark/System,
tap Set.
3. Desktop and other devices still show Cyan.

🤖 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 20:53:23 +00:00

119 lines
4.4 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;
}
/// Saves [mode] on this device only; it is never synced.
void setMode(ThemeMode mode) {
if (mode == state) return;
state = mode;
unawaited(
ref.read(savedPrefsProvider).setString(appearanceModeKey, mode.name),
);
}
}
/// This device's Light, Dark or System choice. See [AppearanceModeNotifier].
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;
}