worldclock: saving and loading locations list with preferences API. 56/90356/5
authorRadoslaw Czerski <r.czerski@samsung.com>
Tue, 4 Oct 2016 13:26:02 +0000 (15:26 +0200)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Wed, 5 Oct 2016 12:01:49 +0000 (05:01 -0700)
Change-Id: Ic51139bba2e1b2e8b236bb36ed0b7e4cdfa85951
Signed-off-by: Radoslaw Czerski <r.czerski@samsung.com>
clock/inc/Model/WorldClock.h
clock/inc/View/WorldClockView.h
clock/src/Model/WorldClock.cpp
clock/src/Presenter/WorldClockPresenter.cpp
clock/src/View/WorldClockView.cpp

index 69149d1..25c0e92 100644 (file)
@@ -152,6 +152,12 @@ namespace model {
 
                private:
 
+                       enum ItemKeyType {
+                               CITY,
+                               COUNTRY,
+                               OFFSET
+                       };
+
                        /**
                         * @brief Gets time zone by its no in time_zone_ list
                         */
@@ -173,6 +179,41 @@ namespace model {
                         */
                        static std::vector<Timezone> time_zones_;
 
+                       /**
+                        * @brief Saves items list state using app_preferences API
+                        * @details It saves every particular item(location)
+                        */
+                       void SaveItemsList();
+
+                       /**
+                        * @brief Loads items list state from data stored using app_preferences API
+                        */
+                       void LoadItemsList();
+
+                       /**
+                        * @brief Gets key identify given type(location struct field) for no'th item
+                        * @details The key is used while storing data using preferences API
+                        * @param type type of location struct field( see ItemKeyType enum)
+                        * @param no order number of item
+                        * @return created key
+                        */
+                       const char *GetItemKey(int type, int no);
+
+                       /**
+                        * @brief Saves single location using app_preferences API
+                        * @param[in] item location to save
+                        * @param[in] i order number of the item
+                        */
+                       void SaveItem(const model::Location *item, int i);
+
+                       /**
+                        * @brief Loads single location using app_preferences API
+                        * @param[in] i order number of the item
+                        *
+                        * @return user location
+                        */
+                       const model::Location *LoadItem(int i);
+
        };
 } /* model */
 
