From 7e8eb40e9accf743bb34bcc2cbe79fcc873519a2 Mon Sep 17 00:00:00 2001 From: Eugene Kurzberg Date: Fri, 4 Mar 2016 11:11:21 +0200 Subject: [PATCH] Added "Done" button disabling in SelectView. Refactored count change logic. Change-Id: Idf710fab646c3f3e96cc3b01f2bb4b46321725f1 Signed-off-by: Eugene Kurzberg --- lib-common/inc/Contacts/SelectView.h | 12 +++++ lib-common/src/Contacts/SelectView.cpp | 82 +++++++++++++++++++++++++++------- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/lib-common/inc/Contacts/SelectView.h b/lib-common/inc/Contacts/SelectView.h index 35e9b08..55a8d44 100644 --- a/lib-common/inc/Contacts/SelectView.h +++ b/lib-common/inc/Contacts/SelectView.h @@ -129,10 +129,22 @@ namespace Contacts virtual void onSelectLimitChanged(size_t selectLimit) { } private: + enum CountChange + { + CountIncrement, + CountDecrement + }; + void updatePageTitle(); void updatePageButtons(); void updateSelectAllItem(); + void updateDoneButtonState(); + void updateSelectAllState(); + + void updateSelectCount(CountChange change); + void updateItemCount(CountChange change, SelectItem *item); + void createPageButtons(); void destroyPageButtons(); diff --git a/lib-common/src/Contacts/SelectView.cpp b/lib-common/src/Contacts/SelectView.cpp index fa17374..b8c0163 100644 --- a/lib-common/src/Contacts/SelectView.cpp +++ b/lib-common/src/Contacts/SelectView.cpp @@ -122,18 +122,17 @@ void SelectView::onItemInserted(SelectItem *item) item->setCheckCallback(std::bind(&SelectView::onItemChecked, this, item, _1)); m_Items.push_back(item); - updateSelectAllItem(); + updateItemCount(CountIncrement, item); } void SelectView::onItemRemove(SelectItem *item) { - if (item->isChecked()) { - --m_SelectCount; - updatePageTitle(); + auto it = std::find(m_Items.begin(), m_Items.end(), item); + if (it != m_Items.end()) { + m_Items.erase(it); } - m_Items.erase(std::remove(m_Items.begin(), m_Items.end(), item), m_Items.end()); - updateSelectAllItem(); + updateItemCount(CountDecrement, item); } void SelectView::updatePageTitle() @@ -158,13 +157,18 @@ void SelectView::updatePageButtons() switch (m_SelectMode) { case SelectNone: case SelectSingle: - destroyPageButtons(); + if (m_DoneButton) { + destroyPageButtons(); + } break; case SelectMulti: - createPageButtons(); - page->setContent("title_right_btn", m_DoneButton); - page->setContent("title_left_btn", m_CancelButton); + if (!m_DoneButton) { + createPageButtons(); + page->setContent("title_right_btn", m_DoneButton); + page->setContent("title_left_btn", m_CancelButton); + } + updateDoneButtonState(); break; } } @@ -180,7 +184,7 @@ void SelectView::updateSelectAllItem() m_SelectAllItem->scrollTo(); } - m_SelectAllItem->setChecked(m_SelectCount == m_Items.size()); + updateSelectAllState(); } else { if (m_SelectAllItem) { onSelectAllRemove(); @@ -189,6 +193,54 @@ void SelectView::updateSelectAllItem() } } +void SelectView::updateDoneButtonState() +{ + elm_object_disabled_set(m_DoneButton, m_SelectCount == 0); +} + +void SelectView::updateSelectAllState() +{ + if (m_SelectAllItem) { + m_SelectAllItem->setChecked(m_SelectCount == m_Items.size()); + } +} + +void SelectView::updateSelectCount(CountChange change) +{ + /* CURRENT count if incremented, PREVIOUS count otherwise */ + size_t checkCount = (change == CountIncrement) ? ++m_SelectCount : m_SelectCount--; + + /* m_SelectCount: (all - 1) -> all or all -> (all - 1) */ + if (checkCount == m_Items.size()) { + updateSelectAllState(); + } + /* m_SelectCount: 0 -> 1 or 1 -> 0 */ + if (checkCount == 1) { + updateDoneButtonState(); + } + + updatePageTitle(); +} + +void SelectView::updateItemCount(CountChange change, SelectItem *item) +{ + if (item->isChecked()) { + updateSelectCount(change); + } + + /* PREVIOUS count if incremented, CURRENT count otherwise */ + size_t checkCount = (change == CountIncrement) ? (m_Items.size() - 1) : m_Items.size(); + + /* (all checked -> unchecked inserted) or (one unchecked -> unchecked removed) */ + if (checkCount == m_SelectCount) { + updateSelectAllState(); + } + /* m_Items.size(): 0 -> 1 or 1 -> 0 */ + if (checkCount == 0) { + updateSelectAllItem(); + } +} + void SelectView::createPageButtons() { m_DoneButton = elm_button_add(getEvasObject()); @@ -237,12 +289,7 @@ void SelectView::onItemChecked(SelectItem *item, bool isChecked) if (m_SelectLimit && m_SelectCount == m_SelectLimit && isChecked) { item->setChecked(false); } else { - size_t checkCount = isChecked ? ++m_SelectCount : m_SelectCount--; - if (checkCount == m_Items.size()) { - updateSelectAllItem(); - } - - updatePageTitle(); + updateSelectCount(isChecked ? CountIncrement : CountDecrement); } } @@ -254,6 +301,7 @@ void SelectView::onSelectAllChecked(bool isChecked) m_SelectCount = isChecked ? m_Items.size() : 0; updatePageTitle(); + updateDoneButtonState(); } void SelectView::onSelectAllDestroy() -- 2.7.4