Optimize loading of things
parent
5c63c92064
commit
1ccdda578c
|
|
@ -302,6 +302,7 @@ void ThingManager::getThingsResponse(int /*commandId*/, const QVariantMap ¶m
|
|||
// qCritical() << "Things received:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
||||
if (params.keys().contains("things")) {
|
||||
QVariantList thingsList = params.value("things").toList();
|
||||
QList<Thing*> newThings;
|
||||
foreach (QVariant thingVariant, thingsList) {
|
||||
Thing *thing = unpackThing(this, thingVariant.toMap(), m_thingClasses);
|
||||
if (!thing) {
|
||||
|
|
@ -329,8 +330,9 @@ void ThingManager::getThingsResponse(int /*commandId*/, const QVariantMap ¶m
|
|||
thing->setStateValue(stateTypeId, value);
|
||||
// qDebug() << "Set thing state value:" << thing->stateValue(stateTypeId) << value;
|
||||
}
|
||||
things()->addThing(thing);
|
||||
newThings.append(thing);
|
||||
}
|
||||
things()->addThings(newThings);
|
||||
}
|
||||
qDebug() << "Initializing thing manager took" << m_connectionBenchmark.msecsTo(QDateTime::currentDateTime()) << "ms";
|
||||
m_fetchingData = false;
|
||||
|
|
|
|||
|
|
@ -101,27 +101,39 @@ QVariant Things::data(const QModelIndex &index, int role) const
|
|||
|
||||
void Things::addThing(Thing *thing)
|
||||
{
|
||||
thing->setParent(this);
|
||||
beginInsertRows(QModelIndex(), m_things.count(), m_things.count());
|
||||
m_things.append(thing);
|
||||
addThings({thing});
|
||||
}
|
||||
|
||||
void Things::addThings(const QList<Thing *> things)
|
||||
{
|
||||
if (things.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
beginInsertRows(QModelIndex(), m_things.count(), m_things.count() + things.count() - 1);
|
||||
m_things.append(things);
|
||||
endInsertRows();
|
||||
connect(thing, &Thing::nameChanged, this, [thing, this]() {
|
||||
int idx = m_things.indexOf(thing);
|
||||
if (idx < 0) return;
|
||||
emit dataChanged(index(idx), index(idx), {RoleName});
|
||||
});
|
||||
connect(thing, &Thing::setupStatusChanged, this, [thing, this]() {
|
||||
int idx = m_things.indexOf(thing);
|
||||
if (idx < 0) return;
|
||||
emit dataChanged(index(idx), index(idx), {RoleSetupStatus, RoleSetupDisplayMessage});
|
||||
});
|
||||
connect(thing->states(), &States::dataChanged, this, [thing, this]() {
|
||||
int idx = m_things.indexOf(thing);
|
||||
if (idx < 0) return;
|
||||
emit dataChanged(index(idx), index(idx));
|
||||
});
|
||||
|
||||
foreach (Thing *thing, things) {
|
||||
thing->setParent(this);
|
||||
connect(thing, &Thing::nameChanged, this, [thing, this]() {
|
||||
int idx = m_things.indexOf(thing);
|
||||
if (idx < 0) return;
|
||||
emit dataChanged(index(idx), index(idx), {RoleName});
|
||||
});
|
||||
connect(thing, &Thing::setupStatusChanged, this, [thing, this]() {
|
||||
int idx = m_things.indexOf(thing);
|
||||
if (idx < 0) return;
|
||||
emit dataChanged(index(idx), index(idx), {RoleSetupStatus, RoleSetupDisplayMessage});
|
||||
});
|
||||
connect(thing->states(), &States::dataChanged, this, [thing, this]() {
|
||||
int idx = m_things.indexOf(thing);
|
||||
if (idx < 0) return;
|
||||
emit dataChanged(index(idx), index(idx));
|
||||
});
|
||||
emit thingAdded(thing);
|
||||
}
|
||||
|
||||
emit countChanged();
|
||||
emit thingAdded(thing);
|
||||
}
|
||||
|
||||
void Things::removeThing(Thing *thing)
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ public:
|
|||
QVariant data(const QModelIndex & index, int role = RoleName) const override;
|
||||
|
||||
void addThing(Thing *thing);
|
||||
void addThings(const QList<Thing*> things);
|
||||
void removeThing(Thing *thing);
|
||||
|
||||
void clearModel();
|
||||
|
|
|
|||
Loading…
Reference in New Issue