配置化json strings

This commit is contained in:
2025-11-19 20:16:19 +08:00
parent 0196128008
commit 8dbaa9dcf6
5 changed files with 478 additions and 154 deletions

View File

@@ -25,126 +25,18 @@
static NSDictionary<NSString *, NSString *> *map;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
map = @{
//
@"letter_q_lower": @"key_q", // q
@"letter_q_upper": @"key_q", // Q
@"letter_w_lower": @"key_w", // w
@"letter_w_upper": @"key_w", // W
@"letter_e_lower": @"key_e", // e
@"letter_e_upper": @"key_e", // E
@"letter_r_lower": @"key_r", // r
@"letter_r_upper": @"key_r", // R
@"letter_t_lower": @"key_t", // t
@"letter_t_upper": @"key_t", // T
@"letter_y_lower": @"key_y", // y
@"letter_y_upper": @"key_y", // Y
@"letter_u_lower": @"key_u", // u
@"letter_u_upper": @"key_u", // U
@"letter_i_lower": @"key_i", // i
@"letter_i_upper": @"key_i", // I
@"letter_o_lower": @"key_o", // o
@"letter_o_upper": @"key_o", // O
@"letter_p_lower": @"key_p", // p
@"letter_p_upper": @"key_p", // P
@"letter_a_lower": @"key_a", // a
@"letter_a_upper": @"key_a", // A
@"letter_s_lower": @"key_s", // s
@"letter_s_upper": @"key_s", // S
@"letter_d_lower": @"key_d", // d
@"letter_d_upper": @"key_d", // D
@"letter_f_lower": @"key_f", // f
@"letter_f_upper": @"key_f", // F
@"letter_g_lower": @"key_g", // g
@"letter_g_upper": @"key_g", // G
@"letter_h_lower": @"key_h", // h
@"letter_h_upper": @"key_h", // H
@"letter_j_lower": @"key_j", // j
@"letter_j_upper": @"key_j", // J
@"letter_k_lower": @"key_k", // k
@"letter_k_upper": @"key_k", // K
@"letter_l_lower": @"key_l", // l
@"letter_l_upper": @"key_l", // L
@"letter_z_lower": @"key_z", // z
@"letter_z_upper": @"key_z", // Z
@"letter_x_lower": @"key_x", // x
@"letter_x_upper": @"key_x", // X
@"letter_c_lower": @"key_c", // c
@"letter_c_upper": @"key_c", // C
@"letter_v_lower": @"key_v", // v
@"letter_v_upper": @"key_v", // V
@"letter_b_lower": @"key_b", // b
@"letter_b_upper": @"key_b", // B
@"letter_n_lower": @"key_n", // n
@"letter_n_upper": @"key_n", // N
@"letter_m_lower": @"key_m", // m
@"letter_m_upper": @"key_m", // M
// 1~0
@"digit_1": @"key_1", // 1
@"digit_2": @"key_2", // 2
@"digit_3": @"key_3", // 3
@"digit_4": @"key_4", // 4
@"digit_5": @"key_5", // 5
@"digit_6": @"key_6", // 6
@"digit_7": @"key_7", // 7
@"digit_8": @"key_8", // 8
@"digit_9": @"key_9", // 9
@"digit_0": @"key_0", // 0
// 123 +
@"sym_minus": @"key_minus", // '-'
@"sym_slash": @"key_slash", // '/'
@"sym_colon": @"key_colon", // ':'
@"sym_semicolon": @"key_semicolon",// ';'
@"sym_paren_l": @"key_paren_l", // '('
@"sym_paren_r": @"key_paren_r", // ')'
@"sym_dollar": @"key_dollar", // '$'
@"sym_amp": @"key_amp", // '&'
@"sym_at": @"key_at", // '@'
@"sym_quote_double": @"key_quote_d", // "
@"sym_comma": @"key_comma", // ','
@"sym_dot": @"key_dot", // '.'
@"sym_question": @"key_question", // '?'
@"sym_exclam": @"key_exclam", // '!'
@"sym_quote_single": @"key_quote", // '
// #+= /
@"sym_bracket_l": @"key_bracket_l", // '['
@"sym_bracket_r": @"key_bracket_r", // ']'
@"sym_brace_l": @"key_brace_l", // '{'
@"sym_brace_r": @"key_brace_r", // '}'
@"sym_hash": @"key_hash", // '#'
@"sym_percent": @"key_percent", // '%'
@"sym_caret": @"key_caret", // '^'
@"sym_asterisk": @"key_asterisk", // '*'
@"sym_plus": @"key_plus", // '+'
@"sym_equal": @"key_equal", // '='
@"sym_underscore": @"key_underscore", // '_'
@"sym_backslash": @"key_backslash", // '\'
@"sym_pipe": @"key_pipe", // '|'
@"sym_tilde": @"key_tilde", // '~'
@"sym_lt": @"key_lt", // '<'
@"sym_gt": @"key_gt", // '>'
@"sym_euro": @"key_euro", // ''
@"sym_pound": @"key_pound", // '£'
@"sym_bullet": @"key_bullet", // ''
//
@"space": @"key_space", //
@"backspace": @"key_del", //
@"shift": @"key_up", // Shift
@"mode_123": @"key_123", // "123"
@"mode_abc": @"key_abc", // "abc"
@"symbols_toggle_more": @"key_symbols_more", // "123 -> #+="
@"symbols_toggle_123": @"key_symbols_123", // "#+= -> 123"
@"ai": @"key_ai", // AI
@"return": @"key_send" // /
};
//
// 使 .strings 便
NSString *path = [[NSBundle mainBundle] pathForResource:@"KBSkinIconMap" ofType:@"strings"];
if (path.length > 0) {
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
if ([dict isKindOfClass:NSDictionary.class]) {
map = dict;
}
}
if (!map) {
map = @{}; //
}
});
return map;
}
@@ -157,6 +49,7 @@
- (void)applySkinWithJSON:(NSDictionary *)skinJSON
fromViewController:(UIViewController *)presenting
mode:(KBSkinSourceMode)mode
completion:(KBSkinApplyCompletion)completion {
if (skinJSON.count == 0) {
if (completion) completion(NO);
@@ -177,10 +70,17 @@
// [KBHUD showInfo:KBLocalized(@"皮肤已应用,键盘需开启“允许完全访问”后才能显示图片")];
// }
#if KB_SKIN_ICON_USE_REMOTE
[self kb_applySkinUsingRemoteIcons:skinJSON completion:completion];
#else
#endif
switch (mode) {
case KBSkinSourceModeLocalBundleZip:
// bundle zip_url bundle zip
[self kb_applySkinUsingLocalBundle:skinJSON completion:completion];
break;
case KBSkinSourceModeRemoteZip:
default:
// zip_url http/https
[self kb_applySkinUsingRemoteIcons:skinJSON completion:completion];
break;
}
}
/// Zip skinJSON zip_url
@@ -231,10 +131,7 @@
__block BOOL zipOK = YES;
#if __has_include(<SSZipArchive/SSZipArchive.h>)
// zip_url Zip
//
// 1) 线 URLhttp/https KBNetworkManager
// 2) zip_url "bundle://" "bundle://001.zip"
// zip_url Zip
if (!hasCachedAssets && zipURL.length > 0) {
dispatch_group_enter(group);
@@ -314,32 +211,16 @@
dispatch_group_leave(group);
};
// bundle zip_url "bundle://001.zip"
if ([zipURL hasPrefix:@"bundle://"]) {
NSString *name = [zipURL substringFromIndex:@"bundle://".length];
NSString *fileName = name ?: @"";
NSString *ext = fileName.pathExtension;
NSString *base = fileName;
if (ext.length == 0) {
ext = @"zip";
} else {
base = [fileName stringByDeletingPathExtension];
// http/https
[[KBNetworkManager shared] GET:zipURL parameters:nil headers:nil completion:^(id jsonOrData, NSURLResponse *response, NSError *error) {
NSData *data = ([jsonOrData isKindOfClass:NSData.class] ? (NSData *)jsonOrData : nil);
if (error || data.length == 0) {
zipOK = NO;
dispatch_group_leave(group);
return;
}
NSString *path = [[NSBundle mainBundle] pathForResource:base ofType:ext];
NSData *data = (path.length > 0) ? [NSData dataWithContentsOfFile:path] : nil;
handleZipData(data);
} else {
//
[[KBNetworkManager shared] GET:zipURL parameters:nil headers:nil completion:^(id jsonOrData, NSURLResponse *response, NSError *error) {
NSData *data = ([jsonOrData isKindOfClass:NSData.class] ? (NSData *)jsonOrData : nil);
if (error || data.length == 0) {
zipOK = NO;
dispatch_group_leave(group);
return;
}
handleZipData(data);
}];
}
}];
}
#else
zipOK = NO;
@@ -383,4 +264,184 @@
});
}
/// bundle skin[@"zip_url"] bundle zip /
/// - AppGroup/Skins/<skinId>/...便 App Group
- (void)kb_applySkinUsingLocalBundle:(NSDictionary *)skin completion:(KBSkinApplyCompletion)completion {
NSString *skinId = skin[@"id"] ?: @"local";
NSString *name = skin[@"name"] ?: skinId;
NSString *zipName = skin[@"zip_url"] ?: @""; // bundle zip
// key_icons
NSDictionary *iconShortNames = nil;
if ([skin[@"key_icons"] isKindOfClass:NSDictionary.class]) {
iconShortNames = skin[@"key_icons"];
} else {
iconShortNames = [self.class kb_defaultIconShortNames];
}
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:AppGroup];
if (!containerURL) {
if (completion) completion(NO);
[KBHUD showInfo:KBLocalized(@"无法访问共享容器,应用皮肤失败")];
return;
}
NSString *skinsRoot = [containerURL.path stringByAppendingPathComponent:@"Skins"];
NSString *skinRoot = [skinsRoot stringByAppendingPathComponent:skinId];
NSString *iconsDir = [skinRoot stringByAppendingPathComponent:@"icons"];
[[NSFileManager defaultManager] createDirectoryAtPath:iconsDir
withIntermediateDirectories:YES
attributes:nil
error:NULL];
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL hasIconsDir = [fm fileExistsAtPath:iconsDir isDirectory:&isDir] && isDir;
NSArray *contents = hasIconsDir ? [fm contentsOfDirectoryAtPath:iconsDir error:NULL] : nil;
BOOL hasCachedAssets = (contents.count > 0);
NSString *bgPath = [skinRoot stringByAppendingPathComponent:@"background.png"];
dispatch_group_t group = dispatch_group_create();
__block BOOL zipOK = YES;
#if __has_include(<SSZipArchive/SSZipArchive.h>)
// zipName bundle zip
if (!hasCachedAssets && zipName.length > 0) {
dispatch_group_enter(group);
// zipName "bundle://001.zip"
NSString *fileName = zipName ?: @"";
if ([fileName hasPrefix:@"bundle://"]) {
fileName = [fileName substringFromIndex:@"bundle://".length];
}
// Images/001.zip
NSString *dir = [fileName stringByDeletingLastPathComponent];
NSString *last = fileName.lastPathComponent;
NSString *ext = last.pathExtension;
NSString *base = last;
if (ext.length == 0) {
ext = @"zip";
} else {
base = [last stringByDeletingPathExtension];
}
NSString *path = nil;
if (dir.length > 0) {
path = [[NSBundle mainBundle] pathForResource:base ofType:ext inDirectory:dir];
} else {
path = [[NSBundle mainBundle] pathForResource:base ofType:ext];
}
NSData *data = (path.length > 0) ? [NSData dataWithContentsOfFile:path] : nil;
if (data.length == 0) {
zipOK = NO;
dispatch_group_leave(group);
} else {
// Zip
[[NSFileManager defaultManager] createDirectoryAtPath:skinRoot
withIntermediateDirectories:YES
attributes:nil
error:NULL];
NSString *zipPath = [skinRoot stringByAppendingPathComponent:@"skin.zip"];
if (![data writeToFile:zipPath atomically:YES]) {
zipOK = NO;
dispatch_group_leave(group);
} else {
NSError *unzipError = nil;
BOOL ok = [SSZipArchive unzipFileAtPath:zipPath
toDestination:skinRoot
overwrite:YES
password:nil
error:&unzipError];
[fm removeItemAtPath:zipPath error:nil];
if (!ok || unzipError) {
zipOK = NO;
dispatch_group_leave(group);
} else {
//
BOOL isDir2 = NO;
NSArray *iconsContent = [fm contentsOfDirectoryAtPath:iconsDir error:NULL];
BOOL iconsValid = ([fm fileExistsAtPath:iconsDir isDirectory:&isDir2] && isDir2 && iconsContent.count > 0);
if (!iconsValid) {
NSArray<NSString *> *subItems = [fm contentsOfDirectoryAtPath:skinRoot error:NULL];
for (NSString *name in subItems) {
if ([name isEqualToString:@"icons"] || [name isEqualToString:@"__MACOSX"]) continue;
NSString *nestedRoot = [skinRoot stringByAppendingPathComponent:name];
BOOL isDirNested = NO;
if (![fm fileExistsAtPath:nestedRoot isDirectory:&isDirNested] || !isDirNested) continue;
NSString *nestedIcons = [nestedRoot stringByAppendingPathComponent:@"icons"];
BOOL isDirNestedIcons = NO;
if ([fm fileExistsAtPath:nestedIcons isDirectory:&isDirNestedIcons] && isDirNestedIcons) {
NSArray *nestedFiles = [fm contentsOfDirectoryAtPath:nestedIcons error:NULL];
if (nestedFiles.count > 0) {
[fm createDirectoryAtPath:iconsDir
withIntermediateDirectories:YES
attributes:nil
error:NULL];
for (NSString *fn in nestedFiles) {
NSString *from = [nestedIcons stringByAppendingPathComponent:fn];
NSString *to = [iconsDir stringByAppendingPathComponent:fn];
[fm removeItemAtPath:to error:nil];
[fm moveItemAtPath:from toPath:to error:nil];
}
}
}
NSString *nestedBg = [nestedRoot stringByAppendingPathComponent:@"background.png"];
if ([fm fileExistsAtPath:nestedBg]) {
[fm removeItemAtPath:bgPath error:nil];
[fm moveItemAtPath:nestedBg toPath:bgPath error:nil];
}
}
}
dispatch_group_leave(group);
}
}
}
}
#else
zipOK = NO;
#endif
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// key_icons -> App Group
NSMutableDictionary<NSString *, NSString *> *iconPathMap = [NSMutableDictionary dictionary];
[iconShortNames enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, NSString *shortName, BOOL *stop) {
if (![shortName isKindOfClass:NSString.class] || shortName.length == 0) return;
NSString *fileName = shortName;
if (fileName.pathExtension.length == 0) {
fileName = [fileName stringByAppendingPathExtension:@"png"];
}
NSString *relative = [NSString stringWithFormat:@"Skins/%@/icons/%@", skinId, fileName];
iconPathMap[identifier] = relative;
}];
NSMutableDictionary *themeJSON = [skin mutableCopy];
themeJSON[@"id"] = skinId;
if (iconPathMap.count > 0) {
themeJSON[@"key_icons"] = iconPathMap.copy;
}
BOOL themeOK = [[KBSkinManager shared] applyThemeFromJSON:themeJSON];
NSData *bgData = [NSData dataWithContentsOfFile:bgPath];
BOOL ok = themeOK;
if (bgData.length > 0) {
ok = [[KBSkinManager shared] applyImageSkinWithData:bgData skinId:skinId name:name];
}
if (!zipOK && !hasCachedAssets) {
ok = NO;
}
if (completion) completion(ok);
[KBHUD showInfo:(ok ? KBLocalized(@"已应用,切到键盘查看") : KBLocalized(@"应用皮肤失败"))];
});
}
@end