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)
: QAbstractListModel(parent)
{
GetDevicesJob *job = Controller::instance().activeConnection()->callApi<GetDevicesJob>();
connect(job, &BaseJob::success, this, [this, job]() {
beginResetModel();
m_devices = job->devices();
endResetModel();
});
connect(&Controller::instance(), &Controller::activeConnectionChanged,
this, &DevicesModel::fetchDevices);
fetchDevices();
}
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
{
if (index.row() < 0 || index.row() >= rowCount(QModelIndex()))
return QVariant();
if (index.row() < 0 || index.row() >= rowCount(QModelIndex())) {
return {};
}
switch (role) {
case Id:
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)
return *m_devices[index.row()].lastSeenTs;
}
return QVariant();
return {};
}
int DevicesModel::rowCount(const QModelIndex &parent) const