From bba3d3d866321e36bc528b4c2a4891722280658a Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sun, 25 Jul 2021 14:42:38 -0500 Subject: [PATCH] Sort conversation and user lists --- application/Conversation.cpp | 12 +++++- application/Conversation.h | 2 + application/views/ConversationListView.cpp | 43 ++++++++++++++++++++++ application/views/ConversationListView.h | 32 ++++++++-------- application/views/ConversationView.cpp | 4 +- application/views/UserListView.cpp | 17 +++++++++ application/views/UserListView.h | 20 +++++----- application/windows/MainWindow.cpp | 7 ++++ application/windows/MainWindow.h | 1 + 9 files changed, 109 insertions(+), 29 deletions(-) diff --git a/application/Conversation.cpp b/application/Conversation.cpp index 2edae05..aeb92bc 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -394,6 +394,7 @@ Conversation::AddUser(User* user) msg.AddString("user_id", user->GetId()); msg.AddString("user_name", user->GetName()); _EnsureUser(&msg); + _SortConversationList(); } @@ -403,6 +404,7 @@ Conversation::RemoveUser(User* user) fUsers.RemoveItemFor(user->GetId()); user->UnregisterObserver(this); 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* Conversation::_GetServer() { return ((TheApp*)be_app)->GetMainWindow()->GetServer(); } - - diff --git a/application/Conversation.h b/application/Conversation.h index aff8b93..b7aa821 100644 --- a/application/Conversation.h +++ b/application/Conversation.h @@ -84,6 +84,8 @@ private: void _EnsureCachePath(); User* _EnsureUser(BMessage* msg); + void _SortConversationList(); + Server* _GetServer(); BMessenger fMessenger; diff --git a/application/views/ConversationListView.cpp b/application/views/ConversationListView.cpp index 734613c..1a85f48 100644 --- a/application/views/ConversationListView.cpp +++ b/application/views/ConversationListView.cpp @@ -29,6 +29,40 @@ const uint32 kOpenSelectedChat = 'CVos'; 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) : BOutlineListView(name) { @@ -105,6 +139,7 @@ ConversationListView::AddConversation(Conversation* chat) return; 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 ConversationListView::CountConversations() { diff --git a/application/views/ConversationListView.h b/application/views/ConversationListView.h index 71fe2ff..0bf051d 100644 --- a/application/views/ConversationListView.h +++ b/application/views/ConversationListView.h @@ -2,8 +2,8 @@ * Copyright 2021, Jaidyn Levesque * All rights reserved. Distributed under the terms of the MIT license. */ -#ifndef CONVERSATIONLIST_H -#define CONVERSATIONLIST_H +#ifndef _CONVERSATION_LIST_H +#define _CONVERSATION_LIST_H #include @@ -16,24 +16,24 @@ class ConversationListView : public BOutlineListView { public: ConversationListView(const char* name); - void MessageReceived(BMessage* msg); - void SelectionChanged(); - void MouseDown(BPoint where); + virtual void MessageReceived(BMessage* msg); + virtual void SelectionChanged(); + virtual void MouseDown(BPoint where); - void AddConversation(Conversation* chat); - void RemoveConversation(Conversation* chat); + void AddConversation(Conversation* chat); + void RemoveConversation(Conversation* chat); + void SortConversation(Conversation* chat); - int32 CountConversations(); - int32 ConversationIndexOf(Conversation* chat); - void SelectConversation(int32 index); + int32 CountConversations(); + int32 ConversationIndexOf(Conversation* chat); + void SelectConversation(int32 index); private: - BPopUpMenu* _ConversationPopUp(); - BPopUpMenu* _BlankPopUp(); + BPopUpMenu* _ConversationPopUp(); + BPopUpMenu* _BlankPopUp(); - ConversationAccountItem* _EnsureAccountItem(Conversation* chat); + ConversationAccountItem* + _EnsureAccountItem(Conversation* chat); }; - -#endif // CONVERSATIONLIST_H - +#endif // _CONVERSATION_LIST_H diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index c3deda7..1f16035 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -230,8 +230,10 @@ ConversationView::UpdateUserList(UserMap users) fUserList->MakeEmpty(); for (int i = 0; i < users.CountItems(); i++) { User* user = users.ValueAt(i); - if (fUserList->HasItem(user->GetListItem()) == false) + if (fUserList->HasItem(user->GetListItem()) == false) { fUserList->AddItem(user->GetListItem()); + fUserList->Sort(); + } } } diff --git a/application/views/UserListView.cpp b/application/views/UserListView.cpp index 89408bd..72ebe8a 100644 --- a/application/views/UserListView.cpp +++ b/application/views/UserListView.cpp @@ -27,6 +27,16 @@ #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) : BListView(name), fChat(NULL) @@ -52,6 +62,13 @@ UserListView::MouseDown(BPoint where) } +void +UserListView::Sort() +{ + SortItems(compare_by_name); +} + + BPopUpMenu* UserListView::_UserPopUp() { diff --git a/application/views/UserListView.h b/application/views/UserListView.h index e27e4c5..f3810a3 100644 --- a/application/views/UserListView.h +++ b/application/views/UserListView.h @@ -27,23 +27,23 @@ enum class UserListView : public BListView { 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: - BPopUpMenu* _UserPopUp(); - BPopUpMenu* _BlankPopUp(); + BPopUpMenu* _UserPopUp(); + BPopUpMenu* _BlankPopUp(); - void _ModerationAction(int32 im_what); - void _ProcessItem(BMessage* itemMsg, BPopUpMenu* menu, Role* user, - Role* target, BString target_id); + void _ModerationAction(int32 im_what); + void _ProcessItem(BMessage* itemMsg, BPopUpMenu* menu, + Role* user, Role* target, BString target_id); Conversation* fChat; }; - #endif // CONVERSATIONLIST_H - diff --git a/application/windows/MainWindow.cpp b/application/windows/MainWindow.cpp index e2efaf8..6ac3e83 100644 --- a/application/windows/MainWindow.cpp +++ b/application/windows/MainWindow.cpp @@ -396,6 +396,13 @@ MainWindow::RemoveConversation(Conversation* chat) } +void +MainWindow::SortConversation(Conversation* chat) +{ + fListView->SortConversation(chat); +} + + void MainWindow::_InitInterface() { diff --git a/application/windows/MainWindow.h b/application/windows/MainWindow.h index e7b37fe..fae141a 100644 --- a/application/windows/MainWindow.h +++ b/application/windows/MainWindow.h @@ -45,6 +45,7 @@ public: void SetConversation(Conversation* chat); void RemoveConversation(Conversation* chat); + void SortConversation(Conversation* chat); Server* GetServer() const { return fServer; }