Refactor and clean up spaces

This commit is contained in:
Tobias Fella
2022-09-23 00:49:33 +02:00
parent 932c3e10fe
commit eee96bc462
6 changed files with 31 additions and 31 deletions

View File

@@ -42,16 +42,6 @@ Kirigami.ScrollablePage {
}
}
Connections {
target: SpaceHierarchyCache
function onSpaceHierarchyChanged() {
if (spaceList.activeSpaceId !== '') {
sortFilterRoomListModel.activeSpaceRooms = SpaceHierarchyCache.getRoomListForSpace(spaceList.activeSpaceId, false);
}
}
}
header: QQC2.Control {
contentItem: QQC2.RoundButton {
id: homeButton
@@ -62,7 +52,7 @@ Kirigami.ScrollablePage {
display: QQC2.AbstractButton.IconOnly
onClicked: {
sortFilterRoomListModel.activeSpaceRooms = [];
sortFilterRoomListModel.activeSpaceId = "";
spaceList.activeSpaceId = '';
listView.positionViewAtIndex(0, ListView.Beginning);
}
@@ -82,13 +72,10 @@ Kirigami.ScrollablePage {
implicitHeight: ListView.view.headerItem.implicitHeight
contentItem: Kirigami.Avatar {
id: del
actions.main: Kirigami.Action {
id: enterSpaceAction
onTriggered: {
spaceList.activeSpaceId = id;
sortFilterRoomListModel.activeSpaceRooms = SpaceHierarchyCache.getRoomListForSpace(id, true);
sortFilterRoomListModel.activeSpaceId = id;
}
}

View File

@@ -178,7 +178,6 @@ int main(int argc, char *argv[])
Login *login = new Login();
ChatBoxHelper chatBoxHelper;
UrlHelper urlHelper;
SpaceHierarchyCache spaceHierarchyCache;
#ifdef HAVE_COLORSCHEME
ColorSchemer colorScheme;
@@ -199,7 +198,7 @@ int main(int argc, char *argv[])
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "EmojiModel", new EmojiModel(&app));
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "CommandModel", new CommandModel(&app));
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "AccountRegistry", &Quotient::AccountRegistry::instance());
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "SpaceHierarchyCache", &spaceHierarchyCache);
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "SpaceHierarchyCache", &SpaceHierarchyCache::instance());
qmlRegisterType<ActionsHandler>("org.kde.neochat", 1, 0, "ActionsHandler");
qmlRegisterType<ChatBoxHelper>("org.kde.neochat", 1, 0, "ChatBoxHelper");
qmlRegisterType<ChatDocumentHandler>("org.kde.neochat", 1, 0, "ChatDocumentHandler");

View File

@@ -4,6 +4,7 @@
#include "sortfilterroomlistmodel.h"
#include "roomlistmodel.h"
#include "spacehierarchycache.h"
SortFilterRoomListModel::SortFilterRoomListModel(QObject *parent)
: QSortFilterProxyModel(parent)
@@ -81,18 +82,23 @@ bool SortFilterRoomListModel::filterAcceptsRow(int source_row, const QModelIndex
&& sourceModel()->data(sourceModel()->index(source_row, 0), RoomListModel::JoinStateRole).toString() != "upgraded"
&& sourceModel()->data(sourceModel()->index(source_row, 0), RoomListModel::IsSpaceRole).toBool() == false;
if (m_activeSpaceRooms.empty())
if (m_activeSpaceId.isEmpty()) {
return acceptRoom;
else
return std::find(m_activeSpaceRooms.begin(),
m_activeSpaceRooms.end(),
sourceModel()->data(sourceModel()->index(source_row, 0), RoomListModel::IdRole).toString())
!= m_activeSpaceRooms.end()
} else {
const auto &rooms = SpaceHierarchyCache::instance().getRoomListForSpace(m_activeSpaceId, false);
return std::find(rooms.begin(), rooms.end(), sourceModel()->data(sourceModel()->index(source_row, 0), RoomListModel::IdRole).toString()) != rooms.end()
&& acceptRoom;
}
}
void SortFilterRoomListModel::setActiveSpaceRooms(QVector<QString> activeSpaceRooms)
QString SortFilterRoomListModel::activeSpaceId() const
{
this->m_activeSpaceRooms = activeSpaceRooms;
return m_activeSpaceId;
}
void SortFilterRoomListModel::setActiveSpaceId(const QString &spaceId)
{
m_activeSpaceId = spaceId;
Q_EMIT activeSpaceIdChanged();
invalidate();
}

View File

@@ -11,7 +11,7 @@ class SortFilterRoomListModel : public QSortFilterProxyModel
Q_PROPERTY(RoomSortOrder roomSortOrder READ roomSortOrder WRITE setRoomSortOrder NOTIFY roomSortOrderChanged)
Q_PROPERTY(QString filterText READ filterText READ filterText WRITE setFilterText NOTIFY filterTextChanged)
Q_PROPERTY(QVector<QString> activeSpaceRooms WRITE setActiveSpaceRooms)
Q_PROPERTY(QString activeSpaceId READ activeSpaceId WRITE setActiveSpaceId NOTIFY activeSpaceIdChanged)
public:
enum RoomSortOrder {
@@ -31,7 +31,8 @@ public:
[[nodiscard]] bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
Q_INVOKABLE void setActiveSpaceRooms(QVector<QString> activeSpaceRooms);
QString activeSpaceId() const;
void setActiveSpaceId(const QString &spaceId);
protected:
[[nodiscard]] bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
@@ -39,9 +40,10 @@ protected:
Q_SIGNALS:
void roomSortOrderChanged();
void filterTextChanged();
void activeSpaceIdChanged();
private:
RoomSortOrder m_sortOrder = Categories;
QString m_filterText;
QVector<QString> m_activeSpaceRooms;
QString m_activeSpaceId;
};

View File

@@ -66,7 +66,7 @@ void SpaceHierarchyCache::populateSpaceHierarchy(const QString &spaceId)
#endif
}
QVector<QString> SpaceHierarchyCache::getRoomListForSpace(const QString &spaceId, bool updateCache)
QVector<QString> &SpaceHierarchyCache::getRoomListForSpace(const QString &spaceId, bool updateCache)
{
if (updateCache) {
populateSpaceHierarchy(spaceId);

View File

@@ -13,14 +13,20 @@ class SpaceHierarchyCache : public QObject
Q_OBJECT
public:
explicit SpaceHierarchyCache(QObject *parent = nullptr);
static SpaceHierarchyCache &instance()
{
static SpaceHierarchyCache _instance;
return _instance;
}
[[nodiscard]] Q_INVOKABLE QVector<QString> getRoomListForSpace(const QString &spaceId, bool updateCache);
[[nodiscard]] QVector<QString> &getRoomListForSpace(const QString &spaceId, bool updateCache);
Q_SIGNALS:
void spaceHierarchyChanged();
private:
explicit SpaceHierarchyCache(QObject *parent = nullptr);
QVector<QString> m_activeSpaceRooms;
QHash<QString, QVector<QString>> m_spaceHierarchy;
void cacheSpaceHierarchy();