From 3b278fdb8f379f6595ffee15736367eb11dbd836 Mon Sep 17 00:00:00 2001 From: Lukasz Stanislawski Date: Mon, 12 Sep 2016 09:41:03 +0200 Subject: [PATCH] Enable alarm edition Open alarm edition view after clicking on item in AlarmView. When click is done new AlarmEditRequestEvent is being sent to EventBus. When alarm has been edited new AlarmEditedEvent is begin sent in order to synchronize data on other views. Change-Id: I122f3c5564f70448dae4841575658ce543ccfeb0 --- clock/inc/Model/Alarm.h | 8 +++--- clock/inc/Model/AlarmEvent.h | 19 +++++++++++++ clock/inc/Presenter/AlarmPresenter.h | 1 + clock/inc/Presenter/EditAlarmPresenter.h | 2 ++ clock/inc/Utils/Serialization.h | 11 ++++++++ clock/inc/View/AlarmView.h | 1 + clock/inc/View/EditAlarmView.h | 2 +- clock/res/edje/alarm.edc | 4 +-- clock/src/Controller/MainController.cpp | 17 +++++++++++- clock/src/Model/Alarm.cpp | 6 ++--- clock/src/Model/AlarmEvent.cpp | 30 +++++++++++++++++++++ clock/src/Presenter/AlarmPresenter.cpp | 28 +++++++++++++++++-- clock/src/Presenter/EditAlarmPresenter.cpp | 43 +++++++++++++++++++++++++++++- clock/src/View/AlarmView.cpp | 11 +++++++- clock/src/View/EditAlarmView.cpp | 17 +++++------- 15 files changed, 175 insertions(+), 25 deletions(-) diff --git a/clock/inc/Model/Alarm.h b/clock/inc/Model/Alarm.h index 1cc24c7..e012554 100644 --- a/clock/inc/Model/Alarm.h +++ b/clock/inc/Model/Alarm.h @@ -30,7 +30,7 @@ namespace model { void Snooze(); void EnableSnooze(); void DisableSnooze(); - bool IsSnoozeEnabled(); + bool IsSnoozeEnabled() const; void SetSnoozeInterval(unsigned int seconds); unsigned int GetSnoozeInterval() const; void SetSnoozeMaxAttemps(unsigned int attemps); @@ -46,8 +46,8 @@ namespace model { WeekFlags GetWeekFlags() const; void SetWeekFlags(WeekFlags flags); void Serialize(utils::IWriter &w) const; - unsigned int GetVolume() const; - void SetVolume(unsigned int volume); + double GetVolume() const; + void SetVolume(double volume); /** Implement guideline requirement. Two alarms are considered * same if they have same time and name */ @@ -58,7 +58,7 @@ namespace model { std::string name; struct { std::string melody; - unsigned int volume; + double volume; } sound; std::string vibration; WeekFlags flags; diff --git a/clock/inc/Model/AlarmEvent.h b/clock/inc/Model/AlarmEvent.h index be7bc77..113ddc0 100644 --- a/clock/inc/Model/AlarmEvent.h +++ b/clock/inc/Model/AlarmEvent.h @@ -18,6 +18,7 @@ #define _CLOCK_ALARM_EVENT_H_ #include "Utils/EventBus.h" +#include "Model/Alarm.h" namespace model { class AlarmCreateRequestEvent : public utils::Event { @@ -34,6 +35,24 @@ namespace model { private: static int custom_type_; }; + class AlarmEditRequestEvent : public utils::Event { + public: + AlarmEditRequestEvent(model::Alarm& alarm); + static int EventType(); + Alarm &GetAlarm() const { return alarm_;} + private: + Alarm &alarm_; + static int custom_type_; + }; + class AlarmEditedEvent : public utils::Event { + public: + AlarmEditedEvent(model::Alarm& alarm); + static int EventType(); + Alarm &GetAlarm() const { return alarm_;} + private: + Alarm &alarm_; + static int custom_type_; + }; } /* model */ #endif diff --git a/clock/inc/Presenter/AlarmPresenter.h b/clock/inc/Presenter/AlarmPresenter.h index 95cc15c..5df16f0 100644 --- a/clock/inc/Presenter/AlarmPresenter.h +++ b/clock/inc/Presenter/AlarmPresenter.h @@ -37,6 +37,7 @@ namespace presenter { void OnItemActiveStatusChanged(int idx); void OnAlarmAddedEvent(utils::Event *e); void OnAlarmRemovedEvent(utils::Event *e); + void OnAlarmEditedEvent(utils::Event *e); void OnDeleteItemClicked(); std::map> alarms_; }; diff --git a/clock/inc/Presenter/EditAlarmPresenter.h b/clock/inc/Presenter/EditAlarmPresenter.h index 3ed6ff7..f0ab43a 100644 --- a/clock/inc/Presenter/EditAlarmPresenter.h +++ b/clock/inc/Presenter/EditAlarmPresenter.h @@ -29,6 +29,8 @@ namespace presenter { view::EditAlarmView &view_; void OnEditDone(); void OnEditCancel(); + view::EditAlarmView::AlarmViewInfo GetDefaultInfo() const; + view::EditAlarmView::AlarmViewInfo GetInfoForAlarm(const model::Alarm *alarm) const; }; } /* presenter */ diff --git a/clock/inc/Utils/Serialization.h b/clock/inc/Utils/Serialization.h index a34ea1b..4d5d3ea 100644 --- a/clock/inc/Utils/Serialization.h +++ b/clock/inc/Utils/Serialization.h @@ -59,6 +59,17 @@ namespace utils { r.Read(&v, sizeof(v)); } + // double + inline void Serialize(IWriter &w, const double v) { + w.Write(&v, sizeof(v)); + } + inline void Deserialize(IReader &r, double *v) { + r.Read(v, sizeof(v)); + } + inline void Deserialize(IReader &r, double &v) { + r.Read(&v, sizeof(v)); + } + // time_t inline void Serialize(IWriter &w, const time_t v) { w.Write(&v, sizeof(v)); diff --git a/clock/inc/View/AlarmView.h b/clock/inc/View/AlarmView.h index 3cbfae2..117aa3c 100644 --- a/clock/inc/View/AlarmView.h +++ b/clock/inc/View/AlarmView.h @@ -52,6 +52,7 @@ namespace view { static void Del(void *data, Evas_Object *obj); static void ItemClicked(void *data, Evas_Object *obj, void *info); static void ItemRealized(void *data, Evas_Object *obj, void *info); + static void ItemSelected(void *data, Evas_Object *obj, void *info); static void ButtonClicked(void *data, Evas_Object *obj, void *info); static void MoreButtonClicked(void *data, Evas_Object *obj, void *info); static void DismissPopupCallback(void *data, Evas_Object *obj, void *info); diff --git a/clock/inc/View/EditAlarmView.h b/clock/inc/View/EditAlarmView.h index edd2ba7..8c8493d 100644 --- a/clock/inc/View/EditAlarmView.h +++ b/clock/inc/View/EditAlarmView.h @@ -47,7 +47,7 @@ namespace view { Evas_Object *GetEvasObject(); const AlarmViewInfo& GetData() const { return data_; } - + void SetData(const AlarmViewInfo &info); protected: virtual void CreateContent(); virtual void DestroyContent(); diff --git a/clock/res/edje/alarm.edc b/clock/res/edje/alarm.edc index 1b8a96c..3b82577 100644 --- a/clock/res/edje/alarm.edc +++ b/clock/res/edje/alarm.edc @@ -9,6 +9,7 @@ collections { style { name: "font1_dim"; base: "font=Tizen:style=Light color=#00000028 font_size=70 ellipsis=1.0"; + tag: "meridiem" "+ font_size=45"; } style { name: "font3_normal"; @@ -20,8 +21,7 @@ collections { } style { name: "font4_normal"; - base: "font=Tizen:style=Regular color=#59AF3AFF font_size=32 - ellipsis=1.0"; + base: "font=Tizen:style=Regular color=#59AF3AFF font_size=32 ellipsis=1.0"; tag: "off" "+ color=#B2B2B2FF"; } style { diff --git a/clock/src/Controller/MainController.cpp b/clock/src/Controller/MainController.cpp index 13b191d..bfe5e53 100644 --- a/clock/src/Controller/MainController.cpp +++ b/clock/src/Controller/MainController.cpp @@ -61,6 +61,8 @@ int MainController::Init() std::bind(&MainController::CreateNewAlarmPage, this, _1)); EventBus::RegisterHandler(AlarmDeleteRequestEvent::EventType(), std::bind(&MainController::CreateNewDeleteAlarmsPage, this, _1)); + EventBus::RegisterHandler(AlarmEditRequestEvent::EventType(), + std::bind(&MainController::CreateNewAlarmPage, this, _1)); return 0; } @@ -71,18 +73,31 @@ void MainController::Deinit() std::bind(&MainController::CreateNewAlarmPage, this, _1)); EventBus::DeregisterHandler(AlarmDeleteRequestEvent::EventType(), std::bind(&MainController::CreateNewDeleteAlarmsPage, this, _1)); + EventBus::DeregisterHandler(AlarmEditRequestEvent::EventType(), + std::bind(&MainController::CreateNewAlarmPage, this, _1)); } void MainController::CreateNewAlarmPage(Event *e) { static EditAlarmView *view; static EditAlarmPresenter *presenter; + Alarm *alarm; + + if (e->GetType() == AlarmCreateRequestEvent::EventType()) { + alarm = nullptr; + } else if (e->GetType() == AlarmEditRequestEvent::EventType()) { + AlarmEditRequestEvent *ev = dynamic_cast(e); + alarm = &ev->GetAlarm(); + } else { + ERR("Unhandled event type"); + return; + } if (view) delete view; if (presenter) delete presenter; view = new EditAlarmView(); - presenter = new EditAlarmPresenter(nullptr, *view); + presenter = new EditAlarmPresenter(alarm, *view); } void MainController::CreateNewDeleteAlarmsPage(Event *e) diff --git a/clock/src/Model/Alarm.cpp b/clock/src/Model/Alarm.cpp index e267971..af856ae 100644 --- a/clock/src/Model/Alarm.cpp +++ b/clock/src/Model/Alarm.cpp @@ -126,7 +126,7 @@ void Alarm::DisableSnooze() snooze_enabled = false; } -bool Alarm::IsSnoozeEnabled() +bool Alarm::IsSnoozeEnabled() const { return snooze_enabled; } @@ -268,12 +268,12 @@ void Alarm::Serialize(utils::IWriter &w) const utils::Serialize(w, time); } -unsigned int Alarm::GetVolume() const +double Alarm::GetVolume() const { return sound.volume; } -void Alarm::SetVolume(unsigned int volume) +void Alarm::SetVolume(double volume) { sound.volume = volume; } diff --git a/clock/src/Model/AlarmEvent.cpp b/clock/src/Model/AlarmEvent.cpp index 8fcc0c6..e7bdbdc 100644 --- a/clock/src/Model/AlarmEvent.cpp +++ b/clock/src/Model/AlarmEvent.cpp @@ -5,6 +5,8 @@ using namespace utils; int AlarmCreateRequestEvent::custom_type_; int AlarmDeleteRequestEvent::custom_type_; +int AlarmEditRequestEvent::custom_type_; +int AlarmEditedEvent::custom_type_; AlarmCreateRequestEvent::AlarmCreateRequestEvent() : Event(AlarmCreateRequestEvent::EventType()) @@ -31,3 +33,31 @@ int AlarmDeleteRequestEvent::EventType() } return custom_type_; } + +AlarmEditRequestEvent::AlarmEditRequestEvent(Alarm &alarm) : + Event(AlarmEditRequestEvent::EventType()), + alarm_(alarm) +{ +} + +int AlarmEditRequestEvent::EventType() +{ + if (custom_type_ == 0) { + custom_type_ = EventBus::RegisterNewEventType(); + } + return custom_type_; +} + +AlarmEditedEvent::AlarmEditedEvent(Alarm &alarm) : + Event(AlarmEditedEvent::EventType()), + alarm_(alarm) +{ +} + +int AlarmEditedEvent::EventType() +{ + if (custom_type_ == 0) { + custom_type_ = EventBus::RegisterNewEventType(); + } + return custom_type_; +} diff --git a/clock/src/Presenter/AlarmPresenter.cpp b/clock/src/Presenter/AlarmPresenter.cpp index 3851ae3..a4d122a 100644 --- a/clock/src/Presenter/AlarmPresenter.cpp +++ b/clock/src/Presenter/AlarmPresenter.cpp @@ -15,11 +15,14 @@ AlarmPresenter::AlarmPresenter(AlarmView *v, AlarmProvider *m) : view(v), model( view->SetButtonClickedCallback(std::bind(&AlarmPresenter::OnAddButtonClicked, this)); view->SetDeleteItemClickedCallback(std::bind(&AlarmPresenter::OnDeleteItemClicked, this)); view->SetItemToggleCallback(std::bind(&AlarmPresenter::OnItemActiveStatusChanged, this, _1)); + view->SetItemSelectCallback(std::bind(&AlarmPresenter::OnItemSelected, this, _1)); EventBus::RegisterHandler(AlarmAddedEvent::EventType(), std::bind(&AlarmPresenter::OnAlarmAddedEvent, this, _1)); EventBus::RegisterHandler(AlarmRemovedEvent::EventType(), std::bind(&AlarmPresenter::OnAlarmRemovedEvent, this, _1)); + EventBus::RegisterHandler(AlarmEditedEvent::EventType(), + std::bind(&AlarmPresenter::OnAlarmEditedEvent, this, _1)); ShowAll(); } @@ -30,6 +33,8 @@ AlarmPresenter::~AlarmPresenter() std::bind(&AlarmPresenter::OnAlarmAddedEvent, this, _1)); EventBus::DeregisterHandler(AlarmRemovedEvent::EventType(), std::bind(&AlarmPresenter::OnAlarmRemovedEvent, this, _1)); + EventBus::DeregisterHandler(AlarmEditedEvent::EventType(), + std::bind(&AlarmPresenter::OnAlarmEditedEvent, this, _1)); } void AlarmPresenter::ShowAll() @@ -50,8 +55,13 @@ void AlarmPresenter::ShowAll() void AlarmPresenter::OnItemSelected(int idx) { - // Get model for given index - // emit new event AlarmEditRequest event + auto it = alarms_.find(idx); + if (it == alarms_.end()) + return; + + Alarm &alarm = it->second.get(); + + EventBus::FireEvent(new AlarmEditRequestEvent(alarm)); } void AlarmPresenter::OnAddButtonClicked() @@ -112,3 +122,17 @@ void AlarmPresenter::OnAlarmRemovedEvent(Event *e) } } } + +void AlarmPresenter::OnAlarmEditedEvent(utils::Event *e) +{ + AlarmEditedEvent *ev = dynamic_cast(e); + auto it = alarms_.begin(); + for (; it != alarms_.end(); ++it) { + if (ev->GetAlarm() == it->second.get()) { + Alarm &alarm = it->second.get(); + view->ItemUpdate(it->first, alarm.GetTime(), alarm.GetName().c_str(), + alarm.GetWeekFlags(), alarm.IsActivated()); + break; + } + } +} diff --git a/clock/src/Presenter/EditAlarmPresenter.cpp b/clock/src/Presenter/EditAlarmPresenter.cpp index 99e1b83..26913bd 100644 --- a/clock/src/Presenter/EditAlarmPresenter.cpp +++ b/clock/src/Presenter/EditAlarmPresenter.cpp @@ -1,10 +1,13 @@ #include "Presenter/EditAlarmPresenter.h" #include "log.h" #include "Model/AlarmProvider.h" +#include "Utils/EventBus.h" +#include "Model/AlarmEvent.h" using namespace presenter; using namespace view; using namespace model; +using namespace utils; EditAlarmPresenter::EditAlarmPresenter(model::Alarm *alarm, view::EditAlarmView& view) : @@ -12,6 +15,41 @@ EditAlarmPresenter::EditAlarmPresenter(model::Alarm *alarm, view::EditAlarmView& { view_.RegisterEditDoneCallback(std::bind(&EditAlarmPresenter::OnEditDone, this)); view_.RegisterEditCancelCallback(std::bind(&EditAlarmPresenter::OnEditCancel, this)); + view_.SetData(GetInfoForAlarm(alarm)); +} + +view::EditAlarmView::AlarmViewInfo EditAlarmPresenter::GetDefaultInfo() const +{ + view::EditAlarmView::AlarmViewInfo data; + + data.time = Time(8, 15, 0); + data.flags.AddDay(WeekDay::MONDAY); + data.flags.AddDay(WeekDay::THURSDAY); + data.flags.AddDay(WeekDay::FRIDAY); + data.snooze = true; + data.volume = 0.6; + data.melody = "Over the horizon"; + data.type = AlarmType::VIBRATION; + data.name = "Alarm"; + + return data; +} + +view::EditAlarmView::AlarmViewInfo EditAlarmPresenter::GetInfoForAlarm(const Alarm *alarm) const +{ + view::EditAlarmView::AlarmViewInfo data; + + if (!alarm) return GetDefaultInfo(); + + data.time = alarm->GetTime(); + data.flags = alarm->GetWeekFlags(); + data.snooze = alarm->IsSnoozeEnabled(); + data.volume = alarm->GetVolume(); + data.melody = alarm->GetMelody(); + data.type = alarm->GetType(); + data.name = alarm->GetName(); + + return data; } void EditAlarmPresenter::OnEditDone() @@ -36,10 +74,13 @@ void EditAlarmPresenter::OnEditDone() } else { alarm->DisableSnooze(); } - alarm->Activate(); if (!alarm_) { + // by default activate alarm after creation + alarm->Activate(); AlarmProvider::GetInstance()->Add(*alarm); + } else { + EventBus::FireEvent(new AlarmEditedEvent(*alarm_)); } AlarmProvider::Sync(); diff --git a/clock/src/View/AlarmView.cpp b/clock/src/View/AlarmView.cpp index 060136f..1b8bcd6 100644 --- a/clock/src/View/AlarmView.cpp +++ b/clock/src/View/AlarmView.cpp @@ -68,6 +68,14 @@ void AlarmView::ItemRealized(void *data, Evas_Object *obj, void *info) elm_object_item_signal_emit(id->it, "alarm,state,disabled", "clock"); } +void AlarmView::ItemSelected(void *data, Evas_Object *obj, void *info) +{ + ItemData *id = static_cast(data); + if (id->instance->onSelected_) id->instance->onSelected_((uintptr_t)id->it); + + elm_genlist_item_selected_set(id->it, EINA_FALSE); +} + Evas_Object *AlarmView::ContentGet(void *data, Evas_Object *obj, const char *part) { ItemData *id = static_cast(data); @@ -76,6 +84,7 @@ Evas_Object *AlarmView::ContentGet(void *data, Evas_Object *obj, const char *par Evas_Object *toggle = elm_check_add(obj); elm_object_style_set(toggle, "on&off"); elm_check_state_set(toggle, id->active ? EINA_TRUE : EINA_FALSE); + evas_object_propagate_events_set(toggle, EINA_FALSE); evas_object_smart_callback_add(toggle, "changed", AlarmView::ItemClicked, id); evas_object_show(toggle); return toggle; @@ -194,7 +203,7 @@ int AlarmView::ItemAppend(Time time, const char *name, const WeekFlags flags, b data, NULL, ELM_GENLIST_ITEM_NONE, - NULL, NULL); + AlarmView::ItemSelected, data); return (uintptr_t)data->it; } diff --git a/clock/src/View/EditAlarmView.cpp b/clock/src/View/EditAlarmView.cpp index eaa03a3..7fe6392 100644 --- a/clock/src/View/EditAlarmView.cpp +++ b/clock/src/View/EditAlarmView.cpp @@ -141,16 +141,6 @@ void EditAlarmView::CreateContent() elm_theme_extension_add(NULL, Utils::GetAppResourcePath(Utils::APP_DIR_RESOURCE, "edje/edit_alarm.edj")); - // FIXME remove, for test purpose only - data_.time = Time(8, 15, 0); - data_.flags.AddDay(WeekDay::MONDAY); - data_.flags.AddDay(WeekDay::THURSDAY); - data_.flags.AddDay(WeekDay::FRIDAY); - data_.snooze = true; - data_.volume = 0.6; - data_.melody = "Over the horizon"; - data_.type = AlarmType::VIBRATION; - CreateGenlistItems(); } @@ -277,6 +267,7 @@ void EditAlarmView::CreateGenlistItems() EditAlarmView *view = static_cast(data); if (!strcmp(part, "elm.swallow.slider")) { Evas_Object *sldr = elm_slider_add(o); + elm_slider_min_max_set(sldr, 0.0, 1.0); elm_slider_value_set(sldr, view->data_.volume); evas_object_smart_callback_add(sldr, "delay,changed", EditAlarmView::SliderChangedCallback, view); @@ -437,3 +428,9 @@ void EditAlarmView::OnWeekFlagsPagePopped(void) { elm_genlist_item_update(repeat_it_); } + +void EditAlarmView::SetData(const AlarmViewInfo &info) +{ + data_ = info; + elm_genlist_realized_items_update(content_); +} -- 2.7.4