Fixed catalog core

This commit is contained in:
r.kuznetsov 2018-06-18 17:56:50 +03:00 committed by Daria Volvenkova
parent 260f552a56
commit d29ac0f639
8 changed files with 51 additions and 56 deletions

View file

@ -646,7 +646,7 @@ Java_com_mapswithme_maps_bookmarks_data_BookmarkManager_nativeGetCatalogDownload
JNIEnv * env, jobject, jstring serverId)
{
auto & bm = frm()->GetBookmarkManager();
return ToJavaString(env, bm.GetCatalogDownloadUrl(ToNativeString(env, serverId)));
return ToJavaString(env, bm.GetCatalog().GetDownloadUrl(ToNativeString(env, serverId)));
}
JNIEXPORT jstring JNICALL
@ -654,7 +654,7 @@ Java_com_mapswithme_maps_bookmarks_data_BookmarkManager_nativeGetCatalogFrontend
JNIEnv * env, jobject)
{
auto & bm = frm()->GetBookmarkManager();
return ToJavaString(env, bm.GetCatalogFrontendUrl());
return ToJavaString(env, bm.GetCatalog().GetFrontendUrl());
}
JNIEXPORT jboolean JNICALL

View file

@ -260,7 +260,7 @@ bool BookmarkCategory::IsCategoryFromCatalog() const
std::string BookmarkCategory::GetCatalogDeeplink() const
{
std::ostringstream ss;
ss << "mapsme://dlink.maps.me/catalogue?id=" << m_serverId << "&name=" << UrlEncode(GetName());
ss << "https://dlink.maps.me/catalogue?id=" << m_serverId << "&name=" << UrlEncode(GetName());
return ss.str();
}

View file

@ -53,6 +53,16 @@ void BookmarkCatalog::UnregisterDownloadedId(std::string const & id)
m_downloadedIds.erase(id);
}
bool BookmarkCatalog::IsDownloading(std::string const & id) const
{
return m_downloadingIds.find(id) != m_downloadingIds.cend();
}
bool BookmarkCatalog::HasDownloaded(std::string const & id) const
{
return m_downloadedIds.find(id) != m_downloadedIds.cend();
}
std::vector<std::string> BookmarkCatalog::GetDownloadingNames() const
{
std::vector<std::string> names;
@ -66,46 +76,33 @@ void BookmarkCatalog::Download(std::string const & id, std::string const & name,
std::function<void()> && startHandler,
platform::RemoteFile::ResultHandler && finishHandler)
{
if (m_downloadingIds.find(id) != m_downloadingIds.cend() ||
m_downloadedIds.find(id) != m_downloadedIds.cend())
{
if (IsDownloading(id) || HasDownloaded(id))
return;
}
m_downloadingIds.insert(std::make_pair(id, name));
if (startHandler)
startHandler();
static uint32_t counter = 0;
auto const path = my::JoinPath(GetPlatform().TmpDir(), "file" + strings::to_string(++counter));
auto const url = BuildCatalogDownloadUrl(id);
if (url.empty())
platform::RemoteFile remoteFile(BuildCatalogDownloadUrl(id));
remoteFile.DownloadAsync(path, [startHandler = std::move(startHandler)](std::string const &)
{
if (finishHandler)
{
finishHandler(platform::RemoteFile::Result(url, platform::RemoteFile::Status::NetworkError,
"Empty server URL."), {});
}
return;
}
platform::RemoteFile remoteFile(url);
remoteFile.DownloadAsync(path, [finishHandler = std::move(finishHandler)]
(platform::RemoteFile::Result && result, std::string const & filePath)
if (startHandler)
startHandler();
}, [finishHandler = std::move(finishHandler)] (platform::RemoteFile::Result && result,
std::string const & filePath)
{
if (finishHandler)
finishHandler(std::move(result), filePath);
});
}
std::string BookmarkCatalog::GetCatalogDownloadUrl(std::string const & serverId) const
std::string BookmarkCatalog::GetDownloadUrl(std::string const & serverId) const
{
return BuildCatalogDownloadUrl(serverId);
}
std::string BookmarkCatalog::GetCatalogFrontendUrl() const
std::string BookmarkCatalog::GetFrontendUrl() const
{
return kCatalogFrontendServer;
}

View file

@ -15,15 +15,17 @@ public:
void RegisterDownloadedId(std::string const & id);
void UnregisterDownloadedId(std::string const & id);
void Download(std::string const & id, std::string const & name,
std::function<void()> && startHandler,
platform::RemoteFile::ResultHandler && finishHandler);
bool IsDownloading(std::string const & id) const;
bool HasDownloaded(std::string const & id) const;
size_t GetDownloadingCount() const { return m_downloadingIds.size(); }
std::vector<std::string> GetDownloadingNames() const;
std::string GetCatalogDownloadUrl(std::string const & serverId) const;
std::string GetCatalogFrontendUrl() const;
std::string GetDownloadUrl(std::string const & serverId) const;
std::string GetFrontendUrl() const;
private:
std::map<std::string, std::string> m_downloadingIds;

View file

@ -1470,6 +1470,9 @@ void BookmarkManager::CreateCategories(KMLDataCollection && dataCollection, bool
auto & fileData = *data.second;
auto & categoryData = fileData.m_categoryData;
if (FromCatalog(fileData))
m_bookmarkCatalog.RegisterDownloadedId(fileData.m_serverId);
if (!UserMarkIdStorage::Instance().CheckIds(fileData) || HasDuplicatedIds(fileData))
{
//TODO: notify subscribers(like search subsystem). This KML could have been indexed.
@ -2085,8 +2088,6 @@ void BookmarkManager::ImportDownloadedFromCatalog(std::string const & id, std::s
CreateCategories(std::move(*collection));
m_bookmarkCatalog.RegisterDownloadedId(id);
if (m_onCatalogImportFinished)
m_onCatalogImportFinished(id, true /* successful */);
});
@ -2099,16 +2100,6 @@ void BookmarkManager::ImportDownloadedFromCatalog(std::string const & id, std::s
});
}
size_t BookmarkManager::GetDownloadingFromCatalogCount() const
{
return m_bookmarkCatalog.GetDownloadingCount();
}
std::vector<std::string> BookmarkManager::GetDownloadingFromCatalogNames() const
{
return m_bookmarkCatalog.GetDownloadingNames();
}
bool BookmarkManager::IsCategoryFromCatalog(kml::MarkGroupId categoryId) const
{
auto cat = GetBmCategory(categoryId);
@ -2126,14 +2117,10 @@ std::string BookmarkManager::GetCategoryCatalogDeeplink(kml::MarkGroupId categor
return cat->GetCatalogDeeplink();
}
std::string BookmarkManager::GetCatalogDownloadUrl(std::string const & serverId) const
BookmarkCatalog const & BookmarkManager::GetCatalog() const
{
return m_bookmarkCatalog.GetCatalogDownloadUrl(serverId);
}
std::string BookmarkManager::GetCatalogFrontendUrl() const
{
return m_bookmarkCatalog.GetCatalogFrontendUrl();
CHECK_THREAD_CHECKER(m_threadChecker, ());
return m_bookmarkCatalog;
}
void BookmarkManager::EnableTestMode(bool enable)

View file

@ -294,12 +294,9 @@ public:
OnCatalogImportFinishedHandler && onCatalogImportFinished);
void DownloadFromCatalogAndImport(std::string const & id, std::string const & name);
void ImportDownloadedFromCatalog(std::string const & id, std::string const & filePath);
size_t GetDownloadingFromCatalogCount() const;
std::vector<std::string> GetDownloadingFromCatalogNames() const;
bool IsCategoryFromCatalog(kml::MarkGroupId categoryId) const;
std::string GetCategoryCatalogDeeplink(kml::MarkGroupId categoryId) const;
std::string GetCatalogDownloadUrl(std::string const & serverId) const;
std::string GetCatalogFrontendUrl() const;
BookmarkCatalog const & GetCatalog() const;
/// These functions are public for unit tests only. You shouldn't call them from client code.
void EnableTestMode(bool enable);

