Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(activity): change last activity time from QDate to QDateTime
Browse files Browse the repository at this point in the history
* Allows for strict sorting based on last activity in the future
  • Loading branch information
anthonybilinski committed May 17, 2019
1 parent ca397ae commit d55332e
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/persistence/ifriendsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class IFriendSettings
virtual int getFriendCircleID(const ToxPk& pk) const = 0;
virtual void setFriendCircleID(const ToxPk& pk, int circleID) = 0;

virtual QDate getFriendActivity(const ToxPk& pk) const = 0;
virtual void setFriendActivity(const ToxPk& pk, const QDate& date) = 0;
virtual QDateTime getFriendActivity(const ToxPk& pk) const = 0;
virtual void setFriendActivity(const ToxPk& pk, const QDateTime& date) = 0;

virtual void saveFriendSettings(const ToxPk& pk) = 0;
virtual void removeFriendSettings(const ToxPk& pk) = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/persistence/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void Settings::loadPersonal(QString profileName, const ToxEncrypt* passKey)
fp.circleID = ps.value("circle", -1).toInt();

if (getEnableLogging())
fp.activity = ps.value("activity", QDate()).toDate();
fp.activity = ps.value("activity", QDateTime()).toDateTime();
friendLst.insert(ToxId(fp.addr).getPublicKey().getByteArray(), fp);
}
ps.endArray();
Expand Down Expand Up @@ -2031,17 +2031,17 @@ void Settings::setFriendCircleID(const ToxPk& id, int circleID)
frnd.circleID = circleID;
}

QDate Settings::getFriendActivity(const ToxPk& id) const
QDateTime Settings::getFriendActivity(const ToxPk& id) const
{
QMutexLocker locker{&bigLock};
auto it = friendLst.find(id.getByteArray());
if (it != friendLst.end())
return it->activity;

return QDate();
return QDateTime();
}

