mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
**Category:** new-feature **User Impact:** Mobile now keeps each community’s appearance in sync with desktop, including theme, accent, and system-mode preference. **Problem:** Appearance choices were device-local, so the same account could look different between desktop and mobile. Live sync could also stop after the relay closed a subscription. **Solution:** Store each community’s encrypted appearance preference on its relay using the shared desktop wire contract, restore it from a local identity-scoped cache, and apply replacement events live. Closed subscriptions now recover with guarded backoff and fetch the latest preference so no update is lost during the gap. <details> <summary>File changes</summary> **mobile/lib/app.dart** Connects community appearance state to the authenticated app lifecycle. **mobile/lib/features/settings/accent_picker_page.dart** Aligns mobile accent choices and selection behavior with the shared catalog. **mobile/lib/features/settings/settings_page/appearance_section.dart** Clarifies the active appearance and hides accent controls when the Buzz theme owns its neutral accent. **mobile/lib/features/settings/theme_picker_page.dart** Persists catalog theme choices through the community-scoped provider. **mobile/lib/shared/theme/accent_colors.dart** Matches desktop’s accent catalog and wire values. **mobile/lib/shared/theme/buzz_theme.dart** Keeps Buzz visually neutral without discarding the user’s stored accent for other themes. **mobile/lib/shared/theme/community_theme_preference.dart** Defines and validates the versioned desktop-compatible appearance payload. **mobile/lib/shared/theme/community_theme_provider.dart** Coordinates cache-first appearance loading with account and community changes. **mobile/lib/shared/theme/community_theme_sync.dart** Adds encrypted NIP-78 relay persistence, live replacement handling, deterministic ordering, safe seeding, and resilient subscription recovery. **mobile/lib/shared/theme/theme.dart** Exports the community appearance modules. **mobile/test/features/settings/theme_picker_page_test.dart** Covers the updated settings behavior. **mobile/test/shared/crypto/nip44_interop_test.dart** Proves Dart decrypts a desktop-produced nostr-rs NIP-44 v2 preference. **mobile/test/shared/theme/buzz_theme_test.dart** Covers Buzz’s neutral rendering and stored-accent restoration. **mobile/test/shared/theme/community_theme_preference_test.dart** Covers wire parsing, validation, migration, and future-version handling. **mobile/test/shared/theme/community_theme_sync_test.dart** Covers cache/relay lifecycle, replacement ordering, switching races, absence-only seeding, and closed-subscription recovery. </details> ## Reproduction steps 1. Sign into desktop and mobile with the same account and join the same community relay. 2. On desktop, choose a distinctive non-Buzz theme and accent; mobile should update without a local toggle. 3. Restart mobile and confirm it restores the same appearance. 4. Change the mobile theme and accent and confirm desktop follows. 5. Leave mobile idle or backgrounded through a relay reconnect, then change desktop again; mobile should resubscribe and catch up automatically. 6. Switch communities and confirm each community restores only its own appearance. --------- Signed-off-by: Taylor Ho <taylorkmho@gmail.com> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@buzz.block.builderlab.xyz>
168 lines
5.2 KiB
Dart
168 lines
5.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'accent_colors.dart';
|
|
import 'theme_catalog.dart';
|
|
import 'theme_provider.dart' show effectiveTheme, schemeForAppearanceMode;
|
|
|
|
const communityThemeDTag = 'community-theme';
|
|
const defaultCommunityTheme = CommunityThemePreference(
|
|
theme: 'buzz',
|
|
accent: '#3b82f6',
|
|
followSystem: true,
|
|
);
|
|
|
|
class CommunityThemePreference {
|
|
final int version;
|
|
final String theme;
|
|
final String accent;
|
|
final bool followSystem;
|
|
|
|
const CommunityThemePreference({
|
|
this.version = 1,
|
|
required this.theme,
|
|
required this.accent,
|
|
required this.followSystem,
|
|
});
|
|
|
|
factory CommunityThemePreference.fromJson(Map<String, dynamic> json) {
|
|
if (json['version'] != 1 ||
|
|
json['theme'] is! String ||
|
|
findTheme(json['theme'] as String) == null ||
|
|
json['accent'] is! String ||
|
|
accentIndexForWireValue(json['accent'] as String) == null ||
|
|
json['followSystem'] is! bool) {
|
|
throw const FormatException('Invalid community theme preference');
|
|
}
|
|
return CommunityThemePreference(
|
|
theme: json['theme'] as String,
|
|
accent: json['accent'] as String,
|
|
followSystem: json['followSystem'] as bool,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'version': version,
|
|
'theme': theme,
|
|
'accent': accent,
|
|
'followSystem': followSystem,
|
|
};
|
|
|
|
ThemeMode get mode {
|
|
if (followSystem) return ThemeMode.system;
|
|
return findTheme(theme)?.isDark == true ? ThemeMode.dark : ThemeMode.light;
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is CommunityThemePreference &&
|
|
theme == other.theme &&
|
|
accent == other.accent &&
|
|
followSystem == other.followSystem;
|
|
|
|
@override
|
|
int get hashCode => Object.hash(theme, accent, followSystem);
|
|
}
|
|
|
|
class CommunityThemeStorage {
|
|
static const _prefix = 'buzz-community-theme.v1';
|
|
static const _outboxPrefix = 'buzz-community-theme-outbox.v1';
|
|
static const _migrationPrefix = 'buzz-community-theme-migrated.v1';
|
|
static const _legacyModeKey = 'buzz_theme_mode';
|
|
static const _legacyAccentKey = 'buzz_accent_color';
|
|
static const _legacySchemeKey = 'buzz_color_scheme';
|
|
|
|
final SharedPreferences prefs;
|
|
|
|
const CommunityThemeStorage(this.prefs);
|
|
|
|
String key(String pubkey, String relayUrl) =>
|
|
'$_prefix:$pubkey:${Uri.encodeComponent(normalizeCommunityRelayUrl(relayUrl))}';
|
|
|
|
String outboxKey(String pubkey, String relayUrl) =>
|
|
'$_outboxPrefix:$pubkey:${Uri.encodeComponent(normalizeCommunityRelayUrl(relayUrl))}';
|
|
|
|
CommunityThemePreference? _readKey(String storageKey) {
|
|
try {
|
|
final raw = prefs.getString(storageKey);
|
|
if (raw == null) return null;
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is! Map<String, dynamic>) return null;
|
|
return CommunityThemePreference.fromJson(decoded);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
CommunityThemePreference? read(String pubkey, String relayUrl) =>
|
|
_readKey(key(pubkey, relayUrl));
|
|
|
|
CommunityThemePreference? readOutbox(String pubkey, String relayUrl) =>
|
|
_readKey(outboxKey(pubkey, relayUrl));
|
|
|
|
Future<bool> write(
|
|
String pubkey,
|
|
String relayUrl,
|
|
CommunityThemePreference preference,
|
|
) => prefs.setString(key(pubkey, relayUrl), jsonEncode(preference.toJson()));
|
|
|
|
Future<bool> writeOutbox(
|
|
String pubkey,
|
|
String relayUrl,
|
|
CommunityThemePreference preference,
|
|
) => prefs.setString(
|
|
outboxKey(pubkey, relayUrl),
|
|
jsonEncode(preference.toJson()),
|
|
);
|
|
|
|
Future<void> clearOutbox(
|
|
String pubkey,
|
|
String relayUrl,
|
|
CommunityThemePreference acknowledged,
|
|
) async {
|
|
if (readOutbox(pubkey, relayUrl) == acknowledged) {
|
|
await prefs.remove(outboxKey(pubkey, relayUrl));
|
|
}
|
|
}
|
|
|
|
bool hasMigrated(String pubkey) =>
|
|
prefs.getBool('$_migrationPrefix:$pubkey') == true;
|
|
|
|
Future<bool> markMigrated(String pubkey) =>
|
|
prefs.setBool('$_migrationPrefix:$pubkey', true);
|
|
|
|
Future<void> writeLegacy(CommunityThemePreference preference) async {
|
|
await prefs.setString(_legacyModeKey, preference.mode.name);
|
|
await prefs.setString(_legacySchemeKey, preference.theme);
|
|
await prefs.setInt(
|
|
_legacyAccentKey,
|
|
accentIndexForWireValue(preference.accent) ?? defaultAccentIndex,
|
|
);
|
|
}
|
|
|
|
CommunityThemePreference legacyPreference() {
|
|
final modeName = prefs.getString(_legacyModeKey);
|
|
final mode =
|
|
ThemeMode.values.where((value) => value.name == modeName).firstOrNull ??
|
|
ThemeMode.system;
|
|
final storedTheme = prefs.getString(_legacySchemeKey);
|
|
final theme = findTheme(storedTheme ?? 'buzz')?.name ?? 'buzz';
|
|
final legacyAccent = prefs.getInt(_legacyAccentKey);
|
|
final resolvedTheme = switch (mode) {
|
|
ThemeMode.system => schemeForAppearanceMode(theme, mode) ?? theme,
|
|
ThemeMode.light ||
|
|
ThemeMode.dark => effectiveTheme(theme, mode)?.name ?? theme,
|
|
};
|
|
return CommunityThemePreference(
|
|
theme: resolvedTheme,
|
|
accent: legacyAccentWireValue(legacyAccent),
|
|
followSystem: mode == ThemeMode.system,
|
|
);
|
|
}
|
|
}
|
|
|
|
String normalizeCommunityRelayUrl(String relayUrl) =>
|
|
relayUrl.trim().replaceFirst(RegExp(r'/+$'), '').toLowerCase();
|