diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index c29a210182..48c5590ade 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2477,6 +2477,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_mediaview_doc_image" = "File"; "lng_mediaview_today" = "today at {time}"; "lng_mediaview_yesterday" = "yesterday at {time}"; +"lng_mediaview_just_now" = "just now"; +"lng_mediaview_minutes_ago#one" = "{count} minute ago"; +"lng_mediaview_minutes_ago#other" = "{count} minutes ago"; +"lng_mediaview_hours_ago#one" = "{count} hour ago"; +"lng_mediaview_hours_ago#other" = "{count} hours ago"; "lng_mediaview_date_time" = "{date} at {time}"; "lng_mediaview_set_userpic" = "Set as Main"; "lng_mediaview_report_profile_photo" = "Report"; diff --git a/Telegram/SourceFiles/media/stories/media_stories_header.cpp b/Telegram/SourceFiles/media/stories/media_stories_header.cpp index fca65c8cab..3c089c9bd8 100644 --- a/Telegram/SourceFiles/media/stories/media_stories_header.cpp +++ b/Telegram/SourceFiles/media/stories/media_stories_header.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/unixtime.h" #include "data/data_user.h" #include "media/stories/media_stories_controller.h" +#include "lang/lang_keys.h" #include "ui/controls/userpic_button.h" #include "ui/text/format_values.h" #include "ui/widgets/labels.h" @@ -23,10 +24,60 @@ namespace { constexpr auto kNameOpacity = 1.; constexpr auto kDateOpacity = 0.8; +struct Timestamp { + QString text; + TimeId changes = 0; +}; + +[[nodiscard]] Timestamp ComposeTimestamp(TimeId when, TimeId now) { + const auto minutes = (now - when) / 60; + if (!minutes) { + return { tr::lng_mediaview_just_now(tr::now), 61 - (now - when) }; + } else if (minutes < 60) { + return { + tr::lng_mediaview_minutes_ago(tr::now, lt_count, minutes), + 61 - ((now - when) % 60), + }; + } + const auto hours = (now - when) / 3600; + if (hours < 12) { + return { + tr::lng_mediaview_hours_ago(tr::now, lt_count, hours), + 3601 - ((now - when) % 3600), + }; + } + const auto whenFull = base::unixtime::parse(when); + const auto nowFull = base::unixtime::parse(now); + const auto locale = QLocale(); + auto tomorrow = nowFull; + tomorrow.setDate(nowFull.date().addDays(1)); + tomorrow.setTime(QTime(0, 0, 1)); + const auto seconds = int(nowFull.secsTo(tomorrow)); + if (whenFull.date() == nowFull.date()) { + const auto whenTime = locale.toString( + whenFull.time(), + QLocale::ShortFormat); + return { + tr::lng_mediaview_today(tr::now, lt_time, whenTime), + seconds, + }; + } else if (whenFull.date().addDays(1) == nowFull.date()) { + const auto whenTime = locale.toString( + whenFull.time(), + QLocale::ShortFormat); + return { + tr::lng_mediaview_yesterday(tr::now, lt_time, whenTime), + seconds, + }; + } + return { Ui::FormatDateTime(whenFull) }; +} + } // namespace Header::Header(not_null controller) -: _controller(controller) { +: _controller(controller) +, _dateUpdateTimer([=] { updateDateText(); }) { } Header::~Header() { @@ -66,13 +117,29 @@ void Header::show(HeaderData data) { raw->setGeometry(layout.header); }, raw->lifetime()); } + auto timestamp = ComposeTimestamp(data.date, base::unixtime::now()); _date = std::make_unique( _widget.get(), - Ui::FormatDateTime(base::unixtime::parse(data.date)), + std::move(timestamp.text), st::storiesHeaderDate); _date->setOpacity(kDateOpacity); _date->show(); _date->move(st::storiesHeaderDatePosition); + + if (timestamp.changes > 0) { + _dateUpdateTimer.callOnce(timestamp.changes * crl::time(1000)); + } +} + +void Header::updateDateText() { + if (!_date || !_data || !_data->date) { + return; + } + auto timestamp = ComposeTimestamp(_data->date, base::unixtime::now()); + _date->setText(timestamp.text); + if (timestamp.changes > 0) { + _dateUpdateTimer.callOnce(timestamp.changes * crl::time(1000)); + } } } // namespace Media::Stories diff --git a/Telegram/SourceFiles/media/stories/media_stories_header.h b/Telegram/SourceFiles/media/stories/media_stories_header.h index 3f0be2e5cd..32c310bff9 100644 --- a/Telegram/SourceFiles/media/stories/media_stories_header.h +++ b/Telegram/SourceFiles/media/stories/media_stories_header.h @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "base/timer.h" #include "ui/userpic_view.h" namespace Ui { @@ -34,11 +35,14 @@ public: void show(HeaderData data); private: + void updateDateText(); + const not_null _controller; std::unique_ptr _widget; std::unique_ptr _date; std::optional _data; + base::Timer _dateUpdateTimer; };