Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MWMSearchTableViewController.mm « TableView « Search « UI « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c430d6b766eccb71cc470c22f8c6ea686b1fc70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#import "MWMSearchTableViewController.h"
#import "MWMLocationManager.h"
#import "MWMSearchChangeModeView.h"
#import "MWMSearchCommonCell.h"
#import "MWMSearchSuggestionCell.h"
#import "MWMSearchTableView.h"
#import "MapsAppDelegate.h"
#import "Statistics.h"
#import "SwiftBridge.h"

@interface MWMSearchTableViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(weak, nonatomic) IBOutlet UITableView * tableView;

@property(weak, nonatomic) id<MWMSearchTableViewProtocol> delegate;

@end

@implementation MWMSearchTableViewController

- (nonnull instancetype)initWithDelegate:(id<MWMSearchTableViewProtocol>)delegate
{
  self = [super init];
  if (self)
    _delegate = delegate;
  return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self setupTableView];
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  self.tableView.hidden = NO;
  [(MWMSearchTableView *)self.view hideNoResultsView:YES];
  [self reloadData];
}

- (void)mwm_refreshUI { [self.view mwm_refreshUI]; }
- (void)setupTableView
{
  UITableView * tableView = self.tableView;
  tableView.estimatedRowHeight = 80.;
  tableView.rowHeight = UITableViewAutomaticDimension;
  [tableView registerWithCellClass:[MWMSearchSuggestionCell class]];
  [tableView registerWithCellClass:[MWMSearchCommonCell class]];
  [tableView registerWithCellClass:[MWMAdBanner class]];
}

- (void)reloadData { [self.tableView reloadData]; }
#pragma mark - Layout

- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
  [coordinator
      animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self onSearchResultsUpdated];
      }
                      completion:nil];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [MWMSearch resultsCount];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  auto const row = indexPath.row;
  auto const containerIndex = [MWMSearch containerIndexWithRow:row];
  switch ([MWMSearch resultTypeWithRow:row])
  {
  case MWMSearchItemTypeRegular:
  {
    auto cell = static_cast<MWMSearchCommonCell *>([tableView
        dequeueReusableCellWithCellClass:[MWMSearchCommonCell class]
                               indexPath:indexPath]);
    auto const & result = [MWMSearch resultWithContainerIndex:containerIndex];
    auto const isLocalAds = [MWMSearch isLocalAdsWithContainerIndex:containerIndex];
    [cell config:result isLocalAds:isLocalAds];
    return cell;
  }
  case MWMSearchItemTypeMopub:
  {
    auto cell = static_cast<MWMAdBanner *>(
        [tableView dequeueReusableCellWithCellClass:[MWMAdBanner class] indexPath:indexPath]);
    auto ad = [MWMSearch adWithContainerIndex:containerIndex];
    [cell configWithAd:ad containerType:MWMAdBannerContainerTypeSearch];
    return cell;
  }
  case MWMSearchItemTypeSuggestion:
  {
    auto cell = static_cast<MWMSearchSuggestionCell *>([tableView
        dequeueReusableCellWithCellClass:[MWMSearchSuggestionCell class]
                               indexPath:indexPath]);
    auto const & suggestion = [MWMSearch resultWithContainerIndex:containerIndex];
    [cell config:suggestion];
    cell.isLastCell = row == [MWMSearch suggestionsCount] - 1;
    return cell;
  }
  }
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  id<MWMSearchTableViewProtocol> delegate = self.delegate;
  auto const row = indexPath.row;
  auto const containerIndex = [MWMSearch containerIndexWithRow:row];
  switch ([MWMSearch resultTypeWithRow:row])
  {
  case MWMSearchItemTypeRegular:
  {
    MWMSearchTextField * textField = delegate.searchTextField;
    [MWMSearch saveQuery:textField.text forInputLocale:textField.textInputMode.primaryLanguage];
    auto const & result = [MWMSearch resultWithContainerIndex:containerIndex];
    [delegate processSearchWithResult:result];
    break;
  }
  case MWMSearchItemTypeMopub: break;
  case MWMSearchItemTypeSuggestion:
  {
    auto const & suggestion = [MWMSearch resultWithContainerIndex:containerIndex];
    NSString * suggestionString = @(suggestion.GetSuggestionString());
    [Statistics logEvent:kStatEventName(kStatSearch, kStatSelectResult)
          withParameters:@{kStatValue : suggestionString, kStatScreen : kStatSearch}];
    [delegate searchText:suggestionString forInputLocale:nil];
  }
  }
}

#pragma mark - MWMSearchObserver

- (void)onSearchCompleted
{
  BOOL const noResults = [MWMSearch resultsCount] == 0;
  self.tableView.hidden = noResults;
  [(MWMSearchTableView *)self.view hideNoResultsView:!noResults];
}

- (void)onSearchResultsUpdated
{
  if (!IPAD && [MWMSearch isSearchOnMap])
    return;

  [self reloadData];
}

@end