<commit_before>#include "map/guides_manager.hpp"

#include "map/bookmark_catalog.hpp"

#include "partners_api/utm.hpp"

#include "drape_frontend/drape_engine.hpp"
#include "drape_frontend/visual_params.hpp"

#include "platform/preferred_languages.hpp"

#include "geometry/intersection_score.hpp"

#include "private.h"

#include <utility>

namespace
{
auto constexpr kRequestAttemptsCount = 3;
// This constant is empirically calculated based on geometry::IntersectionScore.
// When screen scales are different more than 11.15 percent
// it is equal less than 80 percents screen rectangles intersection.
auto constexpr kScaleEps = 0.1115;
}  // namespace

GuidesManager::GuidesManager(CloseGalleryFn && closeGalleryFn)
  : m_closeGallery(std::move(closeGalleryFn))
{
  CHECK(m_closeGallery != nullptr, ());
}

GuidesManager::GuidesState GuidesManager::GetState() const
{
  return m_state;
}

void GuidesManager::SetStateListener(GuidesStateChangedFn const & onStateChanged)
{
  m_onStateChanged = onStateChanged;
  if (m_onStateChanged != nullptr)
    m_onStateChanged(m_state);
}

void GuidesManager::UpdateViewport(ScreenBase const & screen)
{
  m_lastShownViewport = m_screen.GlobalRect();
  auto const zoom = df::GetDrawTileScale(screen);

  if (m_state == GuidesState::Disabled || m_state == GuidesState::FatalNetworkError)
  {
    m_screen = screen;
    m_zoom = zoom;
    return;
  }

  if (screen.GlobalRect().GetLocalRect().IsEmptyInterior())
    return;

  m_closeGallery();

  if (IsRequestParamsInitialized())
  {
    auto const scaleStronglyChanged =
      fabs(m_screen.GetScale() - screen.GetScale()) / m_screen.GetScale() > kScaleEps;

    if (!scaleStronglyChanged)
    {
      m2::AnyRectD::Corners currentCorners;
      m_screen.GlobalRect().GetGlobalPoints(currentCorners);

      m2::AnyRectD::Corners screenCorners;
      screen.GlobalRect().GetGlobalPoints(screenCorners);

      auto const score = geometry::GetIntersectionScoreForPoints(currentCorners, screenCorners);

      // If more than 80% of viewport rect intersects with last requested rect then return.
      if (score > 0.8)
        return;
    }
  }

  m_screen = screen;
  m_zoom = zoom;

  RequestGuides();
}

void GuidesManager::Invalidate()
{
  // TODO: Implement.
}

void GuidesManager::Reconnect()
{
  if (m_state != GuidesState::FatalNetworkError)
    return;

  ChangeState(GuidesState::Enabled);
  RequestGuides();
}

void GuidesManager::SetEnabled(bool enabled)
{
  auto const newState = enabled ? GuidesState::Enabled : GuidesState::Disabled;
  if (newState == m_state)
    return;

  Clear();
  ChangeState(newState);
  m_shownGuides.clear();

  if (!enabled)
    return;

  RequestGuides();
}

bool GuidesManager::IsEnabled() const
{
  return m_state != GuidesState::Disabled;
}

void GuidesManager::ChangeState(GuidesState newState)
{
  if (m_state == newState)
    return;
  m_state = newState;
  if (m_onStateChanged != nullptr)
    m_onStateChanged(newState);
}

void GuidesManager::RequestGuides()
{
  if (!IsRequestParamsInitialized())
    return;
<commit_msg>[guides on map] switch to network error state when internet is absent.<commit_after>#include "map/guides_manager.hpp"

#include "map/bookmark_catalog.hpp"

#include "partners_api/utm.hpp"

#include "drape_frontend/drape_engine.hpp"
#include "drape_frontend/visual_params.hpp"

#include "platform/preferred_languages.hpp"

#include "geometry/intersection_score.hpp"

#include "private.h"

#include <utility>

namespace
{
auto constexpr kRequestAttemptsCount = 3;
// This constant is empirically calculated based on geometry::IntersectionScore.
// When screen scales are different more than 11.15 percent
// it is equal less than 80 percents screen rectangles intersection.
auto constexpr kScaleEps = 0.1115;
}  // namespace

GuidesManager::GuidesManager(CloseGalleryFn && closeGalleryFn)
  : m_closeGallery(std::move(closeGalleryFn))
{
  CHECK(m_closeGallery != nullptr, ());
}

GuidesManager::GuidesState GuidesManager::GetState() const
{
  return m_state;
}

void GuidesManager::SetStateListener(GuidesStateChangedFn const & onStateChanged)
{
  m_onStateChanged = onStateChanged;
  if (m_onStateChanged != nullptr)
    m_onStateChanged(m_state);
}

void GuidesManager::UpdateViewport(ScreenBase const & screen)
{
  m_lastShownViewport = m_screen.GlobalRect();
  auto const zoom = df::GetDrawTileScale(screen);

  if (m_state == GuidesState::Disabled || m_state == GuidesState::FatalNetworkError)
  {
    m_screen = screen;
    m_zoom = zoom;
    return;
  }

  if (screen.GlobalRect().GetLocalRect().IsEmptyInterior())
    return;

  m_closeGallery();

  if (IsRequestParamsInitialized())
  {
    auto const scaleStronglyChanged =
      fabs(m_screen.GetScale() - screen.GetScale()) / m_screen.GetScale() > kScaleEps;

    if (!scaleStronglyChanged)
    {
      m2::AnyRectD::Corners currentCorners;
      m_screen.GlobalRect().GetGlobalPoints(currentCorners);

      m2::AnyRectD::Corners screenCorners;
      screen.GlobalRect().GetGlobalPoints(screenCorners);

      auto const score = geometry::GetIntersectionScoreForPoints(currentCorners, screenCorners);

      // If more than 80% of viewport rect intersects with last requested rect then return.
      if (score > 0.8)
        return;
    }
  }

  m_screen = screen;
  m_zoom = zoom;

  RequestGuides();
}

void GuidesManager::Invalidate()
{
  // TODO: Implement.
}

void GuidesManager::Reconnect()
{
  if (m_state != GuidesState::FatalNetworkError)
    return;

  ChangeState(GuidesState::Enabled);
  RequestGuides();
}

void GuidesManager::SetEnabled(bool enabled)
{
  auto const newState = enabled ? GuidesState::Enabled : GuidesState::Disabled;
  if (newState == m_state)
    return;

  Clear();
  m_shownGuides.clear();
  ChangeState(newState);

  if (!enabled)
    return;

  if (!GetPlatform().IsConnected())
  {
    ChangeState(GuidesState::FatalNetworkError);
    return;
  }

  RequestGuides();
}

bool GuidesManager::IsEnabled() const
{
  return m_state != GuidesState::Disabled;
}

void GuidesManager::ChangeState(GuidesState newState)
{
  if (m_state == newState)
    return;
  m_state = newState;
  if (m_onStateChanged != nullptr)
    m_onStateChanged(newState);
}

void GuidesManager::RequestGuides()
{
  if (!IsRequestParamsInitialized())
    return;

{'fraction_non_alphanumeric': 0.11365442695914799, 'fraction_numerical': 0.005722460658082976, 'mean_word_length': 4.725204731574158, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5734491', 'n_tokens_mistral': 2121, 'n_tokens_neox': 2023, 'n_words': 415}