View file

@ -21,6 +21,9 @@ RemoteFile::RemoteFile(std::string url, bool allowRedirection)
RemoteFile::Result RemoteFile::Download(std::string const & filePath) const
{
if (m_url.empty())
return {m_url, Status::NetworkError, "Empty URL"};
platform::HttpClient request(m_url);
request.SetTimeout(kRequestTimeoutInSec);
if (request.RunHttpRequest())
@ -60,15 +63,21 @@ RemoteFile::Result RemoteFile::Download(std::string const & filePath) const
return {m_url, Status::NetworkError, "Unspecified network error"};
}
void RemoteFile::DownloadAsync(std::string const & filePath, ResultHandler && handler) const
void RemoteFile::DownloadAsync(std::string const & filePath,
StartDownloadingHandler && startDownloadingHandler,
ResultHandler && resultHandler) const
{
RemoteFile remoteFile = *this;
GetPlatform().RunTask(Platform::Thread::Network,
[filePath, remoteFile = std::move(remoteFile), handler = std::move(handler)]()
[filePath, remoteFile = std::move(remoteFile),
startDownloadingHandler = std::move(startDownloadingHandler),
resultHandler = std::move(resultHandler)]
{
if (startDownloadingHandler)
startDownloadingHandler(filePath);
auto result = remoteFile.Download(filePath);
if (handler)
handler(std::move(result), filePath);
if (resultHandler)
resultHandler(std::move(result), filePath);
});
}
} // namespace platform

View file

@ -40,8 +40,11 @@ public:
Result Download(std::string const & filePath) const;
using StartDownloadingHandler = std::function<void(std::string const & filePath)>;
using ResultHandler = std::function<void(Result &&, std::string const & filePath)>;
void DownloadAsync(std::string const & filePath, ResultHandler && handler) const;
void DownloadAsync(std::string const & filePath,
StartDownloadingHandler && startDownloadingHandler,
ResultHandler && resultHandler) const;
private:
std::string const m_url;