AlarmPresenter: add update on alarm add
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Wed, 31 Aug 2016 13:05:48 +0000 (15:05 +0200)
committerŁukasz Stanisławski <l.stanislaws@samsung.com>
Tue, 13 Sep 2016 06:20:45 +0000 (08:20 +0200)
Add new Added/Removed events to broadcast creation/removal
of alarms.

Change-Id: Ia9cb122a66d7b31b420a3416db48553c668d212f

clock/inc/Model/AlarmProviderEvent.h [new file with mode: 0644]
clock/inc/Presenter/AlarmPresenter.h
clock/src/Model/AlarmProviderEvent.cpp [new file with mode: 0644]
clock/src/Model/AlarmProviderFile.cpp
clock/src/Presenter/AlarmPresenter.cpp

diff --git a/clock/inc/Model/AlarmProviderEvent.h b/clock/inc/Model/AlarmProviderEvent.h
new file mode 100644 (file)
index 0000000..630e507
--- /dev/null
@@ -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
index 883c120..88dfc7e 100644 (file)
 
 #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 (file)
index 0000000..5507bd9
--- /dev/null
@@ -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_;
+}
index 7a2c6fc..3ebf038 100644 (file)
@@ -1,6 +1,7 @@
 #include "Internal/AlarmProviderFile.h"
+#include "Model/AlarmProviderEvent.h"
+#include "Utils/EventBus.h"
 
-#include <Ecore_File.h>
 #include <cstdlib>
 #include <algorithm>
 
@@ -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)
index 6483d0f..02f2a95 100644 (file)
@@ -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<std::reference_wrapper<Alarm>> 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<AlarmAddedEvent*>(e);
+
+       view->ItemAppend(
+                       ev->GetAlarm().GetTime(),
+                       ev->GetAlarm().GetName().c_str(),
+                       ev->GetAlarm().GetWeekFlags(),
+                       ev->GetAlarm().IsActivated());
+}