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_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();
}

View File

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

View File

@ -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()
{

View File

@ -2,8 +2,8 @@
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* 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 <OutlineListView.h>
@ -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

View File

@ -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();
}
}
}

View File

@ -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()
{

View File

@ -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

View File

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

View File

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