Add custom themes, holiday themes, chat backgrounds and holiday animations
- Themes: name + background + accent, derived surfaces/text/bubbles; editor with live preview; share codes and .jsonl import/export - Theme icons: swap tab, send, settings, node avatar and map marker icons for named icons or pictures (lossless JPEG XL) - Chat backgrounds per channel/DM (color or JPEG XL picture with fade); themes can set one, optionally for every chat - Map follows custom themes (panels, accent, tile tint, markers) - Holiday themes (Halloween, Thanksgiving, Christmas, New Year's) with generated backgrounds, icons and map markers; automatic switching from 17 days before each holiday - Holiday greeting animations (bats, turkeys, Santa, fireworks) matched across all app languages via translation strings
This commit is contained in:
1 parent
780183b586
commit
4a574185e8
124 files changed
+12686
-292
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import 'custom_theme.dart';
|
||||
import 'image_codec_support.dart';
|
||||
import 'translation_support.dart';
|
||||
|
||||
@@ -135,6 +136,25 @@ class AppSettings {
|
||||
/// different first-hop repeaters also satisfies the channel resend.
|
||||
final bool channelMinHopsCountRepeaters;
|
||||
final String themeMode;
|
||||
|
||||
/// Themes the user made. Built-in presets live in [CustomTheme.presets].
|
||||
final List<CustomTheme> customThemes;
|
||||
|
||||
/// ID of the active custom or preset theme; null uses the stock theme with
|
||||
/// [themeMode].
|
||||
final String? activeThemeId;
|
||||
|
||||
/// Play a short animation for holiday greetings ("Happy Halloween", ...).
|
||||
final bool holidayAnimationsEnabled;
|
||||
|
||||
/// Show each holiday's theme automatically in the days before it (see
|
||||
/// `activeHolidayFor`), without changing [activeThemeId].
|
||||
final bool holidayAutoSwitch;
|
||||
|
||||
/// Occurrence key (`ActiveHoliday.occurrenceKey`) of the holiday whose
|
||||
/// automatic theme the user overrode by picking a theme themselves. Only
|
||||
/// that occurrence is skipped; the next holiday switches again.
|
||||
final String? holidayThemeDismissed;
|
||||
final String? languageOverride; // null = system default
|
||||
final bool appDebugLogEnabled;
|
||||
final Map<String, String> batteryChemistryByDeviceId;
|
||||
@@ -265,6 +285,11 @@ class AppSettings {
|
||||
this.channelMinHopsRetries = 1,
|
||||
this.channelMinHopsCountRepeaters = false,
|
||||
this.themeMode = 'system',
|
||||
this.customThemes = const [],
|
||||
this.activeThemeId,
|
||||
this.holidayAnimationsEnabled = true,
|
||||
this.holidayAutoSwitch = true,
|
||||
this.holidayThemeDismissed,
|
||||
this.languageOverride,
|
||||
this.appDebugLogEnabled = false,
|
||||
Map<String, String>? batteryChemistryByDeviceId,
|
||||
@@ -356,6 +381,11 @@ class AppSettings {
|
||||
'channel_min_hops_retries': channelMinHopsRetries,
|
||||
'channel_min_hops_count_repeaters': channelMinHopsCountRepeaters,
|
||||
'theme_mode': themeMode,
|
||||
'custom_themes': customThemes.map((t) => t.toJson()).toList(),
|
||||
'active_theme_id': activeThemeId,
|
||||
'holiday_animations_enabled': holidayAnimationsEnabled,
|
||||
'holiday_auto_switch': holidayAutoSwitch,
|
||||
'holiday_theme_dismissed': holidayThemeDismissed,
|
||||
'language_override': languageOverride,
|
||||
'app_debug_log_enabled': appDebugLogEnabled,
|
||||
'battery_chemistry_by_device_id': batteryChemistryByDeviceId,
|
||||
@@ -494,6 +524,18 @@ class AppSettings {
|
||||
channelMinHopsCountRepeaters:
|
||||
json['channel_min_hops_count_repeaters'] as bool? ?? false,
|
||||
themeMode: json['theme_mode'] as String? ?? 'system',
|
||||
customThemes:
|
||||
(json['custom_themes'] as List<dynamic>?)
|
||||
?.whereType<Map>()
|
||||
.map((e) => CustomTheme.fromJson(Map<String, dynamic>.from(e)))
|
||||
.where((t) => t.id.isNotEmpty)
|
||||
.toList() ??
|
||||
const [],
|
||||
activeThemeId: json['active_theme_id'] as String?,
|
||||
holidayAnimationsEnabled:
|
||||
json['holiday_animations_enabled'] as bool? ?? true,
|
||||
holidayAutoSwitch: json['holiday_auto_switch'] as bool? ?? true,
|
||||
holidayThemeDismissed: json['holiday_theme_dismissed'] as String?,
|
||||
languageOverride: json['language_override'] as String?,
|
||||
appDebugLogEnabled: json['app_debug_log_enabled'] as bool? ?? false,
|
||||
batteryChemistryByDeviceId:
|
||||
@@ -637,6 +679,11 @@ class AppSettings {
|
||||
int? channelMinHopsRetries,
|
||||
bool? channelMinHopsCountRepeaters,
|
||||
String? themeMode,
|
||||
List<CustomTheme>? customThemes,
|
||||
Object? activeThemeId = _unset,
|
||||
bool? holidayAnimationsEnabled,
|
||||
bool? holidayAutoSwitch,
|
||||
Object? holidayThemeDismissed = _unset,
|
||||
Object? languageOverride = _unset,
|
||||
bool? appDebugLogEnabled,
|
||||
Map<String, String>? batteryChemistryByDeviceId,
|
||||
@@ -725,6 +772,16 @@ class AppSettings {
|
||||
channelMinHopsCountRepeaters:
|
||||
channelMinHopsCountRepeaters ?? this.channelMinHopsCountRepeaters,
|
||||
themeMode: themeMode ?? this.themeMode,
|
||||
customThemes: customThemes ?? this.customThemes,
|
||||
activeThemeId: activeThemeId == _unset
|
||||
? this.activeThemeId
|
||||
: activeThemeId as String?,
|
||||
holidayAnimationsEnabled:
|
||||
holidayAnimationsEnabled ?? this.holidayAnimationsEnabled,
|
||||
holidayAutoSwitch: holidayAutoSwitch ?? this.holidayAutoSwitch,
|
||||
holidayThemeDismissed: holidayThemeDismissed == _unset
|
||||
? this.holidayThemeDismissed
|
||||
: holidayThemeDismissed as String?,
|
||||
languageOverride: languageOverride == _unset
|
||||
? this.languageOverride
|
||||
: languageOverride as String?,
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../theme/theme_icons.dart' show isThemeAssetPath;
|
||||
import 'custom_theme.dart';
|
||||
|
||||
/// Background drawn behind a chat's messages: a color or a picture.
|
||||
///
|
||||
/// [fade] blends the theme's normal background over it (0 = full strength,
|
||||
/// 1 = invisible) so text drawn outside bubbles stays readable.
|
||||
@immutable
|
||||
class ChatBackground {
|
||||
static const double defaultFade = 0.4;
|
||||
static const double minFade = 0.1;
|
||||
static const double maxFade = 0.9;
|
||||
|
||||
final Color? color;
|
||||
|
||||
/// ID of a picture in the chat background image store.
|
||||
final String? imageId;
|
||||
|
||||
/// Picture bytes carried inside a share code or `.jsonl` file, before it
|
||||
/// has been written to the image store. Never persisted in settings.
|
||||
final Uint8List? inlineImage;
|
||||
|
||||
/// A JPEG XL picture bundled with the app (built-in themes).
|
||||
final String? asset;
|
||||
|
||||
final double fade;
|
||||
|
||||
const ChatBackground.color(Color this.color, {this.fade = defaultFade})
|
||||
: imageId = null,
|
||||
inlineImage = null,
|
||||
asset = null;
|
||||
|
||||
const ChatBackground.image(String this.imageId, {this.fade = defaultFade})
|
||||
: color = null,
|
||||
inlineImage = null,
|
||||
asset = null;
|
||||
|
||||
const ChatBackground.asset(String this.asset, {this.fade = defaultFade})
|
||||
: color = null,
|
||||
imageId = null,
|
||||
inlineImage = null;
|
||||
|
||||
const ChatBackground._({
|
||||
this.color,
|
||||
this.imageId,
|
||||
this.inlineImage,
|
||||
this.asset,
|
||||
required this.fade,
|
||||
});
|
||||
|
||||
bool get isImage => imageId != null || inlineImage != null || asset != null;
|
||||
|
||||
ChatBackground withFade(double value) => ChatBackground._(
|
||||
color: color,
|
||||
imageId: imageId,
|
||||
inlineImage: inlineImage,
|
||||
asset: asset,
|
||||
fade: value.clamp(minFade, maxFade).toDouble(),
|
||||
);
|
||||
|
||||
/// A copy whose picture lives in the image store under [id].
|
||||
ChatBackground withStoredImage(String id) =>
|
||||
ChatBackground._(imageId: id, fade: fade);
|
||||
|
||||
/// A copy carrying the picture bytes for sharing.
|
||||
ChatBackground withInlineImage(Uint8List bytes) =>
|
||||
ChatBackground._(imageId: imageId, inlineImage: bytes, fade: fade);
|
||||
|
||||
/// Storage form: a color or an image-store ID.
|
||||
Map<String, dynamic> toJson() => {
|
||||
if (color != null) 'color': colorToHex(color!),
|
||||
if (imageId != null) 'image': imageId,
|
||||
if (asset != null) 'asset': asset,
|
||||
'fade': fade,
|
||||
};
|
||||
|
||||
/// Sharing form: a color or the picture itself. Null when this is a
|
||||
/// picture whose bytes were not attached.
|
||||
Map<String, dynamic>? toShareJson() {
|
||||
if (color != null) return {'color': colorToHex(color!), 'fade': fade};
|
||||
// Bundled pictures exist in every install, so the path is enough.
|
||||
if (asset != null) return {'asset': asset, 'fade': fade};
|
||||
if (inlineImage != null) {
|
||||
return {'imageData': base64Encode(inlineImage!), 'fade': fade};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static ChatBackground? fromJson(Object? json) {
|
||||
if (json is! Map) return null;
|
||||
final rawFade = json['fade'];
|
||||
final fade = (rawFade is num ? rawFade.toDouble() : defaultFade)
|
||||
.clamp(minFade, maxFade)
|
||||
.toDouble();
|
||||
final color = parseHexColor(json['color']);
|
||||
if (color != null) return ChatBackground.color(color, fade: fade);
|
||||
final asset = json['asset'];
|
||||
if (isThemeAssetPath(asset)) {
|
||||
return ChatBackground.asset(asset as String, fade: fade);
|
||||
}
|
||||
final imageId = json['image'];
|
||||
if (imageId is String && imageId.isNotEmpty) {
|
||||
return ChatBackground.image(imageId, fade: fade);
|
||||
}
|
||||
final data = json['imageData'];
|
||||
if (data is String && data.isNotEmpty) {
|
||||
try {
|
||||
return ChatBackground._(inlineImage: base64Decode(data), fade: fade);
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is ChatBackground &&
|
||||
other.color == color &&
|
||||
other.imageId == imageId &&
|
||||
other.asset == asset &&
|
||||
other.fade == fade &&
|
||||
identical(other.inlineImage, inlineImage);
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(color, imageId, asset, fade, inlineImage);
|
||||
}
|
||||
@@ -0,0 +1,394 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../theme/theme_icons.dart';
|
||||
import 'chat_background.dart';
|
||||
|
||||
/// A user-made theme.
|
||||
///
|
||||
/// Kept deliberately small: a theme is a name plus two colors. Everything
|
||||
/// else (surfaces, text, borders, chat bubbles, light vs dark) is derived
|
||||
/// from those two, so anyone can make a readable theme in a few taps.
|
||||
/// Optionally it swaps some app icons for other icons or small pictures.
|
||||
///
|
||||
/// The share code is plain JSON, so a theme can also be written by hand:
|
||||
///
|
||||
/// ```json
|
||||
/// {"name": "Forest", "background": "#0E1A13", "accent": "#4ADE80",
|
||||
/// "icons": {"map": {"icon": "park"}}}
|
||||
/// ```
|
||||
class CustomTheme {
|
||||
static const Object _unset = Object();
|
||||
|
||||
/// Prefix for the IDs of the built-in [presets].
|
||||
static const presetIdPrefix = 'preset:';
|
||||
|
||||
final String id;
|
||||
final String name;
|
||||
|
||||
/// Main screen color. Its brightness decides whether the theme is light or
|
||||
/// dark.
|
||||
final Color background;
|
||||
|
||||
/// Buttons, switches, links, highlights and your own chat bubbles.
|
||||
final Color accent;
|
||||
|
||||
/// Replacement icons keyed by [ThemeIconSlot.key]; empty keeps defaults.
|
||||
final Map<String, ThemeIconOverride> icons;
|
||||
|
||||
/// Background for chats that have none of their own.
|
||||
final ChatBackground? chatBackground;
|
||||
|
||||
/// When true, [chatBackground] also replaces chats' own backgrounds.
|
||||
final bool chatBackgroundEverywhere;
|
||||
|
||||
const CustomTheme({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.background,
|
||||
required this.accent,
|
||||
this.icons = const {},
|
||||
this.chatBackground,
|
||||
this.chatBackgroundEverywhere = false,
|
||||
});
|
||||
|
||||
bool get isPreset => id.startsWith(presetIdPrefix);
|
||||
|
||||
/// True when [background] is dark enough to need light text.
|
||||
bool get isDark => isDarkColor(background);
|
||||
|
||||
CustomTheme copyWith({
|
||||
String? id,
|
||||
String? name,
|
||||
Color? background,
|
||||
Color? accent,
|
||||
Map<String, ThemeIconOverride>? icons,
|
||||
Object? chatBackground = _unset,
|
||||
bool? chatBackgroundEverywhere,
|
||||
}) {
|
||||
return CustomTheme(
|
||||
id: id ?? this.id,
|
||||
name: name ?? this.name,
|
||||
background: background ?? this.background,
|
||||
accent: accent ?? this.accent,
|
||||
icons: icons ?? this.icons,
|
||||
chatBackground: identical(chatBackground, _unset)
|
||||
? this.chatBackground
|
||||
: chatBackground as ChatBackground?,
|
||||
chatBackgroundEverywhere:
|
||||
chatBackgroundEverywhere ?? this.chatBackgroundEverywhere,
|
||||
);
|
||||
}
|
||||
|
||||
/// Storage form: chat background pictures are referenced by image-store ID.
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
..._contentJson(),
|
||||
if (chatBackground != null) 'chat_background': chatBackground!.toJson(),
|
||||
if (chatBackgroundEverywhere) 'chat_background_everywhere': true,
|
||||
};
|
||||
|
||||
Map<String, dynamic> _contentJson() => {
|
||||
'name': name,
|
||||
'background': colorToHex(background),
|
||||
'accent': colorToHex(accent),
|
||||
if (icons.isNotEmpty)
|
||||
'icons': icons.map((key, value) => MapEntry(key, value.toJson())),
|
||||
};
|
||||
|
||||
factory CustomTheme.fromJson(Map<String, dynamic> json) {
|
||||
return CustomTheme(
|
||||
id: json['id'] as String? ?? '',
|
||||
name: (json['name'] as String? ?? '').trim(),
|
||||
background: parseHexColor(json['background']) ?? const Color(0xFF0B1220),
|
||||
accent: parseHexColor(json['accent']) ?? const Color(0xFF0EA5E9),
|
||||
icons: parseThemeIcons(json['icons']),
|
||||
chatBackground: ChatBackground.fromJson(json['chat_background']),
|
||||
chatBackgroundEverywhere: json['chat_background_everywhere'] == true,
|
||||
);
|
||||
}
|
||||
|
||||
/// Human-readable JSON for sharing (no ID). Pictures are embedded, so a
|
||||
/// theme with pictures makes a longer code. A chat background picture is
|
||||
/// only included once its bytes are attached (see
|
||||
/// `ChatBackgroundService.embedThemeImages`).
|
||||
String toShareCode() {
|
||||
final chat = chatBackground?.toShareJson();
|
||||
return jsonEncode({
|
||||
..._contentJson(),
|
||||
'chatBackground': ?chat,
|
||||
if (chat != null && chatBackgroundEverywhere)
|
||||
'chatBackgroundEverywhere': true,
|
||||
});
|
||||
}
|
||||
|
||||
/// Parses a share code. Returns null when [code] is not a valid theme.
|
||||
/// The returned theme carries [newId].
|
||||
static CustomTheme? fromShareCode(String code, {required String newId}) {
|
||||
try {
|
||||
final decoded = jsonDecode(code.trim());
|
||||
if (decoded is! Map) return null;
|
||||
final name = decoded['name'];
|
||||
final background = parseHexColor(decoded['background']);
|
||||
final accent = parseHexColor(decoded['accent']);
|
||||
if (name is! String || name.trim().isEmpty) return null;
|
||||
if (background == null || accent == null) return null;
|
||||
return CustomTheme(
|
||||
id: newId,
|
||||
name: name.trim(),
|
||||
background: background,
|
||||
accent: accent,
|
||||
icons: parseThemeIcons(decoded['icons']),
|
||||
// Only colors and inline pictures: an image-store ID from another
|
||||
// device would point at nothing here.
|
||||
chatBackground: _sharedChatBackground(decoded['chatBackground']),
|
||||
chatBackgroundEverywhere: decoded['chatBackgroundEverywhere'] == true,
|
||||
);
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is CustomTheme &&
|
||||
other.id == id &&
|
||||
other.name == name &&
|
||||
other.background == background &&
|
||||
other.accent == accent &&
|
||||
mapEquals(other.icons, icons) &&
|
||||
other.chatBackground == chatBackground &&
|
||||
other.chatBackgroundEverywhere == chatBackgroundEverywhere;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(
|
||||
id,
|
||||
name,
|
||||
background,
|
||||
accent,
|
||||
Object.hashAllUnordered(
|
||||
icons.entries.map((e) => Object.hash(e.key, e.value)),
|
||||
),
|
||||
chatBackground,
|
||||
chatBackgroundEverywhere,
|
||||
);
|
||||
|
||||
static ChatBackground? _sharedChatBackground(Object? json) {
|
||||
final background = ChatBackground.fromJson(json);
|
||||
if (background == null || background.imageId != null) return null;
|
||||
return background;
|
||||
}
|
||||
|
||||
/// Ready-made themes. They also serve as starting points for new ones.
|
||||
static const List<CustomTheme> presets = [
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}forest',
|
||||
name: 'Forest',
|
||||
background: Color(0xFF0E1A13),
|
||||
accent: Color(0xFF4ADE80),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}ocean',
|
||||
name: 'Ocean',
|
||||
background: Color(0xFF071A2B),
|
||||
accent: Color(0xFF22D3EE),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}ember',
|
||||
name: 'Ember',
|
||||
background: Color(0xFF1C1210),
|
||||
accent: Color(0xFFFB923C),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}grape',
|
||||
name: 'Grape',
|
||||
background: Color(0xFF17112A),
|
||||
accent: Color(0xFFC084FC),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}high_contrast',
|
||||
name: 'High Contrast',
|
||||
background: Color(0xFF000000),
|
||||
accent: Color(0xFFFACC15),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}paper',
|
||||
name: 'Paper',
|
||||
background: Color(0xFFFAF7F0),
|
||||
accent: Color(0xFF9A3412),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}mint',
|
||||
name: 'Mint',
|
||||
background: Color(0xFFEFFAF4),
|
||||
accent: Color(0xFF047857),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}sky',
|
||||
name: 'Sky',
|
||||
background: Color(0xFFF0F6FC),
|
||||
accent: Color(0xFF1D4ED8),
|
||||
),
|
||||
];
|
||||
|
||||
/// Holiday themes: colors, a chat background and festive icons, all
|
||||
/// bundled under `assets/themes/<holiday>/`.
|
||||
static const List<CustomTheme> holidayPresets = [
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}halloween',
|
||||
name: 'Halloween',
|
||||
background: Color(0xFF160F22),
|
||||
accent: Color(0xFFFF7A1A),
|
||||
icons: _HolidayIcons.halloween,
|
||||
chatBackground: ChatBackground.asset(
|
||||
'assets/themes/halloween/background.jxl',
|
||||
fade: 0.35,
|
||||
),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}thanksgiving',
|
||||
name: 'Thanksgiving',
|
||||
background: Color(0xFF1C120B),
|
||||
accent: Color(0xFFF2A93B),
|
||||
icons: _HolidayIcons.thanksgiving,
|
||||
chatBackground: ChatBackground.asset(
|
||||
'assets/themes/thanksgiving/background.jxl',
|
||||
fade: 0.3,
|
||||
),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}christmas',
|
||||
name: 'Christmas',
|
||||
background: Color(0xFF0B1F14),
|
||||
accent: Color(0xFFE5484D),
|
||||
icons: _HolidayIcons.christmas,
|
||||
chatBackground: ChatBackground.asset(
|
||||
'assets/themes/christmas/background.jxl',
|
||||
fade: 0.35,
|
||||
),
|
||||
),
|
||||
CustomTheme(
|
||||
id: '${presetIdPrefix}new_year',
|
||||
name: "New Year's",
|
||||
background: Color(0xFF0A1024),
|
||||
accent: Color(0xFFF5C542),
|
||||
icons: _HolidayIcons.newYear,
|
||||
chatBackground: ChatBackground.asset(
|
||||
'assets/themes/new_year/background.jxl',
|
||||
fade: 0.35,
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
static CustomTheme? presetById(String id) {
|
||||
for (final preset in [...presets, ...holidayPresets]) {
|
||||
if (preset.id == id) return preset;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// `#RRGGBB` (alpha dropped — themes are always opaque).
|
||||
String colorToHex(Color color) {
|
||||
final rgb = color.toARGB32() & 0xFFFFFF;
|
||||
return '#${rgb.toRadixString(16).padLeft(6, '0').toUpperCase()}';
|
||||
}
|
||||
|
||||
/// Accepts `#RRGGBB`, `RRGGBB`, `#RGB` or `RGB`. Returns an opaque color, or
|
||||
/// null when [value] is not a hex color.
|
||||
Color? parseHexColor(Object? value) {
|
||||
if (value is! String) return null;
|
||||
var hex = value.trim();
|
||||
if (hex.startsWith('#')) hex = hex.substring(1);
|
||||
if (hex.length == 3) {
|
||||
hex = hex.split('').map((c) => '$c$c').join();
|
||||
}
|
||||
if (hex.length != 6) return null;
|
||||
final rgb = int.tryParse(hex, radix: 16);
|
||||
if (rgb == null) return null;
|
||||
return Color(0xFF000000 | rgb);
|
||||
}
|
||||
|
||||
/// Same threshold Flutter's `ThemeData.estimateBrightnessForColor` uses, so
|
||||
/// "dark" here agrees with Material's text-color choices.
|
||||
bool isDarkColor(Color color) {
|
||||
final luminance = color.computeLuminance();
|
||||
const kThreshold = 0.15;
|
||||
return (luminance + 0.05) * (luminance + 0.05) <= kThreshold;
|
||||
}
|
||||
|
||||
/// Icon sets for [CustomTheme.holidayPresets]: the three tabs and Send.
|
||||
abstract final class _HolidayIcons {
|
||||
static const halloween = {
|
||||
'contacts': ThemeIconOverride.asset('assets/themes/halloween/contacts.jxl'),
|
||||
'channels': ThemeIconOverride.asset('assets/themes/halloween/channels.jxl'),
|
||||
'map': ThemeIconOverride.asset('assets/themes/halloween/map.jxl'),
|
||||
'send': ThemeIconOverride.asset('assets/themes/halloween/send.jxl'),
|
||||
'map_chat': ThemeIconOverride.asset('assets/themes/halloween/map_chat.jxl'),
|
||||
'map_repeater': ThemeIconOverride.asset(
|
||||
'assets/themes/halloween/map_repeater.jxl',
|
||||
),
|
||||
'map_room': ThemeIconOverride.asset('assets/themes/halloween/map_room.jxl'),
|
||||
'map_sensor': ThemeIconOverride.asset(
|
||||
'assets/themes/halloween/map_sensor.jxl',
|
||||
),
|
||||
'map_self': ThemeIconOverride.asset('assets/themes/halloween/map_self.jxl'),
|
||||
};
|
||||
static const thanksgiving = {
|
||||
'contacts': ThemeIconOverride.asset(
|
||||
'assets/themes/thanksgiving/contacts.jxl',
|
||||
),
|
||||
'channels': ThemeIconOverride.asset(
|
||||
'assets/themes/thanksgiving/channels.jxl',
|
||||
),
|
||||
'map': ThemeIconOverride.asset('assets/themes/thanksgiving/map.jxl'),
|
||||
'send': ThemeIconOverride.asset('assets/themes/thanksgiving/send.jxl'),
|
||||
'map_chat': ThemeIconOverride.asset(
|
||||
'assets/themes/thanksgiving/map_chat.jxl',
|
||||
),
|
||||
'map_repeater': ThemeIconOverride.asset(
|
||||
'assets/themes/thanksgiving/map_repeater.jxl',
|
||||
),
|
||||
'map_room': ThemeIconOverride.asset(
|
||||
'assets/themes/thanksgiving/map_room.jxl',
|
||||
),
|
||||
'map_sensor': ThemeIconOverride.asset(
|
||||
'assets/themes/thanksgiving/map_sensor.jxl',
|
||||
),
|
||||
'map_self': ThemeIconOverride.asset(
|
||||
'assets/themes/thanksgiving/map_self.jxl',
|
||||
),
|
||||
};
|
||||
static const christmas = {
|
||||
'contacts': ThemeIconOverride.asset('assets/themes/christmas/contacts.jxl'),
|
||||
'channels': ThemeIconOverride.asset('assets/themes/christmas/channels.jxl'),
|
||||
'map': ThemeIconOverride.asset('assets/themes/christmas/map.jxl'),
|
||||
'send': ThemeIconOverride.asset('assets/themes/christmas/send.jxl'),
|
||||
'map_chat': ThemeIconOverride.asset('assets/themes/christmas/map_chat.jxl'),
|
||||
'map_repeater': ThemeIconOverride.asset(
|
||||
'assets/themes/christmas/map_repeater.jxl',
|
||||
),
|
||||
'map_room': ThemeIconOverride.asset('assets/themes/christmas/map_room.jxl'),
|
||||
'map_sensor': ThemeIconOverride.asset(
|
||||
'assets/themes/christmas/map_sensor.jxl',
|
||||
),
|
||||
'map_self': ThemeIconOverride.asset('assets/themes/christmas/map_self.jxl'),
|
||||
};
|
||||
static const newYear = {
|
||||
'contacts': ThemeIconOverride.asset('assets/themes/new_year/contacts.jxl'),
|
||||
'channels': ThemeIconOverride.asset('assets/themes/new_year/channels.jxl'),
|
||||
'map': ThemeIconOverride.asset('assets/themes/new_year/map.jxl'),
|
||||
'send': ThemeIconOverride.asset('assets/themes/new_year/send.jxl'),
|
||||
'map_chat': ThemeIconOverride.asset('assets/themes/new_year/map_chat.jxl'),
|
||||
'map_repeater': ThemeIconOverride.asset(
|
||||
'assets/themes/new_year/map_repeater.jxl',
|
||||
),
|
||||
'map_room': ThemeIconOverride.asset('assets/themes/new_year/map_room.jxl'),
|
||||
'map_sensor': ThemeIconOverride.asset(
|
||||
'assets/themes/new_year/map_sensor.jxl',
|
||||
),
|
||||
'map_self': ThemeIconOverride.asset('assets/themes/new_year/map_self.jxl'),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import 'custom_theme.dart';
|
||||
|
||||
/// How many days before a holiday its theme starts being applied
|
||||
/// automatically. The holiday day itself is distance 0.
|
||||
const int holidayThemeLeadDays = 17;
|
||||
|
||||
/// The holidays that have an automatic theme, in calendar order.
|
||||
enum ThemeHoliday {
|
||||
halloween('${CustomTheme.presetIdPrefix}halloween'),
|
||||
thanksgiving('${CustomTheme.presetIdPrefix}thanksgiving'),
|
||||
christmas('${CustomTheme.presetIdPrefix}christmas'),
|
||||
newYear('${CustomTheme.presetIdPrefix}new_year');
|
||||
|
||||
const ThemeHoliday(this.themeId);
|
||||
|
||||
/// ID of the matching preset in [CustomTheme.holidayPresets].
|
||||
final String themeId;
|
||||
|
||||
/// The holiday's date in [year]: Halloween Oct 31, US Thanksgiving (the
|
||||
/// fourth Thursday of November), Christmas Dec 25, New Year's Day Jan 1.
|
||||
DateTime dateIn(int year) {
|
||||
switch (this) {
|
||||
case ThemeHoliday.halloween:
|
||||
return DateTime(year, 10, 31);
|
||||
case ThemeHoliday.thanksgiving:
|
||||
final nov1 = DateTime(year, 11, 1);
|
||||
final toThursday = (DateTime.thursday - nov1.weekday + 7) % 7;
|
||||
return DateTime(year, 11, 1 + toThursday + 21);
|
||||
case ThemeHoliday.christmas:
|
||||
return DateTime(year, 12, 25);
|
||||
case ThemeHoliday.newYear:
|
||||
return DateTime(year, 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One occurrence of a [ThemeHoliday] that is currently within its window.
|
||||
class ActiveHoliday {
|
||||
final ThemeHoliday holiday;
|
||||
|
||||
/// Local date (midnight) of this occurrence of the holiday.
|
||||
final DateTime date;
|
||||
|
||||
/// Whole days from today until [date]; 0 on the holiday itself.
|
||||
final int daysAway;
|
||||
|
||||
const ActiveHoliday(this.holiday, this.date, this.daysAway);
|
||||
|
||||
String get themeId => holiday.themeId;
|
||||
|
||||
/// Identifies this occurrence, e.g. `preset:christmas@2026-12-25`, so a
|
||||
/// dismissal applies to one year's holiday only.
|
||||
String get occurrenceKey {
|
||||
String two(int v) => v.toString().padLeft(2, '0');
|
||||
return '$themeId@${date.year}-${two(date.month)}-${two(date.day)}';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is ActiveHoliday &&
|
||||
other.holiday == holiday &&
|
||||
other.date == date &&
|
||||
other.daysAway == daysAway;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(holiday, date, daysAway);
|
||||
|
||||
@override
|
||||
String toString() => 'ActiveHoliday($occurrenceKey, $daysAway days away)';
|
||||
}
|
||||
|
||||
/// The holiday whose theme should be showing on [today] (local date; the
|
||||
/// time of day is ignored), or null when none is close enough.
|
||||
///
|
||||
/// For each holiday the next occurrence on or after today is taken (today
|
||||
/// counts, and the year wraps, so on Dec 20 New Year's is the coming Jan 1).
|
||||
/// Holidays 0..[holidayThemeLeadDays] days away are candidates and the
|
||||
/// nearest one wins. That means a holiday keeps its theme up to and including
|
||||
/// its own day even if a later holiday's window has opened (Thanksgiving
|
||||
/// before Christmas, Christmas before New Year's), and the next holiday takes
|
||||
/// over the day after. Once a holiday has passed and no other is within the
|
||||
/// window, null is returned and the user's own theme applies again.
|
||||
ActiveHoliday? activeHolidayFor(DateTime today) {
|
||||
// Day arithmetic in UTC so daylight-saving shifts never skew the count.
|
||||
final day = DateTime.utc(today.year, today.month, today.day);
|
||||
ActiveHoliday? best;
|
||||
for (final holiday in ThemeHoliday.values) {
|
||||
var date = holiday.dateIn(today.year);
|
||||
var target = DateTime.utc(date.year, date.month, date.day);
|
||||
if (target.isBefore(day)) {
|
||||
date = holiday.dateIn(today.year + 1);
|
||||
target = DateTime.utc(date.year, date.month, date.day);
|
||||
}
|
||||
final daysAway = target.difference(day).inDays;
|
||||
if (daysAway > holidayThemeLeadDays) continue;
|
||||
if (best == null || daysAway < best.daysAway) {
|
||||
best = ActiveHoliday(holiday, date, daysAway);
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
Reference in new issue
Block a user