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>
This commit is contained in:
Arthur Garza
2026-11-23 14:53:23 -06:00
committed by GitHub
parent e600cdbde2
commit a8cc77ac34
4 changed files with 259 additions and 31 deletions

View File

@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:buzz/shared/theme/buzz_icons.dart';
import '../../shared/theme/theme.dart';
@@ -22,7 +23,7 @@ String appearanceModeLabel(ThemeMode mode) =>
Future<void> showAccentColorPickerSheet({
required BuildContext context,
required ColorScheme colorScheme,
required int selectedIndex,
required int? pickedIndex,
required ValueChanged<int> onSelected,
}) => showBuzzModalBottomSheet<void>(
context: context,
@@ -33,27 +34,34 @@ Future<void> showAccentColorPickerSheet({
DuoToolbarScope.maybeOf(context)?.strip != null &&
usesConversationPanes(context),
colorScheme: colorScheme,
initialIndex: selectedIndex,
pickedIndex: pickedIndex,
onSelected: onSelected,
),
);
class _AccentColorPicker extends HookWidget {
class _AccentColorPicker extends HookConsumerWidget {
const _AccentColorPicker({
required this.colorScheme,
required this.compact,
required this.initialIndex,
required this.pickedIndex,
required this.onSelected,
});
final ColorScheme colorScheme;
final bool compact;
final int initialIndex;
/// The swatch the user already tapped, or null to follow the saved color.
final int? pickedIndex;
final ValueChanged<int> onSelected;
@override
Widget build(BuildContext context) {
final selected = useState(initialIndex);
Widget build(BuildContext context, WidgetRef ref) {
final picked = useState(pickedIndex);
// Until a tap, the marker follows the saved color, including a sync.
final saved = ref.watch(
communityThemeProvider.select((t) => chatColorIndex(t.accent)),
);
final selected = picked.value ?? saved;
return SafeArea(
top: false,
child: Padding(
@@ -83,10 +91,10 @@ class _AccentColorPicker extends HookWidget {
itemBuilder: (context, index) => _AccentSheetOption(
index: index,
color: chatColorForScheme(colorScheme, index),
selected: selected.value == index,
selected: selected == index,
onTap: () {
unawaited(HapticFeedback.selectionClick());
selected.value = index;
picked.value = index;
onSelected(index);
},
),

View File

@@ -10,9 +10,14 @@ class _ThemePreviewExperience extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final inRail = DuoToolbarScope.maybeOf(context)?.strip != null;
final stored = ref.read(communityThemeProvider);
final draftMode = useState(ref.read(appearanceModeProvider));
final draftAccent = useState(chatColorIndex(stored.accent));
final pickedAccent = useState<int?>(null);
// Until the user taps a swatch, the preview follows the saved color,
// including one synced from another device while this sheet is open.
final savedAccent = ref.watch(
communityThemeProvider.select((t) => chatColorIndex(t.accent)),
);
final draftAccent = pickedAccent.value ?? savedAccent;
final platformBrightness = MediaQuery.platformBrightnessOf(context);
final reduceMotion = MediaQuery.disableAnimationsOf(context);
final brightness = switch (draftMode.value) {
@@ -36,22 +41,27 @@ class _ThemePreviewExperience extends HookConsumerWidget {
void chooseColor() => showAccentColorPickerSheet(
context: context,
colorScheme: selectedBaseScheme,
selectedIndex: draftAccent.value,
onSelected: (index) => draftAccent.value = index,
pickedIndex: pickedAccent.value,
onSelected: (index) => pickedAccent.value = index,
);
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.
// they are, so this device's mode never changes other devices. A color
// the user did not tap keeps its saved value, so a legacy color that
// only maps to a swatch is not republished as that swatch.
final current = ref.read(communityThemeProvider);
ref
.read(communityThemeProvider.notifier)
.setPreference(
CommunityThemePreference(
theme: current.theme,
accent: chatColorChoices[draftAccent.value].wireValue,
accent: switch (pickedAccent.value) {
final index? => chatColorChoices[index].wireValue,
null => current.accent,
},
followSystem: current.followSystem,
),
);
@@ -89,7 +99,7 @@ class _ThemePreviewExperience extends HookConsumerWidget {
child: _ThemePreviewPage(
theme: selectedEntry,
mode: draftMode.value,
accentIndex: draftAccent.value,
accentIndex: draftAccent,
platformBrightness: platformBrightness,
),
),
@@ -116,7 +126,7 @@ class _ThemePreviewExperience extends HookConsumerWidget {
semanticLabel: 'Chat color',
color: chatColorForScheme(
selectedBaseScheme,
draftAccent.value,
draftAccent,
),
onTap: chooseColor,
),
@@ -161,7 +171,7 @@ class _ThemePreviewExperience extends HookConsumerWidget {
_PreviewSheetCircleAction(
key: const ValueKey('theme-preview-accent-action-button'),
semanticLabel: 'Chat color',
color: chatColorForScheme(selectedBaseScheme, draftAccent.value),
color: chatColorForScheme(selectedBaseScheme, draftAccent),
onTap: chooseColor,
),
_AppearanceCycleAction(
@@ -213,10 +223,7 @@ class _ThemePreviewExperience extends HookConsumerWidget {
child: _PreviewSheetCircleAction(
key: const ValueKey('theme-preview-accent-action-button'),
semanticLabel: 'Chat color',
color: chatColorForScheme(
selectedBaseScheme,
draftAccent.value,
),
color: chatColorForScheme(selectedBaseScheme, draftAccent),
onTap: chooseColor,
),
),

View File

@@ -33,6 +33,7 @@ class AppearanceModeNotifier extends Notifier<ThemeMode> {
ThemeMode.system;
}
/// Saves [mode] on this device only; it is never synced.
void setMode(ThemeMode mode) {
if (mode == state) return;
state = mode;
@@ -42,6 +43,7 @@ class AppearanceModeNotifier extends Notifier<ThemeMode> {
}
}
/// This device's Light, Dark or System choice. See [AppearanceModeNotifier].
final appearanceModeProvider =
NotifierProvider<AppearanceModeNotifier, ThemeMode>(
AppearanceModeNotifier.new,

View File

@@ -1,12 +1,17 @@
import 'dart:convert';
import 'package:buzz/features/settings/theme_picker_page.dart';
import 'package:buzz/shared/relay/relay.dart';
import 'package:buzz/shared/theme/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:buzz/shared/widgets/ios_navigation_bar.dart';
import 'package:buzz/shared/widgets/ios_glass_navigation_button.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../helpers/fake_relay.dart';
import '../../helpers/widget_helpers.dart';
Future<SharedPreferences> _prefs(Map<String, Object> initial) async {
@@ -264,16 +269,17 @@ void main() {
testWidgets('picker uses the device mode and keeps the synced one', (
tester,
) async {
// Desktop's legacy Cyan only maps to the Jade swatch; a mode-only Set
// must not republish it as Jade's Green to other devices.
const synced = CommunityThemePreference(
theme: buzzDarkThemeName,
accent: '#3b82f6',
accent: '#06b6d4',
followSystem: false,
);
final notifier = _RecordingCommunityThemeNotifier(synced);
final prefs = await _pumpPicker(
tester,
prefs: {appearanceModeKey: ThemeMode.light.name},
themeNotifier: () => notifier,
themeNotifier: () => _SeededCommunityThemeNotifier(synced),
);
expect(find.text('Light'), findsOneWidget);
// Light -> Dark -> System.
@@ -287,8 +293,206 @@ void main() {
await tester.pumpAndSettle();
expect(prefs.getString(appearanceModeKey), ThemeMode.system.name);
// The synced theme and followSystem survive a mode change untouched.
expect(notifier.lastPreference, synced);
// The synced preference survives a mode change untouched, through the
// real setPreference path.
final container = ProviderScope.containerOf(
tester.element(find.byType(MaterialApp)),
);
expect(container.read(communityThemeProvider), synced);
});
group('a legacy chat color', () {
// Desktop's legacy Cyan and Indigo have no swatch of their own: Cyan
// shows as Jade and Indigo as Cobalt. These run the real notifier over
// a fake relay, so Set goes through the real save and publish path.
const cyan = '#06b6d4';
const indigo = '#6366f1';
String swatch(String name) =>
chatColorChoices.firstWhere((color) => color.name == name).wireValue;
CommunityThemePreference preference(String accent) =>
CommunityThemePreference(
theme: buzzDarkThemeName,
accent: accent,
followSystem: false,
);
late FakeRelay relay;
late ProviderContainer container;
var revision = 1;
NostrEvent synced(String accent) => relay.sign(
kind: EventKind.readState,
tags: const [
['d', communityThemeDTag],
['t', communityThemeDTag],
],
content: jsonEncode(preference(accent).toJson()),
createdAt: revision++,
author: relay.keys,
);
Future<void> pumpPicker(WidgetTester tester, String accent) async {
relay = FakeRelay();
revision = 1;
relay.seed([synced(accent)]);
final prefs = await _prefs({});
// Open the picker once the synced color has loaded, as a user would.
final open = ValueNotifier(false);
addTearDown(open.dispose);
await tester.pumpWidget(
WidgetHelpers.testable(
child: ValueListenableBuilder(
valueListenable: open,
builder: (_, isOpen, _) =>
isOpen ? const ThemePickerPage() : const SizedBox.shrink(),
),
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
...relay.overrides,
communityThemeCryptoProvider.overrideWithValue(
CommunityThemeCrypto(encrypt: (s) => s, decrypt: (s) => s),
),
],
),
);
container = ProviderScope.containerOf(
tester.element(find.byType(MaterialApp)),
);
// The app's theme keeps this provider listened in production.
container.listen(communityThemeProvider, (_, _) {});
await tester.pumpAndSettle();
expect(container.read(communityThemeProvider).accent, accent);
open.value = true;
await tester.pumpAndSettle();
}
Future<void> receive(WidgetTester tester, String accent) async {
relay.deliver(synced(accent));
await tester.pumpAndSettle();
expect(container.read(communityThemeProvider).accent, accent);
}
int shownSwatch(WidgetTester tester) {
final bubble =
tester
.widget<DecoratedBox>(
find.byKey(const ValueKey('theme-chat-outgoing-bubble')),
)
.decoration
as BoxDecoration;
// System mode follows the test platform's light appearance, and one
// mode tap would make it Light: the preview is light either way.
return chatColorChoices.indexWhere(
(choice) =>
mobileAppearanceTheme(
Brightness.light,
choice.wireValue,
).extension<ChatColors>()!.outgoing ==
bubble.color,
);
}
int index(String name) =>
chatColorChoices.indexWhere((color) => color.name == name);
Future<void> tapSwatch(WidgetTester tester, String name) async {
final option = find.byKey(ValueKey('accent-option-${index(name)}'));
await tester.tap(
find.byKey(const ValueKey('theme-preview-accent-action-button')),
);
await tester.pumpAndSettle();
await tester.ensureVisible(option);
await tester.pumpAndSettle();
await tester.tap(option);
await tester.pumpAndSettle();
Navigator.of(tester.element(option)).pop();
await tester.pumpAndSettle();
}
Future<void> cycleModeAndSet(WidgetTester tester) async {
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')));
// Past the sync manager's publish debounce.
await tester.pumpAndSettle(const Duration(seconds: 3));
}
List<String> publishedAccents() => [
for (final event in relay.published)
if (event.getTagValue('d') == communityThemeDTag)
(jsonDecode(event.content) as Map<String, dynamic>)['accent']
as String,
];
testWidgets('publishes the swatch it maps to when the user taps it', (
tester,
) async {
await pumpPicker(tester, cyan);
await tapSwatch(tester, 'Jade');
await cycleModeAndSet(tester);
expect(container.read(communityThemeProvider).accent, swatch('Jade'));
expect(publishedAccents(), [swatch('Jade')]);
});
testWidgets('keeps a tapped swatch when a sync lands under the picker', (
tester,
) async {
await pumpPicker(tester, cyan);
await tapSwatch(tester, 'Cobalt');
await receive(tester, indigo);
await cycleModeAndSet(tester);
expect(container.read(communityThemeProvider).accent, swatch('Cobalt'));
expect(publishedAccents(), [swatch('Cobalt')]);
});
testWidgets('keeps a synced color on a mode-only Set', (tester) async {
await pumpPicker(tester, cyan);
await receive(tester, indigo);
await cycleModeAndSet(tester);
expect(container.read(communityThemeProvider).accent, indigo);
expect(publishedAccents(), isEmpty);
});
testWidgets('the preview follows a sync until the user taps a swatch', (
tester,
) async {
await pumpPicker(tester, cyan);
expect(shownSwatch(tester), index('Jade'));
await receive(tester, indigo);
expect(shownSwatch(tester), index('Cobalt'));
await tapSwatch(tester, 'Gray');
await receive(tester, cyan);
expect(shownSwatch(tester), index('Gray'));
});
testWidgets('the open color grid follows a sync until the user taps', (
tester,
) async {
final semantics = tester.ensureSemantics();
await pumpPicker(tester, cyan);
await tester.tap(
find.byKey(const ValueKey('theme-preview-accent-action-button')),
);
await tester.pumpAndSettle();
Finder option(String name) =>
find.byKey(ValueKey('accent-option-${index(name)}'));
void expectMarked(String name) => expect(
tester.getSemantics(option(name)),
isSemantics(isSelected: true),
);
expectMarked('Jade');
await receive(tester, indigo);
expectMarked('Cobalt');
await tester.ensureVisible(option('Gray'));
await tester.pumpAndSettle();
await tester.tap(option('Gray'));
await tester.pumpAndSettle();
await receive(tester, cyan);
expectMarked('Gray');
semantics.dispose();
});
});
for (final brightness in Brightness.values) {
@@ -396,14 +600,11 @@ void main() {
}
class _RecordingCommunityThemeNotifier extends CommunityThemeNotifier {
_RecordingCommunityThemeNotifier([this.initial = defaultCommunityTheme]);
final CommunityThemePreference initial;
int preferenceWrites = 0;
CommunityThemePreference? lastPreference;
@override
CommunityThemePreference build() => initial;
CommunityThemePreference build() => defaultCommunityTheme;
@override
void setPreference(CommunityThemePreference preference) {
@@ -412,3 +613,13 @@ class _RecordingCommunityThemeNotifier extends CommunityThemeNotifier {
state = preference;
}
}
/// Real notifier, seeded with a synced preference.
class _SeededCommunityThemeNotifier extends CommunityThemeNotifier {
_SeededCommunityThemeNotifier(this.initial);
final CommunityThemePreference initial;
@override
CommunityThemePreference build() => initial;
}