From f2cd0f29a297b3e18c58d509362dc14e5e258274 Mon Sep 17 00:00:00 2001 From: Sergei Kobec Date: Tue, 24 May 2016 11:15:52 +0300 Subject: [PATCH] TizenRefApp-6335 Extract SearchResult from SearchData Change-Id: I6b3fe9c785dc2e0423ca61b6c16297bca0015029 Signed-off-by: Sergei Kobec --- lib-common/inc/Contacts/Model/SearchData.h | 53 +++++--------- lib-common/inc/Contacts/Model/SearchResult.h | 80 ++++++++++++++++++++++ lib-common/src/Contacts/Model/SearchData.cpp | 34 ++++----- lib-common/src/Contacts/Model/SearchResult.cpp | 49 +++++++++++++ .../inc/Contacts/List/Model/PersonSearchData.h | 2 +- lib-contacts/inc/Contacts/List/PersonSearchItem.h | 11 +-- lib-contacts/src/Contacts/List/ListView.cpp | 3 +- .../src/Contacts/List/Model/PersonSearchData.cpp | 36 +++++----- .../src/Contacts/List/PersonSearchItem.cpp | 55 ++++++++------- 9 files changed, 212 insertions(+), 111 deletions(-) create mode 100644 lib-common/inc/Contacts/Model/SearchResult.h create mode 100644 lib-common/src/Contacts/Model/SearchResult.cpp diff --git a/lib-common/inc/Contacts/Model/SearchData.h b/lib-common/inc/Contacts/Model/SearchData.h index 9d7934a..0fe03c1 100644 --- a/lib-common/inc/Contacts/Model/SearchData.h +++ b/lib-common/inc/Contacts/Model/SearchData.h @@ -19,6 +19,7 @@ #define CONTACTS_MODEL_SEARCH_DATA_H #include "Contacts/Model/ContactData.h" +#include "Contacts/Model/SearchResult.h" #include "Common/Utils.h" #include "Utils/Range.h" @@ -32,15 +33,9 @@ namespace Contacts { public: /** - * @brief Determines which field conform to searchable string - * @see compare() + * @brief Callback to be called when SearchResult is set */ - enum MatchedField - { - MatchedNone, /**< Not matched */ - MatchedName, /**< Matched by name */ - MatchedNumber /**< Matched by number */ - }; + typedef std::function ChangeCallback; /** * @brief Create SearchData object @@ -75,50 +70,36 @@ namespace Contacts * name/number SearchData field. Create found substring if found one. * * @param[in] str Searchable string - * @return true if found something, false if not + * @return Search result if found something, nullptr if not */ - virtual bool compare(const std::string &str) = 0; + virtual SearchResultPtr compare(const std::string &str) = 0; /** - * @return Matched field - * @see MatchedField - */ - MatchedField getMatchedField() const; - - /** - * @return Matched string - */ - const char *getMatchedString() const; - - /** - * @return Matched substring + * @return ContactData reference */ - const Common::Substring &getMatchedSubstring() const; + ContactData &getContactData(); /** - * @return ContactData reference + * @return Search result */ - ContactData &getContactData(); + const SearchResult *getSearchResult() const; - protected: /** - * @brief Set all match parameters at once - * @param[in] field @see MatchedField - * @param[in] str Matched string - * @param[in] substr Matched substring + * @brief Set search result + * @param[in] searchResult Search result */ - void setMatch(MatchedField field, const char *str, Common::Substring substr); + void setSearchResult(SearchResultPtr searchResult); /** - * @brief Reset all match parameters to their default values + * @brief Set change callback + * @param[in] callback @see ChangedCallback */ - void resetMatch(); + void setChangeCallback(ChangeCallback callback); private: ContactData &m_ContactData; - MatchedField m_MatchedField; - const char *m_MatchedString; - Common::Substring m_MatchedSubstring; + SearchResultPtr m_SearchResult; + ChangeCallback m_OnChanged; }; } } diff --git a/lib-common/inc/Contacts/Model/SearchResult.h b/lib-common/inc/Contacts/Model/SearchResult.h new file mode 100644 index 0000000..1e1e9f4 --- /dev/null +++ b/lib-common/inc/Contacts/Model/SearchResult.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef CONTACTS_MODEL_SEARCH_RESULT_H +#define CONTACTS_MODEL_SEARCH_RESULT_H + +#include "Common/Utils.h" +#include + +namespace Contacts +{ + namespace Model + { + class SearchResult; + + typedef std::unique_ptr SearchResultPtr; + + class EXPORT_API SearchResult + { + public: + /** + * @brief Determines which field conform to searchable string + * @see compare() + */ + enum MatchedField + { + MatchedNone, /**< Not matched */ + MatchedName, /**< Matched by name */ + MatchedNumber /**< Matched by number */ + }; + + SearchResult(); + + /** + * @brief Create SearchResult object + * @param[in] field @see MatchedField + * @param[in] str Matched string + * @param[in] substr Matched substring + */ + SearchResult(MatchedField field, const char *str, ::Common::Substring substr); + + /** + * @return Matched field + * @see MatchedField + */ + MatchedField getMatchedField() const; + + /** + * @return Matched string + */ + const char *getMatchedString() const; + + /** + * @return Matched substring + */ + const ::Common::Substring &getMatchedSubstring() const; + + private: + MatchedField m_MatchedField; + const char *m_MatchedString; + ::Common::Substring m_MatchedSubstring; + }; + } +} + +#endif /* CONTACTS_MODEL_SEARCH_RESULT_H */ diff --git a/lib-common/src/Contacts/Model/SearchData.cpp b/lib-common/src/Contacts/Model/SearchData.cpp index b85a301..75f8730 100644 --- a/lib-common/src/Contacts/Model/SearchData.cpp +++ b/lib-common/src/Contacts/Model/SearchData.cpp @@ -22,9 +22,7 @@ using namespace Common; SearchData::SearchData(ContactData &contactData) : ContactData(contactData.getType()), - m_ContactData(contactData), - m_MatchedField(MatchedNone), - m_MatchedString(nullptr) + m_ContactData(contactData) { } @@ -48,34 +46,26 @@ const char *SearchData::getImagePath() const return m_ContactData.getImagePath(); } -SearchData::MatchedField SearchData::getMatchedField() const +ContactData &SearchData::getContactData() { - return m_MatchedField; + return m_ContactData; } -const char *SearchData::getMatchedString() const +const SearchResult *SearchData::getSearchResult() const { - return m_MatchedString; + return m_SearchResult.get(); } -const Substring &SearchData::getMatchedSubstring() const +void SearchData::setSearchResult(SearchResultPtr searchResult) { - return m_MatchedSubstring; -} + m_SearchResult = std::move(searchResult); -ContactData &SearchData::getContactData() -{ - return m_ContactData; -} - -void SearchData::setMatch(MatchedField field, const char *str, Common::Substring substr) -{ - m_MatchedField = field; - m_MatchedString = str; - m_MatchedSubstring = std::move(substr); + if (m_OnChanged) { + m_OnChanged(); + } } -void SearchData::resetMatch() +void SearchData::setChangeCallback(ChangeCallback callback) { - setMatch(MatchedNone, nullptr, {}); + m_OnChanged = std::move(callback); } diff --git a/lib-common/src/Contacts/Model/SearchResult.cpp b/lib-common/src/Contacts/Model/SearchResult.cpp new file mode 100644 index 0000000..5ee1333 --- /dev/null +++ b/lib-common/src/Contacts/Model/SearchResult.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "Contacts/Model/SearchResult.h" + +using namespace Contacts::Model; +using Common::Substring; + +SearchResult::SearchResult() + : m_MatchedField(MatchedNone), + m_MatchedString(nullptr) +{ +} + +SearchResult::SearchResult(MatchedField field, const char *str, Common::Substring substr) + : m_MatchedField(field), + m_MatchedString(str), + m_MatchedSubstring(substr) +{ +} + +SearchResult::MatchedField SearchResult::getMatchedField() const +{ + return m_MatchedField; +} + +const char *SearchResult::getMatchedString() const +{ + return m_MatchedString; +} + +const Substring &SearchResult::getMatchedSubstring() const +{ + return m_MatchedSubstring; +} diff --git a/lib-contacts/inc/Contacts/List/Model/PersonSearchData.h b/lib-contacts/inc/Contacts/List/Model/PersonSearchData.h index 2c45308..18407a5 100644 --- a/lib-contacts/inc/Contacts/List/Model/PersonSearchData.h +++ b/lib-contacts/inc/Contacts/List/Model/PersonSearchData.h @@ -50,7 +50,7 @@ namespace Contacts /** * @see SearchData::compare() */ - virtual bool compare(const std::string &str) override; + virtual Contacts::Model::SearchResultPtr compare(const std::string &str) override; private: friend class SearchProvider; diff --git a/lib-contacts/inc/Contacts/List/PersonSearchItem.h b/lib-contacts/inc/Contacts/List/PersonSearchItem.h index 6598c0e..397aae8 100644 --- a/lib-contacts/inc/Contacts/List/PersonSearchItem.h +++ b/lib-contacts/inc/Contacts/List/PersonSearchItem.h @@ -34,24 +34,19 @@ namespace Contacts public: /** * @brief Create person search item - * @param[in] person Person object - */ - explicit PersonSearchItem(Model::Person &person); - - /** - * @set Person search data * @param[in] searchData @ref PersonSearchData */ - void setSearchData(Model::PersonSearchData *searchData); + explicit PersonSearchItem(Model::PersonSearchData &searchData); private: virtual char *getText(Evas_Object *parent, const char *part) override; virtual Eina_Bool compare(Evas_Object *parent, void *filter) override; std::string getHighlightedStr() const; + void onSearchDataChanged(); private: - Model::PersonSearchData *m_SearchData; + Model::PersonSearchData &m_SearchData; }; } } diff --git a/lib-contacts/src/Contacts/List/ListView.cpp b/lib-contacts/src/Contacts/List/ListView.cpp index e797cb3..bbd3e3a 100644 --- a/lib-contacts/src/Contacts/List/ListView.cpp +++ b/lib-contacts/src/Contacts/List/ListView.cpp @@ -525,8 +525,7 @@ void ListView::deletePersonGroupItem(PersonGroupItem *group) PersonItem *ListView::createPersonItem(PersonSearchData &searchData) { - PersonSearchItem *item = new PersonSearchItem(searchData.getPerson()); - item->setSearchData(&searchData); + PersonSearchItem *item = new PersonSearchItem(searchData); searchData.setUpdateCallback(std::bind(&ListView::updatePersonItem, this, item, _1)); searchData.setDeleteCallback(std::bind(&ListView::deletePersonItem, this, item)); return item; diff --git a/lib-contacts/src/Contacts/List/Model/PersonSearchData.cpp b/lib-contacts/src/Contacts/List/Model/PersonSearchData.cpp index ae5b213..f9093b3 100644 --- a/lib-contacts/src/Contacts/List/Model/PersonSearchData.cpp +++ b/lib-contacts/src/Contacts/List/Model/PersonSearchData.cpp @@ -20,6 +20,7 @@ #include "Contacts/Model/ContactNumberData.h" #include +using namespace Contacts::Model; using namespace Contacts::List::Model; PersonSearchData::PersonSearchData(Person &person) @@ -34,35 +35,34 @@ Person &PersonSearchData::getPerson() const char *PersonSearchData::getNumber() const { - if (getMatchedField() == MatchedNumber && getMatchedString()) { - return getMatchedString(); + const SearchResult *searchResult = getSearchResult(); + if (searchResult) { + if (searchResult->getMatchedField() == SearchResult::MatchedNumber && searchResult->getMatchedString()) { + return searchResult->getMatchedString(); + } } return SearchData::getNumber(); } -bool PersonSearchData::compare(const std::string &str) +SearchResultPtr PersonSearchData::compare(const std::string &str) { if (str.empty()) { - resetMatch(); - return true; + return SearchResultPtr(new SearchResult()); } - const char *pos = strstr(getName(), str.c_str()); + const char *pos = strcasestr(getName(), str.c_str()); if (pos) { - setMatch(MatchedName, getName(), { pos, str.size() }); - return true; - } - - auto &person = static_cast(getContactData()); - for (auto &&number : person.getNumbers()) { - pos = strstr(number->getNumber(), str.c_str()); - if (pos) { - setMatch(MatchedNumber, number->getNumber(), { pos, str.size() }); - return true; + return SearchResultPtr(new SearchResult(SearchResult::MatchedName, getName(), { pos, str.size() })); + } else { + auto &person = static_cast(getContactData()); + for (auto &&number : person.getNumbers()) { + pos = strstr(number->getNumber(), str.c_str()); + if (pos) { + return SearchResultPtr(new SearchResult(SearchResult::MatchedNumber, number->getNumber(), { pos, str.size() })); + } } } - resetMatch(); - return false; + return nullptr; } diff --git a/lib-contacts/src/Contacts/List/PersonSearchItem.cpp b/lib-contacts/src/Contacts/List/PersonSearchItem.cpp index 9f99cc9..4f01cf3 100644 --- a/lib-contacts/src/Contacts/List/PersonSearchItem.cpp +++ b/lib-contacts/src/Contacts/List/PersonSearchItem.cpp @@ -25,35 +25,32 @@ using namespace Contacts::List::Model; using Ux::SelectResult; using Common::highlightStr; -PersonSearchItem::PersonSearchItem(Person &person) - : PersonItem(person), m_SearchData(nullptr) +PersonSearchItem::PersonSearchItem(Model::PersonSearchData &searchData) + : PersonItem(searchData.getPerson()), m_SearchData(searchData) { -} - -void PersonSearchItem::setSearchData(Model::PersonSearchData *searchData) -{ - m_SearchData = searchData; - elm_genlist_item_fields_update(getObjectItem(), PART_CONTACT_NAME, ELM_GENLIST_ITEM_FIELD_TEXT); - elm_genlist_item_fields_update(getObjectItem(), PART_SUBTEXT, ELM_GENLIST_ITEM_FIELD_TEXT); + m_SearchData.setChangeCallback(std::bind(&PersonSearchItem::onSearchDataChanged, this)); } char *PersonSearchItem::getText(Evas_Object *parent, const char *part) { - SearchData::MatchedField matchedField = m_SearchData->getMatchedField(); + const SearchResult *searchResult = m_SearchData.getSearchResult(); + if (searchResult) { + SearchResult::MatchedField matchedField = searchResult->getMatchedField(); - if (matchedField == SearchData::MatchedName) { - if (strcmp(part, PART_CONTACT_NAME) == 0) { - return strdup(getHighlightedStr().c_str()); - } - } else if (matchedField == SearchData::MatchedNumber) { - if (strcmp(part, PART_SUBTEXT) == 0) { - return strdup(getHighlightedStr().c_str()); + if (matchedField == SearchResult::MatchedName) { + if (strcmp(part, PART_CONTACT_NAME) == 0) { + return strdup(getHighlightedStr().c_str()); + } + } else if (matchedField == SearchResult::MatchedNumber) { + if (strcmp(part, PART_SUBTEXT) == 0) { + return strdup(getHighlightedStr().c_str()); + } } - } - if (matchedField != SearchData::MatchedNone) { - if (strcmp(part, PART_SUBTEXT) == 0) { - return Utils::safeDup(m_SearchData->getNumber()); + if (matchedField != SearchResult::MatchedNone) { + if (strcmp(part, PART_SUBTEXT) == 0) { + return Utils::safeDup(m_SearchData.getNumber()); + } } } @@ -63,15 +60,25 @@ char *PersonSearchItem::getText(Evas_Object *parent, const char *part) Eina_Bool PersonSearchItem::compare(Evas_Object *parent, void *filter) { const char *str = filter ? (const char *) filter : ""; - bool isEqual = m_SearchData->compare(str); + + SearchResultPtr searchResult = m_SearchData.compare(str); + bool isEqual = (bool)searchResult; + m_SearchData.setSearchResult(std::move(searchResult)); setExcluded(!isEqual); - setSearchData(m_SearchData); return isEqual; } std::string PersonSearchItem::getHighlightedStr() const { - return highlightStr(m_SearchData->getMatchedString(), m_SearchData->getMatchedSubstring()); + const SearchResult *searchResult = m_SearchData.getSearchResult(); + return searchResult + ? highlightStr(searchResult->getMatchedString(), searchResult->getMatchedSubstring()) : ""; +} + +void PersonSearchItem::onSearchDataChanged() +{ + elm_genlist_item_fields_update(getObjectItem(), PART_CONTACT_NAME, ELM_GENLIST_ITEM_FIELD_TEXT); + elm_genlist_item_fields_update(getObjectItem(), PART_SUBTEXT, ELM_GENLIST_ITEM_FIELD_TEXT); } -- 2.7.4