Add implementation of variadic notify function to Observable class 65/210065/6
authorLukasz Wlazly <l.wlazly@partner.samsung.com>
Mon, 15 Jul 2019 11:00:03 +0000 (13:00 +0200)
committerLukasz Oleksak <l.oleksak@samsung.com>
Tue, 16 Jul 2019 12:40:36 +0000 (12:40 +0000)
Change-Id: Id33437727ce50afb5d027e0b5feb9468532e89df

src/presenter/AppContext.cpp
src/presenter/AppContext.hpp
src/utils/Observable.hpp
src/utils/ObservableProperty.hpp

index d7c989c..752060e 100644 (file)
@@ -20,9 +20,7 @@ void AppContext::push(std::unique_ptr<Presenter> presenter)
                presentersStack_.back()->notOnTop();
        presentersStack_.push_back(std::move(presenter));
        presentersStack_.back()->onTop();
-       presenterToBroadcast_ = presentersStack_.back().get();
-       countOfPagesToPop_ = 1;
-       notify();
+       Observable<Presenter *, size_t>::notify(presentersStack_.back().get(), 1);
 }
 
 void AppContext::pop(size_t countOfPagesToPop)
@@ -30,23 +28,20 @@ void AppContext::pop(size_t countOfPagesToPop)
        if (countOfPagesToPop < 1)
                return;
 
-       presenterToBroadcast_ = nullptr;
        if (countOfPagesToPop > presentersStack_.size())
-               countOfPagesToPop_ = presentersStack_.size();
-       else
-               countOfPagesToPop_ = countOfPagesToPop;
+               countOfPagesToPop = presentersStack_.size();
 
        if (presentersStack_.size() > 0)
                presentersStack_.back()->notOnTop();
 
-       for (auto i = 0u; i < countOfPagesToPop_; ++i)
+       for (auto i = 0u; i < countOfPagesToPop; ++i)
                presentersStack_.pop_back();
 
        if (presentersStack_.empty()) {
                ui_app_exit();
        } else {
                presentersStack_.back()->onTop();
-               notify();
+               Observable<Presenter *, size_t>::notify(nullptr, countOfPagesToPop);
        }
 }
 
@@ -65,8 +60,7 @@ void AppContext::pushModal(std::unique_ptr<ModalPresenter> presenter)
        if (presentersStack_.size() > 0)
                presentersStack_.back()->notOnTop();
        modalPresentersStack_.push_back(std::move(presenter));
-       modalPresenterToBroadcast_ = modalPresentersStack_.back().get();
-       notifyAboutModal();
+       Observable<ModalPresenter *>::notify(modalPresentersStack_.back().get());
 }
 
 void AppContext::popModal()
@@ -74,8 +68,7 @@ void AppContext::popModal()
        if (presentersStack_.size() > 0)
                presentersStack_.back()->onTop();
        modalPresentersStack_.pop_back();
-       modalPresenterToBroadcast_ = nullptr;
-       notifyAboutModal();
+       Observable<ModalPresenter *>::notify(nullptr);
 }
 
 ModalPresenter *AppContext::modalsBack()
@@ -87,17 +80,3 @@ bool AppContext::modalsEmpty()
 {
        return modalPresentersStack_.empty();
 }
-
-void AppContext::notify()
-{
-       for (auto &c : this->Observable<Presenter *, size_t>::onChangeCallbacks_)
-               if (c)
-                       c(presenterToBroadcast_, countOfPagesToPop_);
-}
-
-void AppContext::notifyAboutModal()
-{
-       for (auto &c : this->Observable<ModalPresenter *>::onChangeCallbacks_)
-               if (c)
-                       c(modalPresenterToBroadcast_);
-}
index 7d6c2b3..e9cb31b 100644 (file)
@@ -18,8 +18,6 @@ class AppContext : public Observable<Presenter *, size_t>, public Observable<Mod
        void pop(size_t countOfPagesToPop = 1);
        Presenter *back();
        bool empty();
-       void notify();
-       void notifyAboutModal();
 
        void pushModal(std::unique_ptr<ModalPresenter> presenter);
        void popModal();
@@ -30,9 +28,6 @@ class AppContext : public Observable<Presenter *, size_t>, public Observable<Mod
        private:
        std::vector<std::unique_ptr<Presenter>> presentersStack_;
        std::vector<std::unique_ptr<ModalPresenter>> modalPresentersStack_;
-       Presenter *presenterToBroadcast_ = nullptr;
-       size_t countOfPagesToPop_ = 1;
-       ModalPresenter *modalPresenterToBroadcast_ = nullptr;
 };
 
 #endif
\ No newline at end of file
index 3661709..79accd9 100644 (file)
@@ -16,17 +16,14 @@ class Observable
        }
 
        protected:
-       void notify();
+       void notify(ARGS... args)
+       {
+               for (auto &c : this->onChangeCallbacks_)
+                       if (c)
+                               c(args...);
+       }
 
        std::vector<std::function<void(ARGS...)>> onChangeCallbacks_;
 };
 
-template <>
-inline void Observable<>::notify()
-{
-       for (auto &c : this->onChangeCallbacks_)
-               if (c)
-                       c();
-}
-
 #endif
\ No newline at end of file
index 906f647..e38b833 100644 (file)
@@ -23,18 +23,10 @@ class ObservableProperty : public Observable<V>
                        return *this;
 
                value_ = std::move(val);
-               notify();
+               this->notify(value_);
                return *this;
        }
 
-       protected:
-       void notify()
-       {
-               for (auto &c : this->onChangeCallbacks_)
-                       if (c)
-                               c(value_);
-       }
-
        V value_{};
 };