From dd298281e269aeb785e23d830defab313149b15f Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Thu, 3 Jun 2021 23:39:50 -0500 Subject: [PATCH] =?UTF-8?q?Changes=20to=20Notifier=E2=86=92Observer=20rela?= =?UTF-8?q?tions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- application/Conversation.cpp | 24 +++++----- application/Conversation.h | 5 +-- application/Server.cpp | 1 - application/User.cpp | 2 +- application/views/ConversationView.cpp | 62 ++++---------------------- application/views/ConversationView.h | 8 ++-- 6 files changed, 28 insertions(+), 74 deletions(-) diff --git a/application/Conversation.cpp b/application/Conversation.cpp index 6744d44..92c8c8e 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -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; } diff --git a/application/Conversation.h b/application/Conversation.h index ec53224..5b7ecaa 100644 --- a/application/Conversation.h +++ b/application/Conversation.h @@ -24,16 +24,15 @@ class Server; typedef KeyMap 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); diff --git a/application/Server.cpp b/application/Server.cpp index a7c9ceb..5e05f90 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -162,7 +162,6 @@ Server::ImMessage(BMessage* msg) BString statusMsg; if (msg->FindString("message", &statusMsg) == B_OK) { user->SetNotifyPersonalStatus(statusMsg); -// contact->GetView()->UpdatePersonalMessage(); } break; } diff --git a/application/User.cpp b/application/User.cpp index c1dfb77..3d7482b 100644 --- a/application/User.cpp +++ b/application/User.cpp @@ -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; diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index f9bb916..56878c0 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -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(); } diff --git a/application/views/ConversationView.h b/application/views/ConversationView.h index 3c25c1d..66d8a44 100644 --- a/application/views/ConversationView.h +++ b/application/views/ConversationView.h @@ -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: