TizenRefApp-5468 Implement discard changes prompt popup 38/57638/1
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Thu, 21 Jan 2016 07:59:20 +0000 (09:59 +0200)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Thu, 21 Jan 2016 08:00:54 +0000 (10:00 +0200)
Change-Id: I9e66f676b95ce11f1c5475142593b61dc5cd3803
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
15 files changed:
lib-contact/inc/Contacts/Input/InputView.h
lib-contact/inc/Contacts/Model/ContactArray.h
lib-contact/inc/Contacts/Model/ContactDateField.h
lib-contact/inc/Contacts/Model/ContactEnumField.h
lib-contact/inc/Contacts/Model/ContactField.h
lib-contact/inc/Contacts/Model/ContactFieldContainer.h
lib-contact/inc/Contacts/Model/ContactTextField.h
lib-contact/inc/Contacts/Model/ContactTypedObject.h
lib-contact/src/Contacts/Input/InputView.cpp
lib-contact/src/Contacts/Model/ContactArray.cpp
lib-contact/src/Contacts/Model/ContactDateField.cpp
lib-contact/src/Contacts/Model/ContactEnumField.cpp
lib-contact/src/Contacts/Model/ContactFieldContainer.cpp
lib-contact/src/Contacts/Model/ContactTextField.cpp
lib-contact/src/Contacts/Model/ContactTypedObject.cpp

index 31473b9..ff61f94 100644 (file)
@@ -60,6 +60,7 @@ namespace Contacts
                        virtual Evas_Object *onCreate(Evas_Object *parent) override;
                        virtual void onCreated() override;
                        virtual void onPageAttached() override;
+                       virtual bool onBackPressed() override;
 
                        ContactFieldItem *createFieldItem(Model::ContactObject &field);
                        Ui::GenlistItem *getNextFieldItem(Model::ContactFieldId fieldId);
@@ -76,6 +77,7 @@ namespace Contacts
 
                        void onDonePressed(Evas_Object *button, void *eventInfo);
                        void onCancelPressed(Evas_Object *button, void *eventInfo);
+                       bool onCancel();
 
                        int m_RecordId;
                        Model::Contact m_Contact;
index b2790c9..1d1c053 100644 (file)
@@ -40,6 +40,11 @@ namespace Contacts
                        virtual void initialize() override;
 
                        /**
+                        * @see ContactField::isChanged()
+                        */
+                       virtual bool isChanged() const override;
+
+                       /**
                         * @brief Add new child field.
                         * @return New child field.
                         */
@@ -53,6 +58,8 @@ namespace Contacts
 
                private:
                        const ContactArrayMetadata &getArrayMetadata() const;
+
+                       size_t m_InitialCount;
                };
        }
 }
index fa2e386..74bfcd7 100644 (file)
@@ -36,11 +36,21 @@ namespace Contacts
                        using ContactField::ContactField;
 
                        /**
+                        * @see ContactField::initialize()
+                        */
+                       virtual void initialize() override;
+
+                       /**
                         * @see ContactInputField::reset()
                         */
                        virtual void reset() override;
 
                        /**
+                        * @see ContactField::isChanged()
+                        */
+                       virtual bool isChanged() const override;
+
+                       /**
                         * @return Date field value.
                         */
                        tm getValue() const;
@@ -54,6 +64,8 @@ namespace Contacts
                private:
                        static tm convertDate(int value);
                        static int convertDate(const tm &date);
+
+                       time_t m_InitialValue;
                };
        }
 }
index ffedd21..6715967 100644 (file)
@@ -39,11 +39,21 @@ namespace Contacts
                        using ContactField::ContactField;
 
                        /**
+                        * @see ContactField::initialize()
+                        */
+                       virtual void initialize() override;
+
+                       /**
                         * @see ContactField::reset()
                         */
                        virtual void reset() override;
 
                        /**
+                        * @see ContactField::isChanged()
+                        */
+                       virtual bool isChanged() const override;
+
+                       /**
                         * @return Array of possible values.
                         */
                        Utils::Range<const int *> getValues() const;
@@ -71,6 +81,8 @@ namespace Contacts
 
                private:
                        const ContactEnumMetadata &getEnumMetadata() const;
+
+                       int m_InitialValue;
                };
        }
 }
index fe212f0..61c5ada 100644 (file)
@@ -78,6 +78,11 @@ namespace Contacts
                        virtual bool isFilled() const { return !isEmpty(); }
 
                        /**
+                        * @return Whether the field value was changed.
+                        */
+                       virtual bool isChanged() const { return true; }
+
+                       /**
                         * @return Whether the field determines that parent object is filled.
                         */
                        bool isRequired() const;
