From 0ce7be17d3fec380f726c1ff16c559344b3e24c1 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Sun, 5 Jun 2016 21:07:55 +0600 Subject: [PATCH] CLOUD: Make ProgressDialog display downloading progress --- backends/cloud/cloudmanager.cpp | 6 ++++++ backends/cloud/cloudmanager.h | 3 +++ backends/cloud/savessyncrequest.cpp | 16 +++++++++++++++- backends/cloud/savessyncrequest.h | 3 +++ backends/cloud/storage.cpp | 9 +++++++++ backends/cloud/storage.h | 3 +++ gui/saveload-dialog.cpp | 2 +- 7 files changed, 40 insertions(+), 2 deletions(-) diff --git a/backends/cloud/cloudmanager.cpp b/backends/cloud/cloudmanager.cpp index f5d60e025c5..4230c1453bd 100644 --- a/backends/cloud/cloudmanager.cpp +++ b/backends/cloud/cloudmanager.cpp @@ -139,6 +139,12 @@ bool CloudManager::isSyncing() { return false; } +double CloudManager::getSyncDownloadingProgress() { + Storage *storage = getCurrentStorage(); + if (storage) return storage->getSyncDownloadingProgress(); + return 1; +} + double CloudManager::getSyncProgress() { Storage *storage = getCurrentStorage(); if (storage) return storage->getSyncProgress(); diff --git a/backends/cloud/cloudmanager.h b/backends/cloud/cloudmanager.h index c7351dab2e3..dbff0184eb6 100644 --- a/backends/cloud/cloudmanager.h +++ b/backends/cloud/cloudmanager.h @@ -90,6 +90,9 @@ public: /** Returns whether there is a SavesSyncRequest running. */ bool isSyncing(); + /** Returns a number in [0, 1] range which represents current sync downloading progress (1 = complete). */ + double getSyncDownloadingProgress(); + /** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ double getSyncProgress(); diff --git a/backends/cloud/savessyncrequest.cpp b/backends/cloud/savessyncrequest.cpp index e066c53a929..072685773e1 100644 --- a/backends/cloud/savessyncrequest.cpp +++ b/backends/cloud/savessyncrequest.cpp @@ -208,12 +208,13 @@ void SavesSyncRequest::directoryCreatedErrorCallback(Networking::ErrorResponse e void SavesSyncRequest::downloadNextFile() { if (_filesToDownload.empty()) { + _currentDownloadingFile = StorageFile("", 0, 0, false); //so getFilesToDownload() would return an empty array sendCommand(kSavesSyncEndedCmd, 0); uploadNextFile(); return; } - sendCommand(kSavesSyncProgressCmd, (int)(getProgress() * 100)); + sendCommand(kSavesSyncProgressCmd, (int)(getDownloadingProgress() * 100)); _currentDownloadingFile = _filesToDownload.back(); _filesToDownload.pop_back(); @@ -295,6 +296,19 @@ void SavesSyncRequest::handle() {} void SavesSyncRequest::restart() { start(); } +double SavesSyncRequest::getDownloadingProgress() { + if (_totalFilesToHandle == 0) { + if (_state == Networking::FINISHED) return 1; //nothing to upload and download => Request ends soon + return 0; //directory not listed yet + } + + if (_totalFilesToHandle == _filesToUpload.size()) return 1; //nothing to download => download complete + + uint32 totalFilesToDownload = _totalFilesToHandle - _filesToUpload.size(); + uint32 filesLeftToDownload = _filesToDownload.size() + (_currentDownloadingFile.name() != "" ? 1 : 0); + return (double)(totalFilesToDownload - filesLeftToDownload) / (double)(totalFilesToDownload); +} + double SavesSyncRequest::getProgress() { if (_totalFilesToHandle == 0) { if (_state == Networking::FINISHED) return 1; //nothing to upload and download => Request ends soon diff --git a/backends/cloud/savessyncrequest.h b/backends/cloud/savessyncrequest.h index 397a8cbcac9..569feb4e453 100644 --- a/backends/cloud/savessyncrequest.h +++ b/backends/cloud/savessyncrequest.h @@ -69,6 +69,9 @@ public: virtual void handle(); virtual void restart(); + /** Returns a number in range [0, 1], where 1 is "complete". */ + double getDownloadingProgress(); + /** Returns a number in range [0, 1], where 1 is "complete". */ double getProgress(); diff --git a/backends/cloud/storage.cpp b/backends/cloud/storage.cpp index c98310d3dd2..5458276e391 100644 --- a/backends/cloud/storage.cpp +++ b/backends/cloud/storage.cpp @@ -124,6 +124,15 @@ bool Storage::isSyncing() { return syncing; } +double Storage::getSyncDownloadingProgress() { + double result = 1; + _runningRequestsMutex.lock(); + if (_savesSyncRequest) + result = _savesSyncRequest->getDownloadingProgress(); + _runningRequestsMutex.unlock(); + return result; +} + double Storage::getSyncProgress() { double result = 1; _runningRequestsMutex.lock(); diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 40ea14a3a1b..058e831e5db 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -147,6 +147,9 @@ public: /** Returns whether there is a SavesSyncRequest running. */ virtual bool isSyncing(); + /** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ + virtual double getSyncDownloadingProgress(); + /** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ virtual double getSyncProgress(); diff --git a/gui/saveload-dialog.cpp b/gui/saveload-dialog.cpp index bdae9efea4a..6d343c88edf 100644 --- a/gui/saveload-dialog.cpp +++ b/gui/saveload-dialog.cpp @@ -52,7 +52,7 @@ SaveLoadCloudSyncProgressDialog::SaveLoadCloudSyncProgressDialog(): Dialog(10, 1 int buttonWidth = 140; int marginBottom = 8; - uint32 progress = (uint32)(100 * CloudMan.getSyncProgress()); + uint32 progress = (uint32)(100 * CloudMan.getSyncDownloadingProgress()); _label = new StaticTextWidget(this, 10, 10, 300, kLineHeight, Common::String::format("Downloading saves (%u%% complete)...", progress), Graphics::kTextAlignCenter); //if (defaultButton)