From c5c193217575e25229837161970f8867435d3cdb Mon Sep 17 00:00:00 2001 From: vng Date: Sat, 1 May 2021 11:09:45 +0300 Subject: [PATCH] [iOS][android] Restore QueuedCountry::OnStartDownloading notification. Signed-off-by: vng --- .../com/mapswithme/maps/DownloaderAdapter.cpp | 14 +++++----- .../downloader_adapter_ios.h | 2 +- .../downloader_adapter_ios.mm | 27 +++++++++++-------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/android/jni/com/mapswithme/maps/DownloaderAdapter.cpp b/android/jni/com/mapswithme/maps/DownloaderAdapter.cpp index 38591950f4..61bc3fbee0 100644 --- a/android/jni/com/mapswithme/maps/DownloaderAdapter.cpp +++ b/android/jni/com/mapswithme/maps/DownloaderAdapter.cpp @@ -56,7 +56,6 @@ void BackgroundDownloaderAdapter::Remove(CountryId const & countryId) g_completionHandlers.erase(*id); } m_queue.Remove(countryId); - } void BackgroundDownloaderAdapter::Clear() @@ -72,7 +71,7 @@ void BackgroundDownloaderAdapter::Clear() }); m_queue.Clear(); -}; +} QueueInterface const & BackgroundDownloaderAdapter::GetQueue() const { @@ -98,6 +97,8 @@ void BackgroundDownloaderAdapter::Download(QueuedCountry && queuedCountry) auto urls = MakeUrlList(queuedCountry.GetRelativeUrl()); auto const path = queuedCountry.GetFileDownloadPath(); + queuedCountry.OnStartDownloading(); + m_queue.Append(std::move(queuedCountry)); DownloadFromLastUrl(countryId, path, std::move(urls)); @@ -108,17 +109,19 @@ void BackgroundDownloaderAdapter::DownloadFromLastUrl(CountryId const & countryI std::vector && urls) { CHECK_THREAD_CHECKER(m_threadChecker, ()); - ASSERT(!urls.empty(), ()); + if (urls.empty()) + return; auto env = jni::GetEnv(); + jni::TScopedLocalRef url(env, jni::ToJavaString(env, urls.back())); auto id = static_cast(env->CallLongMethod(*m_downloadManager, m_downloadManagerEnqueue, url.get())); + urls.pop_back(); jni::HandleJavaException(env); m_queue.SetTaskInfoForCountryId(countryId, id); - urls.pop_back(); auto onFinish = [this, countryId, downloadPath, urls = std::move(urls)](bool status) mutable { CHECK_THREAD_CHECKER(m_threadChecker, ()); @@ -126,9 +129,8 @@ void BackgroundDownloaderAdapter::DownloadFromLastUrl(CountryId const & countryI if (!m_queue.Contains(countryId)) return; - if (!status && urls.size() > 1) + if (!status && !urls.empty()) { - urls.pop_back(); DownloadFromLastUrl(countryId, downloadPath, std::move(urls)); } else diff --git a/storage/background_downloading/downloader_adapter_ios.h b/storage/background_downloading/downloader_adapter_ios.h index e0e39e2a5d..a880be075b 100644 --- a/storage/background_downloading/downloader_adapter_ios.h +++ b/storage/background_downloading/downloader_adapter_ios.h @@ -21,7 +21,7 @@ private: // Trying to download mwm from different servers recursively. void DownloadFromLastUrl(CountryId const & countryId, std::string const & downloadPath, - std::vector const & urls); + std::vector && urls); BackgroundDownloaderQueue m_queue; }; diff --git a/storage/background_downloading/downloader_adapter_ios.mm b/storage/background_downloading/downloader_adapter_ios.mm index 2a7be85f4f..f49ae157d8 100644 --- a/storage/background_downloading/downloader_adapter_ios.mm +++ b/storage/background_downloading/downloader_adapter_ios.mm @@ -68,31 +68,37 @@ void BackgroundDownloaderAdapter::Download(QueuedCountry && queuedCountry) } auto const countryId = queuedCountry.GetCountryId(); - auto const urls = MakeUrlList(queuedCountry.GetRelativeUrl()); + auto urls = MakeUrlList(queuedCountry.GetRelativeUrl()); auto const path = queuedCountry.GetFileDownloadPath(); + queuedCountry.OnStartDownloading(); + m_queue.Append(std::move(queuedCountry)); - DownloadFromLastUrl(countryId, path, urls); + DownloadFromLastUrl(countryId, path, std::move(urls)); } void BackgroundDownloaderAdapter::DownloadFromLastUrl(CountryId const & countryId, std::string const & downloadPath, - std::vector const & urls) + std::vector && urls) { if (urls.empty()) return; - auto onFinish = [this, countryId, downloadPath, urls = urls](NSError *error) mutable { + NSURL * url = [NSURL URLWithString:@(urls.back().c_str())]; + assert(url != nil); + urls.pop_back(); + + auto onFinish = [this, countryId, downloadPath, urls = std::move(urls)](NSError *error) mutable + { downloader::DownloadStatus status = error ? [error toDownloaderError] : downloader::DownloadStatus::Completed; if (!m_queue.Contains(countryId)) return; - if (status == downloader::DownloadStatus::Failed && urls.size() > 1) + if (status == downloader::DownloadStatus::Failed && !urls.empty()) { - urls.pop_back(); - DownloadFromLastUrl(countryId, downloadPath, urls); + DownloadFromLastUrl(countryId, downloadPath, std::move(urls)); } else { @@ -102,16 +108,15 @@ void BackgroundDownloaderAdapter::DownloadFromLastUrl(CountryId const & countryI } }; - auto onProgress = [this, countryId](int64_t totalWritten, int64_t totalExpected) { + auto onProgress = [this, countryId](int64_t totalWritten, int64_t totalExpected) + { if (!m_queue.Contains(countryId)) return; auto const & country = m_queue.GetCountryById(countryId); country.OnDownloadProgress({totalWritten, totalExpected}); }; - - NSURL * url = [NSURL URLWithString:@(urls.back().c_str())]; - assert(url != nil); + BackgroundDownloader * downloader = [BackgroundDownloader sharedBackgroundMapDownloader]; NSUInteger taskId = [downloader downloadWithUrl:url completion:onFinish progress:onProgress];