From 7fcaab05886dad4f2244fa4199d16d9bf89780e0 Mon Sep 17 00:00:00 2001 From: Ilya Grechuhin Date: Mon, 4 Apr 2016 11:13:07 +0300 Subject: [PATCH] [ios] Added storage space check before download. --- .../Migration/MWMMigrationViewController.mm | 4 +- iphone/Maps/Classes/Storage/MWMStorage.mm | 58 ++++++++++++++----- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/iphone/Maps/Classes/Migration/MWMMigrationViewController.mm b/iphone/Maps/Classes/Migration/MWMMigrationViewController.mm index dc8f406553..d08544cfba 100644 --- a/iphone/Maps/Classes/Migration/MWMMigrationViewController.mm +++ b/iphone/Maps/Classes/Migration/MWMMigrationViewController.mm @@ -22,7 +22,7 @@ using namespace storage; @interface MWMStorage () -+ (void)performAction:(TMWMVoidBlock)action alertController:(MWMAlertViewController *)alertController; ++ (void)checkConnectionAndPerformAction:(TMWMVoidBlock)action alertController:(MWMAlertViewController *)alertController; @end @@ -103,7 +103,7 @@ using namespace storage; [view setProgress:static_cast(progress.first) / progress.second]; }; - [MWMStorage performAction:^ + [MWMStorage checkConnectionAndPerformAction:^ { self->m_countryId = f.PreMigrate(position, onStatusChanged, onProgressChanged); if (self->m_countryId != kInvalidCountryId) diff --git a/iphone/Maps/Classes/Storage/MWMStorage.mm b/iphone/Maps/Classes/Storage/MWMStorage.mm index cfdf54a1d0..bcefe4f0da 100644 --- a/iphone/Maps/Classes/Storage/MWMStorage.mm +++ b/iphone/Maps/Classes/Storage/MWMStorage.mm @@ -5,6 +5,8 @@ #include "platform/platform.hpp" +#include "storage/storage_helpers.hpp" + namespace { NSString * const kStorageCanShowNoWifiAlert = @"StorageCanShowNoWifiAlert"; @@ -23,13 +25,12 @@ using namespace storage; + (void)downloadNode:(TCountryId const &)countryId alertController:(MWMAlertViewController *)alertController onSuccess:(TMWMVoidBlock)onSuccess { - [self performAction:^ + [self checkEnoughSpaceFor:countryId andPerformAction:^ { GetFramework().Storage().DownloadNode(countryId); if (onSuccess) onSuccess(); - } - alertController:alertController]; + } alertController:alertController]; } + (void)retryDownloadNode:(TCountryId const &)countryId @@ -39,11 +40,10 @@ using namespace storage; + (void)updateNode:(TCountryId const &)countryId alertController:(MWMAlertViewController *)alertController { - [self performAction:^ + [self checkEnoughSpaceFor:countryId andPerformAction:^ { GetFramework().Storage().UpdateNode(countryId); - } - alertController:alertController]; + } alertController:alertController]; } + (void)deleteNode:(TCountryId const &)countryId alertController:(MWMAlertViewController *)alertController @@ -71,20 +71,48 @@ using namespace storage; GetFramework().ShowNode(countryId); } -+ (void)downloadNodes:(storage::TCountriesVec const &)countryIds alertController:(MWMAlertViewController *)alertController onSuccess:(TMWMVoidBlock)onSuccess ++ (void)downloadNodes:(TCountriesVec const &)countryIds + alertController:(MWMAlertViewController *)alertController + onSuccess:(TMWMVoidBlock)onSuccess { - [self performAction:[countryIds, onSuccess] + size_t requiredSize = accumulate(countryIds.begin(), countryIds.end(), 0, + [](size_t const & size, TCountryId const & countryId) + { + NodeAttrs nodeAttrs; + GetFramework().Storage().GetNodeAttrs(countryId, nodeAttrs); + return size + nodeAttrs.m_mwmSize - nodeAttrs.m_localMwmSize; + }); + size_t constexpr kDownloadExtraSpaceSize = 100 * MB; + requiredSize += kDownloadExtraSpaceSize; + if (GetPlatform().GetWritableStorageStatus(requiredSize) == Platform::TStorageStatus::STORAGE_OK) { - auto & s = GetFramework().Storage(); - for (auto const & countryId : countryIds) - s.DownloadNode(countryId); - if (onSuccess) - onSuccess(); + [self checkConnectionAndPerformAction:[countryIds, onSuccess] + { + auto & s = GetFramework().Storage(); + for (auto const & countryId : countryIds) + s.DownloadNode(countryId); + if (onSuccess) + onSuccess(); + } alertController:alertController]; + } + else + { + [alertController presentNotEnoughSpaceAlert]; } - alertController:alertController]; } -+ (void)performAction:(TMWMVoidBlock)action alertController:(MWMAlertViewController *)alertController ++ (void)checkEnoughSpaceFor:(TCountryId const &)countryId + andPerformAction:(TMWMVoidBlock)action + alertController:(MWMAlertViewController *)alertController +{ + if (IsEnoughSpaceForDownload(countryId, GetFramework().Storage())) + [self checkConnectionAndPerformAction:action alertController:alertController]; + else + [alertController presentNotEnoughSpaceAlert]; +} + ++ (void)checkConnectionAndPerformAction:(TMWMVoidBlock)action + alertController:(MWMAlertViewController *)alertController { switch (Platform::ConnectionStatus()) {