void Settings::setFriendActivity(const ToxPk& id, const QDate& activity)
void Settings::setFriendActivity(const ToxPk& id, const QDateTime& activity)
{
QMutexLocker locker{&bigLock};
auto& frnd = getOrInsertFriendPropRef(id);
Expand Down
8 changes: 4 additions & 4 deletions src/persistence/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "src/persistence/ifriendsettings.h"
#include "src/video/ivideosettings.h"

#include <QDate>
#include <QDateTime>
#include <QFlags>
#include <QFont>
#include <QHash>
Expand Down Expand Up @@ -497,8 +497,8 @@ public slots:
int getFriendCircleID(const ToxPk& id) const override;
void setFriendCircleID(const ToxPk& id, int circleID) override;

QDate getFriendActivity(const ToxPk& id) const override;
void setFriendActivity(const ToxPk& id, const QDate& date) override;
QDateTime getFriendActivity(const ToxPk& id) const override;
void setFriendActivity(const ToxPk& id, const QDateTime& date) override;

void saveFriendSettings(const ToxPk& id) override;
void removeFriendSettings(const ToxPk& id) override;
Expand Down Expand Up @@ -699,7 +699,7 @@ public slots:
QString autoAcceptDir = "";
QString note = "";
int circleID = -1;
QDate activity = QDate();
QDateTime activity = QDateTime();
AutoAcceptCallFlags autoAcceptCall;
bool autoGroupInvite = false;
};
Expand Down
6 changes: 3 additions & 3 deletions src/widget/form/chatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ void ChatForm::searchInBegin(const QString& phrase, const ParameterSearch& param
const bool isFirst = (parameter.period == PeriodSearch::WithTheFirst);
const bool isAfter = (parameter.period == PeriodSearch::AfterDate);
if (isFirst || isAfter) {
if (isFirst || (isAfter && parameter.date < getFirstDate())) {
if (isFirst || (isAfter && parameter.date < getFirstTime().date())) {
const QString pk = f->getPublicKey().toString();
if ((isFirst || parameter.date >= history->getStartDateChatHistory(pk).date())
&& loadHistory(phrase, parameter)) {
Expand All @@ -547,7 +547,7 @@ void ChatForm::searchInBegin(const QString& phrase, const ParameterSearch& param

onSearchDown(phrase, parameter);
} else {
if (parameter.period == PeriodSearch::BeforeDate && parameter.date < getFirstDate()) {
if (parameter.period == PeriodSearch::BeforeDate && parameter.date < getFirstTime().date()) {
const QString pk = f->getPublicKey().toString();
if (parameter.date >= history->getStartDateChatHistory(pk).date()
&& loadHistory(phrase, parameter)) {
Expand Down Expand Up @@ -1167,7 +1167,7 @@ bool ChatForm::loadHistory(const QString& phrase, const ParameterSearch& paramet
const QDateTime newBaseDate =
history->getDateWhereFindPhrase(pk, earliestMessage, phrase, parameter);

if (newBaseDate.isValid() && getFirstDate().isValid() && newBaseDate.date() < getFirstDate()) {
if (newBaseDate.isValid() && getFirstTime().isValid() && newBaseDate.date() < getFirstTime().date()) {
searchAfterLoadHistory = true;
loadHistoryByDateRange(newBaseDate);

Expand Down
36 changes: 18 additions & 18 deletions src/widget/form/genericchatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,14 @@ void GenericChatForm::hideFileMenu()
fileFlyout->animateHide();
}

QDate GenericChatForm::getLatestDate() const
QDateTime GenericChatForm::getLatestTime() const
{
return getDate(chatWidget->getLatestLine());
return getTime(chatWidget->getLatestLine());
}

QDate GenericChatForm::getFirstDate() const
QDateTime GenericChatForm::getFirstTime() const
{
return getDate(chatWidget->getFirstLine());
return getTime(chatWidget->getFirstLine());
}

void GenericChatForm::reloadTheme()
Expand Down Expand Up @@ -401,7 +401,8 @@ ChatMessage::Ptr GenericChatForm::createMessage(const ToxPk& author, const QStri
bool isSelf = author == core->getSelfId().getPublicKey();
QString myNickName = core->getUsername().isEmpty() ? author.toString() : core->getUsername();
QString authorStr = isSelf ? myNickName : resolveToxPk(author);
if (getLatestDate() != QDate::currentDate()) {
const auto now = QDateTime::currentDateTime();
if (getLatestTime().date() != now.date()) {
addSystemDateMessage();
}

Expand All @@ -411,13 +412,12 @@ ChatMessage::Ptr GenericChatForm::createMessage(const ToxPk& author, const QStri
previousId = ToxPk{};
} else {
msg = ChatMessage::createChatMessage(authorStr, message, ChatMessage::NORMAL, isSelf, QDateTime(), colorizeName);
const QDateTime newMsgDateTime = QDateTime::currentDateTime();
if (needsToHideName(author, newMsgDateTime)) {
if (needsToHideName(author, now)) {
msg->hideSender();
}

previousId = author;
prevMsgDateTime = newMsgDateTime;
prevMsgDateTime = now;
}

if (isSent) {
Expand Down Expand Up @@ -552,7 +552,7 @@ void GenericChatForm::onChatMessageFontChanged(const QFont& font)
void GenericChatForm::addSystemInfoMessage(const QString& message, ChatMessage::SystemMessageType type,
const QDateTime& datetime)
{
if (getLatestDate() != QDate::currentDate()) {
if (getLatestTime().date() != QDate::currentDate()) {
addSystemDateMessage();
}

Expand All @@ -569,19 +569,19 @@ void GenericChatForm::addSystemDateMessage()
insertChatMessage(ChatMessage::createChatInfoMessage(dateText, ChatMessage::INFO, QDateTime()));
}

QDate GenericChatForm::getDate(const ChatLine::Ptr &chatLine) const
QDateTime GenericChatForm::getTime(const ChatLine::Ptr &chatLine) const
{
if (chatLine) {
Timestamp* const timestamp = qobject_cast<Timestamp*>(chatLine->getContent(2));

if (timestamp) {
return timestamp->getTime().date();
return timestamp->getTime();
} else {
return QDate::currentDate();
return QDateTime::currentDateTime();
}
}

return QDate();
return QDateTime();
}

void GenericChatForm::disableSearchText()
Expand Down Expand Up @@ -625,7 +625,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
startLine = 0;
} else if (parameter.period == PeriodSearch::AfterDate) {
const auto lambda = [=](const ChatLine::Ptr& item) {
const auto d = getDate(item);
const auto d = getTime(item).date();
return d.isValid() && parameter.date <= d;
};

Expand All @@ -637,7 +637,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
} else if (parameter.period == PeriodSearch::BeforeDate) {
#if QT_VERSION > QT_VERSION_CHECK(5, 6, 0)
const auto lambda = [=](const ChatLine::Ptr& item) {
const auto d = getDate(item);
const auto d = getTime(item).date();
return d.isValid() && parameter.date >= d;
};

Expand All @@ -648,7 +648,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
}
#else
for (int i = lines.size() - 1; i >= 0; --i) {
auto d = getDate(lines[i]);
auto d = getTime(lines[i]).date();
if (d.isValid() && parameter.date >= d) {
startLine = i;
break;
Expand Down Expand Up @@ -1001,9 +1001,9 @@ void GenericChatForm::onSearchTriggered()

void GenericChatForm::updateShowDateInfo(const ChatLine::Ptr& line)
{
const auto date = getDate(line);
const auto date = getTime(line);

if (date.isValid() && date != QDate::currentDate()) {
if (date.isValid() && date != QDateTime::currentDateTime()) {
const auto dateText = QStringLiteral("<b>%1<\b>").arg(date.toString(Settings::getInstance().getDateFormat()));
dateInfo->setText(dateText);
dateInfo->setVisible(true);
Expand Down
6 changes: 3 additions & 3 deletions src/widget/form/genericchatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class GenericChatForm : public QWidget
const QDateTime& datetime);
void addAlertMessage(const ToxPk& author, const QString& message, const QDateTime& datetime, bool colorizeName = false);
static QString resolveToxPk(const ToxPk& pk);
QDate getLatestDate() const;
QDate getFirstDate() const;
QDateTime getLatestTime() const;
QDateTime getFirstTime() const;

signals:
void sendMessage(uint32_t, QString);
Expand Down Expand Up @@ -125,7 +125,7 @@ protected slots:
private:
void retranslateUi();
void addSystemDateMessage();
QDate getDate(const ChatLine::Ptr& chatLine) const;
QDateTime getTime(const ChatLine::Ptr& chatLine) const;

protected:
ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message,
Expand Down
42 changes: 21 additions & 21 deletions src/widget/friendlistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ enum class Time

static const int LAST_TIME = static_cast<int>(Time::Never);

Time getTime(const QDate& date)
Time getTimeBucket(const QDateTime& date)
{
if (date == QDate()) {
if (date == QDateTime()) {
return Time::Never;
}

Expand All @@ -74,15 +74,15 @@ Time getTime(const QDate& date)
// clang-format on

for (Time time : dates.keys()) {
if (dates[time] <= date) {
if (dates[time] <= date.date()) {
return time;
}
}

return Time::LongAgo;
}

QDate getDateFriend(const Friend* contact)
QDateTime getActiveTimeFriend(const Friend* contact)
{
return Settings::getInstance().getFriendActivity(contact->getPublicKey());
}
Expand Down Expand Up @@ -263,10 +263,10 @@ void FriendListWidget::moveFriends(QLayout* layout)
circleWidget->moveFriendWidgets(this);
} else if (friendWidget) {
const Friend* contact = friendWidget->getFriend();
QDate activityDate = getDateFriend(contact);
int time = static_cast<int>(getTime(activityDate));
const auto activityTime = getActiveTimeFriend(contact);
int timeIndex = static_cast<int>(getTimeBucket(activityTime));

QWidget* w = activityLayout->itemAt(time)->widget();
QWidget* w = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(w);
categoryWidget->addFriendWidget(friendWidget, contact->getStatus());
}
Expand Down Expand Up @@ -309,9 +309,9 @@ void FriendListWidget::removeFriendWidget(FriendWidget* w)
{
const Friend* contact = w->getFriend();
if (mode == Activity) {
QDate activityDate = getDateFriend(contact);
int time = static_cast<int>(getTime(activityDate));
QWidget* widget = activityLayout->itemAt(time)->widget();
const auto activityTime = getActiveTimeFriend(contact);
int timeIndex = static_cast<int>(getTimeBucket(activityTime));
QWidget* widget = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget);
categoryWidget->removeFriendWidget(w, contact->getStatus());
categoryWidget->setVisible(categoryWidget->hasChatrooms());
Expand Down Expand Up @@ -401,9 +401,9 @@ void FriendListWidget::onFriendWidgetRenamed(FriendWidget* friendWidget)
const Friend* contact = friendWidget->getFriend();
auto status = contact->getStatus();
if (mode == Activity) {
QDate activityDate = getDateFriend(contact);
int time = static_cast<int>(getTime(activityDate));
QWidget* widget = activityLayout->itemAt(time)->widget();
const auto activityTime = getActiveTimeFriend(contact);
int timeIndex = static_cast<int>(getTimeBucket(activityTime));
QWidget* widget = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget);
categoryWidget->removeFriendWidget(friendWidget, status);
categoryWidget->addFriendWidget(friendWidget, status);
Expand Down Expand Up @@ -452,8 +452,8 @@ void FriendListWidget::cycleContacts(GenericChatroomWidget* activeChatroomWidget
return;
}

QDate activityDate = getDateFriend(friendWidget->getFriend());
index = static_cast<int>(getTime(activityDate));
const auto activityTime = getActiveTimeFriend(friendWidget->getFriend());
index = static_cast<int>(getTimeBucket(activityTime));
QWidget* widget = activityLayout->itemAt(index)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget);

Expand Down Expand Up @@ -627,22 +627,22 @@ void FriendListWidget::moveWidget(FriendWidget* widget, Status::Status s, bool a
circleWidget->addFriendWidget(widget, s);
} else {
const Friend* contact = widget->getFriend();
QDate activityDate = getDateFriend(contact);
int time = static_cast<int>(getTime(activityDate));
QWidget* w = activityLayout->itemAt(time)->widget();
const auto activityTime = getActiveTimeFriend(contact);
int timeIndex = static_cast<int>(getTimeBucket(activityTime));
QWidget* w = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(w);
categoryWidget->addFriendWidget(widget, contact->getStatus());
categoryWidget->show();
}
}

void FriendListWidget::updateActivityDate(const QDate& date)
void FriendListWidget::updateActivityTime(const QDateTime& time)
{
if (mode != Activity)
return;

int time = static_cast<int>(getTime(date));
QWidget* widget = activityLayout->itemAt(time)->widget();
int timeIndex = static_cast<int>(getTimeBucket(time));
QWidget* widget = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = static_cast<CategoryWidget*>(widget);
categoryWidget->updateStatus();

Expand Down
2 changes: 1 addition & 1 deletion src/widget/friendlistwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FriendListWidget : public QWidget

void cycleContacts(GenericChatroomWidget* activeChatroomWidget, bool forward);

void updateActivityDate(const QDate& date);
void updateActivityTime(const QDateTime& date);
void reDraw();

signals:
Expand Down
23 changes: 10 additions & 13 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,10 +1001,10 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)
friendWidgets[friendPk] = widget;
chatForms[friendPk] = friendForm;

QDate activityDate = settings.getFriendActivity(friendPk);
QDate chatDate = friendForm->getLatestDate();
if (chatDate > activityDate && chatDate.isValid()) {
settings.setFriendActivity(friendPk, chatDate);
const auto activityTime = settings.getFriendActivity(friendPk);
const auto chatTime = friendForm->getLatestTime();
if (chatTime > activityTime && chatTime.isValid()) {
settings.setFriendActivity(friendPk, chatTime);
}

contactListWidget->addFriendWidget(widget, Status::Status::Offline, settings.getFriendCircleID(friendPk));
Expand Down Expand Up @@ -1493,15 +1493,12 @@ void Widget::onFriendRequestReceived(const ToxPk& friendPk, const QString& messa
void Widget::updateFriendActivity(const Friend* frnd)
{
const ToxPk& pk = frnd->getPublicKey();
QDate date = settings.getFriendActivity(pk);
if (date != QDate::currentDate()) {
// Update old activity before after new one. Store old date first.
QDate oldDate = settings.getFriendActivity(pk);
settings.setFriendActivity(pk, QDate::currentDate());
FriendWidget* widget = friendWidgets[frnd->getPublicKey()];
contactListWidget->moveWidget(widget, frnd->getStatus());
contactListWidget->updateActivityDate(oldDate);
}
const auto oldTime = settings.getFriendActivity(pk);
const auto newTime = QDateTime::currentDateTime();
settings.setFriendActivity(pk, newTime);
FriendWidget* widget = friendWidgets[frnd->getPublicKey()];
contactListWidget->moveWidget(widget, frnd->getStatus());
contactListWidget->updateActivityTime(oldTime); // update old category widget
}

void Widget::removeFriend(Friend* f, bool fake)
Expand Down

0 comments on commit d55332e

Please sign in to comment.