[ios] Added storage space check before download.

This commit is contained in:
Ilya Grechuhin 2016-04-04 11:13:07 +03:00
parent 9a226eda51
commit 7fcaab0588
2 changed files with 45 additions and 17 deletions

View file

@ -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<CGFloat>(progress.first) / progress.second];
};
[MWMStorage performAction:^
[MWMStorage checkConnectionAndPerformAction:^
{
self->m_countryId = f.PreMigrate(position, onStatusChanged, onProgressChanged);
if (self->m_countryId != kInvalidCountryId)

View file

@ -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())
{