Fix space tree refresh

Stop space hierarchy being duplicated. This is done by making sure old jobs are cleared at all resets and to make doubly sure when a child is inserted it overrides itself so there can never be duplicates
This commit is contained in:
James Graham
2024-03-08 18:36:43 +00:00
parent 4d62ad1938
commit 2a6e63595e
3 changed files with 32 additions and 14 deletions

View File

@@ -38,13 +38,6 @@ void SpaceChildrenModel::setSpace(NeoChatRoom *space)
m_space = space; m_space = space;
Q_EMIT spaceChanged(); Q_EMIT spaceChanged();
for (auto job : m_currentJobs) {
if (job) {
job->abandon();
}
}
m_currentJobs.clear();
auto connection = m_space->connection(); auto connection = m_space->connection();
connect(connection, &Quotient::Connection::loadedRoomState, this, [this](Quotient::Room *room) { connect(connection, &Quotient::Connection::loadedRoomState, this, [this](Quotient::Room *room) {
if (m_pendingChildren.contains(room->name())) { if (m_pendingChildren.contains(room->name())) {
@@ -66,6 +59,17 @@ bool SpaceChildrenModel::loading() const
void SpaceChildrenModel::refreshModel() void SpaceChildrenModel::refreshModel()
{ {
for (auto job : m_currentJobs) {
if (job) {
job->abandon();
}
}
m_currentJobs.clear();
if (m_space == nullptr) {
return;
}
beginResetModel(); beginResetModel();
m_replacedRooms.clear(); m_replacedRooms.clear();
delete m_rootItem; delete m_rootItem;
@@ -120,8 +124,7 @@ void SpaceChildrenModel::insertChildren(std::vector<Quotient::GetSpaceHierarchyJ
insertChildren(job->rooms(), index(insertRow, 0, parent)); insertChildren(job->rooms(), index(insertRow, 0, parent));
}); });
} }
parentItem->insertChild(insertRow, parentItem->insertChild(new SpaceTreeItem(dynamic_cast<NeoChatConnection *>(m_space->connection()),
new SpaceTreeItem(dynamic_cast<NeoChatConnection *>(m_space->connection()),
parentItem, parentItem,
children[i].roomId, children[i].roomId,
children[i].name, children[i].name,

View File

@@ -37,6 +37,11 @@ SpaceTreeItem::~SpaceTreeItem()
qDeleteAll(m_children); qDeleteAll(m_children);
} }
bool SpaceTreeItem::operator==(const SpaceTreeItem &other) const
{
return m_id == other.id();
}
SpaceTreeItem *SpaceTreeItem::child(int number) SpaceTreeItem *SpaceTreeItem::child(int number)
{ {
if (number < 0 || number >= m_children.size()) { if (number < 0 || number >= m_children.size()) {
@@ -50,12 +55,20 @@ int SpaceTreeItem::childCount() const
return m_children.count(); return m_children.count();
} }
bool SpaceTreeItem::insertChild(int row, SpaceTreeItem *newChild) bool SpaceTreeItem::insertChild(SpaceTreeItem *newChild)
{ {
if (row < 0 || row > m_children.size()) { if (newChild == nullptr) {
return false; return false;
} }
m_children.insert(row, newChild);
for (auto it = m_children.begin(), end = m_children.end(); it != end; ++it) {
if (*it == newChild) {
*it = newChild;
return true;
}
}
m_children.append(newChild);
return true; return true;
} }

View File

@@ -35,6 +35,8 @@ public:
Quotient::StateEvents childStates = {}); Quotient::StateEvents childStates = {});
~SpaceTreeItem(); ~SpaceTreeItem();
bool operator==(const SpaceTreeItem &other) const;
/** /**
* @brief Return the child at the given row number. * @brief Return the child at the given row number.
* *
@@ -48,9 +50,9 @@ public:
int childCount() const; int childCount() const;
/** /**
* @brief Insert the given child at the given row number. * @brief Insert the given child.
*/ */
bool insertChild(int row, SpaceTreeItem *newChild); bool insertChild(SpaceTreeItem *newChild);
/** /**
* @brief Remove the child at the given row number. * @brief Remove the child at the given row number.