Integrated new provider model into GroupsProvider. 05/83005/1
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Mon, 8 Aug 2016 14:41:43 +0000 (17:41 +0300)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Mon, 8 Aug 2016 14:51:22 +0000 (17:51 +0300)
Change-Id: Ifb190fd87db3975cb6efa394b6d1ac32aea63686
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
lib-apps-common/inc/Model2/DataItem.h
lib-apps-common/inc/Model2/DataProvider.h
lib-apps-common/src/Model2/DataItem.cpp
lib-contacts/inc/Contacts/Groups/GroupsView.h
lib-contacts/inc/Contacts/Groups/Model/Group.h
lib-contacts/inc/Contacts/Groups/Model/GroupsProvider.h
lib-contacts/src/Contacts/Groups/GroupsView.cpp
lib-contacts/src/Contacts/Groups/Model/Group.cpp
lib-contacts/src/Contacts/Groups/Model/GroupsProvider.cpp

index c92595e..47c965a 100644 (file)
@@ -15,8 +15,8 @@
  *
  */
 
-#ifndef MODEL_DATA_ITEM_H
-#define MODEL_DATA_ITEM_H
+#ifndef MODEL2_DATA_ITEM_H
+#define MODEL2_DATA_ITEM_H
 
 #include <functional>
 #include <memory>
@@ -73,6 +73,12 @@ namespace Model2
 
        protected:
                /**
+                * @brief Mark item as updated without calling update().
+                * @param[in]   changes     Which item data was updated
+                */
+               void setUpdated(int changes);
+
+               /**
                 * @brief Called after update() to update item data.
                 * @param[in]   data    New item data
                 * @return Mask specifying which item data was updated.
@@ -93,4 +99,4 @@ namespace Model2
        typedef std::unique_ptr<DataItem> DataItemPtr;
 }
 
-#endif /* MODEL_DATA_ITEM_H */
+#endif /* MODEL2_DATA_ITEM_H */
index 9e9916a..5be8cbc 100644 (file)
@@ -15,8 +15,8 @@
  *
  */
 
-#ifndef MODEL_DATA_PROVIDER_H
-#define MODEL_DATA_PROVIDER_H
+#ifndef MODEL2_DATA_PROVIDER_H
+#define MODEL2_DATA_PROVIDER_H
 
 #include "Model2/DataItem.h"
 
@@ -150,4 +150,4 @@ namespace Model2
        };
 }
 
-#endif /* MODEL_DATA_PROVIDER_H */
+#endif /* MODEL2_DATA_PROVIDER_H */
index 651d4b8..b08b21b 100644 (file)
@@ -26,7 +26,7 @@ DataItem::DataItem()
 
 void DataItem::update(void *data)
 {
-       m_Changes = onUpdate(data);
+       m_Changes |= onUpdate(data);
        m_ChangeType = ChangeUpdate;
 }
 
@@ -40,6 +40,15 @@ void DataItem::setDeleteCallback(DeleteCallback callback)
        m_OnDeleted = std::move(callback);
 }
 
