AMapOffline.m
4.5 KB
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
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <MAMapKit/MAOfflineMap.h>
#pragma ide diagnostic ignored "OCUnusedClassInspection"
@interface AMapOffline : RCTEventEmitter <RCTBridgeModule>
@end
@implementation AMapOffline
RCT_EXPORT_MODULE()
RCT_REMAP_METHOD(getProvinces,
resolveProvinces: (RCTPromiseResolveBlock) resolve
reject: (RCTPromiseRejectBlock) reject) {
NSMutableArray *provinces = [NSMutableArray new];
for (id item in MAOfflineMap.sharedOfflineMap.provinces) {
MAOfflineProvince *province = (MAOfflineProvince *) item;
NSMutableArray *cities = [NSMutableArray new];
for (id city in province.cities) {
[cities addObject:[self itemData:(MAOfflineCity *) city]];
}
[provinces addObject:@{
@"name": province.name,
@"size": @(province.size),
@"state": [self stateString:province.itemStatus],
@"cities": cities,
}];
}
for (id item in MAOfflineMap.sharedOfflineMap.municipalities) {
[provinces addObject:[self itemData:item]];
}
resolve(provinces);
}
RCT_REMAP_METHOD(getCities,
resolveCities: (RCTPromiseResolveBlock) resolve
reject: (RCTPromiseRejectBlock) reject) {
NSMutableArray *cities = [NSMutableArray new];
for (id city in MAOfflineMap.sharedOfflineMap.cities) {
[cities addObject:[self itemData:(MAOfflineCity *) city]];
}
resolve(cities);
}
RCT_EXPORT_METHOD(download:(NSString *)name) {
MAOfflineItem *item = [self getItem:name];
void (^downloadBlock)(MAOfflineItem *, MAOfflineMapDownloadStatus, id)=^(MAOfflineItem *downloadItem, MAOfflineMapDownloadStatus state, id info) {
NSDictionary *data = (NSDictionary *) info;
double progress = 0;
if (state == MAOfflineMapDownloadStatusProgress) {
progress = [data[MAOfflineMapDownloadReceivedSizeKey] doubleValue] / [data[MAOfflineMapDownloadExpectedSizeKey] doubleValue] * 100;
}
[self sendEventWithName:@"download" body:@{
@"name": name,
@"state": [self downloadStateString:state],
@"progress": @(progress),
}];
};
if (item != nil) {
[MAOfflineMap.sharedOfflineMap downloadItem:item
shouldContinueWhenAppEntersBackground:YES
downloadBlock:downloadBlock];
}
}
RCT_EXPORT_METHOD(remove:(NSString *)name) {
MAOfflineItem *item = [self getItem:name];
if (item != nil) {
[MAOfflineMap.sharedOfflineMap deleteItem:item];
}
}
- (MAOfflineItem *)getItem:(NSString *)name {
BOOL (^predicate)(MAOfflineItem *, NSUInteger, BOOL *)=^BOOL (MAOfflineItem * item, NSUInteger _, BOOL *stop) {
return [name isEqual:item.name];
};
NSUInteger i = [MAOfflineMap.sharedOfflineMap.provinces indexOfObjectPassingTest:predicate];
if (i != NSNotFound) {
return MAOfflineMap.sharedOfflineMap.provinces[i];
}
i = [MAOfflineMap.sharedOfflineMap.municipalities indexOfObjectPassingTest:predicate];
if (i != NSNotFound) {
return MAOfflineMap.sharedOfflineMap.municipalities[i];
}
i = [MAOfflineMap.sharedOfflineMap.cities indexOfObjectPassingTest:predicate];
if (i != NSNotFound) {
return MAOfflineMap.sharedOfflineMap.cities[i];
}
return nil;
}
- (NSString *)downloadStateString:(MAOfflineMapDownloadStatus)code {
switch (code) {
case MAOfflineMapDownloadStatusWaiting: return @"waiting";
case MAOfflineMapDownloadStatusStart: return @"downloading";
case MAOfflineMapDownloadStatusProgress: return @"downloading";
case MAOfflineMapDownloadStatusCompleted: return @"unzip";
case MAOfflineMapDownloadStatusUnzip: return @"unzip";
case MAOfflineMapDownloadStatusFinished: return @"downloaded";
default: return @"";
}
}
- (NSString *)stateString:(MAOfflineItemStatus)code {
switch (code) {
case MAOfflineItemStatusCached: return @"downloading";
case MAOfflineItemStatusExpired: return @"expired";
case MAOfflineItemStatusInstalled: return @"downloaded";
default: return @"";
}
}
- (NSDictionary *)itemData:(MAOfflineCity *)city {
return @{
@"name": city.name,
@"size": @(city.size),
@"state": [self stateString:city.itemStatus],
};
}
- (NSArray<NSString *> *)supportedEvents {
return @[@"download"];
}
@end