Added a new Cocoa Touch dynamic framework target and Cocoa Touch static library target to the main iOS Xcode project. The targets are based on the templates provided by Xcode. They contain headers and source files in the darwin/ and ios/ subdirectories. Headers are explicitly marked public or project-internal instead of implicitly by subdirectory. The SDK targets have a few dependencies that are managed by mason. Transform the config.gypi generated by configure into an .xcconfig file that Xcode uses to fill in compiler and linker flags. Removed the iossdk target from platform.gypi and pared down the platform-lib target. make iproj opens the overall Xcode workspace instead of just the core project. Resolved some warnings that are only now appearing because we’re using the standard warning and error flags instead of the pedantic and somewhat inappropriate flags that were forced by the gyp configuration files. Removed a console message informing developers of a long-ago deleted delegate method; the message was triggering a “selector not found” warning. Conditionalized some deprecated symbol usage that’s still needed for the static framework but not the dynamic framework, due to differing minimum deployment targets. Added a build phase to the static library target that copies headers for use with the static library itself. This would allow us to potentially include a header in the dynamic framework but not the static library and vice-versa. Updated the changelog to note that unused SVG files have been removed from the distribution.
219 lines
8.0 KiB
Plaintext
219 lines
8.0 KiB
Plaintext
#import "MGLOfflinePack_Private.h"
|
|
|
|
#import "MGLOfflineStorage_Private.h"
|
|
#import "MGLOfflineRegion_Private.h"
|
|
#import "MGLTilePyramidOfflineRegion.h"
|
|
|
|
#include <mbgl/storage/default_file_source.hpp>
|
|
|
|
/**
|
|
Assert that the current offline pack is valid.
|
|
|
|
This macro should be used at the beginning of any public-facing instance method
|
|
of `MGLOfflinePack`. For private methods, an assertion is more appropriate.
|
|
*/
|
|
#define MGLAssertOfflinePackIsValid() \
|
|
do { \
|
|
if (_state == MGLOfflinePackStateInvalid) { \
|
|
[NSException raise:@"Invalid offline pack" \
|
|
format: \
|
|
@"-[MGLOfflineStorage removePack:withCompletionHandler:] has been called " \
|
|
@"on this instance of MGLOfflinePack, rendering it invalid. It is an " \
|
|
@"error to send any message to this pack."]; \
|
|
} \
|
|
} while (NO);
|
|
|
|
class MBGLOfflineRegionObserver : public mbgl::OfflineRegionObserver {
|
|
public:
|
|
MBGLOfflineRegionObserver(MGLOfflinePack *pack_) : pack(pack_) {}
|
|
|
|
void statusChanged(mbgl::OfflineRegionStatus status) override;
|
|
void responseError(mbgl::Response::Error error) override;
|
|
void mapboxTileCountLimitExceeded(uint64_t limit) override;
|
|
|
|
private:
|
|
__weak MGLOfflinePack *pack = nullptr;
|
|
};
|
|
|
|
@interface MGLOfflinePack ()
|
|
|
|
@property (nonatomic, weak, nullable) id <MGLOfflinePackDelegate> delegate;
|
|
@property (nonatomic, nullable, readwrite) mbgl::OfflineRegion *mbglOfflineRegion;
|
|
@property (nonatomic, readwrite) MGLOfflinePackProgress progress;
|
|
|
|
@end
|
|
|
|
@implementation MGLOfflinePack
|
|
|
|
- (instancetype)init {
|
|
if (self = [super init]) {
|
|
_state = MGLOfflinePackStateInvalid;
|
|
NSLog(@"%s called; did you mean to call +[MGLOfflineStorage addPackForRegion:withContext:completionHandler:] instead?", __PRETTY_FUNCTION__);
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithMBGLRegion:(mbgl::OfflineRegion *)region {
|
|
if (self = [super init]) {
|
|
_mbglOfflineRegion = region;
|
|
_state = MGLOfflinePackStateUnknown;
|
|
|
|
mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource];
|
|
mbglFileSource->setOfflineRegionObserver(*_mbglOfflineRegion, std::make_unique<MBGLOfflineRegionObserver>(self));
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
NSAssert(_state == MGLOfflinePackStateInvalid, @"MGLOfflinePack was not invalided prior to deallocation.");
|
|
}
|
|
|
|
- (id <MGLOfflineRegion>)region {
|
|
MGLAssertOfflinePackIsValid();
|
|
|
|
const mbgl::OfflineRegionDefinition ®ionDefinition = _mbglOfflineRegion->getDefinition();
|
|
NSAssert([MGLTilePyramidOfflineRegion conformsToProtocol:@protocol(MGLOfflineRegion_Private)], @"MGLTilePyramidOfflineRegion should conform to MGLOfflineRegion_Private.");
|
|
return [(id <MGLOfflineRegion_Private>)[MGLTilePyramidOfflineRegion alloc] initWithOfflineRegionDefinition:regionDefinition];
|
|
}
|
|
|
|
- (NSData *)context {
|
|
MGLAssertOfflinePackIsValid();
|
|
|
|
const mbgl::OfflineRegionMetadata &metadata = _mbglOfflineRegion->getMetadata();
|
|
return [NSData dataWithBytes:&metadata[0] length:metadata.size()];
|
|
}
|
|
|
|
- (void)resume {
|
|
MGLAssertOfflinePackIsValid();
|
|
|
|
mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource];
|
|
mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active);
|
|
}
|
|
|
|
- (void)suspend {
|
|
MGLAssertOfflinePackIsValid();
|
|
|
|
mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource];
|
|
mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive);
|
|
}
|
|
|
|
- (void)invalidate {
|
|
NSAssert(_state != MGLOfflinePackStateInvalid, @"Cannot invalidate an already invalid offline pack.");
|
|
|
|
self.state = MGLOfflinePackStateInvalid;
|
|
mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource];
|
|
mbglFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr);
|
|
self.mbglOfflineRegion = nil;
|
|
}
|
|
|
|
- (void)setState:(MGLOfflinePackState)state {
|
|
if (!self.mbglOfflineRegion) {
|
|
// A progress update has arrived after the call to
|
|
// -[MGLOfflineStorage removePack:withCompletionHandler:] but before the
|
|
// removal is complete and the completion handler is called.
|
|
NSAssert(_state == MGLOfflinePackStateInvalid, @"A valid MGLOfflinePack has no mbgl::OfflineRegion.");
|
|
return;
|
|
}
|
|
|
|
NSAssert(_state != MGLOfflinePackStateInvalid, @"Cannot change the state of an invalid offline pack.");
|
|
|
|
_state = state;
|
|
}
|
|
|
|
- (void)requestProgress {
|
|
MGLAssertOfflinePackIsValid();
|
|
|
|
mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource];
|
|
|
|
__weak MGLOfflinePack *weakSelf = self;
|
|
mbglFileSource->getOfflineRegionStatus(*_mbglOfflineRegion, [&, weakSelf](__unused std::exception_ptr exception, mbgl::optional<mbgl::OfflineRegionStatus> status) {
|
|
if (status) {
|
|
mbgl::OfflineRegionStatus checkedStatus = *status;
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
MGLOfflinePack *strongSelf = weakSelf;
|
|
[strongSelf offlineRegionStatusDidChange:checkedStatus];
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
- (void)offlineRegionStatusDidChange:(mbgl::OfflineRegionStatus)status {
|
|
NSAssert(_state != MGLOfflinePackStateInvalid, @"Cannot change update progress of an invalid offline pack.");
|
|
|
|
switch (status.downloadState) {
|
|
case mbgl::OfflineRegionDownloadState::Inactive:
|
|
self.state = status.complete() ? MGLOfflinePackStateComplete : MGLOfflinePackStateInactive;
|
|
break;
|
|
|
|
case mbgl::OfflineRegionDownloadState::Active:
|
|
self.state = MGLOfflinePackStateActive;
|
|
break;
|
|
}
|
|
|
|
MGLOfflinePackProgress progress;
|
|
progress.countOfResourcesCompleted = status.completedResourceCount;
|
|
progress.countOfBytesCompleted = status.completedResourceSize;
|
|
progress.countOfResourcesExpected = status.requiredResourceCount;
|
|
progress.maximumResourcesExpected = status.requiredResourceCountIsPrecise ? status.requiredResourceCount : UINT64_MAX;
|
|
self.progress = progress;
|
|
|
|
[self.delegate offlinePack:self progressDidChange:progress];
|
|
}
|
|
|
|
NSError *MGLErrorFromResponseError(mbgl::Response::Error error) {
|
|
NSInteger errorCode = MGLErrorCodeUnknown;
|
|
switch (error.reason) {
|
|
case mbgl::Response::Error::Reason::NotFound:
|
|
errorCode = MGLErrorCodeNotFound;
|
|
break;
|
|
|
|
case mbgl::Response::Error::Reason::Server:
|
|
errorCode = MGLErrorCodeBadServerResponse;
|
|
break;
|
|
|
|
case mbgl::Response::Error::Reason::Connection:
|
|
errorCode = MGLErrorCodeConnectionFailed;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return [NSError errorWithDomain:MGLErrorDomain code:errorCode userInfo:@{
|
|
NSLocalizedFailureReasonErrorKey: @(error.message.c_str())
|
|
}];
|
|
}
|
|
|
|
@end
|
|
|
|
void MBGLOfflineRegionObserver::statusChanged(mbgl::OfflineRegionStatus status) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[pack offlineRegionStatusDidChange:status];
|
|
});
|
|
}
|
|
|
|
void MBGLOfflineRegionObserver::responseError(mbgl::Response::Error error) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[pack.delegate offlinePack:pack didReceiveError:MGLErrorFromResponseError(error)];
|
|
});
|
|
}
|
|
|
|
void MBGLOfflineRegionObserver::mapboxTileCountLimitExceeded(uint64_t limit) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[pack.delegate offlinePack:pack didReceiveMaximumAllowedMapboxTiles:limit];
|
|
});
|
|
}
|
|
|
|
@implementation NSValue (MGLOfflinePackAdditions)
|
|
|
|
+ (NSValue *)valueWithMGLOfflinePackProgress:(MGLOfflinePackProgress)progress {
|
|
return [NSValue value:&progress withObjCType:@encode(MGLOfflinePackProgress)];
|
|
}
|
|
|
|
- (MGLOfflinePackProgress)MGLOfflinePackProgressValue {
|
|
MGLOfflinePackProgress progress;
|
|
[self getValue:&progress];
|
|
return progress;
|
|
}
|
|
|
|
@end
|