From 9c3b61ce57c27f7a6f50586fd0f4f3fa5f89fba2 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 4 Jan 2025 21:49:48 +0800 Subject: [PATCH] :sparkles: Lunar calendar festivals --- assets/translations/en-US.json | 6 ++++ assets/translations/zh-CN.json | 6 ++++ assets/translations/zh-HK.json | 10 +++++++ assets/translations/zh-TW.json | 10 +++++++ lib/providers/special_day.dart | 50 +++++++++++++++++++++++++++++++++- lib/screens/home.dart | 2 +- 6 files changed, 82 insertions(+), 2 deletions(-) diff --git a/assets/translations/en-US.json b/assets/translations/en-US.json index fd36369..6b6cbfc 100644 --- a/assets/translations/en-US.json +++ b/assets/translations/en-US.json @@ -414,6 +414,9 @@ "celebrateBirthday": "Happy birthday, {}!", "celebrateMerryXmas": "Merry christmas, {}!", "celebrateNewYear": "Happy new year, {}!", + "celebrateLunarNewYear": "Happy lunar new year, {}!", + "celebrateMidAutumn": "Happy mid-autumn festival, {}!", + "celebrateDragonBoat": "Happy dragon boat festival, {}!", "celebrateValentineDay": "Today is valentine's day, {}!", "celebrateLaborDay": "Today is labor day, {}.", "celebrateMotherDay": "Today is mother's day, {}.", @@ -423,6 +426,9 @@ "celebrateThanksgiving": "Today is thanksgiving day, {}!", "pendingBirthday": "Birthday in {}", "pendingMerryXmas": "Christmas in {}", + "pendingLunarNewYear": "Lunar new year in {}", + "pendingMidAutumn": "Mid-autumn festival in {}", + "pendingDragonBoat": "Dragon boat festival in {}", "pendingNewYear": "New year in {}", "pendingValentineDay": "Valentine's day in {}", "pendingLaborDay": "Labor day in {}", diff --git a/assets/translations/zh-CN.json b/assets/translations/zh-CN.json index e6e3207..b4868b8 100644 --- a/assets/translations/zh-CN.json +++ b/assets/translations/zh-CN.json @@ -410,6 +410,9 @@ "dailyCheckNegativeHint6": "出门", "dailyCheckNegativeHint6Description": "忘带伞遇上大雨", "celebrateBirthday": "生日快乐,{}!", + "celebrateLunarNewYear": "春节快乐,{}!", + "celebrateMidAutumn": "中秋节快乐,{}!", + "celebrateDragonBoat": "端午节快乐,{}!", "celebrateMerryXmas": "圣诞快乐,{}!", "celebrateNewYear": "新年快乐,{}!", "celebrateValentineDay": "今天是情人节,{}!", @@ -419,6 +422,9 @@ "celebrateFatherDay": "今天是父亲节,{}。", "celebrateHalloween": "快乐在圣诞节,{}!", "celebrateThanksgiving": "今天是感恩节,{}!", + "pendingLunarNewYear": "{} 过春节", + "pendingMidAutumn": "{} 过中秋节", + "pendingDragonBoat": "{} 过端午节", "pendingBirthday": "{} 过生日", "pendingMerryXmas": "{} 过圣诞节", "pendingNewYear": "{} 跨年", diff --git a/assets/translations/zh-HK.json b/assets/translations/zh-HK.json index cc61e04..ce8c787 100644 --- a/assets/translations/zh-HK.json +++ b/assets/translations/zh-HK.json @@ -279,6 +279,10 @@ "one": "{} 個附件", "other": "{} 個附件" }, + "messageTyping": { + "one": "{} 正在輸入", + "other": "{} 正在輸入" + }, "fieldAttachmentRandomId": "訪問 ID", "fieldAttachmentAlt": "概述文字", "addAttachmentFromAlbum": "從相冊中添加附件", @@ -406,6 +410,9 @@ "dailyCheckNegativeHint6": "出門", "dailyCheckNegativeHint6Description": "忘帶傘遇上大雨", "celebrateBirthday": "生日快樂,{}!", + "celebrateLunarNewYear": "春節快樂,{}!", + "celebrateMidAutumn": "中秋節快樂,{}!", + "celebrateDragonBoat": "端午節快樂,{}!", "celebrateMerryXmas": "聖誕快樂,{}!", "celebrateNewYear": "新年快樂,{}!", "celebrateValentineDay": "今天是情人節,{}!", @@ -415,6 +422,9 @@ "celebrateFatherDay": "今天是父親節,{}。", "celebrateHalloween": "快樂在聖誕節,{}!", "celebrateThanksgiving": "今天是感恩節,{}!", + "pendingLunarNewYear": "{} 過春節", + "pendingMidAutumn": "{} 過中秋節", + "pendingDragonBoat": "{} 過端午節", "pendingBirthday": "{} 過生日", "pendingMerryXmas": "{} 過聖誕節", "pendingNewYear": "{} 跨年", diff --git a/assets/translations/zh-TW.json b/assets/translations/zh-TW.json index 1f7d2af..5ab094a 100644 --- a/assets/translations/zh-TW.json +++ b/assets/translations/zh-TW.json @@ -279,6 +279,10 @@ "one": "{} 個附件", "other": "{} 個附件" }, + "messageTyping": { + "one": "{} 正在輸入", + "other": "{} 正在輸入" + }, "fieldAttachmentRandomId": "訪問 ID", "fieldAttachmentAlt": "概述文字", "addAttachmentFromAlbum": "從相冊中添加附件", @@ -406,6 +410,9 @@ "dailyCheckNegativeHint6": "出門", "dailyCheckNegativeHint6Description": "忘帶傘遇上大雨", "celebrateBirthday": "生日快樂,{}!", + "celebrateLunarNewYear": "春節快樂,{}!", + "celebrateMidAutumn": "中秋節快樂,{}!", + "celebrateDragonBoat": "端午節快樂,{}!", "celebrateMerryXmas": "聖誕快樂,{}!", "celebrateNewYear": "新年快樂,{}!", "celebrateValentineDay": "今天是情人節,{}!", @@ -415,6 +422,9 @@ "celebrateFatherDay": "今天是父親節,{}。", "celebrateHalloween": "快樂在聖誕節,{}!", "celebrateThanksgiving": "今天是感恩節,{}!", + "pendingLunarNewYear": "{} 過春節", + "pendingMidAutumn": "{} 過中秋節", + "pendingDragonBoat": "{} 過端午節", "pendingBirthday": "{} 過生日", "pendingMerryXmas": "{} 過聖誕節", "pendingNewYear": "{} 跨年", diff --git a/lib/providers/special_day.dart b/lib/providers/special_day.dart index 310fdc6..53b4170 100644 --- a/lib/providers/special_day.dart +++ b/lib/providers/special_day.dart @@ -3,9 +3,12 @@ import 'package:provider/provider.dart'; import 'package:surface/providers/userinfo.dart'; // Stored as key: month, day -const Map kSpecialDays = { +final Map kSpecialDays = { // Birthday is dynamically generated according to the user's profile 'NewYear': (1, 1), + 'LunarNewYear': (lunarToGregorian(null, 1, 1).month, lunarToGregorian(null, 1, 1).day), + 'MidAutumn': (lunarToGregorian(null, 8, 15).month, lunarToGregorian(null, 8, 15).day), + 'DragonBoat': (lunarToGregorian(null, 5, 5).month, lunarToGregorian(null, 5, 5).day), 'ValentineDay': (2, 14), 'LaborDay': (5, 1), 'MotherDay': (5, 11), @@ -19,6 +22,9 @@ const Map kSpecialDays = { const Map kSpecialDaysSymbol = { 'Birthday': '🎂', 'NewYear': '🎉', + 'LunarNewYear': '🎉', + 'MidAutumn': '🥮', + 'DragonBoat': '🐲', 'MerryXmas': '🎄', 'ValentineDay': '💑', 'LaborDay': '🏋️', @@ -134,3 +140,45 @@ class SpecialDayProvider { return (elapsedDuration / totalDuration).clamp(0.0, 1.0); } } + +final Map lunarYearData = { + 2025: LunarYear( + startDate: DateTime(2025, 1, 29), + months: [29, 30, 30, 29, 30, 29, 29, 30, 30, 29, 30, 29], + leapMonth: 0, + ), +}; + +class LunarYear { + final DateTime startDate; + final List months; + final int leapMonth; + + LunarYear({required this.startDate, required this.months, required this.leapMonth}); +} + +DateTime lunarToGregorian(int? year, int month, int day, {bool isLeapMonth = false}) { + year = year ?? DateTime.now().year; + final lunarYear = lunarYearData[year]; + if (lunarYear == null) { + throw Exception('Lunar data for year $year not found'); + } + + int leapMonth = lunarYear.leapMonth; + if (isLeapMonth && (leapMonth == 0 || leapMonth != month)) { + throw Exception('Invalid leap month for year $year'); + } + + int daysFromStart = 0; + for (int i = 0; i < month - 1; i++) { + daysFromStart += lunarYear.months[i]; + } + + if (isLeapMonth) { + daysFromStart += lunarYear.months[month - 1]; + } + + daysFromStart += day - 1; + + return lunarYear.startDate.add(Duration(days: daysFromStart)); +} diff --git a/lib/screens/home.dart b/lib/screens/home.dart index 38e6cfb..b015a8f 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -197,7 +197,7 @@ class _HomeDashSpecialDayWidgetState extends State<_HomeDashSpecialDayWidget> { return Card( child: ListTile( leading: Text(kSpecialDaysSymbol[name] ?? '🎉').fontSize(24), - title: Text('pending$name').tr(args: [RelativeTime(context).format(date)]), + title: Text('pending$name').tr(args: [RelativeTime(context).format(date).replaceFirst('in', '').trim()]), subtitle: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [