Changes to Notifier→Observer relations

The new Notifier→Observer relation chain:
	* Conversation → ConversationItem, ConversationView
	* User → UserListItem, UserInfoWindow, UserPopUp
	* Contact → RosterItem

These line up pretty intuitively― if something changes in the conversation, you
want the list item and view to change too.

But there's one more here that's less intuitive:
	* User → Conversation

If Conversation observes something from a user (i.e., status change), it
immediately knows to do one thing only: invalidate the user list, because
something's changed.
This commit is contained in:
Jaidyn Ann 2021-06-03 23:39:50 -05:00
parent ced04e613a
commit dd298281e2
6 changed files with 28 additions and 74 deletions

View File

@ -32,6 +32,7 @@ Conversation::Conversation(BString id, BMessenger msgn)
fDateFormatter()
{
fConversationItem = new ConversationItem(fName.String(), this);
RegisterObserver(fConversationItem);
}
@ -77,26 +78,24 @@ Conversation::ImMessage(BMessage* msg)
void
Conversation::ObserveString(int32 what, BString str)
{
if (fChatView != NULL)
fChatView->ObserveString(what, str);
GetView()->InvalidateUserList();
}
void
Conversation::ObserveInteger(int32 what, int32 value)
{
GetView()->InvalidateUserList();
}
void
Conversation::ObservePointer(int32 what, void* ptr)
{
if (fChatView != NULL)
fChatView->ObservePointer(what, ptr);
GetView()->InvalidateUserList();
}
void
Conversation::ObserveInteger(int32 what, int32 val)
{
if (fChatView != NULL)
fChatView->ObserveInteger(what, val);
}
BMessenger
Conversation::Messenger() const
@ -162,6 +161,7 @@ void
Conversation::RemoveUser(User* user)
{
fUsers.RemoveItemFor(user->GetId());
user->UnregisterObserver(this);
GetView()->UpdateUserList(fUsers);
}
@ -190,6 +190,7 @@ Conversation::GetView()
logMsg.AddStrings("body", logs);
fChatView->MessageReceived(&logMsg);
RegisterObserver(fChatView);
return fChatView;
}
@ -290,6 +291,7 @@ Conversation::_EnsureUser(BMessage* msg)
fUsers.AddItem(id, user);
GetView()->UpdateUserList(fUsers);
}
user->RegisterObserver(this);
return user;
}

View File

@ -24,16 +24,15 @@ class Server;
typedef KeyMap<BString, User*> UserMap;
class Conversation : public Observer {
class Conversation : public Notifier, public Observer {
public:
Conversation(BString id, BMessenger msgn);
BString GetId() const;
// Handles required state changes from an IM message; forwards to ChatWindow
void ImMessage(BMessage* msg);
// Observer inherits; just forwards to ChatWindow
// Tell the ConversationView to invalidate user list
void ObserveString(int32 what, BString str);
void ObserveInteger(int32 what, int32 value);
void ObservePointer(int32 what, void* ptr);

View File

@ -162,7 +162,6 @@ Server::ImMessage(BMessage* msg)
BString statusMsg;
if (msg->FindString("message", &statusMsg) == B_OK) {
user->SetNotifyPersonalStatus(statusMsg);
// contact->GetView()->UpdatePersonalMessage();
}
break;
}

View File

@ -143,7 +143,7 @@ UserItem*
User::GetListItem()
{
if (fListItem == NULL) {
fListItem = new UserItem(fName, this);
fListItem = new UserItem(fName, this, (int32)fStatus);
RegisterObserver(fListItem);
}
return fListItem;

View File

@ -206,75 +206,29 @@ ConversationView::UpdateUserList(UserMap users)
}
void
ConversationView::InvalidateUserList()
{
for (int i = 0; i < fUserList->CountItems(); i++)
fUserList->InvalidateItem(i);
}
void
ConversationView::ObserveString(int32 what, BString str)
{
// switch (what) {
// case STR_CONTACT_NAME:
// if (Lock()) {
// SetTitle(str);
// Unlock();
// }
// break;
// case STR_PERSONAL_STATUS:
// break;
// }
}
void
ConversationView::ObservePointer(int32 what, void* ptr)
{
// switch (what) {
// case PTR_AVATAR_BITMAP:
// break;
// }
}
void
ConversationView::ObserveInteger(int32 what, int32 val)
{
switch (what) {
case INT_CONTACT_STATUS:
if (fUserList->CountItems() <= 2)
AppendStatus((CayaStatus)val);
break;
}
}
void
ConversationView::AppendStatus(CayaStatus status)
{
BString message(fContact->GetName());
switch (status) {
case CAYA_ONLINE:
message << " is available";
break;
case CAYA_AWAY:
message << " is away";
break;
case CAYA_DO_NOT_DISTURB:
message << " is busy, please do not disturb!";
break;
case CAYA_CUSTOM_STATUS:
message << " has set a custom status.";
break;
case CAYA_INVISIBLE:
message << " is invisible.";
break;
case CAYA_OFFLINE:
message << " is offline";
break;
default:
break;
}
fReceiveView->Append(message.String(), COL_TEXT, COL_TEXT, R_TEXT);
fReceiveView->Append("\n", COL_TEXT, COL_TEXT, R_TEXT);
fReceiveView->ScrollToSelection();
}

View File

@ -10,6 +10,7 @@
#include "CayaConstants.h"
#include "Conversation.h"
#include "Observer.h"
class BStringView;
class BTextView;
@ -20,7 +21,7 @@ class User;
class UserListView;
class ConversationView : public BGroupView {
class ConversationView : public BGroupView, public Observer {
public:
ConversationView();
ConversationView(Conversation* chat);
@ -34,17 +35,16 @@ public:
Conversation* GetConversation();
void SetConversation(Conversation* chat);
void UpdateAvatar();
void UpdatePersonalMessage();
void UpdateUserList(UserMap users);
void InvalidateUserList();
void ObserveString(int32 what, BString str);
void ObservePointer(int32 what, void* ptr);
void ObserveInteger(int32 what, int32 val);
void AppendStatus(CayaStatus status);
void AvoidFocus(bool avoid);
private: