fix(theme): preserve legacy appearance migration

Signed-off-by: kenny lopez <klopez4212@gmail.com>
This commit is contained in:
kenny lopez
2026-08-13 18:35:29 +01:00
parent 693256b561
commit c016e83172
8 changed files with 357 additions and 46 deletions

View File

@@ -29,6 +29,9 @@ class CommunityThemePreference {
final bool glassBackground;
final int glassOpacity;
final bool prominentActiveTab;
final bool includesGlassBackground;
final bool includesGlassOpacity;
final bool includesProminentActiveTab;
const CommunityThemePreference({
this.version = 1,
@@ -38,6 +41,9 @@ class CommunityThemePreference {
this.glassBackground = defaultCommunityGlassBackground,
this.glassOpacity = defaultCommunityGlassOpacity,
this.prominentActiveTab = defaultCommunityProminentActiveTab,
this.includesGlassBackground = true,
this.includesGlassOpacity = true,
this.includesProminentActiveTab = true,
});
factory CommunityThemePreference.fromJson(Map<String, dynamic> json) {
@@ -49,13 +55,16 @@ class CommunityThemePreference {
json['followSystem'] is! bool) {
throw const FormatException('Invalid community theme preference');
}
final glassBackground = json.containsKey('glassBackground')
final includesGlassBackground = json.containsKey('glassBackground');
final includesGlassOpacity = json.containsKey('glassOpacity');
final includesProminentActiveTab = json.containsKey('prominentActiveTab');
final glassBackground = includesGlassBackground
? json['glassBackground']
: defaultCommunityGlassBackground;
final glassOpacity = json.containsKey('glassOpacity')
final glassOpacity = includesGlassOpacity
? json['glassOpacity']
: defaultCommunityGlassOpacity;
final prominentActiveTab = json.containsKey('prominentActiveTab')
final prominentActiveTab = includesProminentActiveTab
? json['prominentActiveTab']
: defaultCommunityProminentActiveTab;
if (glassBackground is! bool ||
@@ -74,6 +83,9 @@ class CommunityThemePreference {
glassBackground: glassBackground,
glassOpacity: glassOpacity.toInt(),
prominentActiveTab: prominentActiveTab,
includesGlassBackground: includesGlassBackground,
includesGlassOpacity: includesGlassOpacity,
includesProminentActiveTab: includesProminentActiveTab,
);
}
@@ -82,9 +94,9 @@ class CommunityThemePreference {
'theme': theme,
'accent': accent,
'followSystem': followSystem,
'glassBackground': glassBackground,
'glassOpacity': glassOpacity,
'prominentActiveTab': prominentActiveTab,
if (includesGlassBackground) 'glassBackground': glassBackground,
if (includesGlassOpacity) 'glassOpacity': glassOpacity,
if (includesProminentActiveTab) 'prominentActiveTab': prominentActiveTab,
};
CommunityThemePreference copyWith({
@@ -102,6 +114,10 @@ class CommunityThemePreference {
glassBackground: glassBackground ?? this.glassBackground,
glassOpacity: glassOpacity ?? this.glassOpacity,
prominentActiveTab: prominentActiveTab ?? this.prominentActiveTab,
includesGlassBackground: glassBackground != null || includesGlassBackground,
includesGlassOpacity: glassOpacity != null || includesGlassOpacity,
includesProminentActiveTab:
prominentActiveTab != null || includesProminentActiveTab,
);
ThemeMode get mode {
@@ -117,7 +133,10 @@ class CommunityThemePreference {
followSystem == other.followSystem &&
glassBackground == other.glassBackground &&
glassOpacity == other.glassOpacity &&
prominentActiveTab == other.prominentActiveTab;
prominentActiveTab == other.prominentActiveTab &&
includesGlassBackground == other.includesGlassBackground &&
includesGlassOpacity == other.includesGlassOpacity &&
includesProminentActiveTab == other.includesProminentActiveTab;
@override
int get hashCode => Object.hash(
@@ -127,6 +146,9 @@ class CommunityThemePreference {
glassBackground,
glassOpacity,
prominentActiveTab,
includesGlassBackground,
includesGlassOpacity,
includesProminentActiveTab,
);
}

View File

@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';
import 'package:buzz/shared/theme/theme.dart';
import 'package:flutter/material.dart';
@@ -24,7 +25,7 @@ void main() {
);
});
test('older v1 payloads gain capability-independent defaults', () {
test('older v1 payloads retain missing desktop-only fields', () {
final preference = CommunityThemePreference.fromJson({
'version': 1,
'theme': 'buzz',
@@ -35,7 +36,56 @@ void main() {
expect(preference.glassBackground, isFalse);
expect(preference.glassOpacity, 65);
expect(preference.prominentActiveTab, isFalse);
expect(preference.toJson(), containsPair('glassOpacity', 65));
expect(preference.toJson(), isNot(contains('glassBackground')));
expect(preference.toJson(), isNot(contains('glassOpacity')));
expect(preference.toJson(), isNot(contains('prominentActiveTab')));
});
test('mobile edits do not widen legacy appearance fields with defaults', () {
final legacy = CommunityThemePreference.fromJson({
'version': 1,
'theme': 'buzz',
'accent': '#3b82f6',
'followSystem': true,
});
final edited = legacy.copyWith(accent: '#ef4444');
expect(edited.toJson(), {
'version': 1,
'theme': 'buzz',
'accent': '#ef4444',
'followSystem': true,
});
expect(legacy.copyWith(glassOpacity: 70).toJson(), {
'version': 1,
'theme': 'buzz',
'accent': '#3b82f6',
'followSystem': true,
'glassOpacity': 70,
});
});
test('mobile appearance limits match the shared wire contract', () {
final contract =
jsonDecode(File('../schema/community-theme-v1.json').readAsStringSync())
as Map<String, dynamic>;
final properties = contract['properties'] as Map<String, dynamic>;
final glassBackground =
properties['glassBackground'] as Map<String, dynamic>;
final glassOpacity = properties['glassOpacity'] as Map<String, dynamic>;
final prominentActiveTab =
properties['prominentActiveTab'] as Map<String, dynamic>;
expect(defaultCommunityGlassBackground, glassBackground['default'] as bool);
expect(defaultCommunityGlassOpacity, glassOpacity['default'] as int);
expect(minCommunityGlassOpacity, glassOpacity['minimum'] as int);
expect(maxCommunityGlassOpacity, glassOpacity['maximum'] as int);
expect(
defaultCommunityProminentActiveTab,
prominentActiveTab['default'] as bool,
);
});
test('rejects invalid theme and appearance values', () {

View File

@@ -11,6 +11,65 @@ import 'package:nostr/nostr.dart' as nostr;
import 'package:shared_preferences/shared_preferences.dart';
void main() {
test(
'mobile edit preserves omitted desktop-only fields from a legacy cache',
() async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final keys = nostr.Keys.generate();
final storage = CommunityThemeStorage(prefs);
await prefs.setString(
storage.key(keys.public, 'https://relay.example'),
jsonEncode({
'version': 1,
'theme': 'buzz',
'accent': '#3b82f6',
'followSystem': true,
}),
);
final session = _ThemeRelaySession(
keys.nsec,
keys.public,
initialStatus: SessionStatus.reconnecting,
);
final container = ProviderContainer(
overrides: [
communityThemeStorageProvider.overrideWithValue(storage),
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
relaySessionProvider.overrideWith(() => session),
],
);
addTearDown(container.dispose);
container.listen(
communityThemeProvider,
(_, _) {},
fireImmediately: true,
);
container
.read(communityThemeProvider.notifier)
.setAccent(accentIndexForWireValue('#ef4444')!);
await _waitUntil(() {
final raw = prefs.getString(
storage.key(keys.public, 'https://relay.example'),
);
if (raw == null) return false;
return (jsonDecode(raw) as Map<String, dynamic>)['accent'] == '#ef4444';
});
final persisted =
jsonDecode(
prefs.getString(
storage.key(keys.public, 'https://relay.example'),
)!,
)
as Map<String, dynamic>;
expect(persisted, isNot(contains('glassBackground')));
expect(persisted, isNot(contains('glassOpacity')));
expect(persisted, isNot(contains('prominentActiveTab')));
},
);
test('mobile edits retain desktop-only appearance preferences', () async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();