|
@@ -7,11 +7,7 @@
|
|
|
|
|
|
#import "XCThemeProtocol.h"
|
|
#import "XCThemeProtocol.h"
|
|
#import "XCTheme-Swift.h"
|
|
#import "XCTheme-Swift.h"
|
|
-
|
|
|
|
-static dispatch_queue_t globalThemeDownloadQueue;
|
|
|
|
-__attribute__((constructor)) static void init(void) {
|
|
|
|
- globalThemeDownloadQueue = dispatch_queue_create("org.forgetive.notebook.themeDownload", DISPATCH_QUEUE_SERIAL);
|
|
|
|
-}
|
|
|
|
|
|
+#import <XCZip/XCZip.h>
|
|
|
|
|
|
@xcmlservice(XCThemeProtocol, XCThemeService)
|
|
@xcmlservice(XCThemeProtocol, XCThemeService)
|
|
@synthesize downloadBlock = _downloadBlock;
|
|
@synthesize downloadBlock = _downloadBlock;
|
|
@@ -88,14 +84,37 @@ __attribute__((constructor)) static void init(void) {
|
|
}
|
|
}
|
|
|
|
|
|
if (!self.downloadBlock) {
|
|
if (!self.downloadBlock) {
|
|
- completion(NO, @"downloadBlock not specified", nil);
|
|
|
|
|
|
+ if (completion) {
|
|
|
|
+ completion(NO, @"downloadBlock not specified", nil);
|
|
|
|
+ }
|
|
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"downloadBlock not specified" userInfo:nil];
|
|
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"downloadBlock not specified" userInfo:nil];
|
|
}
|
|
}
|
|
|
|
|
|
- NSString *downloadTHMPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
|
|
|
|
- downloadTHMPath = [downloadTHMPath stringByAppendingFormat:@"/ThemeResources/%@.thm", themeId];
|
|
|
|
|
|
+ NSString *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
|
|
|
|
+ NSString *downloadTHMPath = [rootPath stringByAppendingFormat:@"/ThemeResources/%@.thm", themeId];
|
|
|
|
+ NSString *extractPath = [rootPath stringByAppendingFormat:@"/ThemeResources/%@", themeId];
|
|
self.downloadBlock(themeId, downloadTHMPath, ^(BOOL success, NSString * _Nonnull reason) {
|
|
self.downloadBlock(themeId, downloadTHMPath, ^(BOOL success, NSString * _Nonnull reason) {
|
|
-
|
|
|
|
|
|
+ BOOL extractSuccess = [SSZipArchive unzipFileAtPath:downloadTHMPath toDestination:extractPath uniqueId:nil];
|
|
|
|
+ [NSFileManager.defaultManager removeItemAtPath:downloadTHMPath error:nil];
|
|
|
|
+ if (!extractSuccess) {
|
|
|
|
+ [NSFileManager.defaultManager removeItemAtPath:extractPath error:nil];
|
|
|
|
+ if (completion) {
|
|
|
|
+ completion(NO, @"Extract download failed", nil);
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ XCThemeSpec *spec = [[XCThemeSpec alloc] initWithThemeId:themeId];
|
|
|
|
+ if (spec) {
|
|
|
|
+ XCThemeSpecModel *specModel = [[XCThemeSpecModel alloc] initWithWrappedObject:spec];
|
|
|
|
+ if (completion) {
|
|
|
|
+ completion(YES, @"", specModel);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ [NSFileManager.defaultManager removeItemAtPath:extractPath error:nil];
|
|
|
|
+ if (completion) {
|
|
|
|
+ completion(NO, @"Theme not completed", nil);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|