|
@@ -8,7 +8,13 @@
|
|
|
#import "XCThemeProtocol.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);
|
|
|
+}
|
|
|
+
|
|
|
@xcmlservice(XCThemeProtocol, XCThemeService)
|
|
|
+@synthesize downloadBlock = _downloadBlock;
|
|
|
|
|
|
- (UIUserInterfaceStyle)currentUserInterfaceStyle {
|
|
|
return XCThemeManager.shared.currentUserInterfaceStyle;
|
|
@@ -23,4 +29,85 @@
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
+#pragma mark - Theme Download
|
|
|
+- (NSArray<XCThemeSpecModel *> *)fetchBundleSpecs {
|
|
|
+ NSString *path = [NSBundle.mainBundle.resourcePath stringByAppendingString:@"/ThemeResources"];
|
|
|
+ NSArray *items = [NSFileManager.defaultManager contentsOfDirectoryAtPath:path error:nil];
|
|
|
+ NSMutableArray<XCThemeSpecModel *> *specs = [NSMutableArray new];
|
|
|
+ for (NSString *file in items) {
|
|
|
+ NSString *filePath = [NSString stringWithFormat:@"%@/%@", path, file];
|
|
|
+ BOOL isDir = NO;
|
|
|
+ [NSFileManager.defaultManager fileExistsAtPath:filePath isDirectory:&isDir];
|
|
|
+ if (isDir) {
|
|
|
+ XCThemeSpec *theme = [[XCThemeSpec alloc] initWithThemeId:filePath];
|
|
|
+ if (theme) {
|
|
|
+ XCThemeSpecModel *specModel = [[XCThemeSpecModel alloc] initWithWrappedObject:theme];
|
|
|
+ [specs addObject:specModel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return [specs copy];
|
|
|
+}
|
|
|
+
|
|
|
+- (NSArray<XCThemeSpecModel *> *)fetchDownloadedSpecs {
|
|
|
+ NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
|
|
|
+ NSString *path = [documentsPath stringByAppendingString:@"/ThemeResources"];
|
|
|
+ NSArray *items = [NSFileManager.defaultManager contentsOfDirectoryAtPath:path error:nil];
|
|
|
+ NSMutableArray<XCThemeSpecModel *> *specs = [NSMutableArray new];
|
|
|
+ for (NSString *file in items) {
|
|
|
+ NSString *filePath = [NSString stringWithFormat:@"%@/%@", path, file];
|
|
|
+ BOOL isDir = NO;
|
|
|
+ [NSFileManager.defaultManager fileExistsAtPath:filePath isDirectory:&isDir];
|
|
|
+ if (isDir) {
|
|
|
+ XCThemeSpec *theme = [[XCThemeSpec alloc] initWithThemeId:filePath];
|
|
|
+ if (theme) {
|
|
|
+ XCThemeSpecModel *specModel = [[XCThemeSpecModel alloc] initWithWrappedObject:theme];
|
|
|
+ [specs addObject:specModel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return [specs copy];
|
|
|
+}
|
|
|
+
|
|
|
+- (NSArray<XCThemeSpecModel *> *)fetchLocalSpecs {
|
|
|
+ NSMutableArray *specs = [NSMutableArray new];
|
|
|
+ [specs addObjectsFromArray:[self fetchBundleSpecs]];
|
|
|
+ [specs addObjectsFromArray:[self fetchDownloadedSpecs]];
|
|
|
+ return [specs copy];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)fetchThemeData:(NSString *)themeId completion:(void (^)(BOOL, NSString * _Nonnull, XCThemeSpecModel * _Nullable))completion {
|
|
|
+ NSArray<XCThemeSpecModel *> *allLocalSpecs = [self fetchLocalSpecs];
|
|
|
+ for (XCThemeSpecModel *spec in allLocalSpecs) {
|
|
|
+ if ([spec.themeId isEqualToString:themeId]) {
|
|
|
+ if (completion) {
|
|
|
+ completion(YES, @"", spec);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!self.downloadBlock) {
|
|
|
+ completion(NO, @"downloadBlock not specified", nil);
|
|
|
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"downloadBlock not specified" userInfo:nil];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSString *downloadTHMPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
|
|
|
+ downloadTHMPath = [downloadTHMPath stringByAppendingFormat:@"/ThemeResources/%@.thm", themeId];
|
|
|
+ self.downloadBlock(themeId, downloadTHMPath, ^(BOOL success, NSString * _Nonnull reason) {
|
|
|
+
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+- (XCThemeSpecModel *)currentTheme {
|
|
|
+ if (XCThemeManager.shared.selectedTheme) {
|
|
|
+ return [[XCThemeSpecModel alloc] initWithWrappedObject:XCThemeManager.shared.selectedTheme];
|
|
|
+ }
|
|
|
+ return nil;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)applyTheme:(XCThemeSpecModel *)localSpec {
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
@end
|