<commit_before>#import "LocationManager.h"
#import "MapsAppDelegate.h"
#import "MapsObservers.h"
#import "MWMConsole.h"
#import "MWMRoutingProtocol.h"
#import "MWMSearchDownloadViewController.h"
#import "MWMSearchManager.h"
#import "MWMSearchTabbedViewController.h"
#import "MWMSearchTabButtonsView.h"
#import "MWMSearchTableViewController.h"
#import "Statistics.h"

#import "3party/Alohalytics/src/alohalytics_objc.h"

#include "Framework.h"
#include "map/active_maps_layout.hpp"

extern NSString * const kAlohalyticsTapEventKey;
extern NSString * const kSearchStateWillChangeNotification = @"SearchStateWillChangeNotification";
extern NSString * const kSearchStateKey = @"SearchStateKey";

@interface MWMSearchManager ()<MWMSearchTableViewProtocol, MWMSearchDownloadProtocol,
                               MWMSearchTabbedViewProtocol, ActiveMapsObserverProtocol,
                               MWMSearchTabButtonsViewProtocol, UITextFieldDelegate>

@property (weak, nonatomic) UIView * parentView;
@property (nonatomic) IBOutlet MWMSearchView * rootView;
@property (weak, nonatomic) IBOutlet UIView * contentView;

@property (nonatomic) IBOutletCollection(MWMSearchTabButtonsView) NSArray * tabButtons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint * scrollIndicatorOffset;
@property (weak, nonatomic) IBOutlet UIView * scrollIndicator;

@property (nonatomic) UINavigationController * navigationController;
@property (nonatomic) MWMSearchTabbedViewController * tabbedController;
@property (nonatomic) MWMSearchTableViewController * tableViewController;
@property (nonatomic) MWMSearchDownloadViewController * downloadController;

@end

@implementation MWMSearchManager
{
  unique_ptr<ActiveMapsObserver> m_mapsObserver;
  int m_mapsObserverSlotId;
}

- (nullable instancetype)initWithParentView:(nonnull UIView *)view
                                   delegate:(nonnull id<MWMSearchManagerProtocol, MWMSearchViewProtocol, MWMRoutingProtocol>)delegate
{
  self = [super init];
  if (self)
  {
    [NSBundle.mainBundle loadNibNamed:@"MWMSearchView" owner:self options:nil];
    self.delegate = delegate;
    self.rootView.delegate = delegate;
    self.parentView = view;
    self.searchTextField.placeholder = L(@"search");
    self.state = MWMSearchManagerStateHidden;
  }
  return self;
}

- (void)beginSearch
{
  if (self.state == MWMSearchManagerStateDefault)
    self.state = MWMSearchManagerStateTableSearch;
  self.searchTextField.isSearching = YES;
}

- (void)endSearch
{
  GetFramework().CancelInteractiveSearch();
  if (self.state != MWMSearchManagerStateHidden)
    self.state = MWMSearchManagerStateDefault;
  self.searchTextField.isSearching = NO;
  self.searchTextField.text = @"";
  [self.tableViewController searchText:@"" forInputLocale:nil];
}

#pragma mark - Actions

- (IBAction)textFieldDidEndEditing:(UITextField *)textField
{
  if (textField.text.length == 0)
    [self endSearch];
}

- (IBAction)textFieldTextDidChange:(UITextField *)textField
{
  NSString * text = textField.text;
  if (text.length > 0)
  {
    if ([MWMConsole performCommand:text])
    {
      self.state = MWMSearchManagerStateHidden;
    }
    else
    {
      [self beginSearch];
      [self.tableViewController searchText:text
                            forInputLocale:textField.textInputMode.primaryLanguage];
    }
  }
  else
  {
    [self endSearch];
  }
}

