Improved bookmarks sharing interface

This commit is contained in:
r.kuznetsov 2018-02-26 10:49:54 +03:00 committed by Aleksandr Zatsepin
parent e36328fe40
commit 8297665a32
3 changed files with 64 additions and 19 deletions

View file

@ -175,8 +175,13 @@ using TLoopBlock = void (^)(Observer observer);
+ (NSURL *)beginShareCategory:(MWMMarkGroupID)groupId
{
auto const filePath = GetFramework().GetBookmarkManager().BeginSharing(groupId);
NSURL * url = [NSURL fileURLWithPath:@(filePath.c_str()) isDirectory:NO];
auto const sharingResult = GetFramework().GetBookmarkManager().BeginSharing(groupId);
if (sharingResult.m_code != BookmarkManager::SharingResult::Code::Success)
{
//TODO(igrechuhin): show smth
return nil;
}
NSURL * url = [NSURL fileURLWithPath:@(sharingResult.m_sharingPath.c_str()) isDirectory:NO];
NSAssert(url != nil, @"Invalid share category url");
return url;
}

View file

@ -1172,24 +1172,25 @@ void BookmarkManager::SetInvalidTokenHandler(Cloud::InvalidTokenHandler && onInv
m_bookmarkCloud.SetInvalidTokenHandler(std::move(onInvalidToken));
}
std::string BookmarkManager::BeginSharing(df::MarkGroupID categoryId)
BookmarkManager::SharingResult BookmarkManager::BeginSharing(df::MarkGroupID categoryId)
{
auto const it = m_activeSharing.find(categoryId);
if (it != m_activeSharing.end())
{
// In case if the sharing has already begun.
if (GetPlatform().IsFileExistsByFullPath(it->second))
return it->second;
return SharingResult(it->second);
}
auto const f = GetFileForSharing(categoryId);
if (f.empty())
auto const result = GetFileForSharing(categoryId);
if (result.m_code != SharingResult::Code::Success)
{
m_activeSharing.erase(categoryId);
return {};
return result;
}
m_activeSharing[categoryId] = f;
return f;
m_activeSharing[categoryId] = result.m_sharingPath;
return result;
}
void BookmarkManager::EndSharing(df::MarkGroupID categoryId)
@ -1204,11 +1205,19 @@ void BookmarkManager::EndSharing(df::MarkGroupID categoryId)
m_activeSharing.erase(categoryId);
}
std::string BookmarkManager::GetFileForSharing(df::MarkGroupID categoryId)
bool BookmarkManager::IsCategoryEmpty(df::MarkGroupID categoryId) const
{
return GetUserMarkIds(categoryId).empty() && GetTrackIds(categoryId).empty();
}
BookmarkManager::SharingResult BookmarkManager::GetFileForSharing(df::MarkGroupID categoryId)
{
if (IsCategoryEmpty(categoryId))
return SharingResult(SharingResult::Code::EmptyCategory);
auto const filePath = GetCategoryFileName(categoryId);
if (!GetPlatform().IsFileExistsByFullPath(filePath))
return {};
return SharingResult(SharingResult::Code::FileError, "Bookmarks file does not exist.");
auto ext = my::GetFileExtension(filePath);
strings::AsciiToLower(ext);
@ -1218,15 +1227,16 @@ std::string BookmarkManager::GetFileForSharing(df::MarkGroupID categoryId)
auto const tmpFilePath = my::JoinFoldersToPath(GetPlatform().TmpDir(), fileName + KMZ_EXTENSION);
if (ext == KMZ_EXTENSION)
{
if (!my::CopyFileX(filePath, tmpFilePath))
return {};
return tmpFilePath;
if (my::CopyFileX(filePath, tmpFilePath))
return SharingResult(tmpFilePath);
return SharingResult(SharingResult::Code::FileError, "Could not copy file.");
}
if (CreateZipFromPathDeflatedAndDefaultCompression(filePath, tmpFilePath))
return tmpFilePath;
if (!CreateZipFromPathDeflatedAndDefaultCompression(filePath, tmpFilePath))
return SharingResult(SharingResult::Code::ArchiveError, "Could not create archive.");
return {};
return SharingResult(tmpFilePath);
}
df::GroupIDSet BookmarkManager::MarksChangesTracker::GetAllGroupIds() const

View file

@ -192,9 +192,39 @@ public:
std::unique_ptr<User::Subscriber> GetUserSubscriber();
void SetInvalidTokenHandler(Cloud::InvalidTokenHandler && onInvalidToken);
std::string BeginSharing(df::MarkGroupID categoryId);
struct SharingResult
{
enum class Code
{
Success,
EmptyCategory,
ArchiveError,
FileError
};
Code m_code;
std::string m_sharingPath;
std::string m_errorString;
explicit SharingResult(std::string const & sharingPath)
: m_code(Code::Success)
, m_sharingPath(sharingPath)
{}
explicit SharingResult(Code code)
: m_code(code)
{}
SharingResult(Code code, std::string const & errorString)
: m_code(code)
, m_errorString(errorString)
{}
};
SharingResult BeginSharing(df::MarkGroupID categoryId);
void EndSharing(df::MarkGroupID categoryId);
bool IsCategoryEmpty(df::MarkGroupID categoryId) const;
/// These functions are public for unit tests only. You shouldn't call them from client code.
void SaveToKML(df::MarkGroupID groupId, std::ostream & s);
void CreateCategories(KMLDataCollection && dataCollection, bool autoSave = true);
@ -331,7 +361,7 @@ private:
void SaveToKML(BookmarkCategory * group, std::ostream & s);
std::string GetFileForSharing(df::MarkGroupID categoryId);
SharingResult GetFileForSharing(df::MarkGroupID categoryId);
Callbacks m_callbacks;
MarksChangesTracker m_changesTracker;