1
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#import "KBConfig.h"
|
||||
|
||||
static NSString * const kKBKeyboardLayoutConfigFileName = @"kb_keyboard_layout_config";
|
||||
static NSString * const kKBKeyboardLayoutI18nFileName = @"kb_keyboard_layouts_i18n";
|
||||
|
||||
@implementation KBKeyboardLayoutMetrics
|
||||
@end
|
||||
@@ -92,13 +93,66 @@ static NSString * const kKBKeyboardLayoutConfigFileName = @"kb_keyboard_layout_c
|
||||
static KBKeyboardLayoutConfig *config = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
NSString *path = [[NSBundle mainBundle] pathForResource:kKBKeyboardLayoutConfigFileName ofType:@"json"];
|
||||
NSData *data = path.length ? [NSData dataWithContentsOfFile:path] : nil;
|
||||
config = data ? [KBKeyboardLayoutConfig configFromJSONData:data] : nil;
|
||||
config = [[KBKeyboardLayoutConfig alloc] init];
|
||||
[config kb_loadMainConfig];
|
||||
[config kb_loadI18nConfig];
|
||||
});
|
||||
return config;
|
||||
}
|
||||
|
||||
- (void)kb_loadMainConfig {
|
||||
NSString *path = [[NSBundle mainBundle] pathForResource:kKBKeyboardLayoutConfigFileName ofType:@"json"];
|
||||
NSData *data = path.length ? [NSData dataWithContentsOfFile:path] : nil;
|
||||
if (data.length == 0) { return; }
|
||||
|
||||
KBKeyboardLayoutConfig *mainConfig = [KBKeyboardLayoutConfig configFromJSONData:data];
|
||||
if (mainConfig) {
|
||||
self.metrics = mainConfig.metrics;
|
||||
self.designWidth = mainConfig.designWidth;
|
||||
self.keyDefs = mainConfig.keyDefs;
|
||||
self.layouts = mainConfig.layouts;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)kb_loadI18nConfig {
|
||||
NSString *path = [[NSBundle mainBundle] pathForResource:kKBKeyboardLayoutI18nFileName ofType:@"json"];
|
||||
NSData *data = path.length ? [NSData dataWithContentsOfFile:path] : nil;
|
||||
if (data.length == 0) {
|
||||
NSLog(@"[KBKeyboardLayoutConfig] i18n layout file not found");
|
||||
return;
|
||||
}
|
||||
|
||||
NSError *error = nil;
|
||||
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
||||
if (error || ![json isKindOfClass:[NSDictionary class]]) {
|
||||
NSLog(@"[KBKeyboardLayoutConfig] Failed to parse i18n layout file: %@", error);
|
||||
return;
|
||||
}
|
||||
|
||||
NSDictionary *dict = (NSDictionary *)json;
|
||||
NSDictionary *layoutsRaw = dict[@"layouts"];
|
||||
if (![layoutsRaw isKindOfClass:[NSDictionary class]]) {
|
||||
NSLog(@"[KBKeyboardLayoutConfig] No layouts found in i18n file");
|
||||
return;
|
||||
}
|
||||
|
||||
NSMutableDictionary<NSString *, KBKeyboardLayout *> *mergedLayouts = [NSMutableDictionary dictionaryWithDictionary:self.layouts ?: @{}];
|
||||
|
||||
[layoutsRaw enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
||||
if (![key isKindOfClass:[NSString class]] || ![obj isKindOfClass:[NSDictionary class]]) {
|
||||
return;
|
||||
}
|
||||
KBKeyboardLayout *layout = [KBKeyboardLayout mj_objectWithKeyValues:obj];
|
||||
if (layout) {
|
||||
mergedLayouts[key] = layout;
|
||||
}
|
||||
}];
|
||||
|
||||
self.layouts = mergedLayouts.copy;
|
||||
NSLog(@"[KBKeyboardLayoutConfig] Loaded %lu i18n layouts, total: %lu",
|
||||
(unsigned long)layoutsRaw.count, (unsigned long)self.layouts.count);
|
||||
}
|
||||
|
||||
+ (instancetype)configFromJSONData:(NSData *)data {
|
||||
if (data.length == 0) { return nil; }
|
||||
NSError *error = nil;
|
||||
|
||||
Reference in New Issue
Block a user