Show syncing progress at startup

This commit is contained in:
Aditya Kulkarni
2019-10-24 19:16:37 -07:00
parent c3fcdcb4c5
commit 2fabc1ec8d
7 changed files with 78 additions and 36 deletions

View File

@@ -58,10 +58,53 @@ void ConnectionLoader::doAutoConnect() {
// After the lib is initialized, try to do get info
connection->doRPC("info", "", [=](auto reply) {
// If success, set the connection
// If success, set the connection
main->logger->write("Connection is online.");
this->doRPCSetConnection(connection);
}, [=](auto err) {});
QAtomicInteger<bool>* isSyncing = new QAtomicInteger<bool>();
isSyncing->store(true);
// Do a sync at startup
syncTimer = new QTimer(main);
connection->doRPCWithDefaultErrorHandling("sync", "", [=](auto) {
isSyncing->store(false);
// Cancel the timer
syncTimer->deleteLater();
// When sync is done, set the connection
this->doRPCSetConnection(connection);
});
// While it is syncing, we'll show the status updates while it is alive.
QObject::connect(syncTimer, &QTimer::timeout, [=]() {
qDebug() << "Sync timer" << isSyncing->load();
// Check the sync status
if (isSyncing->load()) {
// Get the sync status
connection->doRPC("syncstatus", "", [=](json reply) {
qDebug() << QString::fromStdString("Sync statys = ") << QString::fromStdString(reply.dump());
if (isSyncing->load() && reply.find("synced_blocks") != reply.end()) {
qint64 synced = reply["synced_blocks"].get<json::number_unsigned_t>();
qint64 total = reply["total_blocks"].get<json::number_unsigned_t>();
showInformation("Synced " + QString::number(synced) + " / " + QString::number(total));
}
},
[=](QString err) {
qDebug() << "Sync error" << err;
});
} else {
delete isSyncing;
}
});
syncTimer->setInterval(1* 1000);
syncTimer->start(1000);
}, [=](QString err) {
showError(err);
});
}
void ConnectionLoader::createOrRestore(bool dangerous, QString server) {
@@ -75,6 +118,7 @@ void ConnectionLoader::createOrRestore(bool dangerous, QString server) {
}
void ConnectionLoader::doRPCSetConnection(Connection* conn) {
qDebug() << "Connectionloader finished, setting connection";
rpc->setConnection(conn);
d->accept();
@@ -88,20 +132,9 @@ Connection* ConnectionLoader::makeConnection(std::shared_ptr<ConnectionConfig> c
// Update the UI with the status
void ConnectionLoader::showInformation(QString info, QString detail) {
static int rescanCount = 0;
if (detail.toLower().startsWith("rescan")) {
rescanCount++;
}
if (rescanCount > 10) {
detail = detail + "\n" + QObject::tr("This may take several hours");
}
qDebug() << "Showing info " << info << ":" << detail;
connD->status->setText(info);
connD->statusDetail->setText(detail);
if (rescanCount < 10)
main->logger->write(info + ":" + detail);
}
/**