- (IBAction)cancelButtonPressed
{
  [[Statistics instance] logEvent:kStatEventName(kStatSearch, kStatCancel)];
  [Alohalytics logEvent:kAlohalyticsTapEventKey withValue:@"searchCancel"];
  self.state = MWMSearchManagerStateHidden;
  MapsAppDelegate * a = MapsAppDelegate.theApp;
  MWMRoutingPlaneMode const m = a.routingPlaneMode;
  if (m == MWMRoutingPlaneModeSearchDestination || m == MWMRoutingPlaneModeSearchSource)
    a.routingPlaneMode = MWMRoutingPlaneModePlacePage;
}

- (void)tabButtonPressed:(MWMSearchTabButtonsView *)sender
{
  [self.searchTextField resignFirstResponder];
  [self.tabbedController tabButtonPressed:sender];
}

#pragma mark - Layout

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
  [self.navigationController willRotateToInterfaceOrientation:toInterfaceOrientation
                                                     duration:duration];
}

- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
  [self.navigationController viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(nonnull UITextField *)textField
{
  [textField resignFirstResponder];
  if (textField.text.length != 0)
    self.state = MWMSearchManagerStateMapSearch;
  return YES;
}

#pragma mark - MWMSearchTabbedViewProtocol

- (void)searchText:(NSString *)text forInputLocale:(NSString *)locale
{
  [self beginSearch];
  self.searchTextField.text = text;
  NSString * inputLocale = locale ? locale : self.searchTextField.textInputMode.primaryLanguage;
  [self.tableViewController searchText:text forInputLocale:inputLocale];
}

- (void)tapMyPositionFromHistory
{
  MapsAppDelegate * a = MapsAppDelegate.theApp;
  MWMRoutePoint const p = MWMRoutePoint::MWMRoutePoint(a.m_locationManager.lastLocation.mercator);
  if (a.routingPlaneMode == MWMRoutingPlaneModeSearchSource)
    [self.delegate buildRouteFrom:p];
  else if (a.routingPlaneMode == MWMRoutingPlaneModeSearchDestination)
    [self.delegate buildRouteTo:p];
  else
    NSAssert(false, @"Incorrect state for process my position tap");
  if (!IPAD)
    a.routingPlaneMode = MWMRoutingPlaneModePlacePage;
  self.state = MWMSearchManagerStateHidden;
}

- (void)processSearchWithResult:(search::Result const &)result query:(search::QuerySaver::TSearchRequest const &)query
{
  auto & f = GetFramework();
  f.SaveSearchQuery(query);
  MapsAppDelegate * a = MapsAppDelegate.theApp;
  MWMRoutingPlaneMode const m = a.routingPlaneMode;
  MWMRoutePoint const p = {result.GetFeatureCenter(), @(result.GetString())};
  if (m == MWMRoutingPlaneModeSearchSource)
    [self.delegate buildRouteFrom:p];
  else if (m == MWMRoutingPlaneModeSearchDestination)
     [self.delegate buildRouteTo:p];
  else
    f.ShowSearchResult(result);
  if (!IPAD)
    a.routingPlaneMode = MWMRoutingPlaneModePlacePage;
  self.state = MWMSearchManagerStateHidden;
}

#pragma mark - MWMSearchDownloadMapRequest

- (void)selectMapsAction
{
  [self.delegate actionDownloadMaps];
}

#pragma mark - ActiveMapsObserverProtocol

- (void)countryStatusChangedAtPosition:(int)position inGroup:(ActiveMapsLayout::TGroup const &)group
{
  auto const status =
      GetFramework().GetCountryTree().GetActiveMapLayout().GetCountryStatus(group, position);
  [self updateTopController];
  if (status == TStatus::EDownloadFailed)
    [self.downloadController setDownloadFailed];
  if (self.state == MWMSearchManagerStateTableSearch ||
      self.state == MWMSearchManagerStateMapSearch)
  {
    NSString * text = self.searchTextField.text;
    if (text.length > 0)<commit_msg>[ios] Fixed bug with pp after search.<commit_after>#import "LocationManager.h"
#import "MapsAppDelegate.h"
#import "MapsObservers.h"
#import "MWMConsole.h"
#import "MWMRoutingProtocol.h"
#import "MWMSearchDownloadViewController.h"
#import "MWMSearchManager.h"
#import "MWMSearchTabbedViewController.h"
#import "MWMSearchTabButtonsView.h"
#import "MWMSearchTableViewController.h"
#import "Statistics.h"

#import "3party/Alohalytics/src/alohalytics_objc.h"

#include "Framework.h"
#include "map/active_maps_layout.hpp"

extern NSString * const kAlohalyticsTapEventKey;
extern NSString * const kSearchStateWillChangeNotification = @"SearchStateWillChangeNotification";
extern NSString * const kSearchStateKey = @"SearchStateKey";

@interface MWMSearchManager ()<MWMSearchTableViewProtocol, MWMSearchDownloadProtocol,
                               MWMSearchTabbedViewProtocol, ActiveMapsObserverProtocol,
                               MWMSearchTabButtonsViewProtocol, UITextFieldDelegate>

@property (weak, nonatomic) UIView * parentView;
@property (nonatomic) IBOutlet MWMSearchView * rootView;
@property (weak, nonatomic) IBOutlet UIView * contentView;

@property (nonatomic) IBOutletCollection(MWMSearchTabButtonsView) NSArray * tabButtons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint * scrollIndicatorOffset;
@property (weak, nonatomic) IBOutlet UIView * scrollIndicator;

@property (nonatomic) UINavigationController * navigationController;
@property (nonatomic) MWMSearchTabbedViewController * tabbedController;
@property (nonatomic) MWMSearchTableViewController * tableViewController;
@property (nonatomic) MWMSearchDownloadViewController * downloadController;

@end

@implementation MWMSearchManager
{
  unique_ptr<ActiveMapsObserver> m_mapsObserver;
  int m_mapsObserverSlotId;
}

- (nullable instancetype)initWithParentView:(nonnull UIView *)view
                                   delegate:(nonnull id<MWMSearchManagerProtocol, MWMSearchViewProtocol, MWMRoutingProtocol>)delegate
{
  self = [super init];
  if (self)
  {
    [NSBundle.mainBundle loadNibNamed:@"MWMSearchView" owner:self options:nil];
    self.delegate = delegate;
    self.rootView.delegate = delegate;
    self.parentView = view;
    self.searchTextField.placeholder = L(@"search");
    self.state = MWMSearchManagerStateHidden;
  }
  return self;
}

- (void)beginSearch
{
  if (self.state == MWMSearchManagerStateDefault)
    self.state = MWMSearchManagerStateTableSearch;
  self.searchTextField.isSearching = YES;
}

- (void)endSearch
{
  GetFramework().CancelInteractiveSearch();
  if (self.state != MWMSearchManagerStateHidden)
    self.state = MWMSearchManagerStateDefault;
  self.searchTextField.isSearching = NO;
  self.searchTextField.text = @"";
  [self.tableViewController searchText:@"" forInputLocale:nil];
}

#pragma mark - Actions

- (IBAction)textFieldDidEndEditing:(UITextField *)textField
{
  if (textField.text.length == 0)
    [self endSearch];
}

- (IBAction)textFieldTextDidChange:(UITextField *)textField
{
  NSString * text = textField.text;
  if (text.length > 0)
  {
    if ([MWMConsole performCommand:text])
    {
      self.state = MWMSearchManagerStateHidden;
    }
    else
    {
      [self beginSearch];
      [self.tableViewController searchText:text
                            forInputLocale:textField.textInputMode.primaryLanguage];
    }
  }
  else
  {
    [self endSearch];
  }
}

- (IBAction)cancelButtonPressed
{
  [[Statistics instance] logEvent:kStatEventName(kStatSearch, kStatCancel)];
  [Alohalytics logEvent:kAlohalyticsTapEventKey withValue:@"searchCancel"];
  self.state = MWMSearchManagerStateHidden;
  MapsAppDelegate * a = MapsAppDelegate.theApp;
  MWMRoutingPlaneMode const m = a.routingPlaneMode;
  if (m == MWMRoutingPlaneModeSearchDestination || m == MWMRoutingPlaneModeSearchSource)
    a.routingPlaneMode = MWMRoutingPlaneModePlacePage;
}

- (void)tabButtonPressed:(MWMSearchTabButtonsView *)sender
{
  [self.searchTextField resignFirstResponder];
  [self.tabbedController tabButtonPressed:sender];
}

#pragma mark - Layout

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
  [self.navigationController willRotateToInterfaceOrientation:toInterfaceOrientation
                                                     duration:duration];
}

- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
  [self.navigationController viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(nonnull UITextField *)textField
{
  [textField resignFirstResponder];
  if (textField.text.length != 0)
    self.state = MWMSearchManagerStateMapSearch;
  return YES;
}

#pragma mark - MWMSearchTabbedViewProtocol

- (void)searchText:(NSString *)text forInputLocale:(NSString *)locale
{
  [self beginSearch];
  self.searchTextField.text = text;
  NSString * inputLocale = locale ? locale : self.searchTextField.textInputMode.primaryLanguage;
  [self.tableViewController searchText:text forInputLocale:inputLocale];
}

- (void)tapMyPositionFromHistory
{
  MapsAppDelegate * a = MapsAppDelegate.theApp;
  MWMRoutePoint const p = MWMRoutePoint::MWMRoutePoint(a.m_locationManager.lastLocation.mercator);
  if (a.routingPlaneMode == MWMRoutingPlaneModeSearchSource)
    [self.delegate buildRouteFrom:p];
  else if (a.routingPlaneMode == MWMRoutingPlaneModeSearchDestination)
    [self.delegate buildRouteTo:p];
  else
    NSAssert(false, @"Incorrect state for process my position tap");
  if (!IPAD)
    a.routingPlaneMode = MWMRoutingPlaneModePlacePage;
  self.state = MWMSearchManagerStateHidden;
}

- (void)processSearchWithResult:(search::Result const &)result query:(search::QuerySaver::TSearchRequest const &)query
{
  auto & f = GetFramework();
  f.SaveSearchQuery(query);
  MapsAppDelegate * a = MapsAppDelegate.theApp;
  MWMRoutingPlaneMode const m = a.routingPlaneMode;
  MWMRoutePoint const p = {result.GetFeatureCenter(), @(result.GetString())};
  if (m == MWMRoutingPlaneModeSearchSource)
    [self.delegate buildRouteFrom:p];
  else if (m == MWMRoutingPlaneModeSearchDestination)
     [self.delegate buildRouteTo:p];
  else
    f.ShowSearchResult(result);
  if (!IPAD && a.routingPlaneMode != MWMRoutingPlaneModeNone)
    a.routingPlaneMode = MWMRoutingPlaneModePlacePage;
  self.state = MWMSearchManagerStateHidden;
}

#pragma mark - MWMSearchDownloadMapRequest

- (void)selectMapsAction
{
  [self.delegate actionDownloadMaps];
}

#pragma mark - ActiveMapsObserverProtocol

- (void)countryStatusChangedAtPosition:(int)position inGroup:(ActiveMapsLayout::TGroup const &)group
{
  auto const status =
      GetFramework().GetCountryTree().GetActiveMapLayout().GetCountryStatus(group, position);
  [self updateTopController];
  if (status == TStatus::EDownloadFailed)
    [self.downloadController setDownloadFailed];
  if (self.state == MWMSearchManagerStateTableSearch ||
      self.state == MWMSearchManagerStateMapSearch)
  {
    NSString * text = self.searchTextField.text;
    if (text.length > 0)
{'fraction_non_alphanumeric': 0.09785719271305926, 'fraction_numerical': 0.0006979828296223913, 'mean_word_length': 5.226857887874837, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7393859', 'n_tokens_mistral': 4478, 'n_tokens_neox': 4252, 'n_words': 759}