From e2dd416da70ceb04f756cba5a33b3acfb103ef45 Mon Sep 17 00:00:00 2001 From: vng Date: Fri, 16 Aug 2013 17:59:44 +0300 Subject: [PATCH] Save last category and color for bookmarks. --- android/jni/com/mapswithme/maps/Framework.cpp | 4 +- .../maps/bookmarks/data/BookmarkManager.cpp | 8 +-- iphone/Maps/Bookmarks/PlacePageVC.mm | 61 ++++++++-------- map/bookmark.cpp | 26 ++++--- map/bookmark.hpp | 11 ++- map/bookmark_manager.cpp | 70 +++++++++++++------ map/bookmark_manager.hpp | 11 ++- map/framework.cpp | 22 +++--- map/framework.hpp | 6 +- 9 files changed, 128 insertions(+), 91 deletions(-) diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index 872b2c8db2..5b9a6479f0 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -563,9 +563,7 @@ namespace android void Framework::ReplaceBookmark(BookmarkAndCategory const & ind, Bookmark & bm) { - BookmarkCategory * pCat = m_work.GetBmCategory(ind.first); - pCat->ReplaceBookmark(ind.second, bm); - pCat->SaveToKMLFile(); + m_work.ReplaceBookmark(ind.first, ind.second, bm); } size_t Framework::ChangeBookmarkCategory(BookmarkAndCategory const & ind, size_t newCat) diff --git a/android/jni/com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp b/android/jni/com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp index 37d2b1ecfe..ebe6923a30 100644 --- a/android/jni/com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp +++ b/android/jni/com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp @@ -89,15 +89,15 @@ extern "C" const m2::PointD glbPoint(MercatorBounds::LonToX(lon), MercatorBounds::LatToY(lat)); - /// @todo Add with the last pin's color. - Bookmark bmk(glbPoint, jni::ToNativeString(env, name), "placemark-red"); - return g_framework->AddBookmark(frm()->LastEditedCategory(), bmk).second; + ::Framework * f = frm(); + Bookmark bmk(glbPoint, jni::ToNativeString(env, name), f->LastEditedBMType()); + return g_framework->AddBookmark(frm()->LastEditedBMCategory(), bmk).second; } JNIEXPORT jint JNICALL Java_com_mapswithme_maps_bookmarks_data_BookmarkManager_nativeGetLastEditedCategory( JNIEnv * env, jobject thiz) { - return frm()->LastEditedCategory(); + return frm()->LastEditedBMCategory(); } } diff --git a/iphone/Maps/Bookmarks/PlacePageVC.mm b/iphone/Maps/Bookmarks/PlacePageVC.mm index 8386f2c6ac..f02e9b1835 100644 --- a/iphone/Maps/Bookmarks/PlacePageVC.mm +++ b/iphone/Maps/Bookmarks/PlacePageVC.mm @@ -117,7 +117,6 @@ typedef enum {Editing, Saved} Mode; @property(nonatomic, assign) BookmarkAndCategory pinEditedBookmark; @property(nonatomic, assign) CGPoint pinGlobalPosition; -@property(nonatomic, copy) NSString * pinType; @property (nonatomic, retain) NSArray * pinsArray; @property (nonatomic, retain) PlaceAndCompasView * placeAndCompass; @@ -130,13 +129,11 @@ typedef enum {Editing, Saved} Mode; self = [super initWithStyle:UITableViewStyleGrouped]; if (self) { - char const * bestType = info.GetBestType(); char const * pinName = info.GetPinName().c_str(); [self initializeProperties:[NSString stringWithUTF8String:pinName ? pinName : ""] notes:@"" - color:@"placemark-red" - category:MakeEmptyBookmarkAndCategory() point:point - type:[NSString stringWithUTF8String: bestType ? bestType : ""]]; + color:@"" + category:MakeEmptyBookmarkAndCategory() point:point]; m_mode = Editing; [self createPinPickerArray]; } @@ -151,10 +148,9 @@ typedef enum {Editing, Saved} Mode; m_mode = Editing; [self initializeProperties:[NSString stringWithUTF8String:apiPoint.m_name.c_str()] notes:@"" - color:@"placemark-red" - category:MakeEmptyBookmarkAndCategory() - point:CGPointMake(MercatorBounds::LonToX(apiPoint.m_lon), MercatorBounds::LatToY(apiPoint.m_lat)) - type:@""]; + color:@"" + category:MakeEmptyBookmarkAndCategory() + point:CGPointMake(MercatorBounds::LonToX(apiPoint.m_lon), MercatorBounds::LatToY(apiPoint.m_lat))]; [self createPinPickerArray]; } return self; @@ -165,22 +161,21 @@ typedef enum {Editing, Saved} Mode; self = [super initWithStyle:UITableViewStyleGrouped]; if (self) { - BookmarkCategory * cat = GetFramework().GetBmCategory(bmAndCat.first); - Bookmark * bm = cat->GetBookmark(bmAndCat.second); + Framework const & f = GetFramework(); + + BookmarkCategory const * cat = f.GetBmCategory(bmAndCat.first); + Bookmark const * bm = cat->GetBookmark(bmAndCat.second); search::AddressInfo info; CGPoint const pt = CGPointMake(bm->GetOrg().x, bm->GetOrg().y); - GetFramework().GetAddressInfoForGlobalPoint(bm->GetOrg(), info); + f.GetAddressInfoForGlobalPoint(bm->GetOrg(), info); - char const * c = info.GetBestType(); - [self initializeProperties:[NSString stringWithUTF8String:bm->GetName().c_str()] notes:[NSString stringWithUTF8String:bm->GetDescription().c_str()] color:[NSString stringWithUTF8String:bm->GetType().c_str()] - category:bmAndCat - point:CGPointMake(pt.x, pt.y) - type:[NSString stringWithUTF8String: c ? c : ""]]; + category:bmAndCat + point:CGPointMake(pt.x, pt.y)]; m_mode = Saved; [self createPinPickerArray]; @@ -196,10 +191,9 @@ typedef enum {Editing, Saved} Mode; m_mode = Editing; [self initializeProperties:name notes:@"" - color:@"placemark-red" - category:MakeEmptyBookmarkAndCategory() - point:point - type:@""]; + color:@"" + category:MakeEmptyBookmarkAndCategory() + point:point]; [self createPinPickerArray]; } return self; @@ -213,7 +207,6 @@ typedef enum {Editing, Saved} Mode; self.pinTitle = nil; self.pinNotes = nil; self.pinColor = nil; - self.pinType = nil; self.placeAndCompass = nil; [super dealloc]; } @@ -620,6 +613,7 @@ typedef enum {Editing, Saved} Mode; [[Statistics instance] logEvent:@"Bookmark Description Field" withParameters:@{@"Changed" : @"NO"}]; else [[Statistics instance] logEvent:@"Bookmark Description Field" withParameters:@{@"Changed" : @"YES"}]; + if (_pinEditedBookmark.first != m_categoryIndex) { [[Statistics instance] logEvent:@"Bookmark Category" withParameters:@{@"Changed" : @"YES"}]; @@ -630,10 +624,10 @@ typedef enum {Editing, Saved} Mode; else { [[Statistics instance] logEvent:@"Bookmark Category" withParameters:@{@"Changed" : @"NO"}]; - bm->SetName([self.pinTitle UTF8String]); - bm->SetType([self.pinColor UTF8String]); - bm->SetDescription([self.pinNotes UTF8String]); - cat->SaveToKMLFile(); + + Bookmark newBm(bm->GetOrg(), [self.pinTitle UTF8String], [self.pinColor UTF8String]); + newBm.SetDescription([self.pinNotes UTF8String]); + f.ReplaceBookmark(_pinEditedBookmark.first, _pinEditedBookmark.second, newBm); } } } @@ -646,18 +640,19 @@ typedef enum {Editing, Saved} Mode; _pinEditedBookmark = pair(index, GetFramework().AddBookmark(index, bm)); } --(void)initializeProperties:(NSString *)name notes:(NSString *)notes color:(NSString *)color category:(BookmarkAndCategory) bmAndCat point: - (CGPoint)point type:(NSString *)type +-(void)initializeProperties:(NSString *)name notes:(NSString *)notes color:(NSString *)color category:(BookmarkAndCategory) bmAndCat point:(CGPoint)point { + Framework & f = GetFramework(); + self.pinTitle = name; self.pinNotes = notes; - self.pinColor = color; - self.pinEditedBookmark =bmAndCat; - m_categoryIndex = bmAndCat.first == - 1 ? GetFramework().LastEditedCategory():bmAndCat.first; + self.pinColor = (color.length == 0 ? [NSString stringWithUTF8String:f.LastEditedBMType().c_str()] : color); + self.pinEditedBookmark = bmAndCat; + m_categoryIndex = (bmAndCat.first == - 1 ? f.LastEditedBMCategory() : bmAndCat.first); self.pinGlobalPosition = point; - self.pinType = type; + m_categoryIndexStatistics = m_categoryIndex; - m_numberOfCategories = GetFramework().GetBmCategoriesCount(); + m_numberOfCategories = f.GetBmCategoriesCount(); } -(UITextView *)createTextFieldForCell:(UIFont *)font color:(UIColor *)color diff --git a/map/bookmark.cpp b/map/bookmark.cpp index da23f9ee3f..efdb28780f 100644 --- a/map/bookmark.cpp +++ b/map/bookmark.cpp @@ -105,27 +105,28 @@ namespace } } -namespace bookmark_impl +namespace { + static char const * s_arrSupportedColors[] = + { + "placemark-red", "placemark-blue", "placemark-purple", "placemark-yellow", + "placemark-pink", "placemark-brown", "placemark-green", "placemark-orange" + }; + class KMLParser { // Fixes icons which are not supported by MapsWithMe string GetSupportedBMType(string const & s) const { - static char const * icons[] = { - "placemark-red", "placemark-blue", "placemark-purple", "placemark-yellow", - "placemark-pink", "placemark-brown", "placemark-green", "placemark-orange" - }; - // Remove leading '#' symbol string const result = s.substr(1); - for (size_t i = 0; i < ARRAY_SIZE(icons); ++i) - if (result == icons[i]) + for (size_t i = 0; i < ARRAY_SIZE(s_arrSupportedColors); ++i) + if (result == s_arrSupportedColors[i]) return result; // Not recognized symbols are replaced with default one LOG(LWARNING, ("Icon", result, "for bookmark", m_name, "is not supported")); - return icons[0]; + return s_arrSupportedColors[0]; } BookmarkCategory & m_category; @@ -273,10 +274,15 @@ namespace bookmark_impl }; } +string BookmarkCategory::GetDefaultType() +{ + return s_arrSupportedColors[0]; +} + bool BookmarkCategory::LoadFromKML(ReaderPtr const & reader) { ReaderSource > src(reader); - bookmark_impl::KMLParser parser(*this); + KMLParser parser(*this); if (ParseXML(src, parser, true)) return true; else diff --git a/map/bookmark.hpp b/map/bookmark.hpp index 7fc22b73a5..a2e04ad42a 100644 --- a/map/bookmark.hpp +++ b/map/bookmark.hpp @@ -30,10 +30,10 @@ public: m2::PointD const & GetOrg() const { return m_org; } string const & GetName() const { return m_name; } - void SetName(string const & name) { m_name = name; } + //void SetName(string const & name) { m_name = name; } /// @return Now its a bookmark color - name of icon file string const & GetType() const { return m_type; } - void SetType(string const & type) { m_type = type; } + //void SetType(string const & type) { m_type = type; } m2::RectD GetViewport() const { return m2::RectD(m_org, m_org); } string const & GetDescription() const { return m_description; } @@ -47,11 +47,6 @@ public: void SetScale(double scale) { m_scale = scale; } }; -namespace bookmark_impl -{ - class KMLParser; -} - class BookmarkCategory : private noncopyable { string m_name; @@ -70,6 +65,8 @@ public: void ClearBookmarks(); + static string GetDefaultType(); + /// @name Theese functions are called from Framework only. //@{ void AddBookmark(Bookmark const & bm); diff --git a/map/bookmark_manager.cpp b/map/bookmark_manager.cpp index c414dcfff4..7679c423f7 100644 --- a/map/bookmark_manager.cpp +++ b/map/bookmark_manager.cpp @@ -2,6 +2,7 @@ #include "framework.hpp" #include "../platform/platform.hpp" +#include "../platform/settings.hpp" #include "../indexer/scales.hpp" @@ -12,7 +13,8 @@ #include "../std/vector.hpp" -BookmarkManager::BookmarkManager(Framework& f):m_framework(f) +BookmarkManager::BookmarkManager(Framework & f) + : m_framework(f) { m_additionalPoiLayer = new BookmarkCategory(""); } @@ -23,7 +25,25 @@ BookmarkManager::~BookmarkManager() ClearBookmarks(); } -void BookmarkManager::drawCategory(BookmarkCategory const * cat, shared_ptr const & e) +namespace +{ +char const * BOOKMARK_CATEGORY = "LastBookmarkCategory"; +char const * BOOKMARK_TYPE = "LastBookmarkType"; +} + +void BookmarkManager::SaveState() const +{ + Settings::Set(BOOKMARK_CATEGORY, m_lastCategoryUrl); + Settings::Set(BOOKMARK_TYPE, m_lastType); +} + +void BookmarkManager::LoadState() +{ + Settings::Get(BOOKMARK_CATEGORY, m_lastCategoryUrl); + Settings::Get(BOOKMARK_TYPE, m_lastType); +} + +void BookmarkManager::DrawCategory(BookmarkCategory const * cat, shared_ptr const & e) { Navigator & navigator = m_framework.GetNavigator(); InformationDisplay & informationDisplay = m_framework.GetInformationDisplay(); @@ -38,10 +58,7 @@ void BookmarkManager::drawCategory(BookmarkCategory const * cat, shared_ptrGetType().c_str(), - navigator.GtoP(org)); - } + informationDisplay.drawPlacemark(pDrawer, bm->GetType().c_str(), navigator.GtoP(org)); } } @@ -56,21 +73,20 @@ void BookmarkManager::LoadBookmarks() ClearBookmarks(); string const dir = GetPlatform().WritableDir(); + Platform::FilesList files; Platform::GetFilesByExt(dir, BOOKMARKS_FILE_EXTENSION, files); for (size_t i = 0; i < files.size(); ++i) - { - LoadBookmark(dir+files[i]); - } + LoadBookmark(dir + files[i]); + + LoadState(); } void BookmarkManager::LoadBookmark(string const & filePath) { BookmarkCategory * cat = BookmarkCategory::CreateFromKMLFile(filePath); if (cat) - { m_categories.push_back(cat); - } } size_t BookmarkManager::AddBookmark(size_t categoryIndex, Bookmark & bm) @@ -84,7 +100,20 @@ size_t BookmarkManager::AddBookmark(size_t categoryIndex, Bookmark & bm) pCat->SaveToKMLFile(); m_lastCategoryUrl = pCat->GetFileName(); - return pCat->GetBookmarksCount() - 1; + m_lastType = bm.GetType(); + SaveState(); + + return (pCat->GetBookmarksCount() - 1); +} + +void BookmarkManager::ReplaceBookmark(size_t catIndex, size_t bmIndex, Bookmark const & bm) +{ + BookmarkCategory * pCat = m_categories[catIndex]; + pCat->ReplaceBookmark(bmIndex, bm); + pCat->SaveToKMLFile(); + + m_lastType = bm.GetType(); + SaveState(); } size_t BookmarkManager::LastEditedBMCategory() @@ -94,13 +123,18 @@ size_t BookmarkManager::LastEditedBMCategory() if (m_categories[i]->GetFileName() == m_lastCategoryUrl) return i; } + if (m_categories.empty()) - { m_categories.push_back(new BookmarkCategory(m_framework.GetStringsBundle().GetString("my_places"))); - } + return 0; } +string BookmarkManager::LastEditedBMType() const +{ + return (m_lastType.empty() ? BookmarkCategory::GetDefaultType() : m_lastType); +} + BookmarkCategory * BookmarkManager::GetBmCategory(size_t index) const { return (index < m_categories.size() ? m_categories[index] : 0); @@ -117,15 +151,11 @@ void BookmarkManager::DrawBookmarks(shared_ptr const & e) for (size_t i = 0; i < m_categories.size(); ++i) { if (GetBmCategory(i)->IsVisible()) - { - drawCategory(m_categories[i], e); - } + DrawCategory(m_categories[i], e); } if (m_additionalPoiLayer->IsVisible()) - { - drawCategory(m_additionalPoiLayer, e); - } + DrawCategory(m_additionalPoiLayer, e); } void BookmarkManager::DeleteBmCategory(CategoryIter i) diff --git a/map/bookmark_manager.hpp b/map/bookmark_manager.hpp index caaef46958..6277144836 100644 --- a/map/bookmark_manager.hpp +++ b/map/bookmark_manager.hpp @@ -11,14 +11,19 @@ class BookmarkManager : private noncopyable { vector m_categories; string m_lastCategoryUrl; + string m_lastType; Framework & m_framework; BookmarkCategory * m_additionalPoiLayer; typedef vector::iterator CategoryIter; - void drawCategory(BookmarkCategory const * cat, shared_ptr const & e); + void DrawCategory(BookmarkCategory const * cat, shared_ptr const & e); + + void SaveState() const; + void LoadState(); + public: - BookmarkManager(Framework& f); + BookmarkManager(Framework & f); ~BookmarkManager(); void ClearBookmarks(); @@ -29,8 +34,10 @@ public: /// Client should know where it adds bookmark size_t AddBookmark(size_t categoryIndex, Bookmark & bm); + void ReplaceBookmark(size_t catIndex, size_t bmIndex, Bookmark const & bm); size_t LastEditedBMCategory(); + string LastEditedBMType() const; inline size_t GetBmCategoriesCount() const { return m_categories.size(); } diff --git a/map/framework.cpp b/map/framework.cpp index 736278afb9..40dd226a5b 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -371,9 +371,8 @@ void Framework::RemoveLocalMaps() void Framework::LoadBookmarks() { - if (!GetPlatform().IsPro()) - return; - m_bmManager.LoadBookmarks(); + if (GetPlatform().IsPro()) + m_bmManager.LoadBookmarks(); } size_t Framework::AddBookmark(size_t categoryIndex, Bookmark & bm) @@ -381,6 +380,11 @@ size_t Framework::AddBookmark(size_t categoryIndex, Bookmark & bm) return m_bmManager.AddBookmark(categoryIndex, bm); } +void Framework::ReplaceBookmark(size_t catIndex, size_t bmIndex, Bookmark const & bm) +{ + m_bmManager.ReplaceBookmark(catIndex, bmIndex, bm); +} + size_t Framework::AddCategory(string const & categoryName) { return m_bmManager.CreateBmCategory(categoryName); @@ -405,11 +409,6 @@ BookmarkCategory * Framework::GetBmCategory(size_t index) const return m_bmManager.GetBmCategory(index); } -size_t Framework::LastEditedCategory() -{ - return m_bmManager.LastEditedBMCategory(); -} - bool Framework::DeleteBmCategory(size_t index) { return m_bmManager.DeleteBmCategory(index); @@ -1367,12 +1366,15 @@ anim::Controller * Framework::GetAnimController() const return m_animController.get(); } +/* void Framework::AddBookmarkAndSetViewport(Bookmark & bm, m2::RectD const & viewPort) { - size_t const catIndex = LastEditedCategory(); - m_bmManager.AddBookmark(catIndex, bm); + bm.SetType(LastEditedBMType()); + m_bmManager.AddBookmark(LastEditedBMCategory(), bm); + ShowRectExVisibleScale(viewPort); } +*/ bool Framework::SetViewportByURL(string const & url, url_scheme::ApiPoint & balloonPoint) { diff --git a/map/framework.hpp b/map/framework.hpp index 4709597226..679c51e0c7 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -178,6 +178,7 @@ public: /// @return Created bookmark index in category. size_t AddBookmark(size_t categoryIndex, Bookmark & bm); + void ReplaceBookmark(size_t catIndex, size_t bmIndex, Bookmark const & bm); /// @return Created bookmark category index. size_t AddCategory(string const & categoryName); @@ -185,7 +186,8 @@ public: /// @returns 0 if category is not found BookmarkCategory * GetBmCategory(size_t index) const; - size_t LastEditedCategory(); + size_t LastEditedBMCategory() { return m_bmManager.LastEditedBMCategory(); } + string LastEditedBMType() const { return m_bmManager.LastEditedBMType(); } /// Delete bookmarks category with all bookmarks. /// @return true if category was deleted @@ -258,7 +260,7 @@ public: private: search::Engine * GetSearchEngine() const; - void AddBookmarkAndSetViewport(Bookmark & bm, m2::RectD const & viewPort); + //void AddBookmarkAndSetViewport(Bookmark & bm, m2::RectD const & viewPort); public: m2::RectD GetCurrentViewport() const;