102 lines
4.3 KiB
Dart
102 lines
4.3 KiB
Dart
import 'package:flutter/material.dart';
|
||
|
||
/// A holiday look (FigCraft Designs/holiday-themes/v3, themes.json): while a
|
||
/// holiday's window is on, the app shows its background, its target (locked)
|
||
/// and its hit colour (until the user picks another one). Afterwards everything
|
||
/// is back to the user's own settings. Saved targets are never changed.
|
||
class HolidayTheme {
|
||
final String id; // "01" … "10"; "00" is the everyday look
|
||
final String name; // Slovenian name, also shown in the app
|
||
final int targetId; // TargetStyle id (70–79)
|
||
final Color color; // hit colour, the same in dark and light
|
||
// Window: month-day range (inclusive, may cross the new year) …
|
||
final (int, int)? from;
|
||
final (int, int)? to;
|
||
// … or days relative to Easter Sunday (inclusive)
|
||
final (int, int)? easter;
|
||
|
||
const HolidayTheme(this.id, this.name, this.targetId, this.color, {this.from, this.to, this.easter});
|
||
|
||
String background(bool dark) => _background(id, dark);
|
||
|
||
/// The background when no holiday is on ("00 Privzeto").
|
||
static String everydayBackground(bool dark) => _background('00', dark);
|
||
|
||
static String _background(String id, bool dark) => 'assets/backgrounds/bg${id}_${dark ? 'dark' : 'light'}.webp';
|
||
|
||
/// In the order of themes.json: when windows overlap, the first one wins.
|
||
static const List<HolidayTheme> all = [
|
||
HolidayTheme('01', 'Božič', 70, Color(0xFFF2C12E), from: (12, 1), to: (12, 26)),
|
||
HolidayTheme('02', 'Novo leto', 71, Color(0xFF2EC7FF), from: (12, 27), to: (1, 2)),
|
||
HolidayTheme('03', 'Valentinovo', 73, Color(0xFFFFD27A), from: (2, 12), to: (2, 15)),
|
||
HolidayTheme('04', 'Pust', 75, Color(0xFFFF2F6D), easter: (-52, -46)),
|
||
HolidayTheme('05', 'Velika noč', 72, Color(0xFFFF8A1A), easter: (-7, 1)),
|
||
HolidayTheme('06', 'Praznik dela', 77, Color(0xFF2F8CFF), from: (4, 30), to: (5, 2)),
|
||
HolidayTheme('07', 'Kresna noč', 78, Color(0xFF7FE0FF), from: (6, 21), to: (6, 23)),
|
||
HolidayTheme('08', 'Dan državnosti', 79, Color(0xFFF2C12E), from: (6, 24), to: (6, 26)),
|
||
HolidayTheme('09', 'Noč čarovnic', 74, Color(0xFF7CFF6B), from: (10, 25), to: (11, 1)),
|
||
HolidayTheme('10', 'Martinovo', 76, Color(0xFF2EC7FF), from: (11, 8), to: (11, 11)),
|
||
];
|
||
|
||
/// The holiday on [day], or null on an ordinary day.
|
||
static HolidayTheme? on(DateTime day) {
|
||
final d = DateTime(day.year, day.month, day.day);
|
||
for (final h in all) {
|
||
if (h.covers(d)) return h;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
bool covers(DateTime d) {
|
||
final e = easter;
|
||
if (e != null) {
|
||
final first = easterDay(d.year, e.$1);
|
||
final last = easterDay(d.year, e.$2);
|
||
return !d.isBefore(first) && !d.isAfter(last);
|
||
}
|
||
final md = d.month * 100 + d.day;
|
||
final a = from!.$1 * 100 + from!.$2;
|
||
final b = to!.$1 * 100 + to!.$2;
|
||
return a <= b ? md >= a && md <= b : md >= a || md <= b; // crosses the new year
|
||
}
|
||
|
||
/// First and last day of this holiday's window around [today] (for "until …").
|
||
(DateTime, DateTime) window(DateTime today) {
|
||
final e = easter;
|
||
if (e != null) {
|
||
return (easterDay(today.year, e.$1), easterDay(today.year, e.$2));
|
||
}
|
||
final crosses = from!.$1 * 100 + from!.$2 > to!.$1 * 100 + to!.$2;
|
||
final startYear = crosses && today.month <= to!.$1 ? today.year - 1 : today.year;
|
||
final start = DateTime(startYear, from!.$1, from!.$2);
|
||
final end = DateTime(crosses ? startYear + 1 : startYear, to!.$1, to!.$2);
|
||
return (start, end);
|
||
}
|
||
|
||
/// The day [offset] days from Easter Sunday. Calendar days, not 24-hour
|
||
/// steps: those would land on the evening before across the March clock change.
|
||
static DateTime easterDay(int year, int offset) {
|
||
final e = easterSunday(year);
|
||
return DateTime(e.year, e.month, e.day + offset);
|
||
}
|
||
|
||
/// Easter Sunday (anonymous Gregorian algorithm).
|
||
static DateTime easterSunday(int year) {
|
||
final a = year % 19;
|
||
final b = year ~/ 100;
|
||
final c = year % 100;
|
||
final d = b ~/ 4;
|
||
final e = b % 4;
|
||
final f = (b + 8) ~/ 25;
|
||
final g = (b - f + 1) ~/ 3;
|
||
final h = (19 * a + b - d - g + 15) % 30;
|
||
final i = c ~/ 4;
|
||
final k = c % 4;
|
||
final l = (32 + 2 * e + 2 * i - h - k) % 7;
|
||
final m = (a + 11 * h + 22 * l) ~/ 451;
|
||
final month = (h + l - 7 * m + 114) ~/ 31;
|
||
final day = (h + l - 7 * m + 114) % 31 + 1;
|
||
return DateTime(year, month, day);
|
||
}
|
||
}
|