From 81649f0b4abb43119c937a869882bb0c3dbdb52e Mon Sep 17 00:00:00 2001 From: Lukasz Stanislawski Date: Wed, 31 Aug 2016 15:05:48 +0200 Subject: [PATCH] AlarmPresenter: add update on alarm add Add new Added/Removed events to broadcast creation/removal of alarms. Change-Id: Ia9cb122a66d7b31b420a3416db48553c668d212f --- clock/inc/Model/AlarmProviderEvent.h | 44 ++++++++++++++++++++++++++++++++++ clock/inc/Presenter/AlarmPresenter.h | 3 +++ clock/src/Model/AlarmProviderEvent.cpp | 33 +++++++++++++++++++++++++ clock/src/Model/AlarmProviderFile.cpp | 9 ++++--- clock/src/Presenter/AlarmPresenter.cpp | 29 ++++++++++++++++++++-- 5 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 clock/inc/Model/AlarmProviderEvent.h create mode 100644 clock/src/Model/AlarmProviderEvent.cpp diff --git a/clock/inc/Model/AlarmProviderEvent.h b/clock/inc/Model/AlarmProviderEvent.h new file mode 100644 index 0000000..630e507 --- /dev/null +++ b/clock/inc/Model/AlarmProviderEvent.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _CLOCK_ALARM_PROVIDER_EVENT_H_ +#define _CLOCK_ALARM_PROVIDER_EVENT_H_ + +#include "Utils/EventBus.h" +#include "Model/Alarm.h" + +namespace model { + class AlarmAddedEvent : public utils::Event { + public: + AlarmAddedEvent(Alarm &alarm); + const Alarm& GetAlarm() const { return alarm_; } + static int EventType(); + private: + Alarm &alarm_; + static int custom_type_; + }; + class AlarmRemovedEvent : public utils::Event { + public: + AlarmRemovedEvent(Alarm &alarm); + const Alarm& GetAlarm() const { return alarm_; } + static int EventType(); + private: + Alarm &alarm_; + static int custom_type_; + }; +} /* model */ + +#endif diff --git a/clock/inc/Presenter/AlarmPresenter.h b/clock/inc/Presenter/AlarmPresenter.h index 883c120..88dfc7e 100644 --- a/clock/inc/Presenter/AlarmPresenter.h +++ b/clock/inc/Presenter/AlarmPresenter.h @@ -19,17 +19,20 @@ #include "View/AlarmView.h" #include "Model/AlarmProvider.h" +#include "Model/AlarmProviderEvent.h" namespace presenter { class AlarmPresenter { public: AlarmPresenter(view::AlarmView *view, model::AlarmProvider *model); + ~AlarmPresenter(); private: view::AlarmView *view; model::AlarmProvider *model; void ShowAll(); void OnItemSelected(int idx); void OnItemActiveStatusChanged(int idx); + void OnAlarmAddedEvent(utils::Event *e); }; } /* presenters */ diff --git a/clock/src/Model/AlarmProviderEvent.cpp b/clock/src/Model/AlarmProviderEvent.cpp new file mode 100644 index 0000000..5507bd9 --- /dev/null +++ b/clock/src/Model/AlarmProviderEvent.cpp @@ -0,0 +1,33 @@ +#include "Model/AlarmProviderEvent.h" + +using namespace model; +using namespace utils; + +int AlarmAddedEvent::custom_type_; +int AlarmRemovedEvent::custom_type_; + +AlarmAddedEvent::AlarmAddedEvent(Alarm &alarm) : + Event(AlarmAddedEvent::EventType()), alarm_(alarm) +{ +} + +int AlarmAddedEvent::EventType() +{ + if (custom_type_ == 0) { + custom_type_ = EventBus::RegisterNewEventType(); + } + return custom_type_; +} + +AlarmRemovedEvent::AlarmRemovedEvent(Alarm &alarm) : + Event(AlarmAddedEvent::EventType()), alarm_(alarm) +{ +} + +int AlarmRemovedEvent::EventType() +{ + if (custom_type_ == 0) { + custom_type_ = EventBus::RegisterNewEventType(); + } + return custom_type_; +} diff --git a/clock/src/Model/AlarmProviderFile.cpp b/clock/src/Model/AlarmProviderFile.cpp index 7a2c6fc..3ebf038 100644 --- a/clock/src/Model/AlarmProviderFile.cpp +++ b/clock/src/Model/AlarmProviderFile.cpp @@ -1,6 +1,7 @@ #include "Internal/AlarmProviderFile.h" +#include "Model/AlarmProviderEvent.h" +#include "Utils/EventBus.h" -#include #include #include @@ -19,9 +20,8 @@ void AlarmProviderFile::Add(model::Alarm& alarm) if (it == alarms.end()) { alarms.push_back(alarm); + EventBus::FireEvent(new AlarmAddedEvent(alarm)); } - - //FIXME emit AlarmAddedEvent } void AlarmProviderFile::Remove(model::Alarm& alarm) @@ -30,9 +30,8 @@ void AlarmProviderFile::Remove(model::Alarm& alarm) if (it != alarms.end()) { alarms.erase(it); + EventBus::FireEvent(new AlarmRemovedEvent(alarm)); } - - //FIXME emit AlarmRemovedEvent } AlarmProviderFile::AlarmProviderFile(IReader &r) diff --git a/clock/src/Presenter/AlarmPresenter.cpp b/clock/src/Presenter/AlarmPresenter.cpp index 6483d0f..02f2a95 100644 --- a/clock/src/Presenter/AlarmPresenter.cpp +++ b/clock/src/Presenter/AlarmPresenter.cpp @@ -1,9 +1,13 @@ #include "Presenter/AlarmPresenter.h" +#include "Model/AlarmProviderEvent.h" +#include "Utils/EventBus.h" #include "log.h" using namespace presenter; using namespace view; using namespace model; +using namespace utils; +using std::placeholders::_1; AlarmPresenter::AlarmPresenter(AlarmView *v, AlarmProvider *m) : view(v), model(m) { @@ -11,18 +15,25 @@ AlarmPresenter::AlarmPresenter(AlarmView *v, AlarmProvider *m) : view(v), model( //view.RegisterOnItemSelected(std::function<>()); //view.RegisterOnAlarmAddClicked(std::function<>()); + EventBus::RegisterHandler(AlarmAddedEvent::EventType(), + std::bind(&AlarmPresenter::OnAlarmAddedEvent, this, _1)); + ShowAll(); } +AlarmPresenter::~AlarmPresenter() +{ + EventBus::DeregisterHandler(AlarmAddedEvent::EventType(), + std::bind(&AlarmPresenter::OnAlarmAddedEvent, this, _1)); +} + void AlarmPresenter::ShowAll() { std::vector> alarms = model->GetAlarms(); - DBG("ShowAll()"); view->Clear(); for (auto a: alarms) { - DBG("ItemAppend()"); view->ItemAppend( a.get().GetTime(), a.get().GetName().c_str(), @@ -54,3 +65,17 @@ void AlarmPresenter::OnItemActiveStatusChanged(int idx) //AlarmProvider::Sync(); } + +void AlarmPresenter::OnAlarmAddedEvent(Event *e) +{ + if (!e || (e->GetType() != AlarmAddedEvent::EventType())) + return; + + AlarmAddedEvent *ev = dynamic_cast(e); + + view->ItemAppend( + ev->GetAlarm().GetTime(), + ev->GetAlarm().GetName().c_str(), + ev->GetAlarm().GetWeekFlags(), + ev->GetAlarm().IsActivated()); +} -- 2.7.4