Review fixes.

This commit is contained in:
Daria Volvenkova 2017-12-11 20:34:37 +03:00 committed by Yuri Gorshenin
parent 0341d4508b
commit dd15c6d90f
6 changed files with 154 additions and 72 deletions

View file

@ -27,6 +27,8 @@
#include <algorithm>
using namespace std::placeholders;
namespace
{
char const * BOOKMARK_CATEGORY = "LastBookmarkCategory";
@ -64,23 +66,14 @@ std::string const GenerateValidAndUniqueFilePathForKML(std::string const & fileN
}
} // namespace
BookmarkManager::BookmarkManager(GetStringsBundleFn && getStringsBundleFn,
CreatedBookmarksCallback && createdBookmarksCallback,
UpdatedBookmarksCallback && updatedBookmarksCallback,
DeletedBookmarksCallback && deletedBookmarksCallback)
: m_getStringsBundle(std::move(getStringsBundleFn))
, m_createdBookmarksCallback(std::move(createdBookmarksCallback))
, m_updatedBookmarksCallback(std::move(updatedBookmarksCallback))
, m_deletedBookmarksCallback(std::move(deletedBookmarksCallback))
, m_bookmarksListeners(std::bind(&BookmarkManager::OnCreateUserMarks, this,
std::placeholders::_1, std::placeholders::_2),
std::bind(&BookmarkManager::OnUpdateUserMarks, this,
std::placeholders::_1, std::placeholders::_2),
std::bind(&BookmarkManager::OnDeleteUserMarks, this,
std::placeholders::_1, std::placeholders::_2))
BookmarkManager::BookmarkManager(Callbacks && callbacks)
: m_callbacks(std::move(callbacks))
, m_bookmarksListeners(std::bind(&BookmarkManager::OnCreateUserMarks, this, _1, _2),
std::bind(&BookmarkManager::OnUpdateUserMarks, this, _1, _2),
std::bind(&BookmarkManager::OnDeleteUserMarks, this, _1, _2))
, m_needTeardown(false)
{
ASSERT(m_getStringsBundle != nullptr, ());
ASSERT(m_callbacks.m_getStringsBundle != nullptr, ());
m_userMarkLayers.emplace_back(my::make_unique<SearchUserMarkContainer>());
m_userMarkLayers.emplace_back(my::make_unique<ApiUserMarkContainer>());
@ -382,7 +375,7 @@ size_t BookmarkManager::LastEditedBMCategory()
}
if (m_categories.empty())
CreateBmCategory(m_getStringsBundle().GetString("my_places"));
CreateBmCategory(m_callbacks.m_getStringsBundle().GetString("my_places"));
return 0;
}
@ -397,52 +390,53 @@ BookmarkCategory * BookmarkManager::GetBmCategory(size_t index) const
return (index < m_categories.size() ? m_categories[index].get() : 0);
}
void BookmarkManager::OnCreateUserMarks(UserMarkContainer * container, df::IDCollection const & markIds)
void BookmarkManager::OnCreateUserMarks(UserMarkContainer const & container, df::IDCollection const & markIds)
{
if (container->GetType() != UserMark::Type::BOOKMARK)
if (container.GetType() != UserMark::Type::BOOKMARK)
return;
if (m_createdBookmarksCallback == nullptr)
if (m_callbacks.m_createdBookmarksCallback == nullptr)
return;
std::vector<std::pair<df::MarkID, BookmarkData>> marksInfo;
GetBookmarksData(container, markIds, marksInfo);
m_createdBookmarksCallback(marksInfo);
m_callbacks.m_createdBookmarksCallback(marksInfo);
}
void BookmarkManager::OnUpdateUserMarks(UserMarkContainer * container, df::IDCollection const & markIds)
void BookmarkManager::OnUpdateUserMarks(UserMarkContainer const & container, df::IDCollection const & markIds)
{
if (container->GetType() != UserMark::Type::BOOKMARK)
if (container.GetType() != UserMark::Type::BOOKMARK)
return;
if (m_updatedBookmarksCallback == nullptr)
if (m_callbacks.m_updatedBookmarksCallback == nullptr)
return;
std::vector<std::pair<df::MarkID, BookmarkData>> marksInfo;
GetBookmarksData(container, markIds, marksInfo);
m_updatedBookmarksCallback(marksInfo);
m_callbacks.m_updatedBookmarksCallback(marksInfo);
}
void BookmarkManager::OnDeleteUserMarks(UserMarkContainer * container, df::IDCollection const & markIds)
void BookmarkManager::OnDeleteUserMarks(UserMarkContainer const & container, df::IDCollection const & markIds)
{
if (container->GetType() != UserMark::Type::BOOKMARK)
if (container.GetType() != UserMark::Type::BOOKMARK)
return;
if (m_deletedBookmarksCallback == nullptr)
if (m_callbacks.m_deletedBookmarksCallback == nullptr)
return;
m_deletedBookmarksCallback(markIds);
m_callbacks.m_deletedBookmarksCallback(markIds);
}
void BookmarkManager::GetBookmarksData(UserMarkContainer * container, df::IDCollection const & markIds,
void BookmarkManager::GetBookmarksData(UserMarkContainer const & container, df::IDCollection const & markIds,
std::vector<std::pair<df::MarkID, BookmarkData>> & data) const
{
data.clear();
data.reserve(markIds.size());
for (auto markId : markIds)
{
auto const userMark = container->GetUserMarkById(markId);
auto const userMark = container.GetUserMarkById(markId);
ASSERT(userMark != nullptr, ());
ASSERT(dynamic_cast<Bookmark const *>(userMark) != nullptr, ());

View file

@ -26,15 +26,10 @@ class BookmarkManager final
using CategoryIter = CategoriesCollection::iterator;
using UserMarkLayers = std::vector<std::unique_ptr<UserMarkContainer>>;
using GetStringsBundleFn = std::function<StringsBundle const &()>;
public:
using AsyncLoadingStartedCallback = std::function<void()>;
using AsyncLoadingFinishedCallback = std::function<void()>;
using AsyncLoadingFileCallback = std::function<void(std::string const &, bool)>;
using CreatedBookmarksCallback = std::function<void(std::vector<std::pair<df::MarkID, BookmarkData>> const &)>;
using UpdatedBookmarksCallback = std::function<void(std::vector<std::pair<df::MarkID, BookmarkData>> const &)>;
using DeletedBookmarksCallback = std::function<void(std::vector<df::MarkID> const &)>;
struct AsyncLoadingCallbacks
{
@ -44,10 +39,29 @@ public:
AsyncLoadingFileCallback m_onFileSuccess;
};
explicit BookmarkManager(GetStringsBundleFn && getStringsBundleFn,
CreatedBookmarksCallback && createdBookmarksCallback,
UpdatedBookmarksCallback && updatedBookmarksCallback,
DeletedBookmarksCallback && deletedBookmarksCallback);
struct Callbacks
{
using GetStringsBundleFn = std::function<StringsBundle const &()>;
using CreatedBookmarksCallback = std::function<void(std::vector<std::pair<df::MarkID, BookmarkData>> const &)>;
using UpdatedBookmarksCallback = std::function<void(std::vector<std::pair<df::MarkID, BookmarkData>> const &)>;
using DeletedBookmarksCallback = std::function<void(std::vector<df::MarkID> const &)>;
template <typename StringsBundleGetter, typename CreateListener, typename UpdateListener, typename DeleteListener>
Callbacks(StringsBundleGetter && stringsBundleGetter, CreateListener && createListener,
UpdateListener && updateListener, DeleteListener && deleteListener)
: m_getStringsBundle(std::forward<StringsBundleGetter>(stringsBundleGetter))
, m_createdBookmarksCallback(std::forward<CreateListener>(createListener))
, m_updatedBookmarksCallback(std::forward<UpdateListener>(updateListener))
, m_deletedBookmarksCallback(std::forward<DeleteListener>(deleteListener))
{}
GetStringsBundleFn m_getStringsBundle;
CreatedBookmarksCallback m_createdBookmarksCallback;
UpdatedBookmarksCallback m_updatedBookmarksCallback;
DeletedBookmarksCallback m_deletedBookmarksCallback;
};
explicit BookmarkManager(Callbacks && callbacks);
~BookmarkManager();
void SetDrapeEngine(ref_ptr<df::DrapeEngine> engine);
@ -113,16 +127,13 @@ private:
void NotifyAboutFile(bool success, std::string const & filePath, bool isTemporaryFile);
void LoadBookmarkRoutine(std::string const & filePath, bool isTemporaryFile);
void OnCreateUserMarks(UserMarkContainer * container, df::IDCollection const & markIds);
void OnUpdateUserMarks(UserMarkContainer * container, df::IDCollection const & markIds);
void OnDeleteUserMarks(UserMarkContainer * container, df::IDCollection const & markIds);
void GetBookmarksData(UserMarkContainer * container, df::IDCollection const & markIds,
void OnCreateUserMarks(UserMarkContainer const & container, df::IDCollection const & markIds);
void OnUpdateUserMarks(UserMarkContainer const & container, df::IDCollection const & markIds);
void OnDeleteUserMarks(UserMarkContainer const & container, df::IDCollection const & markIds);
void GetBookmarksData(UserMarkContainer const & container, df::IDCollection const & markIds,
std::vector<std::pair<df::MarkID, BookmarkData>> & data) const;
GetStringsBundleFn m_getStringsBundle;
CreatedBookmarksCallback m_createdBookmarksCallback;
UpdatedBookmarksCallback m_updatedBookmarksCallback;
DeletedBookmarksCallback m_deletedBookmarksCallback;
Callbacks m_callbacks;
UserMarkContainer::Listeners m_bookmarksListeners;
df::DrapeEngineSafePtr m_drapeEngine;

View file

@ -359,19 +359,19 @@ Framework::Framework(FrameworkParams const & params)
: m_startForegroundTime(0.0)
, m_storage(platform::migrate::NeedMigrate() ? COUNTRIES_OBSOLETE_FILE : COUNTRIES_FILE)
, m_enabledDiffs(params.m_enableDiffs)
, m_bmManager([this]() -> StringsBundle const & { return m_stringsBundle; },
[](std::vector<std::pair<df::MarkID, BookmarkData>> const & marks)
{
// TODO: Add processing of the created marks.
},
[](std::vector<std::pair<df::MarkID, BookmarkData>> const & marks)
{
// TODO: Add processing of the updated marks.
},
[](std::vector<df::MarkID> const & marks)
{
// TODO: Add processing of the deleted marks.
})
, m_bmManager(BookmarkManager::Callbacks([this]() -> StringsBundle const & { return m_stringsBundle; },
[](std::vector<std::pair<df::MarkID, BookmarkData>> const & marks)
{
// TODO: Add processing of the created marks.
},
[](std::vector<std::pair<df::MarkID, BookmarkData>> const & marks)
{
// TODO: Add processing of the updated marks.
},
[](std::vector<df::MarkID> const & marks)
{
// TODO: Add processing of the deleted marks.
}))
, m_isRenderingEnabled(true)
, m_routingManager(RoutingManager::Callbacks([this]() -> Index & { return m_model.GetIndex(); },
[this]() -> storage::CountryInfoGetter & { return GetCountryInfoGetter(); },

View file

@ -13,8 +13,11 @@
#include "coding/internal/file_data.hpp"
#include "std/fstream.hpp"
#include "std/unique_ptr.hpp"
#include <fstream>
#include <memory>
#include <set>
using namespace std;
namespace
{
@ -652,3 +655,83 @@ UNIT_TEST(TrackParsingTest_2)
TEST_GREATER(track->GetLayerCount(), 0, ());
TEST_EQUAL(track->GetColor(0), dp::Color(57, 255, 32, 255), ());
}
UNIT_TEST(Bookmarks_Listeners)
{
set<df::MarkID> createdMarksResult;
set<df::MarkID> updatedMarksResult;
set<df::MarkID> deletedMarksResult;
set<df::MarkID> createdMarks;
set<df::MarkID> updatedMarks;
set<df::MarkID> deletedMarks;
auto const checkNotifications = [&](BookmarkCategory & cat)
{
cat.NotifyChanges();
TEST_EQUAL(createdMarks, createdMarksResult, ());
TEST_EQUAL(updatedMarks, updatedMarksResult, ());
TEST_EQUAL(deletedMarks, deletedMarksResult, ());
};
auto const refresh = [&](BookmarkCategory & cat)
{
df::MarkIDCollection createdIds;
df::MarkIDCollection deletedIds;
cat.AcceptChanges(createdIds, deletedIds);
createdMarksResult.clear();
updatedMarksResult.clear();
deletedMarksResult.clear();
createdMarks.clear();
updatedMarks.clear();
deletedMarks.clear();
};
auto const onCreate = [&createdMarksResult](UserMarkContainer const & container, df::IDCollection const & markIds)
{
createdMarksResult.insert(markIds.begin(), markIds.end());
};
auto const onUpdate = [&updatedMarksResult](UserMarkContainer const & container, df::IDCollection const & markIds)
{
updatedMarksResult.insert(markIds.begin(), markIds.end());
};
auto const onDelete = [&deletedMarksResult](UserMarkContainer const & container, df::IDCollection const & markIds)
{
deletedMarksResult.insert(markIds.begin(), markIds.end());
};
BookmarkCategory cat("Default", UserMarkContainer::Listeners(onCreate, onUpdate, onDelete));
auto bookmark0 = static_cast<Bookmark *>(cat.CreateUserMark(m2::PointD(0.0, 0.0)));
bookmark0->SetData(BookmarkData("name 0", "type 0"));
createdMarks.insert(bookmark0->GetId());
auto bookmark1 = static_cast<Bookmark *>(cat.CreateUserMark(m2::PointD(0.0, 0.0)));
bookmark1->SetData(BookmarkData("name 1", "type 1"));
createdMarks.insert(bookmark1->GetId());
createdMarks.erase(cat.GetUserMark(0)->GetId());
cat.DeleteUserMark(0);
checkNotifications(cat);
refresh(cat);
bookmark0 = static_cast<Bookmark *>(cat.GetUserMarkForEdit(0));
bookmark0->SetName("name 3");
updatedMarks.insert(cat.GetUserMark(0)->GetId());
checkNotifications(cat);
refresh(cat);
deletedMarks.insert(cat.GetUserMark(0)->GetId());
bookmark0 = static_cast<Bookmark *>(cat.GetUserMarkForEdit(0));
bookmark0->SetName("name 4");
cat.DeleteUserMark(0);
bookmark1 = static_cast<Bookmark *>(cat.CreateUserMark(m2::PointD(0.0, 0.0)));
createdMarks.insert(bookmark1->GetId());
bookmark1->SetData(BookmarkData("name 5", "type 5"));
checkNotifications(cat);
refresh(cat);
}

View file

@ -73,11 +73,6 @@ void UserMarkContainer::SetDrapeEngine(ref_ptr<df::DrapeEngine> engine)
m_drapeEngine.Set(engine);
}
void UserMarkContainer::SetListeners(Listeners const & listeners)
{
m_listeners = listeners;
}
UserMark const * UserMarkContainer::GetUserMarkById(df::MarkID id) const
{
for (auto const & mark : m_userMarks)
@ -111,7 +106,7 @@ void UserMarkContainer::NotifyListeners()
if (m_listeners.m_createListener != nullptr && !m_createdMarks.empty())
{
df::IDCollection marks(m_createdMarks.begin(), m_createdMarks.end());
m_listeners.m_createListener(this, marks);
m_listeners.m_createListener(*this, marks);
}
if (m_listeners.m_updateListener != nullptr)
{
@ -122,12 +117,12 @@ void UserMarkContainer::NotifyListeners()
marks.push_back(mark->GetId());
}
if (!marks.empty())
m_listeners.m_updateListener(this, marks);
m_listeners.m_updateListener(*this, marks);
}
if (m_listeners.m_deleteListener != nullptr && !m_removedMarks.empty())
{
df::IDCollection marks(m_removedMarks.begin(), m_removedMarks.end());
m_listeners.m_deleteListener(this, marks);
m_listeners.m_deleteListener(*this, marks);
}
}

View file

@ -39,7 +39,7 @@ class UserMarkContainer : public df::UserMarksProvider
{
public:
using TUserMarksList = std::deque<std::unique_ptr<UserMark>>;
using NotifyChangesFn = std::function<void (UserMarkContainer *, df::IDCollection const &)>;
using NotifyChangesFn = std::function<void (UserMarkContainer const &, df::IDCollection const &)>;
struct Listeners
{
@ -62,7 +62,6 @@ public:
~UserMarkContainer() override;
void SetDrapeEngine(ref_ptr<df::DrapeEngine> engine);
void SetListeners(Listeners const & listeners);
UserMark const * GetUserMarkById(df::MarkID id) const;
// If not found mark on rect result is nullptr.