Update join button in room directory after join

This commit is contained in:
Nicolas Werner 2021-08-17 14:21:04 +02:00
parent b01496f9b4
commit 028bcd5b7c
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
4 changed files with 28 additions and 10 deletions

View file

@ -132,9 +132,8 @@ ApplicationWindow {
Button { Button {
id: joinRoomButton id: joinRoomButton
visible: publicRooms.canJoinRoom(model.roomid) visible: model.canJoin
anchors.centerIn: parent anchors.centerIn: parent
width: Math.ceil(0.1 * roomDirectoryWindow.width)
text: "Join" text: "Join"
onClicked: publicRooms.joinRoom(model.index) onClicked: publicRooms.joinRoom(model.index)
} }

View file

@ -8,10 +8,23 @@
#include <algorithm> #include <algorithm>
RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &s) RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &server)
: QAbstractListModel(parent) : QAbstractListModel(parent)
, server_(s) , server_(server)
{ {
connect(ChatPage::instance(), &ChatPage::newRoom, this, [this](const QString &roomid) {
auto roomid_ = roomid.toStdString();
int i = 0;
for (const auto &room : publicRoomsData_) {
if (room.room_id == roomid_) {
emit dataChanged(index(i), index(i), {Roles::CanJoin});
break;
}
i++;
}
});
connect(this, connect(this,
&RoomDirectoryModel::fetchedRoomsBatch, &RoomDirectoryModel::fetchedRoomsBatch,
this, this,
@ -29,6 +42,7 @@ RoomDirectoryModel::roleNames() const
{Roles::Topic, "topic"}, {Roles::Topic, "topic"},
{Roles::MemberCount, "numMembers"}, {Roles::MemberCount, "numMembers"},
{Roles::Previewable, "canPreview"}, {Roles::Previewable, "canPreview"},
{Roles::CanJoin, "canJoin"},
}; };
} }
@ -67,10 +81,9 @@ RoomDirectoryModel::setSearchTerm(const QString &f)
} }
bool bool
RoomDirectoryModel::canJoinRoom(const QByteArray &room) RoomDirectoryModel::canJoinRoom(const QString &room) const
{ {
const QString room_id(room); return !room.isEmpty() && cache::getRoomInfo({room.toStdString()}).empty();
return !room_id.isEmpty() && !cache::getRoomInfo({room_id.toStdString()}).count(room_id);
} }
std::vector<std::string> std::vector<std::string>
@ -116,6 +129,8 @@ RoomDirectoryModel::data(const QModelIndex &index, int role) const
return QVariant::fromValue(room_chunk.num_joined_members); return QVariant::fromValue(room_chunk.num_joined_members);
case Roles::Previewable: case Roles::Previewable:
return QVariant::fromValue(room_chunk.world_readable); return QVariant::fromValue(room_chunk.world_readable);
case Roles::CanJoin:
return canJoinRoom(QString::fromStdString(room_chunk.room_id));
} }
} }
return {}; return {};

View file

@ -32,7 +32,7 @@ class RoomDirectoryModel : public QAbstractListModel
reachedEndOfPaginationChanged) reachedEndOfPaginationChanged)
public: public:
explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &s = ""); explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &server = "");
enum Roles enum Roles
{ {
@ -41,7 +41,8 @@ public:
AvatarUrl, AvatarUrl,
Topic, Topic,
MemberCount, MemberCount,
Previewable Previewable,
CanJoin,
}; };
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
@ -61,7 +62,6 @@ public:
void fetchMore(const QModelIndex &) override; void fetchMore(const QModelIndex &) override;
Q_INVOKABLE bool canJoinRoom(const QByteArray &room);
Q_INVOKABLE void joinRoom(const int &index = -1); Q_INVOKABLE void joinRoom(const int &index = -1);
signals: signals:
@ -80,6 +80,8 @@ private slots:
const std::string &next_batch); const std::string &next_batch);
private: private:
bool canJoinRoom(const QString &room) const;
static constexpr size_t limit_ = 50; static constexpr size_t limit_ = 50;
std::string server_; std::string server_;

View file

@ -379,6 +379,8 @@ RoomlistModel::addRoom(const QString &room_id, bool suppressInsertNotification)
if (!suppressInsertNotification && if (!suppressInsertNotification &&
((!wasInvite && !wasPreview) || !previewedRooms.empty())) ((!wasInvite && !wasPreview) || !previewedRooms.empty()))
endInsertRows(); endInsertRows();
emit ChatPage::instance()->newRoom(room_id);
} }
} }