diff options
author | rachytski <siarhei.rachytski@gmail.com> | 2012-05-15 19:58:46 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:38:27 +0300 |
commit | 09d97d23cb9ee23c7c9994bd581783a8cbf9d59f (patch) | |
tree | 3f4110b85f8e0bdce119eba1f0d12bdb29242112 /storage | |
parent | e686199c14e4f5c1fed36aa58e12f943a34d3df3 (diff) |
added support for multiple observers to the Storage.
Diffstat (limited to 'storage')
-rw-r--r-- | storage/storage.cpp | 60 | ||||
-rw-r--r-- | storage/storage.hpp | 29 |
2 files changed, 62 insertions, 27 deletions
diff --git a/storage/storage.cpp b/storage/storage.cpp index b5b0187341..47c2a49735 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -56,7 +56,7 @@ namespace storage // return "Unknown error"; // } - Storage::Storage() + Storage::Storage() : m_currentSlotId(0) { LoadCountriesFile(false); } @@ -167,7 +167,7 @@ namespace storage else { // notify about "In Queue" status - NotifyStatusChanhed(index); + NotifyStatusChanged(index); } } @@ -187,10 +187,10 @@ namespace storage } }; - void Storage::NotifyStatusChanhed(TIndex const & index) const + void Storage::NotifyStatusChanged(TIndex const & index) const { - if (m_observerChange) - m_observerChange(index); + for (list<CountryObservers>::const_iterator it = m_observers.begin(); it != m_observers.end(); ++it) + it->m_changeCountryFn(index); } void Storage::DownloadNextCountryFromQueue() @@ -210,7 +210,7 @@ namespace storage bind(&Storage::OnServerListDownloaded, this, _1))); // new status for country, "Downloading" - NotifyStatusChanhed(index); + NotifyStatusChanged(index); return; } } @@ -225,7 +225,7 @@ namespace storage } // new status for country, "OnDisk" - NotifyStatusChanhed(index); + NotifyStatusChanged(index); } } @@ -299,7 +299,7 @@ namespace storage } DeactivateAndDeleteCountry(country, m_removeMap); - NotifyStatusChanhed(index); + NotifyStatusChanged(index); if (bounds != m2::RectD::GetEmptyRect()) m_updateRect(bounds); @@ -320,17 +320,32 @@ namespace storage } } - void Storage::Subscribe(TObserverChangeCountryFunction change, - TObserverProgressFunction progress) + int Storage::Subscribe(TChangeCountryFunction change, + TProgressFunction progress) { - m_observerChange = change; - m_observerProgress = progress; + CountryObservers obs; + + obs.m_changeCountryFn = change; + obs.m_progressFn = progress; + obs.m_slotId = m_currentSlotId++; + + m_observers.push_back(obs); + + return obs.m_slotId; } - void Storage::Unsubscribe() + void Storage::Unsubscribe(int slotId) { - m_observerChange.clear(); - m_observerProgress.clear(); + for (list<CountryObservers>::iterator it = m_observers.begin(); + it != m_observers.end(); + ++it) + { + if (it->m_slotId == slotId) + { + m_observers.erase(it); + return; + } + } } void Storage::OnMapDownloadFinished(HttpRequest & request) @@ -349,7 +364,7 @@ namespace storage m_failedCountries.insert(index); // notify GUI about failed country - NotifyStatusChanhed(index); + NotifyStatusChanged(index); } else { @@ -399,7 +414,7 @@ namespace storage { // remove from index set m_indexGeneration.erase(index); - NotifyStatusChanhed(index); + NotifyStatusChanged(index); // activate downloaded map piece m_addMap(fName); @@ -410,6 +425,12 @@ namespace storage m_updateRect(header.GetBounds()); } + void Storage::ReportProgress(TIndex const & idx, pair<int64_t, int64_t> const & p) + { + for (list<CountryObservers>::const_iterator it = m_observers.begin(); it != m_observers.end(); ++it) + it->m_progressFn(idx, p); + } + void Storage::OnMapDownloadProgress(HttpRequest & request) { if (m_queue.empty()) @@ -418,12 +439,13 @@ namespace storage return; } - if (m_observerProgress) + if (!m_observers.empty()) { HttpRequest::ProgressT p = request.Progress(); p.first += m_countryProgress.first; p.second = m_countryProgress.second; - m_observerProgress(m_queue.front(), p); + + ReportProgress(m_queue.front(), p); } } diff --git a/storage/storage.hpp b/storage/storage.hpp index 05bd2c604d..09859c5cc7 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -81,10 +81,19 @@ namespace storage /// @name Communicate with GUI //@{ - typedef function<void (TIndex const &)> TObserverChangeCountryFunction; - typedef function<void (TIndex const &, pair<int64_t, int64_t> const &)> TObserverProgressFunction; - TObserverChangeCountryFunction m_observerChange; - TObserverProgressFunction m_observerProgress; + typedef function<void (TIndex const &)> TChangeCountryFunction; + typedef function<void (TIndex const &, pair<int64_t, int64_t> const &)> TProgressFunction; + + int m_currentSlotId; + + struct CountryObservers + { + TChangeCountryFunction m_changeCountryFn; + TProgressFunction m_progressFn; + int m_slotId; + }; + + list<CountryObservers> m_observers; //@} /// @name Communicate with Framework @@ -111,6 +120,8 @@ namespace storage /// @TODO temporarily made public for Android, refactor void LoadCountriesFile(bool forceReload); + void ReportProgress(TIndex const & index, pair<int64_t, int64_t> const & p); + public: Storage(); @@ -128,9 +139,11 @@ namespace storage /// @name Current impl supports only one observer //@{ - void Subscribe(TObserverChangeCountryFunction change, - TObserverProgressFunction progress); - void Unsubscribe(); + + /// @return unique identifier that should be used with Unsubscribe function + int Subscribe(TChangeCountryFunction change, + TProgressFunction progress); + void Unsubscribe(int slotId); //@} size_t CountriesCount(TIndex const & index) const; @@ -145,7 +158,7 @@ namespace storage void CheckForUpdate(); - void NotifyStatusChanhed(TIndex const & index) const; + void NotifyStatusChanged(TIndex const & index) const; int64_t GetCurrentVersion() const; }; |