index 0d707a2..603b678 100644 (file)
@@ -58,6 +58,8 @@ namespace view {
                        void ShowEmptyListLabel();
                        void HideEmptyListLabel();
 
+                       void PostItemExistMessage();
+
                private:
                        void CreateTimezoneDetails();
                        void CreateCustomLocationsList();
index 74e215a..bf34870 100644 (file)
@@ -16,6 +16,8 @@
 
 #include <app_preference.h>
 #include <algorithm>
+#include <string>
+#include <sstream>
 
 #include "Model/WorldClock.h"
 #include "Model/Location.h"
@@ -135,6 +137,8 @@ std::vector<Timezone> WorldClock::time_zones_ = {
 
 WorldClock::WorldClock()
 {
+       LoadItemsList();
+
        int current_tz = 1;
        int ret = preference_get_int("WORLD_CLOCK_MAP_CURRENT_TIMEZONE", &current_tz);
        if (ret != PREFERENCE_ERROR_NONE) {
@@ -146,6 +150,8 @@ WorldClock::WorldClock()
 
 WorldClock::~WorldClock()
 {
+       SaveItemsList();
+
        int ret = preference_set_int("WORLD_CLOCK_MAP_CURRENT_TIMEZONE", GetCurrentTimezoneNo());
        if (ret != PREFERENCE_ERROR_NONE) {
                DBG("preference_set_int failed[%d]: %s", ret, get_error_message(ret));
@@ -232,12 +238,148 @@ void model::WorldClock::MoveCurrentTimezone(Direction direction)
 bool WorldClock::AddItemToCustomList(const model::Location *l)
 {
        if (!user_locations_.empty()) {
-               auto it = std::find(user_locations_.begin(), user_locations_.end(), l);
-               if (it != user_locations_.end()) {
-                       DBG("Location already exists in custom list");
-                       return false;
+               for (auto it = user_locations_.begin(); it != user_locations_.end(); it++) {
+
+                       if (!(*it)->name.compare(l->name) && !(*it)->country.compare(l->country)) {
+                               DBG("Location already exists in custom list");
+                               return false;
+                       }
                }
        }
        user_locations_.push_back(l);
        return true;
 }
+
+void WorldClock::SaveItemsList()
+{
+       int ret = preference_remove_all();
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_remove_all failed[%d]: %s", ret, get_error_message(ret));
+       }
+
+       ret = preference_set_int("WORLD_CLOCK_LIST_SIZE", (int)user_locations_.size());
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_set_int failed[%d]: %s", ret, get_error_message(ret));
+               return;
+       }
+
+       int no = 0;
+       for (auto it = user_locations_.begin(); it != user_locations_.end();) {
+               SaveItem(*it, no++);
+               delete *it;
+               it = user_locations_.erase(it);
+       }
+}
+
+void WorldClock::LoadItemsList()
+{
+       int list_size;
+       bool existing;
+
+       int ret = preference_is_existing("WORLD_CLOCK_LIST_SIZE", &existing);
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_is_existing failed[%d]: %s", ret, get_error_message(ret));
+               return;
+       }
+       if (!existing) {
+               DBG("Given Key does not exist yet. Skipping!");
+               return;
+       }
+
+       ret = preference_get_int("WORLD_CLOCK_LIST_SIZE", &list_size);
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_get_int failed[%d]: %s", ret, get_error_message(ret));
+               return;
+       }
+       if (!user_locations_.empty()){
+               ERR("User locations list is not empty!");
+               return;
+       }
+
+       for (int i = 0; i < list_size; i++) {
+               const Location *l = LoadItem(i);
+               if (l)
+                       user_locations_.push_back(l);
+       }
+}
+
+
+const char *WorldClock::GetItemKey(int type, int no)
+{
+       static std::stringstream ss;
+       ss.clear();
+
+       ss << "ITEM_" << no << "_";
+       switch (type) {
+               case CITY:
+                       ss << "CITY";
+                       break;
+               case COUNTRY:
+                       ss << "COUNTRY";
+                       break;
+               case OFFSET:
+                       ss << "OFFSET";
+                       break;
+               default:
+                       ERR("Invalid type!");
+                       return NULL;
+       }
+
+       return ss.str().c_str();
+}
+
+void WorldClock::SaveItem(const model::Location *item, int i)
+{
+       int ret = preference_set_string(GetItemKey(CITY, i), item->name.c_str());
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_set_string failed[%d]: %s", ret, get_error_message(ret));
+               return;
+       }
+       ret = preference_set_string(GetItemKey(COUNTRY, i), item->country.c_str());
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_set_string failed[%d]: %s", ret, get_error_message(ret));
+               return;
+       }
+       ret = preference_set_int(GetItemKey(OFFSET, i), item->gmt_offset_);
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_set_int failed[%d]: %s", ret, get_error_message(ret));
+       }
+}
+
+const model::Location *WorldClock::LoadItem(int i)
+{
+       model::Location *location = new Location;
+
+       char *city_name;
+       char *country_name;
+
+       int ret = preference_get_string(GetItemKey(CITY, i), &city_name);
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_get_string failed[%d]: %s", ret, get_error_message(ret));
+               delete location;
+               return NULL;
+       }
+       ret = preference_get_string(GetItemKey(COUNTRY, i), &country_name);
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_get_string failed[%d]: %s", ret, get_error_message(ret));
+               free(city_name);
+               delete location;
+               return NULL;
+       }
+       ret = preference_get_int(GetItemKey(OFFSET, i), &location->gmt_offset_);
+       if (ret != PREFERENCE_ERROR_NONE) {
+               ERR("preference_get_int failed[%d]: %s", ret, get_error_message(ret));
+               free(city_name);
+               free(country_name);
+               delete location;
+               return NULL;
+       }
+
+       location->name = city_name;
+       location->country = country_name;
+
+       free(city_name);
+       free(country_name);
+
+       return location;
+}
index bb53aa6..d6c40cb 100644 (file)
@@ -33,6 +33,9 @@ WorldClockPresenter::WorldClockPresenter(view::WorldClockView *view, model::Worl
 
        view_->UpdateMapAndTimezoneDetails(model_->GetCurrentTimezone());
 
+       for (auto it = model_->user_locations_.begin(); it != model_->user_locations_.end(); it++)
+               view_->AppendItemToCustomList(*it);
+
        UpdateEmptyListBackground();
 }
 
@@ -92,6 +95,8 @@ void WorldClockPresenter::OnItemAdded()
                model_->SetCurrentTimezone(t);
                view_->UpdateMapAndTimezoneDetails(model_->GetCurrentTimezone());
                view_->AppendItemToCustomList(l);
+       } else {
+               view_->PostItemExistMessage();
        }
 
        UpdateEmptyListBackground();
index 6d4d072..4705df8 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <efl_extension.h>
 #include <eina_str.h>
+#include <notification_status.h>
 
 #include "View/WorldClockView.h"
 #include "View/MainView.h"
@@ -655,3 +656,9 @@ void WorldClockView::HideEmptyListLabel()
 {
        elm_layout_signal_emit(world_clock_, "emptylist.label.hide", "world_clock");
 }
+
+void WorldClockView::PostItemExistMessage()
+{
+       // TODO This text is temporary. Waiting for UX Designer response
+       notification_status_message_post("Location already exists in the list");
+}