Skip to content

Commit

Permalink
Merge pull request #371 from matthewvogt/mer-1221
Browse files Browse the repository at this point in the history
[lipstick] Sort notifications before population to reduce comparisons. Contributes to MER#1221
  • Loading branch information
matthewvogt committed Aug 10, 2015
2 parents 3d7a09d + 74d0e3c commit f918233
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions src/notifications/notificationlistmodel.cpp
Expand Up @@ -16,6 +16,47 @@
#include "notificationmanager.h"
#include "notificationlistmodel.h"

namespace {

int compare(const LipstickNotification &lhs, const LipstickNotification &rhs)
{
const QDateTime lhsTimestamp(lhs.timestamp()), rhsTimestamp(rhs.timestamp());
if (lhsTimestamp < rhsTimestamp) {
return -1;
}
if (rhsTimestamp < lhsTimestamp) {
return 1;
}
return 0;
}

bool operator<(const LipstickNotification &lhs, const LipstickNotification &rhs)
{
int timestampComparison(compare(lhs, rhs));
if (timestampComparison > 0) {
// Later notifications sort first
return true;
} else if (timestampComparison == 0) {
// For matching timestamps, sort the higher ID first
if (lhs.replacesId() > rhs.replacesId()) {
return true;
}
}
return false;
}

bool compareNotifications(const QObject *lhs, const QObject *rhs)
{
return *(static_cast<const LipstickNotification *>(lhs)) < *(static_cast<const LipstickNotification *>(rhs));
}

void sortNotifications(QList<QObject *> &notifications)
{
std::sort(notifications.begin(), notifications.end(), compareNotifications);
}

}

NotificationListModel::NotificationListModel(QObject *parent) :
QObjectListModel(parent),
m_populated(false)
Expand Down Expand Up @@ -49,10 +90,11 @@ void NotificationListModel::init()
foreach(uint id, NotificationManager::instance()->notificationIds()) {
LipstickNotification *notification = NotificationManager::instance()->notification(id);
if (notificationShouldBeShown(notification)) {
insertItem(indexFor(notification), notification);
initialNotifications.append(notification);
}
}

sortNotifications(initialNotifications);
addItems(initialNotifications);
}

Expand Down Expand Up @@ -94,13 +136,8 @@ int NotificationListModel::indexFor(LipstickNotification *notification)
if (notification->replacesId() == notificationAtIndex->replacesId()) {
continue;
}

if (notification->timestamp() > notificationAtIndex->timestamp()) {
if (*notification < *notificationAtIndex) {
return index;
} else if (notification->timestamp() == notificationAtIndex->timestamp()) {
if (notification->replacesId() > notificationAtIndex->replacesId()) {
return index;
}
}
}
return itemCount();
Expand Down

0 comments on commit f918233

Please sign in to comment.