index bca1ffd..e39c593 100644 (file)
@@ -34,6 +34,11 @@ namespace Contacts
                        using ContactField::ContactField;
 
                        /**
+                        * @see ContactField::reset()
+                        */
+                       virtual void reset() override;
+
+                       /**
                         * @see ContactField::isEmpty()
                         */
                        virtual bool isEmpty() const override;
@@ -44,9 +49,9 @@ namespace Contacts
                        virtual bool isFilled() const override;
 
                        /**
-                        * @see ContactField::reset()
+                        * @see ContactField::isChanged()
                         */
-                       virtual void reset() override;
+                       virtual bool isChanged() const override;
 
                        /**
                         * @return Begin iterator.
@@ -65,6 +70,11 @@ namespace Contacts
                         */
                        ContactField *getField(unsigned index) const;
 
+                       /**
+                        * @return Child field count.
+                        */
+                       size_t getFieldCount() const;
+
                protected:
                        ContactField &addField(contacts_record_h record,
                                        const ContactFieldMetadata &metadata);
index 180233a..20e88e4 100644 (file)
@@ -19,6 +19,7 @@
 #define CONTACTS_MODEL_CONTACT_TEXT_FIELD_H
 
 #include "Contacts/Model/ContactField.h"
+#include <string>
 
 namespace Contacts
 {
@@ -35,9 +36,9 @@ namespace Contacts
                        using ContactField::ContactField;
 
                        /**
-                        * @see ContactField::isEmpty()
+                        * @see ContactField::initialize()
                         */
-                       virtual bool isEmpty() const override;
+                       virtual void initialize() override;
 
                        /**
                         * @see ContactField::reset()
@@ -45,6 +46,16 @@ namespace Contacts
                        virtual void reset() override;
 
                        /**
+                        * @see ContactField::isEmpty()
+                        */
+                       virtual bool isEmpty() const override;
+
+                       /**
+                        * @see ContactField::isChanged()
+                        */
+                       virtual bool isChanged() const override;
+
+                       /**
                         * @return Text field value.
                         */
                        const char *getValue() const;
@@ -53,6 +64,9 @@ namespace Contacts
                         * @brief Set text field value.
                         */
                        void setValue(const char *value);
+
+               private:
+                       std::string m_InitialValue;
                };
        }
 }
index 5f69bac..4a5be63 100644 (file)
@@ -48,6 +48,11 @@ namespace Contacts
                        virtual void reset() override;
 
                        /**
+                        * @see ContactField::isChanged()
+                        */
+                       virtual bool isChanged() const override;
+
+                       /**
                         * @return Field that represents object's type.
                         */
                        ContactEnumField &getTypeField() const;
index 10d3e00..9bdbb7d 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "App/Path.h"
 #include "Ui/Genlist.h"
+#include "Ui/Popup.h"
 #include "Utils/Callback.h"
 #include "Utils/Logger.h"
 #include "Utils/Range.h"
@@ -113,6 +114,11 @@ void InputView::onPageAttached()
        page->setContent("title_left_btn", cancelButton);
 }
 
+bool InputView::onBackPressed()
+{
+       return onCancel();
+}
+
 ContactFieldItem *InputView::createFieldItem(ContactObject &field)
 {
        ContactFieldItem *item = nullptr;
@@ -225,5 +231,27 @@ void InputView::onDonePressed(Evas_Object *button, void *eventInfo)
 
 void InputView::onCancelPressed(Evas_Object *button, void *eventInfo)
 {
-       delete this;
+       if (onCancel()) {
+               delete this;
+       }
+}
+
+bool InputView::onCancel()
+{
+       if (!m_Contact.isChanged()) {
+               return true;
+       }
+
+       Ui::Popup *popup = new Ui::Popup();
+       popup->create(getEvasObject());
+       popup->setTitle("IDS_PB_HEADER_DISCARD_CHANGES_ABB");
+       popup->setText("IDS_PB_POP_ALL_CHANGES_WILL_BE_DISCARDED");
+
+       popup->addButton("IDS_PB_BUTTON_CANCEL");
+       popup->addButton("IDS_PB_BUTTON_DISCARD_ABB", [this] {
+               delete this;
+               return true;
+       });
+
+       return false;
 }
index 0e9a18d..0eb35d4 100644 (file)
@@ -30,6 +30,17 @@ void ContactArray::initialize()
                contacts_record_get_child_record_at_p(getRecord(), getPropertyId(), i, &record);
                ContactFieldContainer::addField(record, getArrayMetadata().element);
        }
+
+       m_InitialCount = getFieldCount();
+}
+
+bool ContactArray::isChanged() const
+{
+       if (getFieldCount() < m_InitialCount) {
+               return true;
+       }
+
+       return ContactFieldContainer::isChanged();
 }
 
 ContactField &ContactArray::addField()
index 46092a6..ec45bda 100644 (file)
 
 using namespace Contacts::Model;
 
+void ContactDateField::initialize()
+{
+       tm value = getValue();
+       m_InitialValue = mktime(&value);
+}
+
 void ContactDateField::reset()
 {
        time_t now = time(nullptr);
        setValue(*localtime(&now));
 }
 
+bool ContactDateField::isChanged() const
+{
+       tm value = getValue();
+       return difftime(mktime(&value), m_InitialValue) != 0;
+}
+
 tm ContactDateField::getValue() const
 {
        int date = 0;
index ebfbb82..603b8d9 100644 (file)
 
 using namespace Contacts::Model;
 
+void ContactEnumField::initialize()
+{
+       m_InitialValue = getValue();
+}
+
 void ContactEnumField::reset()
 {
        setValue(getEnumMetadata().defaultValue);
 }
 
+bool ContactEnumField::isChanged() const
+{
+       return m_InitialValue != getValue();
+}
+
 Utils::Range<const int *> ContactEnumField::getValues() const
 {
        return getEnumMetadata().values;
index 96e9238..ddc9d21 100644 (file)
 
 using namespace Contacts::Model;
 
-bool ContactFieldContainer::isEmpty() const
+void ContactFieldContainer::reset()
 {
-       bool isEmpty = true;
-       for (auto &&field : *this) {
-               if (!field.isEmpty()) {
-                       isEmpty = false;
-                       break;
-               }
+       for (auto &&field : m_Fields) {
+               field->reset();
        }
+}
 
-       return isEmpty;
+bool ContactFieldContainer::isEmpty() const
+{
+       return !std::any_of(m_Fields.begin(), m_Fields.end(), [](const ContactFieldPtr &field) {
+               return !field->isEmpty();
+       });
 }
 
 bool ContactFieldContainer::isFilled() const
 {
-       bool isFilled = false;
-       for (auto &&field : *this) {
-               if (field.isRequired() && field.isFilled()) {
-                       isFilled = true;
-                       break;
-               }
-       }
-
-       return isFilled;
+       return std::any_of(m_Fields.begin(), m_Fields.end(), [](const ContactFieldPtr &field) {
+               return field->isRequired() && field->isFilled();
+       });
 }
 
-void ContactFieldContainer::reset()
+bool ContactFieldContainer::isChanged() const
 {
-       for (auto &&field : *this) {
-               field.reset();
-       }
+       return std::any_of(m_Fields.begin(), m_Fields.end(), std::mem_fn(&ContactField::isChanged));
 }
 
 ContactFieldIterator ContactFieldContainer::begin() const
@@ -74,6 +67,11 @@ ContactField *ContactFieldContainer::getField(unsigned index) const
        return nullptr;
 }
 
+size_t ContactFieldContainer::getFieldCount() const
+{
+       return m_Fields.size();
+}
+
 ContactField &ContactFieldContainer::addField(contacts_record_h record,
                const ContactFieldMetadata &metadata)
 {
index 2e60bb7..bb91227 100644 (file)
 
 using namespace Contacts::Model;
 
-bool ContactTextField::isEmpty() const
+void ContactTextField::initialize()
 {
        const char *value = getValue();
-       return !(value && *value);
+       if (value) {
+               m_InitialValue = value;
+       }
 }
 
 void ContactTextField::reset()
@@ -31,6 +33,22 @@ void ContactTextField::reset()
        setValue("");
 }
 
+bool ContactTextField::isEmpty() const
+{
+       const char *value = getValue();
+       return !(value && *value);
+}
+
+bool ContactTextField::isChanged() const
+{
+       const char *value = getValue();
+       if (!value) {
+               value = "";
+       }
+
+       return m_InitialValue.compare(value) != 0;
+}
+
 const char *ContactTextField::getValue() const
 {
        char *value = nullptr;
index 8c3eb33..0f89cb9 100644 (file)
@@ -35,6 +35,25 @@ void ContactTypedObject::reset()
        m_LabelField->reset();
 }
 
+bool ContactTypedObject::isChanged() const
+{
+       if (ContactObject::isChanged()) {
+               return true;
+       }
+
+       if (!isEmpty()) {
+               if (m_TypeField->isChanged()) {
+                       return true;
+               }
+
+               if (getTypeField().hasCustomValue()) {
+                       return m_LabelField->isChanged();
+               }
+       }
+
+       return false;
+}
+
 ContactEnumField &ContactTypedObject::getTypeField() const
 {
        return m_TypeField->cast<ContactEnumField>();