+void DataItem::setUpdated(int changes)
+{
+       if (m_ChangeType == ChangeNone) {
+               m_ChangeType = ChangeUpdate;
+       }
+
+       m_Changes |= changes;
+}
+
 void DataItem::onUpdated()
 {
        switch (m_ChangeType) {
index c697374..9d0bc97 100644 (file)
@@ -62,7 +62,7 @@ namespace Contacts
                        virtual void onSelectAllInsert(Ui::GenItem *item) override;
                        void updateCreateItem();
 
-                       void onInserted(::Model::DataItem &data);
+                       void onInserted(::Model2::DataItem &data);
                        GroupItem *createItem(Model::Group &group);
                        void insertItem(GroupItem *item);
                        void updateItem(GroupItem *item, int changes);
index e70e8c3..0cc2de7 100644 (file)
@@ -18,7 +18,7 @@
 #ifndef CONTACTS_GROUPS_MODEL_GROUP_H
 #define CONTACTS_GROUPS_MODEL_GROUP_H
 
-#include "Model/DataItem.h"
+#include "Model2/DataItem.h"
 #include "Utils/UniString.h"
 
 #include <contacts.h>
@@ -29,7 +29,7 @@ namespace Contacts
        {
                namespace Model
                {
-                       class Group : public ::Model::DataItem
+                       class Group : public ::Model2::DataItem
                        {
                        public:
                                /**
@@ -84,7 +84,7 @@ namespace Contacts
                                friend class GroupsProvider;
 
                                const Utils::UniString &getSortValue() const;
-                               void update(contacts_record_h record);
+                               virtual int onUpdate(void *data) override;
                                void updateMembersCount();
 
                                contacts_record_h m_Record;
index 90cf0d8..6969dd9 100644 (file)
@@ -18,7 +18,7 @@
 #ifndef CONTACTS_GROUPS_MODEL_GROUPS_PROVIDER_H
 #define CONTACTS_GROUPS_MODEL_GROUPS_PROVIDER_H
 
-#include "Model/DataProvider.h"
+#include "Model2/DataProvider.h"
 
 #include <contacts.h>
 
@@ -30,21 +30,20 @@ namespace Contacts
                {
                        class Group;
 
-                       class GroupsProvider : public ::Model::DataProvider
+                       class GroupsProvider : public ::Model2::DataProvider
                        {
                        public:
                                GroupsProvider();
                                virtual ~GroupsProvider() override;
 
-                               /**
-                                * @brief Get group list
-                                * @return List of group objects
-                                */
-                               virtual const DataList &getDataList() override final;
-                               virtual void clearDataList() override final;
-
                        private:
-                               void onChanged(const char *uri);
+                               virtual void onInitialize() override;
+                               virtual void onUpdate() override;
+
+                               void updateGroups();
+                               void updateMembers();
+
+                               void onGroupsChanged(const char *uri);
                                void onMembersChanged(const char *uri);
 
                                void insertGroup(int id);
@@ -54,15 +53,19 @@ namespace Contacts
                                Group *createGroup(contacts_record_h record);
                                DataList::const_iterator findGroup(int id);
 
+                               enum UpdateType
+                               {
+                                       UpdateNone,
+                                       UpdateGroups  = 1 << 0,
+                                       UpdateMembers = 1 << 1
+                               };
+
                                int m_DbVersion;
                                int m_MembersDbVersion;
-                               DataList m_List;
-                               bool m_IsFilled;
+                               int m_UpdateType;
                        };
                }
        }
 }
 
-
-
 #endif /* CONTACTS_GROUPS_MODEL_GROUPS_PROVIDER_H */
index 2a2b5d8..7169873 100644 (file)
@@ -68,15 +68,23 @@ Evas_Object *GroupsView::onCreate(Evas_Object *parent)
 
        m_GroupsItem = new GenGroupItem("IDS_PB_OPT_GROUPS");
        m_Genlist->insert(m_GroupsItem);
-       for (auto &&data : m_Provider.getDataList()) {
-               GroupItem *groupItem = createItem(*static_cast<Group *>(data));
-               const char *name = groupItem->getGroup().getName();
-               if (safeCmp(name, "Co-workers") || safeCmp(name, "Family")
-                               || safeCmp(name, "Friends")) {
-                       m_MainItems.push_back(groupItem);
+
+       m_Provider.initialize([this] {
+               for (auto &&data : m_Provider.getDataList()) {
+                       Group &group = static_cast<Group &>(*data);
+                       GroupItem *groupItem = createItem(group);
+
+                       const char *name = group.getName();
+                       if (safeCmp(name, "Co-workers")
+                        || safeCmp(name, "Family")
+                        || safeCmp(name, "Friends")) {
+                               m_MainItems.push_back(groupItem);
+                       }
+
+                       insertItem(groupItem);
+                       addSelectItem(groupItem);
                }
-               insertItem(groupItem);
-       }
+       });
 
        updateCreateItem();
        return m_Genlist->getEvasObject();
@@ -150,11 +158,12 @@ void GroupsView::updateCreateItem()
        }
 }
 
-void GroupsView::onInserted(::Model::DataItem &data)
+void GroupsView::onInserted(::Model2::DataItem &data)
 {
        Group &group = static_cast<Group &>(data);
        GroupItem *item = createItem(group);
        insertItem(item);
+       addSelectItem(item);
 
        if (m_NewGroupId == group.getId()) {
                m_NewGroupId = 0;
@@ -173,7 +182,6 @@ GroupItem *GroupsView::createItem(Group &group)
 void GroupsView::insertItem(GroupItem *item)
 {
        m_GroupsItem->insertSubItem(item, getNextItem(item->getGroup()));
-       addSelectItem(item);
 }
 
 void GroupsView::updateItem(GroupItem *item, int changes)
@@ -211,7 +219,7 @@ bool GroupsView::onDeleteFinished()
        setSelectCallback(nullptr);
        for (auto &&groupItem : m_MainItems) {
                groupItem->setExcluded(false);
-               m_GroupsItem->insertSubItem(groupItem, getNextItem(groupItem->getGroup()));
+               insertItem(groupItem);
        }
 
        return false;
index 54f424e..0107e3d 100644 (file)
@@ -72,8 +72,9 @@ const Utils::UniString &Group::getSortValue() const
        return m_SortValue;
 }
 
-void Group::update(contacts_record_h record)
+int Group::onUpdate(void *data)
 {
+       contacts_record_h record = (contacts_record_h) data;
        int changes = ChangedNone;
        if (!compareRecordsStr(m_Record, record, _contacts_group.name)) {
                m_SortValue.clear();
@@ -83,11 +84,11 @@ void Group::update(contacts_record_h record)
        contacts_record_destroy(m_Record, true);
        m_Record = record;
 
-       onUpdated(changes);
+       return changes;
 }
 
 void Group::updateMembersCount()
 {
        m_MembersCount = Model::getMembersCount(getId());
-       onUpdated(ChangedMembersCount);
+       setUpdated(ChangedMembersCount);
 }
index 458eb80..62c49ca 100644 (file)
 
 using namespace Contacts::Groups::Model;
 using namespace Common::Database;
-using namespace Model;
+using namespace Model2;
 
 GroupsProvider::GroupsProvider()
-       : m_DbVersion(0), m_MembersDbVersion(0), m_IsFilled(false)
+       : m_DbVersion(0), m_MembersDbVersion(0), m_UpdateType(UpdateNone)
 {
 }
 
 GroupsProvider::~GroupsProvider()
 {
-       clearDataList();
+       contacts_db_remove_changed_cb(_contacts_group._uri,
+                       makeCallbackWithLastParam(&GroupsProvider::onGroupsChanged), this);
+       contacts_db_remove_changed_cb(_contacts_group_relation._uri,
+                       makeCallbackWithLastParam(&GroupsProvider::onMembersChanged), this);
 }
 
-const DataProvider::DataList &GroupsProvider::getDataList()
+void GroupsProvider::onInitialize()
 {
-       if (!m_IsFilled) {
-               contacts_db_get_current_version(&m_DbVersion);
-               m_MembersDbVersion = m_DbVersion;
-               contacts_db_add_changed_cb(_contacts_group._uri,
-                               makeCallbackWithLastParam(&GroupsProvider::onChanged), this);
-               contacts_db_add_changed_cb(_contacts_group_relation._uri,
-                               makeCallbackWithLastParam(&GroupsProvider::onMembersChanged), this);
-
-               contacts_list_h list = nullptr;
-               contacts_db_get_all_records(_contacts_group._uri, 0, 0, &list);
-               for (auto &&record : makeRange(list)) {
-                       m_List.push_back(createGroup(record));
-               }
-               contacts_list_destroy(list, false);
-               m_IsFilled = true;
+       contacts_db_get_current_version(&m_DbVersion);
+       m_MembersDbVersion = m_DbVersion;
+       contacts_db_add_changed_cb(_contacts_group._uri,
+                       makeCallbackWithLastParam(&GroupsProvider::onGroupsChanged), this);
+       contacts_db_add_changed_cb(_contacts_group_relation._uri,
+                       makeCallbackWithLastParam(&GroupsProvider::onMembersChanged), this);
+
+       contacts_list_h list = nullptr;
+       contacts_db_get_all_records(_contacts_group._uri, 0, 0, &list);
+
+       DataList dataList;
+       for (auto &&record : makeRange(list)) {
+               dataList.push_back(DataItemPtr(createGroup(record)));
        }
-       return m_List;
+       contacts_list_destroy(list, false);
+
+       onInitialized(std::move(dataList));
 }
 
-void GroupsProvider::clearDataList()
+void GroupsProvider::onUpdate()
 {
-       contacts_db_remove_changed_cb(_contacts_group._uri,
-                       makeCallbackWithLastParam(&GroupsProvider::onChanged), this);
-       contacts_db_remove_changed_cb(_contacts_group_relation._uri,
-                       makeCallbackWithLastParam(&GroupsProvider::onMembersChanged), this);
+       onUpdateStarted();
+       int updateType = m_UpdateType;
+       m_UpdateType = UpdateNone;
 
-       for (auto &&groupData : m_List) {
-               delete groupData;
+       if (updateType & UpdateGroups) {
+               updateGroups();
+       }
+       if (updateType & UpdateMembers) {
+               updateMembers();
        }
-       m_List.clear();
-       m_IsFilled = false;
+       onUpdateFinished();
 }
 
-void GroupsProvider::onChanged(const char *uri)
+void GroupsProvider::updateGroups()
 {
        contacts_list_h changes = nullptr;
        contacts_db_get_changes_by_version(_contacts_group_updated_info._uri, 0,
@@ -91,7 +95,7 @@ void GroupsProvider::onChanged(const char *uri)
                        case CONTACTS_CHANGE_UPDATED:
                        {
                                auto groupIt = findGroup(groupId);
-                               if (groupIt != m_List.end()) {
+                               if (groupIt != getDataList().end()) {
                                        updateGroup(groupIt);
                                }
                                break;
@@ -99,7 +103,7 @@ void GroupsProvider::onChanged(const char *uri)
                        case CONTACTS_CHANGE_DELETED:
                        {
                                auto groupIt = findGroup(groupId);
-                               if (groupIt != m_List.end()) {
+                               if (groupIt != getDataList().end()) {
                                        deleteGroup(groupIt);
                                }
                                break;
@@ -110,7 +114,7 @@ void GroupsProvider::onChanged(const char *uri)
        contacts_list_destroy(changes, true);
 }
 
-void GroupsProvider::onMembersChanged(const char *uri)
+void GroupsProvider::updateMembers()
 {
        contacts_list_h changes = nullptr;
        contacts_db_get_changes_by_version(_contacts_grouprel_updated_info._uri, 0,
@@ -118,8 +122,8 @@ void GroupsProvider::onMembersChanged(const char *uri)
 
        for (auto &&record : makeRange(changes)) {
                auto groupIt = findGroup(getRecordInt(record, _contacts_grouprel_updated_info.group_id));
-               if (groupIt != m_List.end()) {
-                       Group *group = static_cast<Group *>(*groupIt);
+               if (groupIt != getDataList().end()) {
+                       Group *group = static_cast<Group *>(groupIt->get());
                        group->updateMembersCount();
                }
        }
@@ -127,19 +131,30 @@ void GroupsProvider::onMembersChanged(const char *uri)
        contacts_list_destroy(changes, true);
 }
 
+void GroupsProvider::onGroupsChanged(const char *uri)
+{
+       m_UpdateType |= UpdateGroups;
+       update();
+}
+
+void GroupsProvider::onMembersChanged(const char *uri)
+{
+       m_UpdateType |= UpdateMembers;
+       update();
+}
+
 void GroupsProvider::insertGroup(int id)
 {
        contacts_record_h record = nullptr;
        contacts_db_get_record(_contacts_group._uri, id, &record);
        if (record) {
-               m_List.push_back(createGroup(record));
-               onInserted(*m_List.back());
+               insertDataItem(DataItemPtr(createGroup(record)));
        }
 }
 
 void GroupsProvider::updateGroup(DataList::const_iterator groupIt)
 {
-       Group *group = static_cast<Group *>(*groupIt);
+       Group *group = static_cast<Group *>(groupIt->get());
        contacts_record_h record = nullptr;
        contacts_db_get_record(_contacts_group._uri, group->getId(), &record);
        group->update(record);
@@ -147,10 +162,7 @@ void GroupsProvider::updateGroup(DataList::const_iterator groupIt)
 
 void GroupsProvider::deleteGroup(DataList::const_iterator groupIt)
 {
-       Group *group = static_cast<Group *>(*groupIt);
-       m_List.erase(groupIt);
-       group->onDeleted();
-       delete group;
+       deleteDataItem(**groupIt);
 }
 
 Group *GroupsProvider::createGroup(contacts_record_h record)
@@ -160,9 +172,9 @@ Group *GroupsProvider::createGroup(contacts_record_h record)
 
 DataProvider::DataList::const_iterator GroupsProvider::findGroup(int id)
 {
-       return std::find_if(m_List.begin(), m_List.end(),
-               [id](DataItem *dataItem) {
-                       Group *group = static_cast<Group *>(dataItem);
+       return std::find_if(getDataList().begin(), getDataList().end(),
+               [id](const DataItemPtr &dataItem) {
+                       Group *group = static_cast<Group *>(dataItem.get());
                        return group->getId() == id;
                }
        );