TizenRefApp-8715 [Gallery] Add new functionality into Presenter 25/134625/5
authorIgor Nazarov <i.nazarov@samsung.com>
Mon, 19 Jun 2017 13:09:29 +0000 (16:09 +0300)
committerIgor Nazarov <i.nazarov@samsung.com>
Mon, 19 Jun 2017 16:15:19 +0000 (19:15 +0300)
Change-Id: I58ef64e3a4ee4e0e0b6008062b4980b6c71fde0c

inc/gallery/presenters/Presenter.h
src/presenters/Presenter.cpp

index af8a367b01ce0446000120e494f6a831fea4de0b..d021fa4bddc74b09ec87ae6d1d52985ca4c0e956 100644 (file)
@@ -41,17 +41,31 @@ namespace gallery {
                void activateBy(const void *deactivator);
                void deactivateBy(const void *deactivator);
 
+               void addDeactivatorSource(ucl::Widget &source);
+               void delDeactivatorSource(ucl::Widget &source);
+
+       protected:
+               enum {
+                       PF_ADD_DEACTIVATOR_SOURCES = 1,
+                       PF_ADD_SELF_EXCEPT = 2,
+
+                       PF_PASSIVE = 0,
+                       PF_DEACTIVATOR = (PF_ADD_DEACTIVATOR_SOURCES | PF_ADD_SELF_EXCEPT),
+                       PF_DEFAULT = PF_DEACTIVATOR
+               };
+
        protected:
                Presenter(ucl::IRefCountObj &rc);
-               virtual ~Presenter() = default;
+               virtual ~Presenter();
 
-               ucl::Result prepare(ucl::ElmWidget &widget);
+               ucl::Result prepare(ucl::ElmWidget &widget, int flags = PF_DEFAULT);
+               ucl::Result prepare(Presenter &parent, int flags = PF_DEFAULT);
 
                ucl::Window &getWindow();
                bool isWindowReady() const;
 
-               void addDeactivatorSource(ucl::Widget &source);
                void addDeactivatorException(const void *deactivator);
+               void setDeactivatorSink(const ucl::WidgetSRef &sink);
 
                void sendActivate(ucl::Widget &sender);
                void sendDeactivate(ucl::Widget &sender);
@@ -83,6 +97,10 @@ namespace gallery {
                std::unordered_set<const void *> m_deactivatorExceptions;
                std::unordered_set<const void *> m_deactivators;
                ucl::WindowSRef m_window;
+               ucl::WidgetSRef m_sink;
+               ucl::WidgetWRef m_parentSink;
+               bool m_hasBuildInSources;
+               bool m_isChild;
                bool m_isPrepared;
        };
 }
