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

View File

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

View File

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

View File

@ -143,7 +143,7 @@ UserItem*
User::GetListItem() User::GetListItem()
{ {
if (fListItem == NULL) { if (fListItem == NULL) {
fListItem = new UserItem(fName, this); fListItem = new UserItem(fName, this, (int32)fStatus);
RegisterObserver(fListItem); RegisterObserver(fListItem);
} }
return 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 void
ConversationView::ObserveString(int32 what, BString str) ConversationView::ObserveString(int32 what, BString str)
{ {
// switch (what) {
// case STR_CONTACT_NAME:
// if (Lock()) {
// SetTitle(str);
// Unlock();
// }
// break;
// case STR_PERSONAL_STATUS:
// break;
// }
} }
void void
ConversationView::ObservePointer(int32 what, void* ptr) ConversationView::ObservePointer(int32 what, void* ptr)
{ {
// switch (what) {
// case PTR_AVATAR_BITMAP:
// break;
// }
} }
void void
ConversationView::ObserveInteger(int32 what, int32 val) 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 "CayaConstants.h"
#include "Conversation.h" #include "Conversation.h"
#include "Observer.h"
class BStringView; class BStringView;
class BTextView; class BTextView;
@ -20,7 +21,7 @@ class User;
class UserListView; class UserListView;
class ConversationView : public BGroupView { class ConversationView : public BGroupView, public Observer {
public: public:
ConversationView(); ConversationView();
ConversationView(Conversation* chat); ConversationView(Conversation* chat);
@ -34,17 +35,16 @@ public:
Conversation* GetConversation(); Conversation* GetConversation();
void SetConversation(Conversation* chat); void SetConversation(Conversation* chat);
void UpdateAvatar(); void UpdateAvatar();
void UpdatePersonalMessage(); void UpdatePersonalMessage();
void UpdateUserList(UserMap users); void UpdateUserList(UserMap users);
void InvalidateUserList();
void ObserveString(int32 what, BString str); void ObserveString(int32 what, BString str);
void ObservePointer(int32 what, void* ptr); void ObservePointer(int32 what, void* ptr);
void ObserveInteger(int32 what, int32 val); void ObserveInteger(int32 what, int32 val);
void AppendStatus(CayaStatus status);
void AvoidFocus(bool avoid); void AvoidFocus(bool avoid);
private: private: