Feat: add progress indicator

This commit is contained in:
Dorian Zedler 2022-08-03 11:07:40 +02:00
parent d5a26c5a0e
commit 33d3967b30
Signed by: dorian
GPG key ID: 989DE36109AFA354
3 changed files with 73 additions and 8 deletions

View file

@ -7,6 +7,7 @@ LedDisplayBackend::LedDisplayBackend(QObject *parent) : QObject(parent)
this->displayTextModel = new LedDisplayTextModel(this); this->displayTextModel = new LedDisplayTextModel(this);
this->textSetsBuffer.clear(); this->textSetsBuffer.clear();
this->displayBrightness = {{"displayBrightness", 0}, {"automaticBrightnessAdjustment", false}}; this->displayBrightness = {{"displayBrightness", 0}, {"automaticBrightnessAdjustment", false}};
this->runningCommands = 0;
this->waitingCommands = 0; this->waitingCommands = 0;
this->settings = new QSettings(); this->settings = new QSettings();
@ -142,14 +143,16 @@ void LedDisplayBackend::sendBluetoothCommand(OmobiDisplayCommand command, QVaria
qDebug() << "Sending command: \n" << qPrintable(doc.toJson(QJsonDocument::Compact)); qDebug() << "Sending command: \n" << qPrintable(doc.toJson(QJsonDocument::Compact));
if((this->state != Initing && this->waitingCommands > 0) || (this->state == Initing && this->waitingCommands > 1)) { if((this->state != Initing && this->runningCommands > 0) || (this->state == Initing && this->runningCommands > 1)) {
this->waitingCommands++;
QEventLoop loop; QEventLoop loop;
loop.connect(this, &LedDisplayBackend::commandFinished, &loop, &QEventLoop::quit); loop.connect(this, &LedDisplayBackend::commandFinished, &loop, &QEventLoop::quit);
loop.exec(); loop.exec();
this->waitingCommands--;
if(this->state == Idle) return; if(this->state == Idle) return;
} }
this->waitingCommands ++; this->runningCommands ++;
if(this->state == Connected) if(this->state == Connected)
this->setState(Loading); this->setState(Loading);
@ -190,8 +193,9 @@ void LedDisplayBackend::handleBluetoothDataReceived(QString s){
this->settings->setValue(this->bleClient->getCurrentDevice()->getAddress(), this->lastDisplaySecret); this->settings->setValue(this->bleClient->getCurrentDevice()->getAddress(), this->lastDisplaySecret);
this->waitingCommands = 0; this->runningCommands = 0;
emit this->commandFinished(); emit this->commandFinished();
this->setLoadingProgress(0);
this->setState(Initing); this->setState(Initing);
this->sendBluetoothCommand(GetAllTextSetsCommand); this->sendBluetoothCommand(GetAllTextSetsCommand);
@ -215,6 +219,7 @@ void LedDisplayBackend::handleBluetoothDataReceived(QString s){
for(int i = 0; i < this->displayTextModel->maximumTextSets; i++) { for(int i = 0; i < this->displayTextModel->maximumTextSets; i++) {
for(int param = 0; param < DisplayTextSetParameterCount; param++) { for(int param = 0; param < DisplayTextSetParameterCount; param++) {
this->setLoadingProgress(float((i*DisplayTextSetParameterCount) + param +1) / float(this->displayTextModel->maximumTextSets * DisplayTextSetParameterCount));
this->sendBluetoothCommand(GetTextSetParameterCommand, QVariantMap({{"index", i}, {"parameter", param}})); this->sendBluetoothCommand(GetTextSetParameterCommand, QVariantMap({{"index", i}, {"parameter", param}}));
} }
} }
@ -299,15 +304,16 @@ void LedDisplayBackend::refreshLoadingState() {
if(this->state != Initing && this->state != Loading) if(this->state != Initing && this->state != Loading)
return; return;
qDebug() << "Refreshing loading state! Waiting: " << this->waitingCommands; qDebug() << "Refreshing loading state! Waiting: " << this->runningCommands;
emit this->commandFinished(); emit this->commandFinished();
if(this->waitingCommands <= 1) { if(this->runningCommands <= 1 && this->waitingCommands == 0) {
this->waitingCommands = 0; this->runningCommands = 0;
this->setState(Connected); this->setState(Connected);
this->setLoadingProgress(0);
} }
else else
this->waitingCommands--; this->runningCommands--;
} }
void LedDisplayBackend::setState(OmobiDisplayAppState state) { void LedDisplayBackend::setState(OmobiDisplayAppState state) {
@ -320,7 +326,7 @@ void LedDisplayBackend::setState(OmobiDisplayAppState state) {
qDebug() << "Now in " << state << " state"; qDebug() << "Now in " << state << " state";
if(this->state == Idle) { if(this->state == Idle) {
this->waitingCommands = 0; this->runningCommands = 0;
emit this->commandFinished(); emit this->commandFinished();
this->bleClient->startScanningForDevices(); this->bleClient->startScanningForDevices();
} }
@ -350,3 +356,12 @@ void LedDisplayBackend::setDisplayName(QString name) {
// This will restart the display!! // This will restart the display!!
this->sendBluetoothCommand(SetDisplayNameCommand, QVariantMap{{"displayName", name}}); this->sendBluetoothCommand(SetDisplayNameCommand, QVariantMap{{"displayName", name}});
} }
void LedDisplayBackend::setLoadingProgress(float progress) {
this->loadingProgress = progress;
emit this->loadingProgressChanged();
}
float LedDisplayBackend::getLoadingProgress() {
return this->loadingProgress;
}

View file

@ -18,6 +18,7 @@ class LedDisplayBackend : public QObject
Q_PROPERTY(OmobiDisplayAppState state READ getState WRITE setState NOTIFY stateChanged) Q_PROPERTY(OmobiDisplayAppState state READ getState WRITE setState NOTIFY stateChanged)
Q_PROPERTY(LedDisplayTextModel* displayTextModel READ getDisplayTextModel NOTIFY displayTextModelChanged) Q_PROPERTY(LedDisplayTextModel* displayTextModel READ getDisplayTextModel NOTIFY displayTextModelChanged)
Q_PROPERTY(QVariantMap displayBrightness READ getDisplayBrightness WRITE setDisplayBrightness NOTIFY displayBrightnessChanged) Q_PROPERTY(QVariantMap displayBrightness READ getDisplayBrightness WRITE setDisplayBrightness NOTIFY displayBrightnessChanged)
Q_PROPERTY(float loadingProgress READ getLoadingProgress NOTIFY loadingProgressChanged)
public: public:
explicit LedDisplayBackend(QObject *parent = nullptr); explicit LedDisplayBackend(QObject *parent = nullptr);
@ -79,7 +80,10 @@ private:
QBluetoothLeUartClient *bleClient; QBluetoothLeUartClient *bleClient;
QTimer *keepAliveTimer; QTimer *keepAliveTimer;
LedDisplayTextModel* displayTextModel; LedDisplayTextModel* displayTextModel;
int runningCommands;
int waitingCommands; int waitingCommands;
float loadingProgress;
QList<QMap<int, QVariant>> textSetsBuffer; QList<QMap<int, QVariant>> textSetsBuffer;
QVariantMap displayBrightness; QVariantMap displayBrightness;
@ -96,6 +100,7 @@ public slots:
Q_INVOKABLE void setDisplayBrightness(QVariantMap brightness); Q_INVOKABLE void setDisplayBrightness(QVariantMap brightness);
Q_INVOKABLE void setDisplayCode(QString code); Q_INVOKABLE void setDisplayCode(QString code);
Q_INVOKABLE void setDisplayName(QString name); Q_INVOKABLE void setDisplayName(QString name);
Q_INVOKABLE float getLoadingProgress();
private slots: private slots:
void handleBluetoothStateChange(QBluetoothLeUartClient::BluetoothLeUartClientState state); void handleBluetoothStateChange(QBluetoothLeUartClient::BluetoothLeUartClientState state);
@ -115,6 +120,7 @@ private slots:
void refreshLoadingState(); void refreshLoadingState();
void setState(OmobiDisplayAppState state); void setState(OmobiDisplayAppState state);
void setLoadingProgress(float progrss);
signals: signals:
void stateChanged(); void stateChanged();
@ -122,6 +128,7 @@ signals:
void displayTextModelChanged(); void displayTextModelChanged();
void displayBrightnessChanged(); void displayBrightnessChanged();
void commandFinished(); void commandFinished();
void loadingProgressChanged();
}; };

View file

@ -29,6 +29,8 @@ Page {
} }
Chip { Chip {
id: topChip
Layout.fillWidth: true Layout.fillWidth: true
Layout.preferredHeight: 35 Layout.preferredHeight: 35
Layout.alignment: Layout.Center Layout.alignment: Layout.Center
@ -60,6 +62,42 @@ Page {
opacity: root.working ? 1:0 opacity: root.working ? 1:0
} }
Item {
anchors.fill: parent
ProgressBar {
id: loadingProgressBar
anchors {
left: parent.left
right: parent.right
bottom: parent.bottom
}
value: backend.loadingProgress
opacity: 0
Behavior on value {
NumberAnimation {}
}
Behavior on opacity {
NumberAnimation {}
}
}
layer.enabled: true
layer.effect: OpacityMask {
maskSource: Item {
width: topChip.width
height: topChip.height
Rectangle {
anchors.fill: parent
radius: topChip.radius
}
}
}
}
} }
Item { Item {
@ -462,6 +500,11 @@ Page {
statusText: qsTr("loading data...") statusText: qsTr("loading data...")
working: true working: true
} }
PropertyChanges {
target: loadingProgressBar
opacity: 1
}
} }
] ]
} }