mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-23 03:18:49 +03:00
Sort room list by room priority
This commit is contained in:
parent
fc2f08a186
commit
08125e8c44
3 changed files with 39 additions and 10 deletions
|
@ -324,6 +324,15 @@ RoomInfoListItem::updateUnreadMessageCount(int count, int highlightedCount)
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned short int
|
||||||
|
RoomInfoListItem::calculateImportance() const
|
||||||
|
{
|
||||||
|
return (hasUnreadMessages_) +
|
||||||
|
(unreadMsgCount_ != 0) +
|
||||||
|
(unreadHighlightedMsgCount_ != 0) +
|
||||||
|
(isInvite()) * 4;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
RoomInfoListItem::setPressedState(bool state)
|
RoomInfoListItem::setPressedState(bool state)
|
||||||
{
|
{
|
||||||
|
|
|
@ -68,6 +68,8 @@ public:
|
||||||
void updateUnreadMessageCount(int count, int highlightedCount);
|
void updateUnreadMessageCount(int count, int highlightedCount);
|
||||||
void clearUnreadMessageCount() { updateUnreadMessageCount(0, 0); };
|
void clearUnreadMessageCount() { updateUnreadMessageCount(0, 0); };
|
||||||
|
|
||||||
|
unsigned short int calculateImportance() const;
|
||||||
|
|
||||||
QString roomId() { return roomId_; }
|
QString roomId() { return roomId_; }
|
||||||
bool isPressed() const { return isPressed_; }
|
bool isPressed() const { return isPressed_; }
|
||||||
int unreadMessageCount() const { return unreadMsgCount_; }
|
int unreadMessageCount() const { return unreadMsgCount_; }
|
||||||
|
@ -128,7 +130,7 @@ public:
|
||||||
roomType_ = RoomType::Joined;
|
roomType_ = RoomType::Joined;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isInvite() { return roomType_ == RoomType::Invited; }
|
bool isInvite() const { return roomType_ == RoomType::Invited; }
|
||||||
void setReadState(bool hasUnreadMessages)
|
void setReadState(bool hasUnreadMessages)
|
||||||
{
|
{
|
||||||
if (hasUnreadMessages_ != hasUnreadMessages) {
|
if (hasUnreadMessages_ != hasUnreadMessages) {
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
@ -328,30 +329,47 @@ RoomList::updateRoomDescription(const QString &roomid, const DescInfo &info)
|
||||||
emit sortRoomsByLastMessage();
|
emit sortRoomsByLastMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct room_sort {
|
||||||
|
bool operator() (const RoomInfoListItem * a, const RoomInfoListItem * b) const {
|
||||||
|
// Sort by "importance" (i.e. invites before mentions before
|
||||||
|
// notifs before new events before old events), then secondly
|
||||||
|
// by recency.
|
||||||
|
|
||||||
|
// Checking importance first
|
||||||
|
const auto a_importance = a->calculateImportance();
|
||||||
|
const auto b_importance = b->calculateImportance();
|
||||||
|
if(a_importance != b_importance) {
|
||||||
|
return a_importance > b_importance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now sort by recency
|
||||||
|
// Zero if empty, otherwise the time that the event occured
|
||||||
|
const uint64_t a_recency = a->lastMessageInfo().userid.isEmpty() ? 0 :
|
||||||
|
a->lastMessageInfo().datetime.toMSecsSinceEpoch();
|
||||||
|
const uint64_t b_recency = b->lastMessageInfo().userid.isEmpty() ? 0 :
|
||||||
|
b->lastMessageInfo().datetime.toMSecsSinceEpoch();
|
||||||
|
return a_recency > b_recency;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
RoomList::sortRoomsByLastMessage()
|
RoomList::sortRoomsByLastMessage()
|
||||||
{
|
{
|
||||||
isSortPending_ = false;
|
isSortPending_ = false;
|
||||||
|
|
||||||
std::multimap<uint64_t, RoomInfoListItem *, std::greater<uint64_t>> times;
|
std::multiset<RoomInfoListItem *, room_sort> times;
|
||||||
|
|
||||||
for (int ii = 0; ii < contentsLayout_->count(); ++ii) {
|
for (int ii = 0; ii < contentsLayout_->count(); ++ii) {
|
||||||
auto room = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(ii)->widget());
|
auto room = qobject_cast<RoomInfoListItem *>(contentsLayout_->itemAt(ii)->widget());
|
||||||
|
|
||||||
if (!room)
|
if (!room)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Not a room message.
|
|
||||||
if (room->isInvite())
|
|
||||||
times.emplace(std::numeric_limits<uint64_t>::max(), room);
|
|
||||||
else if (room->lastMessageInfo().userid.isEmpty())
|
|
||||||
times.emplace(0, room);
|
|
||||||
else
|
else
|
||||||
times.emplace(room->lastMessageInfo().datetime.toMSecsSinceEpoch(), room);
|
times.insert(room);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto it = times.cbegin(); it != times.cend(); ++it) {
|
for (auto it = times.cbegin(); it != times.cend(); ++it) {
|
||||||
const auto roomWidget = it->second;
|
const auto roomWidget = *it;
|
||||||
const auto currentIndex = contentsLayout_->indexOf(roomWidget);
|
const auto currentIndex = contentsLayout_->indexOf(roomWidget);
|
||||||
const auto newIndex = std::distance(times.cbegin(), it);
|
const auto newIndex = std::distance(times.cbegin(), it);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue