Make User/ConversationInfoWindow Observers

Now they will observe changes in their corresponding object (if the
room's name changes, so will the label in the ConversationInfoWindow,
etc).
This commit is contained in:
Jaidyn Ann 2021-08-04 13:51:31 -05:00
parent 7c2362a851
commit c7593fa7dd
7 changed files with 140 additions and 20 deletions

View File

@ -293,11 +293,12 @@ Conversation::SetNotifySubject(const char* subject)
bool bool
Conversation::SetIconBitmap(BBitmap* icon) Conversation::SetNotifyIconBitmap(BBitmap* icon)
{ {
if (icon != NULL) { if (icon != NULL) {
fIcon = icon; fIcon = icon;
GetView()->UpdateIcon(); GetView()->UpdateIcon();
NotifyPointer(PTR_ROOM_BITMAP, (void*)icon);
return true; return true;
} }
return false; return false;
@ -434,7 +435,9 @@ Conversation::RemoveUser(User* user)
user->UnregisterObserver(this); user->UnregisterObserver(this);
GetView()->UpdateUserList(fUsers); GetView()->UpdateUserList(fUsers);
_SortConversationList(); _SortConversationList();
_UpdateIcon(); _UpdateIcon();
NotifyInteger(INT_ROOM_MEMBERS, fUsers.CountItems());
} }
@ -594,6 +597,7 @@ Conversation::_EnsureUser(BMessage* msg)
user = serverUser; user = serverUser;
GetView()->UpdateUserList(fUsers); GetView()->UpdateUserList(fUsers);
_UpdateIcon(user); _UpdateIcon(user);
NotifyInteger(INT_ROOM_MEMBERS, fUsers.CountItems());
} }
// Not anywhere; create user // Not anywhere; create user
else if (user == NULL) { else if (user == NULL) {
@ -604,6 +608,7 @@ Conversation::_EnsureUser(BMessage* msg)
fUsers.AddItem(id, user); fUsers.AddItem(id, user);
GetView()->UpdateUserList(fUsers); GetView()->UpdateUserList(fUsers);
_UpdateIcon(user); _UpdateIcon(user);
NotifyInteger(INT_ROOM_MEMBERS, fUsers.CountItems());
} }
if (name.IsEmpty() == false) { if (name.IsEmpty() == false) {
@ -642,7 +647,7 @@ Conversation::_UpdateIcon(User* user)
if (user != NULL && fUsers.CountItems() == 2 if (user != NULL && fUsers.CountItems() == 2
&& user->GetId() != GetOwnContact()->GetId() && user->GetId() != GetOwnContact()->GetId()
&& _IsDefaultIcon(user->AvatarBitmap()) == false) { && _IsDefaultIcon(user->AvatarBitmap()) == false) {
fUserIcon = SetIconBitmap(user->AvatarBitmap()); fUserIcon = SetNotifyIconBitmap(user->AvatarBitmap());
return; return;
} }
@ -650,19 +655,19 @@ Conversation::_UpdateIcon(User* user)
{ {
case 0: case 0:
case 1: case 1:
SetIconBitmap(ImageCache::Get()->GetImage("kOnePersonIcon")); SetNotifyIconBitmap(ImageCache::Get()->GetImage("kOnePersonIcon"));
break; break;
case 2: case 2:
SetIconBitmap(ImageCache::Get()->GetImage("kTwoPeopleIcon")); SetNotifyIconBitmap(ImageCache::Get()->GetImage("kTwoPeopleIcon"));
break; break;
case 3: case 3:
SetIconBitmap(ImageCache::Get()->GetImage("kThreePeopleIcon")); SetNotifyIconBitmap(ImageCache::Get()->GetImage("kThreePeopleIcon"));
break; break;
case 4: case 4:
SetIconBitmap(ImageCache::Get()->GetImage("kFourPeopleIcon")); SetNotifyIconBitmap(ImageCache::Get()->GetImage("kFourPeopleIcon"));
break; break;
default: default:
SetIconBitmap(ImageCache::Get()->GetImage("kMorePeopleIcon")); SetNotifyIconBitmap(ImageCache::Get()->GetImage("kMorePeopleIcon"));
break; break;
} }
fUserIcon = false; fUserIcon = false;

View File

@ -43,8 +43,7 @@ public:
void SetNotifyName(const char* name); void SetNotifyName(const char* name);
void SetNotifySubject(const char* subject); void SetNotifySubject(const char* subject);
bool SetNotifyIconBitmap(BBitmap* icon);
bool SetIconBitmap(BBitmap* icon);
BMessenger Messenger() const; BMessenger Messenger() const;
void SetMessenger(BMessenger messenger); void SetMessenger(BMessenger messenger);

View File

@ -12,10 +12,12 @@ enum {
STR_ROOM_SUBJECT, STR_ROOM_SUBJECT,
PTR_AVATAR_BITMAP, PTR_AVATAR_BITMAP,
PTR_ROOM_BITMAP,
INT_ACCOUNT_STATUS, INT_ACCOUNT_STATUS,
INT_CONTACT_STATUS, INT_CONTACT_STATUS,
INT_USER_PERMS, INT_USER_PERMS,
INT_ROOM_MEMBERS,
INT_NEW_MESSAGE, INT_NEW_MESSAGE,
INT_NEW_MENTION, INT_NEW_MENTION,

View File

@ -15,6 +15,8 @@
#include <StringView.h> #include <StringView.h>
#include <TextView.h> #include <TextView.h>
#include "NotifyMessage.h"
#undef B_TRANSLATION_CONTEXT #undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "ConversationInfoWindow" #define B_TRANSLATION_CONTEXT "ConversationInfoWindow"
@ -23,8 +25,8 @@
ConversationInfoWindow::ConversationInfoWindow(Conversation* chat) ConversationInfoWindow::ConversationInfoWindow(Conversation* chat)
: :
BWindow(BRect(200, 200, 300, 400), BWindow(BRect(200, 200, 300, 400),
B_TRANSLATE("Room information"), B_FLOATING_WINDOW, B_TRANSLATE("Room information"), B_FLOATING_WINDOW_LOOK,
B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS), B_NORMAL_WINDOW_FEEL, B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
fChat(chat) fChat(chat)
{ {
_InitInterface(); _InitInterface();
@ -40,6 +42,45 @@ ConversationInfoWindow::~ConversationInfoWindow()
} }
void
ConversationInfoWindow::ObserveString(int32 what, BString string)
{
Lock();
switch (what) {
case STR_ROOM_NAME:
fNameLabel->SetText(string);
break;
}
Unlock();
}
void
ConversationInfoWindow::ObserveInteger(int32 what, int32 num)
{
Lock();
switch (what) {
case INT_ROOM_MEMBERS:
_SetUserCountLabel(num);
break;
}
Unlock();
}
void
ConversationInfoWindow::ObservePointer(int32 what, void* ptr)
{
Lock();
switch (what) {
case PTR_ROOM_BITMAP:
fIcon->SetBitmap((BBitmap*)ptr);
break;
}
Unlock();
}
void void
ConversationInfoWindow::_InitInterface() ConversationInfoWindow::_InitInterface()
{ {

View File

@ -22,7 +22,10 @@ public:
ConversationInfoWindow(Conversation* chat); ConversationInfoWindow(Conversation* chat);
~ConversationInfoWindow(); ~ConversationInfoWindow();
// virtual void Observer virtual void ObserveString(int32 what, BString string);
virtual void ObserveInteger(int32 what, int32 num);
virtual void ObservePointer(int32 what, void* ptr);
private: private:
void _InitInterface(); void _InitInterface();

View File

@ -18,6 +18,7 @@
#include <libinterface/BitmapView.h> #include <libinterface/BitmapView.h>
#include "ImageCache.h" #include "ImageCache.h"
#include "NotifyMessage.h"
#include "User.h" #include "User.h"
#include "Utils.h" #include "Utils.h"
@ -29,12 +30,20 @@
UserInfoWindow::UserInfoWindow(User* user) UserInfoWindow::UserInfoWindow(User* user)
: :
BWindow(BRect(200, 200, 300, 400), BWindow(BRect(200, 200, 300, 400),
B_TRANSLATE("User information"), B_FLOATING_WINDOW, B_TRANSLATE("User information"), B_FLOATING_WINDOW_LOOK,
B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS), B_NORMAL_WINDOW_FEEL, B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
fUser(user) fUser(user)
{ {
_InitInterface(); _InitInterface();
MoveTo(BAlert::AlertPosition(Bounds().Width(), Bounds().Height() / 2)); MoveTo(BAlert::AlertPosition(Bounds().Width(), Bounds().Height() / 2));
fUser->RegisterObserver(this);
}
UserInfoWindow::~UserInfoWindow()
{
fUser->UnregisterObserver(this);
} }
@ -49,25 +58,66 @@ UserInfoWindow::MessageReceived(BMessage* message)
} }
void
UserInfoWindow::ObserveString(int32 what, BString string)
{
Lock();
switch (what) {
case STR_CONTACT_NAME:
fNameLabel->SetText(string);
break;
case STR_PERSONAL_STATUS:
fTextStatusLabel->SetText(string);
break;
}
Unlock();
}
void
UserInfoWindow::ObserveInteger(int32 what, int32 num)
{
Lock();
switch (what) {
case INT_CONTACT_STATUS:
_UpdateStatusViews((UserStatus)num);
break;
}
Unlock();
}
void
UserInfoWindow::ObservePointer(int32 what, void* ptr)
{
Lock();
switch (what) {
case PTR_AVATAR_BITMAP:
fAvatar->SetBitmap((BBitmap*)ptr);
break;
}
Unlock();
}
void void
UserInfoWindow::_InitInterface() UserInfoWindow::_InitInterface()
{ {
fNameLabel = new BStringView("nameLabel", fUser->GetName().String()); fNameLabel = new BStringView("nameLabel", fUser->GetName().String());
fNameLabel->SetFont(be_bold_font); fNameLabel->SetFont(be_bold_font);
UserStatus status = fUser->GetNotifyStatus(); fStatusLabel = new BStringView("statusLabel", "");
fStatusLabel = new BStringView("statusLabel", UserStatusToString(status));
float iconSize = be_plain_font->Size() + 5; float iconSize = be_plain_font->Size() + 5;
BBitmap* statusBitmap =
ImageCache::Get()->GetImage(UserStatusToImageKey(status));
fStatusIcon = new BitmapView("statusIcon"); fStatusIcon = new BitmapView("statusIcon");
fStatusIcon->SetExplicitMaxSize(BSize(iconSize, iconSize)); fStatusIcon->SetExplicitMaxSize(BSize(iconSize, iconSize));
fStatusIcon->SetBitmap(statusBitmap);
fTextStatusLabel = new BStringView("statusMessageLabel", fTextStatusLabel = new BStringView("statusMessageLabel",
fUser->GetNotifyPersonalStatus()); fUser->GetNotifyPersonalStatus());
_UpdateStatusViews(fUser->GetNotifyStatus());
const char* userId = fUser->GetId().String(); const char* userId = fUser->GetId().String();
fIdLabel = new BTextView("idLabel"); fIdLabel = new BTextView("idLabel");
fIdLabel->SetText(userId); fIdLabel->SetText(userId);
@ -109,3 +159,14 @@ UserInfoWindow::_InitInterface()
.End() .End()
.End(); .End();
} }
void
UserInfoWindow::_UpdateStatusViews(UserStatus status)
{
fStatusLabel->SetText(UserStatusToString(status));
BBitmap* statusBitmap =
ImageCache::Get()->GetImage(UserStatusToImageKey(status));
fStatusIcon->SetBitmap(statusBitmap);
}

View File

@ -13,6 +13,8 @@
#include <Window.h> #include <Window.h>
#include <TextView.h> #include <TextView.h>
#include <StringView.h> #include <StringView.h>
#include "AppConstants.h"
#include "Observer.h" #include "Observer.h"
class BitmapView; class BitmapView;
@ -22,12 +24,19 @@ class User;
class UserInfoWindow: public BWindow, public Observer { class UserInfoWindow: public BWindow, public Observer {
public: public:
UserInfoWindow(User* user); UserInfoWindow(User* user);
~UserInfoWindow();
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
virtual void ObserveString(int32 what, BString string);
virtual void ObserveInteger(int32 what, int32 num);
virtual void ObservePointer(int32 what, void* ptr);
private: private:
void _InitInterface(); void _InitInterface();
void _UpdateStatusViews(UserStatus status);
User* fUser; User* fUser;
BitmapView* fAvatar; BitmapView* fAvatar;