Sort conversation and user lists

This commit is contained in:
Jaidyn Ann 2021-07-25 14:42:38 -05:00
parent 0a0b29d349
commit bba3d3d866
9 changed files with 109 additions and 29 deletions

View File

@ -394,6 +394,7 @@ Conversation::AddUser(User* user)
msg.AddString("user_id", user->GetId()); msg.AddString("user_id", user->GetId());
msg.AddString("user_name", user->GetName()); msg.AddString("user_name", user->GetName());
_EnsureUser(&msg); _EnsureUser(&msg);
_SortConversationList();
} }
@ -403,6 +404,7 @@ Conversation::RemoveUser(User* user)
fUsers.RemoveItemFor(user->GetId()); fUsers.RemoveItemFor(user->GetId());
user->UnregisterObserver(this); user->UnregisterObserver(this);
GetView()->UpdateUserList(fUsers); GetView()->UpdateUserList(fUsers);
_SortConversationList();
} }
@ -581,10 +583,16 @@ Conversation::_EnsureUser(BMessage* msg)
} }
void
Conversation::_SortConversationList()
{
if (fUsers.CountItems() <= 2 || fUsers.CountItems() == 3)
((TheApp*)be_app)->GetMainWindow()->SortConversation(this);
}
Server* Server*
Conversation::_GetServer() Conversation::_GetServer()
{ {
return ((TheApp*)be_app)->GetMainWindow()->GetServer(); return ((TheApp*)be_app)->GetMainWindow()->GetServer();
} }

View File

@ -84,6 +84,8 @@ private:
void _EnsureCachePath(); void _EnsureCachePath();
User* _EnsureUser(BMessage* msg); User* _EnsureUser(BMessage* msg);
void _SortConversationList();
Server* _GetServer(); Server* _GetServer();
BMessenger fMessenger; BMessenger fMessenger;

View File

@ -29,6 +29,40 @@ const uint32 kOpenSelectedChat = 'CVos';
const uint32 kLeaveSelectedChat = 'CVcs'; const uint32 kLeaveSelectedChat = 'CVcs';
static int
compare_by_name(const BListItem* _item1, const BListItem* _item2)
{
ConversationItem* item1 = (ConversationItem*)_item1;
ConversationItem* item2 = (ConversationItem*)_item2;
return strcasecmp(item1->GetConversation()->GetName().String(),
item2->GetConversation()->GetName().String());
}
static int
compare_conversations(const BListItem* _item1, const BListItem* _item2)
{
ConversationItem* item1 = (ConversationItem*)_item1;
ConversationItem* item2 = (ConversationItem*)_item2;
int32 userCount1 = item1->GetConversation()->Users().CountItems();
int32 userCount2 = item2->GetConversation()->Users().CountItems();
// Sort by name among chats/rooms
if ((userCount1 <= 2 && userCount2 <= 2)
|| (userCount1 > 2 && userCount2 > 2))
return compare_by_name(item1, item2);
// One-on-one chats should sort above rooms
if (userCount1 <=2 && userCount2 > 2)
return -1;
if (userCount1 > 2 && userCount2 <= 2)
return 1;
return 0;
}
ConversationListView::ConversationListView(const char* name) ConversationListView::ConversationListView(const char* name)
: BOutlineListView(name) : BOutlineListView(name)
{ {
@ -105,6 +139,7 @@ ConversationListView::AddConversation(Conversation* chat)
return; return;
AddUnder(item, superItem); AddUnder(item, superItem);
SortItemsUnder(superItem, true, compare_conversations);
} }
@ -115,6 +150,14 @@ ConversationListView::RemoveConversation(Conversation* chat)
} }
void
ConversationListView::SortConversation(Conversation* chat)
{
ConversationAccountItem* superItem = _EnsureAccountItem(chat);
SortItemsUnder(superItem, true, compare_conversations);
}
int32 int32
ConversationListView::CountConversations() ConversationListView::CountConversations()
{ {

View File

@ -2,8 +2,8 @@
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io> * Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license. * All rights reserved. Distributed under the terms of the MIT license.
*/ */
#ifndef CONVERSATIONLIST_H #ifndef _CONVERSATION_LIST_H
#define CONVERSATIONLIST_H #define _CONVERSATION_LIST_H
#include <OutlineListView.h> #include <OutlineListView.h>
@ -16,24 +16,24 @@ class ConversationListView : public BOutlineListView {
public: public:
ConversationListView(const char* name); ConversationListView(const char* name);
void MessageReceived(BMessage* msg); virtual void MessageReceived(BMessage* msg);
void SelectionChanged(); virtual void SelectionChanged();
void MouseDown(BPoint where); virtual void MouseDown(BPoint where);
void AddConversation(Conversation* chat); void AddConversation(Conversation* chat);
void RemoveConversation(Conversation* chat); void RemoveConversation(Conversation* chat);
void SortConversation(Conversation* chat);
int32 CountConversations(); int32 CountConversations();
int32 ConversationIndexOf(Conversation* chat); int32 ConversationIndexOf(Conversation* chat);
void SelectConversation(int32 index); void SelectConversation(int32 index);
private: private:
BPopUpMenu* _ConversationPopUp(); BPopUpMenu* _ConversationPopUp();
BPopUpMenu* _BlankPopUp(); BPopUpMenu* _BlankPopUp();
ConversationAccountItem* _EnsureAccountItem(Conversation* chat); ConversationAccountItem*
_EnsureAccountItem(Conversation* chat);
}; };
#endif // _CONVERSATION_LIST_H
#endif // CONVERSATIONLIST_H

View File

@ -230,8 +230,10 @@ ConversationView::UpdateUserList(UserMap users)
fUserList->MakeEmpty(); fUserList->MakeEmpty();
for (int i = 0; i < users.CountItems(); i++) { for (int i = 0; i < users.CountItems(); i++) {
User* user = users.ValueAt(i); User* user = users.ValueAt(i);
if (fUserList->HasItem(user->GetListItem()) == false) if (fUserList->HasItem(user->GetListItem()) == false) {
fUserList->AddItem(user->GetListItem()); fUserList->AddItem(user->GetListItem());
fUserList->Sort();
}
} }
} }

View File

@ -27,6 +27,16 @@
#define B_TRANSLATION_CONTEXT "UserListView" #define B_TRANSLATION_CONTEXT "UserListView"
static int
compare_by_name(const void* _item1, const void* _item2)
{
UserItem* item1 = *(UserItem**)_item1;
UserItem* item2 = *(UserItem**)_item2;
return item1->GetUser()->GetName().ICompare(item2->GetUser()->GetName());
}
UserListView::UserListView(const char* name) UserListView::UserListView(const char* name)
: BListView(name), : BListView(name),
fChat(NULL) fChat(NULL)
@ -52,6 +62,13 @@ UserListView::MouseDown(BPoint where)
} }
void
UserListView::Sort()
{
SortItems(compare_by_name);
}
BPopUpMenu* BPopUpMenu*
UserListView::_UserPopUp() UserListView::_UserPopUp()
{ {

View File

@ -27,23 +27,23 @@ enum
class UserListView : public BListView { class UserListView : public BListView {
public: public:
UserListView(const char* name); UserListView(const char* name);
void MouseDown(BPoint where); virtual void MouseDown(BPoint where);
void SetConversation(Conversation* chat) { fChat = chat; } void Sort();
void SetConversation(Conversation* chat) { fChat = chat; }
private: private:
BPopUpMenu* _UserPopUp(); BPopUpMenu* _UserPopUp();
BPopUpMenu* _BlankPopUp(); BPopUpMenu* _BlankPopUp();
void _ModerationAction(int32 im_what); void _ModerationAction(int32 im_what);
void _ProcessItem(BMessage* itemMsg, BPopUpMenu* menu, Role* user, void _ProcessItem(BMessage* itemMsg, BPopUpMenu* menu,
Role* target, BString target_id); Role* user, Role* target, BString target_id);
Conversation* fChat; Conversation* fChat;
}; };
#endif // CONVERSATIONLIST_H #endif // CONVERSATIONLIST_H

View File

@ -396,6 +396,13 @@ MainWindow::RemoveConversation(Conversation* chat)
} }
void
MainWindow::SortConversation(Conversation* chat)
{
fListView->SortConversation(chat);
}
void void
MainWindow::_InitInterface() MainWindow::_InitInterface()
{ {

View File

@ -45,6 +45,7 @@ public:
void SetConversation(Conversation* chat); void SetConversation(Conversation* chat);
void RemoveConversation(Conversation* chat); void RemoveConversation(Conversation* chat);
void SortConversation(Conversation* chat);
Server* GetServer() const { return fServer; } Server* GetServer() const { return fServer; }