Don't assume there is an active connection when loading devices list

Instead listen to Controller::activeConnectionChanged and reset model
when the active connection is changed.

Fix #413
This commit is contained in:
Carl Schwan
2021-06-30 11:23:00 +02:00
parent dd841bb836
commit 54f5097ff4
2 changed files with 22 additions and 9 deletions

View File

@@ -10,18 +10,30 @@
DevicesModel::DevicesModel(QObject *parent) DevicesModel::DevicesModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
{ {
GetDevicesJob *job = Controller::instance().activeConnection()->callApi<GetDevicesJob>(); connect(&Controller::instance(), &Controller::activeConnectionChanged,
connect(job, &BaseJob::success, this, [this, job]() { this, &DevicesModel::fetchDevices);
beginResetModel();
m_devices = job->devices(); fetchDevices();
endResetModel(); }
});
void DevicesModel::fetchDevices()
{
if (Controller::instance().activeConnection()) {
auto job = Controller::instance().activeConnection()->callApi<GetDevicesJob>();
connect(job, &BaseJob::success, this, [this, job]() {
beginResetModel();
m_devices = job->devices();
endResetModel();
});
}
} }
QVariant DevicesModel::data(const QModelIndex &index, int role) const QVariant DevicesModel::data(const QModelIndex &index, int role) const
{ {
if (index.row() < 0 || index.row() >= rowCount(QModelIndex())) if (index.row() < 0 || index.row() >= rowCount(QModelIndex())) {
return QVariant(); return {};
}
switch (role) { switch (role) {
case Id: case Id:
return m_devices[index.row()].deviceId; return m_devices[index.row()].deviceId;
@@ -33,7 +45,7 @@ QVariant DevicesModel::data(const QModelIndex &index, int role) const
if (m_devices[index.row()].lastSeenTs) if (m_devices[index.row()].lastSeenTs)
return *m_devices[index.row()].lastSeenTs; return *m_devices[index.row()].lastSeenTs;
} }
return QVariant(); return {};
} }
int DevicesModel::rowCount(const QModelIndex &parent) const int DevicesModel::rowCount(const QModelIndex &parent) const

View File

@@ -32,5 +32,6 @@ public:
Q_INVOKABLE void setName(int index, const QString &name); Q_INVOKABLE void setName(int index, const QString &name);
private: private:
void fetchDevices();
QVector<Quotient::Device> m_devices; QVector<Quotient::Device> m_devices;
}; };