diff --git a/map/bookmark_manager.cpp b/map/bookmark_manager.cpp index f3412512e2..eae0df2cec 100644 --- a/map/bookmark_manager.cpp +++ b/map/bookmark_manager.cpp @@ -1994,7 +1994,7 @@ void BookmarkManager::LoadBookmark(std::string const & filePath, bool isTemporar // Defer bookmark loading in case of another asynchronous process. if (!m_loadBookmarksFinished || m_asyncLoadingInProgress) { - m_bookmarkLoadingQueue.emplace_back(filePath, isTemporaryFile); + m_bookmarkLoadingQueue.emplace_back(filePath, isTemporaryFile, false); return; } @@ -2002,6 +2002,18 @@ void BookmarkManager::LoadBookmark(std::string const & filePath, bool isTemporar LoadBookmarkRoutine(filePath, isTemporaryFile); } +void BookmarkManager::ReloadBookmark(std::string const & filePath) +{ + CHECK_THREAD_CHECKER(m_threadChecker, ()); + if (!m_loadBookmarksFinished || m_asyncLoadingInProgress) + { + m_bookmarkLoadingQueue.emplace_back(filePath, false, true); + return; + } + NotifyAboutStartAsyncLoading(); + ReloadBookmarkRoutine(filePath); +} + void BookmarkManager::LoadBookmarkRoutine(std::string const & filePath, bool isTemporaryFile) { GetPlatform().RunTask(Platform::Thread::File, [this, filePath, isTemporaryFile]() @@ -2053,6 +2065,36 @@ void BookmarkManager::LoadBookmarkRoutine(std::string const & filePath, bool isT }); } +void BookmarkManager::ReloadBookmarkRoutine(std::string const & filePath) +{ + GetPlatform().RunTask(Platform::Thread::File, [this, filePath]() + { + if (m_needTeardown) + return; + + auto const ext = GetLowercaseFileExt(filePath); + std::unique_ptr kmlData; + if (ext == kKmlExtension) + kmlData = LoadKmlFile(filePath, KmlFileType::Text); + else if (ext == kGpxExtension) + kmlData = LoadKmlFile(filePath, KmlFileType::Gpx); + else + ASSERT(false, ("Unsupported bookmarks extension", ext)); + + if (m_needTeardown) + return; + + auto collection = std::make_shared(); + if (kmlData) + collection->emplace_back(std::move(filePath), std::move(kmlData)); + + if (m_needTeardown) + return; + + NotifyAboutFinishAsyncLoading(std::move(collection)); + }); +} + void BookmarkManager::NotifyAboutStartAsyncLoading() { if (m_needTeardown) @@ -2088,7 +2130,10 @@ void BookmarkManager::NotifyAboutFinishAsyncLoading(KMLDataCollectionPtr && coll if (!m_bookmarkLoadingQueue.empty()) { ASSERT(m_asyncLoadingInProgress, ()); - LoadBookmarkRoutine(m_bookmarkLoadingQueue.front().m_filename, + if (m_bookmarkLoadingQueue.front().m_isReloading) + ReloadBookmarkRoutine(m_bookmarkLoadingQueue.front().m_filename); + else + LoadBookmarkRoutine(m_bookmarkLoadingQueue.front().m_filename, m_bookmarkLoadingQueue.front().m_isTemporaryFile); m_bookmarkLoadingQueue.pop_front(); } @@ -2296,6 +2341,13 @@ bool BookmarkManager::HasBookmark(kml::MarkId markId) const return (GetBookmark(markId) != nullptr); } +bool BookmarkManager::HasTrack(kml::TrackId trackId) const +{ + CHECK_THREAD_CHECKER(m_threadChecker, ()); + ASSERT(IsBookmark(trackId), ()); + return (GetTrack(trackId) != nullptr); +} + void BookmarkManager::UpdateBmGroupIdList() { CHECK_THREAD_CHECKER(m_threadChecker, ()); @@ -2409,7 +2461,7 @@ void BookmarkManager::CheckAndResetLastIds() idStorage.ResetTrackId(); } -bool BookmarkManager::DeleteBmCategory(kml::MarkGroupId groupId) +bool BookmarkManager::DeleteBmCategory(kml::MarkGroupId groupId, bool deleteFile) { CHECK_THREAD_CHECKER(m_threadChecker, ()); auto it = m_categories.find(groupId); @@ -2419,7 +2471,8 @@ bool BookmarkManager::DeleteBmCategory(kml::MarkGroupId groupId) ClearGroup(groupId); m_changesTracker.OnDeleteGroup(groupId); - FileWriter::DeleteFileX(it->second->GetFileName()); + if (deleteFile) + FileWriter::DeleteFileX(it->second->GetFileName()); DeleteCompilations(it->second->GetCategoryData().m_compilationIds); m_categories.erase(it); @@ -3498,7 +3551,7 @@ void BookmarkManager::EditSession::SetCategoryCustomProperty(kml::MarkGroupId ca bool BookmarkManager::EditSession::DeleteBmCategory(kml::MarkGroupId groupId) { - return m_bmManager.DeleteBmCategory(groupId); + return m_bmManager.DeleteBmCategory(groupId, true); } void BookmarkManager::EditSession::NotifyChanges() diff --git a/map/bookmark_manager.hpp b/map/bookmark_manager.hpp index 1b329d9cc3..2d673011af 100644 --- a/map/bookmark_manager.hpp +++ b/map/bookmark_manager.hpp @@ -273,6 +273,7 @@ public: std::string GetCategoryName(kml::MarkGroupId categoryId) const; std::string GetCategoryFileName(kml::MarkGroupId categoryId) const; + kml::MarkGroupId GetCategoryByFileName(std::string const & fileName) const; m2::RectD GetCategoryRect(kml::MarkGroupId categoryId, bool addIconsSize) const; kml::CategoryData const & GetCategoryData(kml::MarkGroupId categoryId) const; @@ -283,6 +284,7 @@ public: size_t GetBmGroupsCount() const { return m_unsortedBmGroupsIdList.size(); }; bool HasBmCategory(kml::MarkGroupId groupId) const; bool HasBookmark(kml::MarkId markId) const; + bool HasTrack(kml::TrackId trackId) const; kml::MarkGroupId LastEditedBMCategory(); kml::PredefinedColor LastEditedBMColor() const; @@ -300,6 +302,7 @@ public: /// Scans and loads all kml files with bookmarks. void LoadBookmarks(); void LoadBookmark(std::string const & filePath, bool isTemporaryFile); + void ReloadBookmark(std::string const & filePath); /// Uses the same file name from which was loaded, or /// creates unique file name on first save and uses it every time. @@ -573,7 +576,7 @@ private: void SetCategoryTags(kml::MarkGroupId categoryId, std::vector const & tags); void SetCategoryAccessRules(kml::MarkGroupId categoryId, kml::AccessRules accessRules); void SetCategoryCustomProperty(kml::MarkGroupId categoryId, std::string const & key, std::string const & value); - bool DeleteBmCategory(kml::MarkGroupId groupId); + bool DeleteBmCategory(kml::MarkGroupId groupId, bool deleteFile); void ClearCategories(); void MoveBookmark(kml::MarkId bmID, kml::MarkGroupId curGroupID, kml::MarkGroupId newGroupID); @@ -609,6 +612,7 @@ private: void NotifyAboutFinishAsyncLoading(KMLDataCollectionPtr && collection); void NotifyAboutFile(bool success, std::string const & filePath, bool isTemporaryFile); void LoadBookmarkRoutine(std::string const & filePath, bool isTemporaryFile); + void ReloadBookmarkRoutine(std::string const & filePath); using BookmarksChecker = std::function; KMLDataCollectionPtr LoadBookmarks(std::string const & dir, std::string_view ext, @@ -628,8 +632,6 @@ private: kml::MarkGroupId CheckAndCreateDefaultCategory(); void CheckAndResetLastIds(); - kml::MarkGroupId GetCategoryByFileName(std::string const & fileName) const; - std::unique_ptr CollectBmGroupKMLData(BookmarkCategory const * group) const; KMLDataCollectionPtr PrepareToSaveBookmarks(kml::GroupIdCollection const & groupIdCollection); @@ -761,12 +763,13 @@ private: bool m_asyncLoadingInProgress = false; struct BookmarkLoaderInfo { - BookmarkLoaderInfo(std::string const & filename, bool isTemporaryFile) - : m_filename(filename), m_isTemporaryFile(isTemporaryFile) + BookmarkLoaderInfo(std::string const & filename, bool isTemporaryFile, bool isReloading) + : m_filename(filename), m_isTemporaryFile(isTemporaryFile), m_isReloading(isReloading) {} std::string m_filename; bool m_isTemporaryFile = false; + bool m_isReloading = false; }; std::list m_bookmarkLoadingQueue;