[lightweight] Delegate is added into lightweight framework

This commit is contained in:
Arsentiy Milchakov 2019-03-21 15:06:53 +03:00 committed by Roman Kuznetsov
parent 9d45071fd1
commit 69fb7d97e8
10 changed files with 101 additions and 63 deletions

View file

@ -1,4 +1,5 @@
#include "map/framework_light.hpp"
#include "map/framework_light_delegate.hpp"
#include "map/local_ads_manager.hpp"
#include "base/assert.hpp"
@ -92,7 +93,9 @@ Java_com_mapswithme_maps_LightFramework_nativeLogLocalAdsEvent(JNIEnv * env, jcl
JNIEXPORT jobject JNICALL
Java_com_mapswithme_maps_LightFramework_nativeGetNotification(JNIEnv * env, jclass clazz)
{
Framework framework(REQUEST_TYPE_NOTIFICATION);
lightweight::Framework framework(lightweight::REQUEST_TYPE_NOTIFICATION);
if (g_framework)
framework.SetDelegate(make_unique<FrameworkLightDelegate>(*g_framework->NativeFramework()));
auto const notification = framework.GetNotification();
if (!notification)

View file

@ -4,8 +4,12 @@
#import "Statistics.h"
#include "map/framework_light.hpp"
#include "map/framework_light_delegate.hpp"
#include "platform/network_policy_ios.h"
#include "Framework.h"
static NSString * const kLastUGCNotificationDate = @"LastUGCNotificationDate";
@implementation LocalNotificationManager
@ -39,8 +43,10 @@ static NSString * const kLastUGCNotificationDate = @"LastUGCNotificationDate";
+ (CoreNotificationWrapper *)reviewNotificationWrapper
{
lightweight::Framework const framework(lightweight::REQUEST_TYPE_NOTIFICATION);
lightweight::Framework framework(lightweight::REQUEST_TYPE_NOTIFICATION);
framework.SetDelegate(make_unique<FrameworkLightDelegate>(GetFramework()));
auto const notificationCandidate = framework.GetNotification();
if (notificationCandidate)
{
auto const notification = notificationCandidate.get();

View file

@ -57,6 +57,7 @@ set(
framework.hpp
framework_light.cpp
framework_light.hpp
framework_light_delegate.hpp
ge0_parser.cpp
ge0_parser.hpp
geourl_process.cpp

View file

@ -3846,37 +3846,6 @@ booking::AvailabilityParams Framework::GetLastBookingAvailabilityParams() const
return m_bookingAvailabilityParams;
}
void Framework::OnPowerFacilityChanged(power_management::Facility const facility, bool enabled)
{
if (facility == power_management::Facility::PerspectiveView ||
facility == power_management::Facility::Buildings3d)
{
bool allow3d = true, allow3dBuildings = true;
Load3dMode(allow3d, allow3dBuildings);
if (facility == power_management::Facility::PerspectiveView)
allow3d = allow3d && enabled;
else
allow3dBuildings = allow3dBuildings && enabled;
Allow3dMode(allow3d, allow3dBuildings);
}
else if (facility == power_management::Facility::TrafficJams)
{
auto trafficState = enabled && LoadTrafficEnabled();
if (trafficState == GetTrafficManager().IsEnabled())
return;
GetTrafficManager().SetEnabled(trafficState);
}
}
void Framework::OnPowerSchemeChanged(power_management::Scheme const actualScheme)
{
if (actualScheme == power_management::Scheme::EconomyMaximum && GetTrafficManager().IsEnabled())
GetTrafficManager().SetEnabled(false);
}
TipsApi const & Framework::GetTipsApi() const
{
return m_tipsApi;
@ -3925,3 +3894,39 @@ bool Framework::MakePlacePageInfo(NotificationCandidate const & notification,
return found;
}
void Framework::OnPowerFacilityChanged(power_management::Facility const facility, bool enabled)
{
if (facility == power_management::Facility::PerspectiveView ||
facility == power_management::Facility::Buildings3d)
{
bool allow3d = true, allow3dBuildings = true;
Load3dMode(allow3d, allow3dBuildings);
if (facility == power_management::Facility::PerspectiveView)
allow3d = allow3d && enabled;
else
allow3dBuildings = allow3dBuildings && enabled;
Allow3dMode(allow3d, allow3dBuildings);
}
else if (facility == power_management::Facility::TrafficJams)
{
auto trafficState = enabled && LoadTrafficEnabled();
if (trafficState == GetTrafficManager().IsEnabled())
return;
GetTrafficManager().SetEnabled(trafficState);
}
}
void Framework::OnPowerSchemeChanged(power_management::Scheme const actualScheme)
{
if (actualScheme == power_management::Scheme::EconomyMaximum && GetTrafficManager().IsEnabled())
GetTrafficManager().SetEnabled(false);
}
notifications::NotificationManager & Framework::GetNotificationManager()
{
return m_notificationManager;
}

View file

@ -927,4 +927,6 @@ public:
// PowerManager::Subscriber override.
void OnPowerFacilityChanged(power_management::Facility const facility, bool enabled) override;
void OnPowerSchemeChanged(power_management::Scheme const actualScheme) override;
notifications::NotificationManager & GetNotificationManager();
};

View file

@ -4,6 +4,7 @@
#include "base/string_utils.hpp"
#include <sstream>
#include <utility>
namespace
{
@ -60,13 +61,17 @@ Framework::Framework(RequestTypeMask request) : m_request(request)
if (request & REQUEST_TYPE_NOTIFICATION)
{
m_notificationManager = std::make_unique<lightweight::NotificationManager>();
request ^= REQUEST_TYPE_NOTIFICATION;
}
CHECK_EQUAL(request, REQUEST_TYPE_EMPTY, ("Incorrect mask type:", request));
}
void Framework::SetDelegate(std::unique_ptr<Delegate> delegate)
{
m_delegate = std::move(delegate);
}
bool Framework::IsUserAuthenticated() const
{
ASSERT(m_request & REQUEST_TYPE_USER_AUTH_STATUS, (m_request));
@ -115,9 +120,20 @@ Statistics * Framework::GetLocalAdsStatistics()
return m_localAdsStatistics.get();
}
boost::optional<notifications::NotificationCandidate> Framework::GetNotification() const
notifications::Notification Framework::GetNotification() const
{
return m_notificationManager->GetNotification();
// Do not disturb from 9p.m. to 10 a.m.
auto const time = notifications::Clock::to_time_t(notifications::Clock::now());
auto const localTime = std::localtime(&time);
if (localTime->tm_hour <= 9 || localTime->tm_hour >= 21)
return {};
if (m_delegate)
return m_delegate->GetNotificationManager().GetNotification();
notifications::NotificationManager notificationManager;
notificationManager.Load();
return notificationManager.GetNotification();
}
std::string FeatureParamsToString(int64_t mwmVersion, std::string const & countryId, uint32_t featureIndex)

View file

@ -49,10 +49,17 @@ using RequestTypeMask = unsigned;
class Framework
{
public:
class Delegate
{
public:
virtual notifications::NotificationManager & GetNotificationManager() = 0;
};
friend struct LightFrameworkTest;
explicit Framework(RequestTypeMask request);
void SetDelegate(std::unique_ptr<Delegate> delegate);
bool IsUserAuthenticated() const;
size_t GetNumberOfUnsentUGC() const;
size_t GetNumberOfUnsentEdits() const;
@ -61,10 +68,11 @@ public:
std::vector<CampaignFeature> GetLocalAdsFeatures(double lat, double lon, double radiusInMeters,
uint32_t maxCount);
Statistics * GetLocalAdsStatistics();
boost::optional<notifications::NotificationCandidate> GetNotification() const;
notifications::Notification GetNotification() const;
private:
RequestTypeMask m_request;
std::unique_ptr<Delegate> m_delegate;
bool m_userAuthStatus = false;
size_t m_numberOfUnsentUGC = 0;
size_t m_numberOfUnsentEdits = 0;
@ -72,7 +80,6 @@ private:
std::unique_ptr<CountryInfoReader> m_countryInfoReader;
std::unique_ptr<LocalAdsFeaturesReader> m_localAdsFeaturesReader;
std::unique_ptr<Statistics> m_localAdsStatistics;
std::unique_ptr<lightweight::NotificationManager> m_notificationManager;
};
std::string FeatureParamsToString(int64_t mwmVersion, std::string const & countryId, uint32_t featureIndex);

View file

@ -0,0 +1,20 @@
#pragma once
#include "map/framework.hpp"
#include "map/framework_light.hpp"
// Implemented in assume that delegate lifetime is shorter than Framework lifetime.
// In other words lightweight::Framework lifetime should be shorter than Framework lifetime.
class FrameworkLightDelegate : public lightweight::Framework::Delegate
{
public:
explicit FrameworkLightDelegate(Framework & framework) : m_framework(framework) {}
notifications::NotificationManager & GetNotificationManager() override
{
return m_framework.GetNotificationManager();
}
private:
Framework & m_framework;
};

View file

@ -146,7 +146,7 @@ void NotificationManager::TrimExpired()
VERIFY(Save(), ());
}
boost::optional<NotificationCandidate> NotificationManager::GetNotification()
Notification NotificationManager::GetNotification()
{
if (Clock::now() - m_queue.m_lastNotificationProvidedTime < kPeriodBetweenNotifications)
return {};

View file

@ -14,6 +14,7 @@
namespace notifications
{
using Notification = boost::optional<NotificationCandidate>;
class NotificationManager : public eye::Subscriber
{
public:
@ -32,7 +33,7 @@ public:
void Load();
void TrimExpired();
boost::optional<NotificationCandidate> GetNotification();
Notification GetNotification();
size_t GetCandidatesCount() const;
// eye::Subscriber overrides:
@ -48,26 +49,3 @@ private:
Queue m_queue;
};
} // namespace notifications
namespace lightweight
{
class NotificationManager
{
public:
NotificationManager() { m_manager.Load(); }
boost::optional<notifications::NotificationCandidate> GetNotification()
{
// Do not disturb from 9p.m. to 10 a.m.
auto const time = notifications::Clock::to_time_t(notifications::Clock::now());
auto const localTime = std::localtime(&time);
if (localTime->tm_hour <= 9 || localTime->tm_hour >= 21)
return {};
return m_manager.GetNotification();
}
private:
notifications::NotificationManager m_manager;
};
} // namespace lightweight