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->textSetsBuffer.clear();
this->displayBrightness = {{"displayBrightness", 0}, {"automaticBrightnessAdjustment", false}};
this->runningCommands = 0;
this->waitingCommands = 0;
this->settings = new QSettings();
@ -142,14 +143,16 @@ void LedDisplayBackend::sendBluetoothCommand(OmobiDisplayCommand command, QVaria
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;
loop.connect(this, &LedDisplayBackend::commandFinished, &loop, &QEventLoop::quit);
loop.exec();
this->waitingCommands--;
if(this->state == Idle) return;
}
this->waitingCommands ++;
this->runningCommands ++;
if(this->state == Connected)
this->setState(Loading);
@ -190,8 +193,9 @@ void LedDisplayBackend::handleBluetoothDataReceived(QString s){
this->settings->setValue(this->bleClient->getCurrentDevice()->getAddress(), this->lastDisplaySecret);
this->waitingCommands = 0;
this->runningCommands = 0;
emit this->commandFinished();
this->setLoadingProgress(0);
this->setState(Initing);
this->sendBluetoothCommand(GetAllTextSetsCommand);
@ -215,6 +219,7 @@ void LedDisplayBackend::handleBluetoothDataReceived(QString s){
for(int i = 0; i < this->displayTextModel->maximumTextSets; i++) {
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}}));
}
}
@ -299,15 +304,16 @@ void LedDisplayBackend::refreshLoadingState() {
if(this->state != Initing && this->state != Loading)
return;
qDebug() << "Refreshing loading state! Waiting: " << this->waitingCommands;
qDebug() << "Refreshing loading state! Waiting: " << this->runningCommands;
emit this->commandFinished();
if(this->waitingCommands <= 1) {
this->waitingCommands = 0;
if(this->runningCommands <= 1 && this->waitingCommands == 0) {
this->runningCommands = 0;
this->setState(Connected);
this->setLoadingProgress(0);
}
else
this->waitingCommands--;
this->runningCommands--;
}
void LedDisplayBackend::setState(OmobiDisplayAppState state) {
@ -320,7 +326,7 @@ void LedDisplayBackend::setState(OmobiDisplayAppState state) {
qDebug() << "Now in " << state << " state";
if(this->state == Idle) {
this->waitingCommands = 0;
this->runningCommands = 0;
emit this->commandFinished();
this->bleClient->startScanningForDevices();
}
@ -350,3 +356,12 @@ void LedDisplayBackend::setDisplayName(QString name) {
// This will restart the display!!
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(LedDisplayTextModel* displayTextModel READ getDisplayTextModel NOTIFY displayTextModelChanged)
Q_PROPERTY(QVariantMap displayBrightness READ getDisplayBrightness WRITE setDisplayBrightness NOTIFY displayBrightnessChanged)
Q_PROPERTY(float loadingProgress READ getLoadingProgress NOTIFY loadingProgressChanged)
public:
explicit LedDisplayBackend(QObject *parent = nullptr);
@ -79,7 +80,10 @@ private:
QBluetoothLeUartClient *bleClient;
QTimer *keepAliveTimer;
LedDisplayTextModel* displayTextModel;
int runningCommands;
int waitingCommands;
float loadingProgress;
QList<QMap<int, QVariant>> textSetsBuffer;
QVariantMap displayBrightness;
@ -96,6 +100,7 @@ public slots:
Q_INVOKABLE void setDisplayBrightness(QVariantMap brightness);
Q_INVOKABLE void setDisplayCode(QString code);
Q_INVOKABLE void setDisplayName(QString name);
Q_INVOKABLE float getLoadingProgress();
private slots:
void handleBluetoothStateChange(QBluetoothLeUartClient::BluetoothLeUartClientState state);
@ -115,6 +120,7 @@ private slots:
void refreshLoadingState();
void setState(OmobiDisplayAppState state);
void setLoadingProgress(float progrss);
signals:
void stateChanged();
@ -122,6 +128,7 @@ signals:
void displayTextModelChanged();
void displayBrightnessChanged();
void commandFinished();
void loadingProgressChanged();
};

View file

@ -29,6 +29,8 @@ Page {
}
Chip {
id: topChip
Layout.fillWidth: true
Layout.preferredHeight: 35
Layout.alignment: Layout.Center
@ -60,6 +62,42 @@ Page {
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 {
@ -462,6 +500,11 @@ Page {
statusText: qsTr("loading data...")
working: true
}
PropertyChanges {
target: loadingProgressBar
opacity: 1
}
}
]
}