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

MWMMapDownloaderViewController.mm « MapDownloader « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 60996cce556b113130965c14da564faa8f06dce6 (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
210
211
212
213
#import "Common.h"
#import "MWMMapDownloaderExtendedDataSource.h"
#import "MWMMapDownloaderSearchDataSource.h"
#import "MWMMapDownloaderViewController.h"
#import "MWMNoMapsViewController.h"
#import "UIColor+MapsMeColor.h"
#import "UIKitCategories.h"

#include "Framework.h"

#include "storage/downloader_search_params.hpp"

namespace
{
NSString * const kNoMapsSegue = @"MapDownloaderEmbedNoMapsSegue";
} // 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) mwm::DownloaderMode mode;

@property (nonatomic) BOOL showAllMapsButtons;

- (void)configViews;

- (void)openAvailableMaps;

- (void)reloadTable;

@end

@interface MWMMapDownloaderViewController () <UISearchBarDelegate, UIScrollViewDelegate, MWMNoMapsViewControllerProtocol>

@property (weak, nonatomic) IBOutlet UIView * statusBarBackground;
@property (weak, nonatomic) IBOutlet UISearchBar * searchBar;
@property (weak, nonatomic) IBOutlet UIView * noMapsContainer;
@property (nonatomic) MWMNoMapsViewController * noMapsController;

@property (nonatomic) MWMMapDownloaderDataSource * searchDataSource;

@end

@implementation MWMMapDownloaderViewController
{
  DownloaderSearchParams m_searchParams;
}

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

- (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)backTap
{
  NSArray<UIViewController *> * viewControllers = self.navigationController.viewControllers;
  UIViewController * previousViewController = viewControllers[viewControllers.count - 2];
  if ([previousViewController isKindOfClass:[MWMBaseMapDownloaderViewController class]])
    [self.navigationController popViewControllerAnimated:YES];
  else
    [self.navigationController popToRootViewControllerAnimated:YES];
}

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

#pragma mark - No Maps

- (void)checkAndConfigNoMapsView
{
  auto const & s = GetFramework().Storage();
  if (![self.parentCountryId isEqualToString:@(s.GetRootId().c_str())])
    return;
  if (self.mode == mwm::DownloaderMode::Available || s.HaveDownloadedCountries())
  {
    [self configAllMapsView];
    self.tableView.hidden = NO;
    self.noMapsContainer.hidden = YES;
  }
  else
  {
    self.showAllMapsButtons = NO;
    self.tableView.hidden = YES;
    self.noMapsContainer.hidden = NO;
  }
}

#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
{
  if (searchText.length == 0)
  {
    self.dataSource = self.defaultDataSource;
    [self reloadTable];
  }
  else
  {
    NSString * primaryLanguage = self.searchBar.textInputMode.primaryLanguage;
    if (primaryLanguage)
      m_searchParams.m_inputLocale = primaryLanguage.UTF8String;

    m_searchParams.m_query = searchText.precomposedStringWithCompatibilityMapping.UTF8String;
    GetFramework().SearchInDownloader(m_searchParams);
  }
}

#pragma mark - UIBarPositioningDelegate

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

#pragma mark - Search

- (void)setupSearchParams
{
  __weak auto weakSelf = self;
  m_searchParams.m_onResults = ^(DownloaderSearchResults const & results)
  {
    __strong auto self = weakSelf;
    if (!self || results.m_endMarker)
      return;
    self.searchDataSource = [[MWMMapDownloaderSearchDataSource alloc] initWithSearchResults:results delegate:self];
    dispatch_async(dispatch_get_main_queue(), ^
    {
      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:kNoMapsSegue])
  {
    self.noMapsController = segue.destinationViewController;
    self.noMapsController.delegate = self;
  }
}

#pragma mark - Configuration

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

@end