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

MWMMapDownloaderViewController.mm « Downloader « UI « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c2d257c5c9aac1681782c40bfccb4ad4b7cfc4b (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#import "MWMMapDownloaderViewController.h"
#import "MWMMapDownloaderExtendedDataSourceWithAds.h"
#import "MWMMapDownloaderSearchDataSource.h"
#import "SwiftBridge.h"

#include "Framework.h"

namespace
{
NSString * const kMapDownloaderNoResultsEmbedViewControllerSegue =
    @"MapDownloaderNoResultsEmbedViewControllerSegue";
}  // namespace

using namespace storage;

@interface MWMBaseMapDownloaderViewController ()

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

@property(nonatomic) MWMMapDownloaderDataSource * dataSource;
@property(nonatomic) MWMMapDownloaderDataSource * defaultDataSource;

@property(nonatomic, readonly) NSString * parentCountryId;
@property(nonatomic, readonly) MWMMapDownloaderMode mode;

@property(nonatomic) BOOL showAllMapsButtons;

- (void)configViews;

- (void)openAvailableMaps;

- (void)reloadTable;

@end

@interface MWMMapDownloaderViewController ()<UISearchBarDelegate, UIScrollViewDelegate>

@property(weak, nonatomic) IBOutlet UIView * statusBarBackground;
@property(weak, nonatomic) IBOutlet UISearchBar * searchBar;
@property(weak, nonatomic) IBOutlet UIView * noMapsContainer;
@property(nonatomic) MWMDownloaderNoResultsEmbedViewController * noResultsEmbedViewController;

@property(nonatomic) MWMMapDownloaderSearchDataSource * searchDataSource;

@property(nonatomic) NSTimeInterval lastSearchTimestamp;

@end

@implementation MWMMapDownloaderViewController
{
  DownloaderSearchParams m_searchParams;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.searchBar.placeholder = L(@"downloader_search_field_hint");
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  UIColor * searchBarColor = [UIColor primary];
  self.statusBarBackground.backgroundColor = self.searchBar.barTintColor = searchBarColor;
  self.searchBar.backgroundImage = [UIImage imageWithColor:searchBarColor];
}

- (void)configViews
{
  [super configViews];
  [self checkAndConfigNoMapsView];
}

#pragma mark - No Maps

- (void)checkAndConfigNoMapsView
{
  auto const & s = GetFramework().GetStorage();
  if (![self.parentCountryId isEqualToString:@(s.GetRootId().c_str())])
    return;

  auto const showResults = ^{
    [self configAllMapsView];
    self.tableView.hidden = NO;
    self.noMapsContainer.hidden = YES;
  };
  auto const showNoResults = ^(MWMDownloaderNoResultsScreen screen) {
    self.showAllMapsButtons = NO;
    self.tableView.hidden = YES;
    self.noMapsContainer.hidden = NO;
    self.noResultsEmbedViewController.screen = screen;
  };

  BOOL const noResults =
      self.dataSource == self.searchDataSource && self.searchDataSource.isEmpty;
  BOOL const isModeAvailable = self.mode == MWMMapDownloaderModeAvailable;
  BOOL const haveActiveMaps = s.HaveDownloadedCountries() || s.IsDownloadInProgress();

  if (noResults)
    showNoResults(MWMDownloaderNoResultsScreenNoSearchResults);
  else if (isModeAvailable || haveActiveMaps)
    showResults();
  else
    showNoResults(MWMDownloaderNoResultsScreenNoMaps);
}

#pragma mark - UISearchBarDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
  [self.searchBar setShowsCancelButton:YES animated:YES];
  [self.navigationController setNavigationBarHidden:YES animated:YES];
  self.tableView.contentInset = self.tableView.scrollIndicatorInsets = {};
  return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
  [self.searchBar setShowsCancelButton:NO animated:YES];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  self.tableView.contentInset = self.tableView.scrollIndicatorInsets = {};
  return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
  self.searchBar.text = @"";
  [self.searchBar resignFirstResponder];
  self.dataSource = self.defaultDataSource;
  [self reloadTable];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  NSString * primaryLanguage = self.searchBar.textInputMode.primaryLanguage;
  if (primaryLanguage)
    m_searchParams.m_inputLocale = primaryLanguage.UTF8String;

  m_searchParams.m_query = searchText.precomposedStringWithCompatibilityMapping.UTF8String;
  [self updateSearchCallback];
  GetFramework().SearchInDownloader(m_searchParams);
}

#pragma mark - UIBarPositioningDelegate

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { return UIBarPositionTopAttached; }
#pragma mark - Search

- (void)updateSearchCallback
{
  __weak auto weakSelf = self;
  NSTimeInterval const timestamp = [NSDate date].timeIntervalSince1970;
  self.lastSearchTimestamp = timestamp;
  m_searchParams.m_onResults = [weakSelf, timestamp](DownloaderSearchResults const & results) {
    __strong auto self = weakSelf;
    if (!self || timestamp != self.lastSearchTimestamp)
      return;
    if (results.m_query.empty())
    {
      self.dataSource = self.defaultDataSource;
    }
    else
    {
      self.searchDataSource =
          [[MWMMapDownloaderSearchDataSource alloc] initWithSearchResults:results delegate:self];
      self.dataSource = self.searchDataSource;
    }
    [self reloadTable];
  };
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
  [self.searchBar resignFirstResponder];
}

#pragma mark - MWMNoMapsViewControllerProtocol

- (void)handleDownloadMapsAction { [self openAvailableMaps]; }
#pragma mark - Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  [super prepareForSegue:segue sender:sender];
  if ([segue.identifier isEqualToString:kMapDownloaderNoResultsEmbedViewControllerSegue])
    self.noResultsEmbedViewController = segue.destinationViewController;
}

#pragma mark - Configuration

- (void)setParentCountryId:(NSString *)parentId mode:(MWMMapDownloaderMode)mode
{
  self.defaultDataSource =
      [[MWMMapDownloaderExtendedDataSourceWithAds alloc] initForRootCountryId:parentId
                                                                     delegate:self
                                                                         mode:mode];
}

#pragma mark - Helpers

- (void)reloadTable
{
  [super reloadTable];
  [self checkAndConfigNoMapsView];
}

@end