L'elenco delle macchine si aggiorna davvero (stato, fase, tempo)
Difetto: le scritte leggevano lo stato con funzioni per indice (phaseTextAt, remainTextAt, ...). In QML un binding che chiama una funzione non registra nessuna dipendenza, quindi veniva valutato una volta sola — all'apertura, prima che i dati arrivassero — e non si aggiornava mai: si vedevano i nomi delle macchine ma nessuno stato. Correzione: il client espone l'elenco come DATO (QVariantList di mappe, proprieta' devices con NOTIFY) piu' la mappa active per la copertina; le schermate leggono modelData.*, e ricevono devicesChanged ad ogni aggiornamento di stato. Verificato nel pacchetto: le chiavi nuove sono presenti, le funzioni rimosse non ci sono piu', MainPage legge modelData.
This commit is contained in:
1 parent
e2c6f28613
commit
c6621e5968
8 files changed
+173
-217
No files matched your search
+2
-2
@@ -21,7 +21,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
|
||||
app->setOrganizationName(QStringLiteral("harbour"));
|
||||
app->setApplicationName(QStringLiteral("thinq"));
|
||||
app->setApplicationVersion(QStringLiteral("0.2.0"));
|
||||
app->setApplicationVersion(QStringLiteral("0.2.1"));
|
||||
|
||||
Settings settings;
|
||||
ThinQClient api(&settings);
|
||||
@@ -34,7 +34,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
QQuickView *view = SailfishApp::createView();
|
||||
|
||||
// Prima riga del log: dice se il device sta girando la build nuova o una stale.
|
||||
qDebug() << "harbour-thinq v0.2.0 build" << __DATE__ << __TIME__;
|
||||
qDebug() << "harbour-thinq v0.2.1 build" << __DATE__ << __TIME__;
|
||||
|
||||
view->rootContext()->setContextProperty(QStringLiteral("appSettings"), &settings);
|
||||
view->rootContext()->setContextProperty(QStringLiteral("api"), &api);
|
||||
|
||||
+54
-110
@@ -45,17 +45,63 @@ bool ThinQClient::configured() const
|
||||
return !m_settings->token().isEmpty();
|
||||
}
|
||||
|
||||
int ThinQClient::activeIndex() const
|
||||
QVariantList ThinQClient::devices() const
|
||||
{
|
||||
// Tutto quello che le schermate devono mostrare, gia' pronto. I binding QML
|
||||
// si aggiornano quando questa proprieta' cambia (devicesChanged): con le
|
||||
// funzioni di lettura, invece, non si sarebbero mai aggiornati.
|
||||
QVariantList list;
|
||||
for (int i = 0; i < m_devices.size(); ++i) {
|
||||
const DeviceEntry &e = m_devices.at(i);
|
||||
const QString total = ThinQJson::durationText(e.state.totalMinutes);
|
||||
|
||||
QVariantMap item;
|
||||
item.insert(QStringLiteral("id"), e.id);
|
||||
item.insert(QStringLiteral("label"), e.label.isEmpty()
|
||||
? QCoreApplication::translate("ThinQ", "Washing machine") : e.label);
|
||||
item.insert(QStringLiteral("hasState"), e.hasState);
|
||||
item.insert(QStringLiteral("offline"), e.offline);
|
||||
item.insert(QStringLiteral("stale"), e.stale);
|
||||
item.insert(QStringLiteral("updated"), e.updated);
|
||||
item.insert(QStringLiteral("phase"), e.hasState
|
||||
? ThinQJson::phaseLabel(e.state)
|
||||
: QCoreApplication::translate("ThinQ", "No data"));
|
||||
item.insert(QStringLiteral("level"), ThinQJson::statusLevel(e.state));
|
||||
item.insert(QStringLiteral("remain"), ThinQJson::durationText(e.state.remainMinutes));
|
||||
item.insert(QStringLiteral("totalLine"), total.isEmpty() ? QString()
|
||||
: QCoreApplication::translate("ThinQ", "of %1").arg(total));
|
||||
item.insert(QStringLiteral("cycle"), e.state.cycleCount);
|
||||
item.insert(QStringLiteral("remote"), e.state.remoteEnabled);
|
||||
item.insert(QStringLiteral("action"), e.hasState
|
||||
? ThinQJson::primaryActionLabel(e.state)
|
||||
: QCoreApplication::translate("ThinQ", "Start"));
|
||||
item.insert(QStringLiteral("canCommand"),
|
||||
e.hasState && !e.offline && e.state.remoteEnabled);
|
||||
list << item;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
QVariantMap ThinQClient::active() const
|
||||
{
|
||||
// Per la copertina: la macchina che sta lavorando, altrimenti la prima che
|
||||
// sa dire qualcosa di se'.
|
||||
int chosen = -1;
|
||||
for (int i = 0; i < m_devices.size() && chosen < 0; ++i) {
|
||||
if (ThinQJson::statusLevel(m_devices.at(i).state) == 1)
|
||||
return i;
|
||||
chosen = i;
|
||||
}
|
||||
for (int i = 0; i < m_devices.size(); ++i) {
|
||||
for (int i = 0; i < m_devices.size() && chosen < 0; ++i) {
|
||||
if (m_devices.at(i).hasState)
|
||||
return i;
|
||||
chosen = i;
|
||||
}
|
||||
return m_devices.isEmpty() ? -1 : 0;
|
||||
if (chosen < 0 && !m_devices.isEmpty())
|
||||
chosen = 0;
|
||||
|
||||
const QVariantList list = devices();
|
||||
if (chosen < 0 || chosen >= list.size())
|
||||
return QVariantMap();
|
||||
return list.at(chosen).toMap();
|
||||
}
|
||||
|
||||
bool ThinQClient::validIndex(int index) const
|
||||
@@ -63,110 +109,6 @@ bool ThinQClient::validIndex(int index) const
|
||||
return index >= 0 && index < m_devices.size();
|
||||
}
|
||||
|
||||
QString ThinQClient::labelAt(int index) const
|
||||
{
|
||||
if (!validIndex(index))
|
||||
return QString();
|
||||
const QString label = m_devices.at(index).label;
|
||||
return label.isEmpty()
|
||||
? QCoreApplication::translate("ThinQ", "Washing machine")
|
||||
: label;
|
||||
}
|
||||
|
||||
bool ThinQClient::hasStateAt(int index) const
|
||||
{
|
||||
return validIndex(index) && m_devices.at(index).hasState;
|
||||
}
|
||||
|
||||
bool ThinQClient::offlineAt(int index) const
|
||||
{
|
||||
return validIndex(index) && m_devices.at(index).offline;
|
||||
}
|
||||
|
||||
bool ThinQClient::staleAt(int index) const
|
||||
{
|
||||
return validIndex(index) && m_devices.at(index).stale;
|
||||
}
|
||||
|
||||
QString ThinQClient::phaseTextAt(int index) const
|
||||
{
|
||||
if (!hasStateAt(index))
|
||||
return QCoreApplication::translate("ThinQ", "No data");
|
||||
return ThinQJson::phaseLabel(m_devices.at(index).state);
|
||||
}
|
||||
|
||||
int ThinQClient::statusLevelAt(int index) const
|
||||
{
|
||||
if (!validIndex(index))
|
||||
return -1;
|
||||
return ThinQJson::statusLevel(m_devices.at(index).state);
|
||||
}
|
||||
|
||||
QString ThinQClient::remainTextAt(int index) const
|
||||
{
|
||||
if (!validIndex(index))
|
||||
return QString();
|
||||
return ThinQJson::durationText(m_devices.at(index).state.remainMinutes);
|
||||
}
|
||||
|
||||
QString ThinQClient::totalTextAt(int index) const
|
||||
{
|
||||
if (!validIndex(index))
|
||||
return QString();
|
||||
return ThinQJson::durationText(m_devices.at(index).state.totalMinutes);
|
||||
}
|
||||
|
||||
QString ThinQClient::totalLineAt(int index) const
|
||||
{
|
||||
const QString total = totalTextAt(index);
|
||||
if (total.isEmpty())
|
||||
return QString();
|
||||
return QCoreApplication::translate("ThinQ", "of %1").arg(total);
|
||||
}
|
||||
|
||||
int ThinQClient::cycleCountAt(int index) const
|
||||
{
|
||||
if (!validIndex(index))
|
||||
return 0;
|
||||
return m_devices.at(index).state.cycleCount;
|
||||
}
|
||||
|
||||
bool ThinQClient::remoteEnabledAt(int index) const
|
||||
{
|
||||
if (!hasStateAt(index))
|
||||
return false;
|
||||
return m_devices.at(index).state.remoteEnabled;
|
||||
}
|
||||
|
||||
QString ThinQClient::primaryLabelAt(int index) const
|
||||
{
|
||||
if (!hasStateAt(index))
|
||||
return QCoreApplication::translate("ThinQ", "Start");
|
||||
return ThinQJson::primaryActionLabel(m_devices.at(index).state);
|
||||
}
|
||||
|
||||
bool ThinQClient::canCommandAt(int index) const
|
||||
{
|
||||
if (!hasStateAt(index))
|
||||
return false;
|
||||
const DeviceEntry &e = m_devices.at(index);
|
||||
return !e.offline && e.state.remoteEnabled;
|
||||
}
|
||||
|
||||
QString ThinQClient::updatedTextAt(int index) const
|
||||
{
|
||||
if (!validIndex(index))
|
||||
return QString();
|
||||
return m_devices.at(index).updated;
|
||||
}
|
||||
|
||||
QString ThinQClient::deviceIdAt(int index) const
|
||||
{
|
||||
if (!validIndex(index))
|
||||
return QString();
|
||||
return m_devices.at(index).id;
|
||||
}
|
||||
|
||||
void ThinQClient::setDelayHours(int hours)
|
||||
{
|
||||
const int clamped = qBound(3, hours, 19);
|
||||
@@ -292,7 +234,7 @@ void ThinQClient::discover()
|
||||
|
||||
void ThinQClient::runPrimaryAt(int index)
|
||||
{
|
||||
if (!hasStateAt(index)) {
|
||||
if (!validIndex(index) || !m_devices.at(index).hasState) {
|
||||
setNotice(QCoreApplication::translate("ThinQ", "No data from the machine yet"), true);
|
||||
return;
|
||||
}
|
||||
@@ -502,6 +444,7 @@ void ThinQClient::applyState(int deviceIndex, bool hadState, int previousLevel,
|
||||
if (m_pollInterval != kPollFastMs)
|
||||
m_pollInterval = kPollFastMs;
|
||||
|
||||
emit devicesChanged();
|
||||
emit stateChanged();
|
||||
noteFinished(deviceIndex, hadState, previousLevel);
|
||||
}
|
||||
@@ -566,6 +509,7 @@ void ThinQClient::handleFailure(int httpStatus, const QByteArray &body,
|
||||
entry.hasState = false;
|
||||
entry.updated.clear();
|
||||
entry.stale = false;
|
||||
emit devicesChanged();
|
||||
emit stateChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
+12
-22
@@ -6,6 +6,8 @@
|
||||
#include <QObject>
|
||||
#include <QStringList>
|
||||
#include <QTimer>
|
||||
#include <QVariantList>
|
||||
#include <QVariantMap>
|
||||
|
||||
#include "thinqjson.h"
|
||||
|
||||
@@ -15,14 +17,17 @@ class QNetworkReply;
|
||||
// Tutte le macchine dell'account, ognuna col suo stato. Una sola classe parla
|
||||
// col cloud LG: nessun ponte, nessun server di mezzo, nessun servizio.
|
||||
//
|
||||
// Il QML legge per indice (labelAt/phaseTextAt/...): l'elenco cambia raramente e
|
||||
// cosi' le stesse funzioni servono la schermata principale e la copertina.
|
||||
// L'elenco si espone come DATO (QVariantList di mappe), non con funzioni di
|
||||
// lettura per indice: in QML un binding che chiama una funzione non registra
|
||||
// nessuna dipendenza e quindi non si aggiorna mai — le macchine si vedevano, ma
|
||||
// lo stato restava vuoto per sempre.
|
||||
class ThinQClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool configured READ configured NOTIFY infoChanged)
|
||||
Q_PROPERTY(int deviceCount READ deviceCount NOTIFY devicesChanged)
|
||||
Q_PROPERTY(int activeIndex READ activeIndex NOTIFY stateChanged)
|
||||
Q_PROPERTY(QVariantList devices READ devices NOTIFY devicesChanged)
|
||||
Q_PROPERTY(QVariantMap active READ active NOTIFY devicesChanged)
|
||||
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
|
||||
Q_PROPERTY(bool polling READ polling NOTIFY pollingChanged)
|
||||
Q_PROPERTY(QString notice READ notice NOTIFY noticeChanged)
|
||||
@@ -36,8 +41,8 @@ public:
|
||||
// invece di chiedere le credenziali.
|
||||
bool configured() const;
|
||||
int deviceCount() const { return m_devices.size(); }
|
||||
// La macchina che sta lavorando (per la copertina), altrimenti la prima.
|
||||
int activeIndex() const;
|
||||
QVariantList devices() const;
|
||||
QVariantMap active() const;
|
||||
bool busy() const { return m_busy; }
|
||||
bool polling() const { return m_pollTimer.isActive(); }
|
||||
QString notice() const { return m_notice; }
|
||||
@@ -45,23 +50,6 @@ public:
|
||||
int delayHours() const { return m_delayHours; }
|
||||
void setDelayHours(int hours);
|
||||
|
||||
Q_INVOKABLE QString labelAt(int index) const;
|
||||
Q_INVOKABLE bool hasStateAt(int index) const;
|
||||
Q_INVOKABLE bool offlineAt(int index) const;
|
||||
Q_INVOKABLE bool staleAt(int index) const;
|
||||
Q_INVOKABLE QString phaseTextAt(int index) const;
|
||||
Q_INVOKABLE int statusLevelAt(int index) const;
|
||||
Q_INVOKABLE QString remainTextAt(int index) const;
|
||||
Q_INVOKABLE QString totalTextAt(int index) const;
|
||||
Q_INVOKABLE QString totalLineAt(int index) const;
|
||||
Q_INVOKABLE int cycleCountAt(int index) const;
|
||||
Q_INVOKABLE bool remoteEnabledAt(int index) const;
|
||||
Q_INVOKABLE QString primaryLabelAt(int index) const;
|
||||
Q_INVOKABLE bool canCommandAt(int index) const;
|
||||
Q_INVOKABLE QString updatedTextAt(int index) const;
|
||||
Q_INVOKABLE QString deviceIdAt(int index) const;
|
||||
Q_INVOKABLE bool validIndex(int index) const;
|
||||
|
||||
Q_INVOKABLE void refresh();
|
||||
Q_INVOKABLE void discover();
|
||||
Q_INVOKABLE void runPrimaryAt(int index);
|
||||
@@ -89,6 +77,8 @@ signals:
|
||||
private:
|
||||
enum RequestKind { KindDevices, KindState, KindCommand };
|
||||
|
||||
bool validIndex(int index) const;
|
||||
|
||||
struct DeviceEntry {
|
||||
QString id;
|
||||
QString label;
|
||||
|
||||
Reference in new issue
Block a user