Improve public room list and add search functionalities.

This commit is contained in:
Black Hat
2019-12-25 15:38:26 +08:00
parent e5032c686a
commit 273e993bdb
5 changed files with 263 additions and 55 deletions

View File

@@ -1,7 +1,5 @@
#include "publicroomlistmodel.h"
#include "csapi/list_public_rooms.h"
PublicRoomListModel::PublicRoomListModel(QObject* parent)
: QAbstractListModel(parent) {}
@@ -14,6 +12,7 @@ void PublicRoomListModel::setConnection(Connection* conn) {
nextBatch = "";
attempted = false;
rooms.clear();
m_server.clear();
if (m_connection) {
m_connection->disconnect(this);
@@ -23,33 +22,109 @@ void PublicRoomListModel::setConnection(Connection* conn) {
m_connection = conn;
if (job) {
job->abandon();
job = nullptr;
}
if (m_connection) {
next();
}
emit connectionChanged();
emit serverChanged();
emit hasMoreChanged();
}
void PublicRoomListModel::setServer(const QString& value) {
if (m_server == value)
return;
m_server = value;
beginResetModel();
nextBatch = "";
attempted = false;
rooms.clear();
endResetModel();
if (job) {
job->abandon();
job = nullptr;
}
if (m_connection) {
next();
}
emit serverChanged();
emit hasMoreChanged();
}
void PublicRoomListModel::setKeyword(const QString& value) {
if (m_keyword == value)
return;
m_keyword = value;
beginResetModel();
nextBatch = "";
attempted = false;
rooms.clear();
endResetModel();
if (job) {
job->abandon();
job = nullptr;
}
if (m_connection) {
next();
}
emit keywordChanged();
emit hasMoreChanged();
}
void PublicRoomListModel::next(int count) {
if (count < 1)
return;
if (attempted && nextBatch.isEmpty())
if (job) {
qDebug() << "PublicRoomListModel: Other jobs running, ignore";
return;
}
if (!hasMore())
return;
auto job = m_connection->callApi<GetPublicRoomsJob>(count, nextBatch);
job = m_connection->callApi<QueryPublicRoomsJob>(
m_server, count, nextBatch, QueryPublicRoomsJob::Filter{m_keyword});
connect(job, &BaseJob::success, this, [=] {
auto resp = job->data();
nextBatch = resp.nextBatch;
connect(job, &BaseJob::finished, this, [=] {
attempted = true;
this->beginInsertRows({}, rooms.count(),
rooms.count() + resp.chunk.count());
rooms.append(resp.chunk);
this->endInsertRows();
if (job->status() == BaseJob::Success) {
auto resp = job->data();
nextBatch = resp.nextBatch;
this->beginInsertRows({}, rooms.count(),
rooms.count() + resp.chunk.count() - 1);
rooms.append(resp.chunk);
this->endInsertRows();
if (resp.nextBatch.isEmpty()) {
emit hasMoreChanged();
}
}
this->job = nullptr;
});
connect(job, &BaseJob::finished, this, [=] { attempted = true; });
}
QVariant PublicRoomListModel::data(const QModelIndex& index, int role) const {
@@ -63,7 +138,31 @@ QVariant PublicRoomListModel::data(const QModelIndex& index, int role) const {
}
auto room = rooms.at(index.row());
if (role == NameRole) {
return room.name;
auto displayName = room.name;
if (!displayName.isEmpty()) {
return displayName;
}
displayName = room.canonicalAlias;
if (!displayName.isEmpty()) {
return displayName;
}
displayName = room.aliases.front();
if (!displayName.isEmpty()) {
return displayName;
}
return room.roomId;
}
if (role == AvatarRole) {
auto avatarUrl = room.avatarUrl;
if (avatarUrl.isEmpty()) {
return "";
}
return avatarUrl.remove(0, 6);
}
if (role == TopicRole) {
return room.topic;
@@ -76,6 +175,7 @@ QHash<int, QByteArray> PublicRoomListModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[AvatarRole] = "avatar";
roles[TopicRole] = "topic";
return roles;
@@ -87,3 +187,7 @@ int PublicRoomListModel::rowCount(const QModelIndex& parent) const {
return rooms.count();
}
bool PublicRoomListModel::hasMore() const {
return !(attempted && nextBatch.isEmpty());
}

View File

@@ -1,20 +1,26 @@
#ifndef PUBLICROOMLISTMODEL_H
#define PUBLICROOMLISTMODEL_H
#include <QObject>
#include <QAbstractListModel>
#include <QObject>
#include "connection.h"
#include "csapi/definitions/public_rooms_response.h"
#include "csapi/list_public_rooms.h"
using namespace Quotient;
class PublicRoomListModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(Connection* connection READ connection WRITE setConnection NOTIFY connectionChanged)
Q_PROPERTY(Connection* connection READ connection WRITE setConnection NOTIFY
connectionChanged)
Q_PROPERTY(QString server READ server WRITE setServer NOTIFY serverChanged)
Q_PROPERTY(
QString keyword READ keyword WRITE setKeyword NOTIFY keywordChanged)
Q_PROPERTY(bool hasMore READ hasMore NOTIFY hasMoreChanged)
public:
enum EventRoles { NameRole = Qt::DisplayRole + 1, TopicRole };
enum EventRoles { NameRole = Qt::DisplayRole + 1, AvatarRole, TopicRole };
PublicRoomListModel(QObject* parent = nullptr);
@@ -26,18 +32,33 @@ class PublicRoomListModel : public QAbstractListModel {
Connection* connection() const { return m_connection; }
void setConnection(Connection* value);
QString server() const { return m_server; }
void setServer(const QString& value);
QString keyword() const { return m_keyword; }
void setKeyword(const QString& value);
bool hasMore() const;
Q_INVOKABLE void next(int count = 50);
private:
Connection* m_connection = nullptr;
QString m_server;
QString m_keyword;
bool attempted = false;
QString nextBatch;
QVector<PublicRoomsChunk> rooms;
signals:
QueryPublicRoomsJob* job = nullptr;
signals:
void connectionChanged();
void serverChanged();
void keywordChanged();
void hasMoreChanged();
};
#endif // PUBLICROOMLISTMODEL_H
#endif // PUBLICROOMLISTMODEL_H