From 534da46a268243dfca42bfec3f00e26b2415a84e Mon Sep 17 00:00:00 2001 From: Vyn Date: Tue, 8 Oct 2024 17:05:52 +0200 Subject: [PATCH] Add hint to show how many days left relative to today --- .../mirai-core/include/mirai-core/DateTime.h | 9 +++++++ src/UiState.cpp | 26 ++++++++++++------- ui/Backend.slint | 3 ++- ui/MainView.slint | 8 ++++-- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/external/mirai-core/include/mirai-core/DateTime.h b/external/mirai-core/include/mirai-core/DateTime.h index c1125ba..c488f63 100644 --- a/external/mirai-core/include/mirai-core/DateTime.h +++ b/external/mirai-core/include/mirai-core/DateTime.h @@ -25,6 +25,15 @@ struct Date { bool operator<(const Date &other) const; bool operator>(const Date &other) const; + std::chrono::year_month_day toStdChrono() const + { + return std::chrono::year_month_day{ + std::chrono::year(year), + std::chrono::month(month), + std::chrono::day(day), + }; + } + int year; unsigned month; unsigned day; diff --git a/src/UiState.cpp b/src/UiState.cpp index 58abc78..d4c0052 100644 --- a/src/UiState.cpp +++ b/src/UiState.cpp @@ -223,7 +223,7 @@ void UiState::setupUtilsCallbacks() std::chrono::month(date.month), std::chrono::day(date.day), }; - return std::format("{:%d %B}", chronoDate); + return std::format("{:%B %d}", chronoDate); }); } @@ -361,15 +361,21 @@ void UiState::reloadTasks() auto ¤tDay = view_[dayIndex]; auto slintEvents = std::make_shared>(); auto slintDayTasks = std::make_shared>(); - slintDays->push_back(Day{ - .sourceId = currentDay.day->source()->id(), - .id = dayIndex, - .date = MiraiDateToSlintDate(currentDay.day->getDate()), - .events = slintEvents, - .tasks = slintDayTasks, - .isLate = currentDay.day->getDate() < todayDate, - .isToday = currentDay.day->getDate() == todayDate, - }); + auto relativeDaysDiff = std::chrono::duration_cast( + std::chrono::sys_days(currentDay.day->getDate().toStdChrono()) - + std::chrono::sys_days(todayDate.toStdChrono()) + ) + .count(); + slintDays->push_back( + Day{.sourceId = currentDay.day->source()->id(), + .id = dayIndex, + .date = MiraiDateToSlintDate(currentDay.day->getDate()), + .events = slintEvents, + .tasks = slintDayTasks, + .isLate = currentDay.day->getDate() < todayDate, + .isToday = currentDay.day->getDate() == todayDate, + .relativeDaysDiff = static_cast(relativeDaysDiff)} + ); for (int taskIndex = 0; taskIndex < currentDay.filteredTasks.size(); ++taskIndex) { auto &task = currentDay.filteredTasks.at(taskIndex); std::vector tags; diff --git a/ui/Backend.slint b/ui/Backend.slint index e5029d0..bb9513f 100644 --- a/ui/Backend.slint +++ b/ui/Backend.slint @@ -26,7 +26,8 @@ export struct Day { events: [Event], tasks: [TaskData], isLate: bool, - isToday: bool + isToday: bool, + relativeDaysDiff: int } struct OpenNewTaskFormParams { diff --git a/ui/MainView.slint b/ui/MainView.slint index e0cbb52..9f7691a 100644 --- a/ui/MainView.slint +++ b/ui/MainView.slint @@ -80,10 +80,14 @@ export component MainView inherits Rectangle { color: day.isLate ? Palette.orange : Palette.foreground; font-size: 1.2rem; } - if day.isToday : VerticalLayout { + VerticalLayout { alignment: center; VText { - text: " - Today"; + text: day.relativeDaysDiff == 0 ? " - today" : + day.relativeDaysDiff == 1 ? " - tomorrow" : + day.relativeDaysDiff == -1 ? " - yesterday" : + day.relativeDaysDiff > 0 ? " - in \{day.relativeDaysDiff} days" : + " - \{-day.relativeDaysDiff} days ago"; color: Palette.foreground-hint; font-size: 1rem; }