mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-06 23:24:01 +02:00
Show relative time in stories, like last seen.
This commit is contained in:
parent
f828caf0d9
commit
3ac7725111
3 changed files with 78 additions and 2 deletions
|
@ -2477,6 +2477,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
"lng_mediaview_doc_image" = "File";
|
"lng_mediaview_doc_image" = "File";
|
||||||
"lng_mediaview_today" = "today at {time}";
|
"lng_mediaview_today" = "today at {time}";
|
||||||
"lng_mediaview_yesterday" = "yesterday 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_date_time" = "{date} at {time}";
|
||||||
"lng_mediaview_set_userpic" = "Set as Main";
|
"lng_mediaview_set_userpic" = "Set as Main";
|
||||||
"lng_mediaview_report_profile_photo" = "Report";
|
"lng_mediaview_report_profile_photo" = "Report";
|
||||||
|
|
|
@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "base/unixtime.h"
|
#include "base/unixtime.h"
|
||||||
#include "data/data_user.h"
|
#include "data/data_user.h"
|
||||||
#include "media/stories/media_stories_controller.h"
|
#include "media/stories/media_stories_controller.h"
|
||||||
|
#include "lang/lang_keys.h"
|
||||||
#include "ui/controls/userpic_button.h"
|
#include "ui/controls/userpic_button.h"
|
||||||
#include "ui/text/format_values.h"
|
#include "ui/text/format_values.h"
|
||||||
#include "ui/widgets/labels.h"
|
#include "ui/widgets/labels.h"
|
||||||
|
@ -23,10 +24,60 @@ namespace {
|
||||||
constexpr auto kNameOpacity = 1.;
|
constexpr auto kNameOpacity = 1.;
|
||||||
constexpr auto kDateOpacity = 0.8;
|
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
|
} // namespace
|
||||||
|
|
||||||
Header::Header(not_null<Controller*> controller)
|
Header::Header(not_null<Controller*> controller)
|
||||||
: _controller(controller) {
|
: _controller(controller)
|
||||||
|
, _dateUpdateTimer([=] { updateDateText(); }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Header::~Header() {
|
Header::~Header() {
|
||||||
|
@ -66,13 +117,29 @@ void Header::show(HeaderData data) {
|
||||||
raw->setGeometry(layout.header);
|
raw->setGeometry(layout.header);
|
||||||
}, raw->lifetime());
|
}, raw->lifetime());
|
||||||
}
|
}
|
||||||
|
auto timestamp = ComposeTimestamp(data.date, base::unixtime::now());
|
||||||
_date = std::make_unique<Ui::FlatLabel>(
|
_date = std::make_unique<Ui::FlatLabel>(
|
||||||
_widget.get(),
|
_widget.get(),
|
||||||
Ui::FormatDateTime(base::unixtime::parse(data.date)),
|
std::move(timestamp.text),
|
||||||
st::storiesHeaderDate);
|
st::storiesHeaderDate);
|
||||||
_date->setOpacity(kDateOpacity);
|
_date->setOpacity(kDateOpacity);
|
||||||
_date->show();
|
_date->show();
|
||||||
_date->move(st::storiesHeaderDatePosition);
|
_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
|
} // namespace Media::Stories
|
||||||
|
|
|
@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "base/timer.h"
|
||||||
#include "ui/userpic_view.h"
|
#include "ui/userpic_view.h"
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
|
@ -34,11 +35,14 @@ public:
|
||||||
void show(HeaderData data);
|
void show(HeaderData data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void updateDateText();
|
||||||
|
|
||||||
const not_null<Controller*> _controller;
|
const not_null<Controller*> _controller;
|
||||||
|
|
||||||
std::unique_ptr<Ui::RpWidget> _widget;
|
std::unique_ptr<Ui::RpWidget> _widget;
|
||||||
std::unique_ptr<Ui::FlatLabel> _date;
|
std::unique_ptr<Ui::FlatLabel> _date;
|
||||||
std::optional<HeaderData> _data;
|
std::optional<HeaderData> _data;
|
||||||
|
base::Timer _dateUpdateTimer;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue