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

DownloaderParentVC.mm « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58a7f8a0a4f1c27b2df2cb33c287ea464559fd7c (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
214
215
216
217
218
219
220
#import "Common.h"
#import "DownloaderParentVC.h"
#import "DiskFreeSpace.h"
#import "MWMAlertViewController.h"

#include "platform/platform.hpp"

@implementation DownloaderParentVC

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self.view addSubview:self.tableView];
}

- (UITableView *)tableView
{
  if (!_tableView) {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  }
  return _tableView;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex != alertView.cancelButtonIndex)
  {
    if (self.selectedInActionSheetOptions == MapOptions::Map)
      [self performAction:DownloaderActionDownloadMap withSizeCheck:NO];
    else if (self.selectedInActionSheetOptions == MapOptions::CarRouting)
      [self performAction:DownloaderActionDownloadCarRouting withSizeCheck:NO];
    else if (self.selectedInActionSheetOptions == MapOptions::MapWithCarRouting)
      [self performAction:DownloaderActionDownloadAll withSizeCheck:NO];
  }
}

- (void)download
{
  if (self.selectedInActionSheetOptions == MapOptions::Map)
    [self performAction:DownloaderActionDownloadMap withSizeCheck:NO];
  else if (self.selectedInActionSheetOptions == MapOptions::CarRouting)
    [self performAction:DownloaderActionDownloadCarRouting withSizeCheck:NO];
  else if (self.selectedInActionSheetOptions == MapOptions::MapWithCarRouting)
    [self performAction:DownloaderActionDownloadAll withSizeCheck:NO];
}

#pragma mark - Virtual methods

- (void)performAction:(DownloaderAction)action withSizeCheck:(BOOL)check {}
- (NSString *)parentTitle { return nil; }
- (NSString *)selectedMapName { return nil; }
- (uint64_t)selectedMapSizeWithOptions:(MapOptions)options { return 0; }
- (TStatus)selectedMapStatus { return TStatus::EUnknown; }
- (MapOptions)selectedMapOptions { return MapOptions::Map; }

#pragma mark - Virtual table view methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return nil; }

#pragma mark - Public methods for successors

- (BOOL)canDownloadSelectedMap
{
  uint64_t const size = [self selectedMapSizeWithOptions:self.selectedInActionSheetOptions];
  NSString * name = [self selectedMapName];

  Platform::EConnectionType const connection = Platform::ConnectionStatus();
  MWMAlertViewController * alert = [[MWMAlertViewController alloc] initWithViewController:self];
  if (connection != Platform::EConnectionType::CONNECTION_NONE)
  {
    if (connection == Platform::EConnectionType::CONNECTION_WWAN && size > 50 * MB)
      [alert presentnoWiFiAlertWithName:name downloadBlock:^{[self download];}];
    else
      return YES;
  }
  else
  {
    [alert presentNoConnectionAlert];
  }
  return NO;
}

- (UIActionSheet *)actionSheetToPerformActionOnSelectedMap
{
  TStatus const status = [self selectedMapStatus];
  MapOptions const options = [self selectedMapOptions];
  [self.actionSheetActions removeAllObjects];
  UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:self.actionSheetTitle delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
  if (status == TStatus::EOnDisk || status == TStatus::EOnDiskOutOfDate)
    [self addButtonWithTitle:L(@"zoom_to_country") action:DownloaderActionZoomToCountry toActionSheet:actionSheet];

  if (status == TStatus::ENotDownloaded || status == TStatus::EOutOfMemFailed || status == TStatus::EDownloadFailed)
  {
    NSString * fullSize = formattedSize([self selectedMapSizeWithOptions:MapOptions::MapWithCarRouting]);
    NSString * onlyMapSize = formattedSize([self selectedMapSizeWithOptions:MapOptions::Map]);
    [self addButtonWithTitle:[NSString stringWithFormat:@"%@, %@", L(@"downloader_download_map"), fullSize] action:DownloaderActionDownloadAll toActionSheet:actionSheet];
    [self addButtonWithTitle:[NSString stringWithFormat:@"%@, %@", L(@"downloader_download_map_no_routing"), onlyMapSize] action:DownloaderActionDownloadMap toActionSheet:actionSheet];
  }

  if (status == TStatus::EOnDiskOutOfDate && options == MapOptions::MapWithCarRouting)
  {
    NSString * size = formattedSize([self selectedMapSizeWithOptions:MapOptions::MapWithCarRouting]);
    [self addButtonWithTitle:[NSString stringWithFormat:@"%@, %@", L(@"downloader_update_map_and_routing"), size] action:DownloaderActionDownloadAll toActionSheet:actionSheet];
  }

  if (status == TStatus::EOnDisk && options == MapOptions::Map)
  {
    NSString * size = formattedSize([self selectedMapSizeWithOptions:MapOptions::CarRouting]);
    NSString * title = [NSString stringWithFormat:@"%@, %@", L(@"downloader_download_routing"), size];
    [self addButtonWithTitle:title action:DownloaderActionDownloadCarRouting toActionSheet:actionSheet];
  }

  if (status == TStatus::EOnDiskOutOfDate && options == MapOptions::Map)
  {
    NSString * size = formattedSize([self selectedMapSizeWithOptions:MapOptions::Map]);
    NSString * title = [NSString stringWithFormat:@"%@, %@", L(@"downloader_update_map"), size];
    [self addButtonWithTitle:title action:DownloaderActionDownloadMap toActionSheet:actionSheet];
    size = formattedSize([self selectedMapSizeWithOptions:MapOptions::MapWithCarRouting]);
    title = [NSString stringWithFormat:@"%@, %@", L(@"downloader_update_map_and_routing"), size];
    [self addButtonWithTitle:title action:DownloaderActionDownloadAll toActionSheet:actionSheet];
  }

  if (status == TStatus::EOnDisk || status == TStatus::EOnDiskOutOfDate)
  {
    if (options == MapOptions::MapWithCarRouting)
      [self addButtonWithTitle:L(@"downloader_delete_routing") action:DownloaderActionDeleteCarRouting toActionSheet:actionSheet];

    [self addButtonWithTitle:L(@"downloader_delete_map") action:DownloaderActionDeleteMap toActionSheet:actionSheet];
    actionSheet.destructiveButtonIndex = actionSheet.numberOfButtons - 1;
  }

  if (status == TStatus::EDownloading || status == TStatus::EInQueue)
  {
    [self addButtonWithTitle:L(@"cancel_download") action:DownloaderActionCancelDownloading toActionSheet:actionSheet];
    actionSheet.destructiveButtonIndex = actionSheet.numberOfButtons - 1;
  }

  if (!IPAD)
  {
    [actionSheet addButtonWithTitle:L(@"cancel")];
    actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
  }

  return actionSheet;
}

- (UIActionSheet *)actionSheetToCancelDownloadingSelectedMap
{
  [self.actionSheetActions removeAllObjects];
  self.actionSheetActions[@0] = @(DownloaderActionCancelDownloading);

  return [[UIActionSheet alloc] initWithTitle:[self actionSheetTitle] delegate:self cancelButtonTitle:L(@"cancel") destructiveButtonTitle:L(@"cancel_download") otherButtonTitles:nil];
}

#pragma mark -

- (NSString *)actionSheetTitle
{
  if ([self parentTitle])
    return [NSString stringWithFormat:@"%@, %@", [self selectedMapName].uppercaseString, [self parentTitle].uppercaseString];
  else
    return [self selectedMapName].uppercaseString;
}

- (void)addButtonWithTitle:(NSString *)title action:(DownloaderAction)action toActionSheet:(UIActionSheet *)actionSheet
{
  [actionSheet addButtonWithTitle:title];
  self.actionSheetActions[@(actionSheet.numberOfButtons - 1)] = @(action);
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex != actionSheet.cancelButtonIndex)
  {
    DownloaderAction const action = (DownloaderAction)[self.actionSheetActions[@(buttonIndex)] integerValue];
    switch (action)
    {
      case DownloaderActionDownloadAll:
      case DownloaderActionDeleteAll:
        self.selectedInActionSheetOptions = MapOptions::MapWithCarRouting;
        break;

      case DownloaderActionDownloadMap:
      case DownloaderActionDeleteMap:
        self.selectedInActionSheetOptions = MapOptions::Map;
        break;

      case DownloaderActionDownloadCarRouting:
      case DownloaderActionDeleteCarRouting:
        self.selectedInActionSheetOptions = MapOptions::CarRouting;
        break;

      default:
        break;
    }

    [self performAction:action withSizeCheck:YES];
  }
}

#pragma mark -

- (NSMutableDictionary *)actionSheetActions
{
  if (!_actionSheetActions)
    _actionSheetActions = [[NSMutableDictionary alloc] init];
  return _actionSheetActions;
}

- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end