index 7906044d85032caec8c58a129ba41d7f39f1c8c0..c2e34f008ba3c62da1fda19f8aa74954610f644a 100644 (file)
@@ -30,25 +30,76 @@ namespace gallery {
 
        Presenter::Presenter(IRefCountObj &rc) :
                RefCountAware(&rc),
+               m_hasBuildInSources(false),
+               m_isChild(false),
                m_isPrepared(false)
        {
        }
 
-       Result Presenter::prepare(ElmWidget &widget)
+       Presenter::~Presenter()
+       {
+               if (m_hasBuildInSources) {
+                       if (m_isChild) {
+                               if (const auto parentSink = m_parentSink.lock()) {
+                                       delDeactivatorSource(*parentSink);
+                               }
+                       } else if (m_window) {
+                               delDeactivatorSource(*m_window);
+                       }
+               }
+       }
+
+       Result Presenter::prepare(ElmWidget &widget, const int flags)
        {
                m_window = asShared(widget.getWindow());
                if (!m_window) {
                        LOG_RETURN(RES_FAIL, "m_window is NULL!");
                }
 
-               addDeactivatorSource(*m_window);
-               addDeactivatorException(m_rc->getObjPtr());
+               if (flags & PF_ADD_DEACTIVATOR_SOURCES) {
+                       addDeactivatorSource(*m_window);
+                       m_hasBuildInSources = true;
+               }
+
+               if (flags & PF_ADD_SELF_EXCEPT) {
+                       addDeactivatorException(m_rc->getObjPtr());
+               }
 
                m_isPrepared = true;
 
                return RES_OK;
        }
 
+       Result Presenter::prepare(Presenter &parent, const int flags)
+       {
+               if (!parent.m_sink) {
+                       LOG_RETURN(RES_FAIL, "parent.m_sink is NULL!");
+               }
+
+               for (auto deactivator: parent.m_deactivators) {
+                       if (m_deactivatorExceptions.find(deactivator) ==
+                                       m_deactivatorExceptions.end()) {
+                               m_deactivators.insert(deactivator);
+                       }
+               }
+
+               if (flags & PF_ADD_DEACTIVATOR_SOURCES) {
+                       addDeactivatorSource(*parent.m_sink);
+                       m_hasBuildInSources = true;
+               }
+
+               if (flags & PF_ADD_SELF_EXCEPT) {
+                       addDeactivatorException(m_rc->getObjPtr());
+               }
+
+               m_window = parent.m_window;
+               m_parentSink = parent.m_sink;
+               m_isChild = true;
+               m_isPrepared = true;
+
+               return RES_OK;
+       }
+
        Window &Presenter::getWindow()
        {
                UCL_ASSERT(isWindowReady(), "m_window is NULL!");
@@ -60,14 +111,6 @@ namespace gallery {
                return !!m_window;
        }
 
-       void Presenter::addDeactivatorSource(Widget &source)
-       {
-               source.addEventHandler(impl::ACTIVATE_BY,
-                               WEAK_DELEGATE(Presenter::onActivateBySmart, asWeak(*this)));
-               source.addEventHandler(impl::DEACTIVATE_BY,
-                               WEAK_DELEGATE(Presenter::onDeactivateBySmart, asWeak(*this)));
-       }
-
        void Presenter::addDeactivatorException(const void *const deactivator)
        {
                const auto pair = m_deactivatorExceptions.insert(deactivator);
@@ -76,6 +119,11 @@ namespace gallery {
                }
        }
 
+       void Presenter::setDeactivatorSink(const WidgetSRef &sink)
+       {
+               m_sink = sink;
+       }
+
        void Presenter::sendActivate(Widget &sender)
        {
                sendDeactivator(sender, impl::ACTIVATE_BY, m_rc->getObjPtr());
@@ -134,10 +182,29 @@ namespace gallery {
                deactivateByImpl({deactivator, false});
        }
 
+       void Presenter::addDeactivatorSource(Widget &source)
+       {
+               source.addEventHandler(impl::ACTIVATE_BY,
+                               WEAK_DELEGATE(Presenter::onActivateBySmart, asWeak(*this)));
+               source.addEventHandler(impl::DEACTIVATE_BY,
+                               WEAK_DELEGATE(Presenter::onDeactivateBySmart, asWeak(*this)));
+       }
+
+       void Presenter::delDeactivatorSource(Widget &source)
+       {
+               source.delEventHandler(impl::ACTIVATE_BY,
+                               WEAK_DELEGATE(Presenter::onActivateBySmart, asWeak(*this)));
+               source.delEventHandler(impl::DEACTIVATE_BY,
+                               WEAK_DELEGATE(Presenter::onDeactivateBySmart, asWeak(*this)));
+       }
+
        void Presenter::activateByImpl(const DeactivatorInfo &info)
        {
                const auto count = m_deactivators.erase(info.deactivator);
                if (m_isPrepared && (count > 0)) {
+                       if (m_sink) {
+                               sendDeactivatorInfo(*m_sink, impl::ACTIVATE_BY, info);
+                       }
                        onActivateBy(info);
                        if (m_deactivators.size() == 0) {
                                onActivate();
@@ -153,6 +220,9 @@ namespace gallery {
                }
                const auto pair = m_deactivators.insert(info.deactivator);
                if (m_isPrepared && pair.second) {
+                       if (m_sink) {
+                               sendDeactivatorInfo(*m_sink, impl::DEACTIVATE_BY, info);
+                       }
                        onDeactivateBy(info);
                        if (m_deactivators.size() == 1) {
                                onDeactivate();