Generate a date separator before local messages

This commit is contained in:
Konstantinos Sideris 2018-02-10 01:09:30 +02:00
parent da6b816228
commit ddcc0f7f60
2 changed files with 21 additions and 11 deletions

View file

@ -93,7 +93,7 @@ public:
const QSharedPointer<QIODevice> data = QSharedPointer<QIODevice>(nullptr)); const QSharedPointer<QIODevice> data = QSharedPointer<QIODevice>(nullptr));
void updatePendingMessage(int txn_id, QString event_id); void updatePendingMessage(int txn_id, QString event_id);
void scrollDown(); void scrollDown();
void addDateSeparator(QDateTime datetime, int position); QLabel *createDateSeparator(QDateTime datetime);
public slots: public slots:
void sliderRangeChanged(int min, int max); void sliderRangeChanged(int min, int max);
@ -135,7 +135,8 @@ private:
//! Decides whether or not to show or hide the scroll down button. //! Decides whether or not to show or hide the scroll down button.
void toggleScrollDownButton(); void toggleScrollDownButton();
void init(); void init();
void addTimelineItem(TimelineItem *item, TimelineDirection direction); void addTimelineItem(TimelineItem *item,
TimelineDirection direction = TimelineDirection::Bottom);
void updateLastSender(const QString &user_id, TimelineDirection direction); void updateLastSender(const QString &user_id, TimelineDirection direction);
void notifyForLastEvent(); void notifyForLastEvent();
void notifyForLastEvent(const TimelineEvent &event); void notifyForLastEvent(const TimelineEvent &event);
@ -245,7 +246,7 @@ TimelineView::addUserMessage(const QString &url,
TimelineItem *view_item = TimelineItem *view_item =
new TimelineItem(widget, local_user_, with_sender, scroll_widget_); new TimelineItem(widget, local_user_, with_sender, scroll_widget_);
pushTimelineItem(view_item); addTimelineItem(view_item);
lastMessageDirection_ = TimelineDirection::Bottom; lastMessageDirection_ = TimelineDirection::Bottom;

View file

@ -450,8 +450,12 @@ TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction)
if (lastItem) { if (lastItem) {
auto oldDate = lastItem->descriptionMessage().datetime; auto oldDate = lastItem->descriptionMessage().datetime;
if (oldDate.daysTo(newDate) != 0) if (oldDate.daysTo(newDate) != 0) {
addDateSeparator(newDate, lastItemPosition); auto separator = createDateSeparator(newDate);
if (separator)
scroll_layout_->addWidget(separator);
}
} }
pushTimelineItem(item); pushTimelineItem(item);
@ -465,8 +469,12 @@ TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction)
if (firstItem) { if (firstItem) {
auto oldDate = firstItem->descriptionMessage().datetime; auto oldDate = firstItem->descriptionMessage().datetime;
if (newDate.daysTo(oldDate) != 0) if (newDate.daysTo(oldDate) != 0) {
addDateSeparator(oldDate, 1); auto separator = createDateSeparator(oldDate);
if (separator)
scroll_layout_->insertWidget(1, separator);
}
} }
} }
@ -501,7 +509,7 @@ TimelineView::addUserMessage(mtx::events::MessageType ty, const QString &body)
TimelineItem *view_item = TimelineItem *view_item =
new TimelineItem(ty, local_user_, body, with_sender, scroll_widget_); new TimelineItem(ty, local_user_, body, with_sender, scroll_widget_);
pushTimelineItem(view_item); addTimelineItem(view_item);
lastMessageDirection_ = TimelineDirection::Bottom; lastMessageDirection_ = TimelineDirection::Bottom;
@ -687,8 +695,8 @@ TimelineView::event(QEvent *event)
return QWidget::event(event); return QWidget::event(event);
} }
void QLabel *
TimelineView::addDateSeparator(QDateTime datetime, int position) TimelineView::createDateSeparator(QDateTime datetime)
{ {
auto now = QDateTime::currentDateTime(); auto now = QDateTime::currentDateTime();
auto days = now.daysTo(datetime); auto days = now.daysTo(datetime);
@ -713,8 +721,9 @@ TimelineView::addDateSeparator(QDateTime datetime, int position)
QString("font-size: %1px").arg(conf::timeline::fonts::dateSeparator)); QString("font-size: %1px").arg(conf::timeline::fonts::dateSeparator));
separator->setAlignment(Qt::AlignCenter); separator->setAlignment(Qt::AlignCenter);
separator->setContentsMargins(0, 15, 0, 15); separator->setContentsMargins(0, 15, 0, 15);
scroll_layout_->insertWidget(position, separator);
} }
return separator;
} }
QString QString