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 {
id: joinRoomButton
visible: publicRooms.canJoinRoom(model.roomid)
visible: model.canJoin
anchors.centerIn: parent
width: Math.ceil(0.1 * roomDirectoryWindow.width)
text: "Join"
onClicked: publicRooms.joinRoom(model.index)
}

View file

@ -8,10 +8,23 @@
#include <algorithm>
RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &s)
RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &server)
: 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,
&RoomDirectoryModel::fetchedRoomsBatch,
this,
@ -29,6 +42,7 @@ RoomDirectoryModel::roleNames() const
{Roles::Topic, "topic"},
{Roles::MemberCount, "numMembers"},
{Roles::Previewable, "canPreview"},
{Roles::CanJoin, "canJoin"},
};
}
@ -67,10 +81,9 @@ RoomDirectoryModel::setSearchTerm(const QString &f)
}
bool
RoomDirectoryModel::canJoinRoom(const QByteArray &room)
RoomDirectoryModel::canJoinRoom(const QString &room) const
{
const QString room_id(room);
return !room_id.isEmpty() && !cache::getRoomInfo({room_id.toStdString()}).count(room_id);
return !room.isEmpty() && cache::getRoomInfo({room.toStdString()}).empty();
}
std::vector<std::string>
@ -116,6 +129,8 @@ RoomDirectoryModel::data(const QModelIndex &index, int role) const
return QVariant::fromValue(room_chunk.num_joined_members);
case Roles::Previewable:
return QVariant::fromValue(room_chunk.world_readable);
case Roles::CanJoin:
return canJoinRoom(QString::fromStdString(room_chunk.room_id));
}
}
return {};

View file

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

View file

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