2025-11-18 20:53:47 +08:00
|
|
|
|
//
|
|
|
|
|
|
// KBSkinService.m
|
|
|
|
|
|
// keyBoard
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#import "KBSkinService.h"
|
|
|
|
|
|
|
|
|
|
|
|
#import "KBSkinManager.h"
|
|
|
|
|
|
#import "KBConfig.h"
|
|
|
|
|
|
#import "KBKeyboardPermissionManager.h"
|
|
|
|
|
|
#import "KBNetworkManager.h"
|
|
|
|
|
|
#import "KBHUD.h"
|
2025-11-20 14:27:57 +08:00
|
|
|
|
#import "KBSkinInstallBridge.h"
|
2025-11-18 20:53:47 +08:00
|
|
|
|
|
2025-11-19 14:54:45 +08:00
|
|
|
|
#if __has_include(<SSZipArchive/SSZipArchive.h>)
|
|
|
|
|
|
#import <SSZipArchive/SSZipArchive.h>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-11-18 20:53:47 +08:00
|
|
|
|
@implementation KBSkinService
|
|
|
|
|
|
|
2025-11-19 14:54:45 +08:00
|
|
|
|
#pragma mark - Icon short-name mapping (local default)
|
|
|
|
|
|
|
2025-11-18 20:53:47 +08:00
|
|
|
|
+ (instancetype)shared {
|
|
|
|
|
|
static KBSkinService *s; static dispatch_once_t onceToken;
|
|
|
|
|
|
dispatch_once(&onceToken, ^{ s = [KBSkinService new]; });
|
|
|
|
|
|
return s;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)applySkinWithJSON:(NSDictionary *)skinJSON
|
|
|
|
|
|
fromViewController:(UIViewController *)presenting
|
2025-11-19 20:16:19 +08:00
|
|
|
|
mode:(KBSkinSourceMode)mode
|
2025-11-18 20:53:47 +08:00
|
|
|
|
completion:(KBSkinApplyCompletion)completion {
|
2025-11-19 20:30:30 +08:00
|
|
|
|
// 模式为“恢复默认皮肤”时,直接调用 KBSkinManager 的 reset 接口,忽略 JSON 内容。
|
|
|
|
|
|
if (mode == KBSkinSourceModeResetToDefault) {
|
|
|
|
|
|
[[KBSkinManager shared] resetToDefault];
|
|
|
|
|
|
if (completion) completion(YES);
|
|
|
|
|
|
[KBHUD showInfo:KBLocalized(@"已恢复默认键盘皮肤")];
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 20:53:47 +08:00
|
|
|
|
if (skinJSON.count == 0) {
|
|
|
|
|
|
if (completion) completion(NO);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-19 19:15:28 +08:00
|
|
|
|
// // 1. 点击应用皮肤时,检查键盘启用 & 完全访问状态,并尽量给出友好提示。
|
|
|
|
|
|
// KBKeyboardPermissionManager *perm = [KBKeyboardPermissionManager shared];
|
|
|
|
|
|
// BOOL enabled = [perm isKeyboardEnabled];
|
|
|
|
|
|
// KBFARecord fa = [perm lastKnownFullAccess];
|
|
|
|
|
|
// BOOL hasFullAccess = (fa == KBFARecordGranted);
|
|
|
|
|
|
//
|
|
|
|
|
|
// if (!enabled || !hasFullAccess) {
|
|
|
|
|
|
// // 引导页(内部有自己的展示策略,避免过度打扰)
|
|
|
|
|
|
// [perm presentPermissionIfNeededFrom:presenting];
|
|
|
|
|
|
//
|
|
|
|
|
|
// // 简单提示:皮肤可以应用,但未开启完全访问时扩展无法读取 App Group 中的图片。
|
|
|
|
|
|
// [KBHUD showInfo:KBLocalized(@"皮肤已应用,键盘需开启“允许完全访问”后才能显示图片")];
|
|
|
|
|
|
// }
|
2025-11-18 20:53:47 +08:00
|
|
|
|
|
2025-11-19 20:16:19 +08:00
|
|
|
|
switch (mode) {
|
|
|
|
|
|
case KBSkinSourceModeLocalBundleZip:
|
|
|
|
|
|
// 本地 bundle 模式:zip_url 为 bundle 内的 zip 文件名
|
|
|
|
|
|
[self kb_applySkinUsingLocalBundle:skinJSON completion:completion];
|
|
|
|
|
|
break;
|
2025-11-25 16:53:38 +08:00
|
|
|
|
// case KBSkinSourceModeRemoteZip:
|
2025-11-19 20:16:19 +08:00
|
|
|
|
default:
|
|
|
|
|
|
// 远程模式:zip_url 为 http/https 地址
|
|
|
|
|
|
[self kb_applySkinUsingRemoteIcons:skinJSON completion:completion];
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-11-18 20:53:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-19 14:54:45 +08:00
|
|
|
|
/// 远程 Zip 模式:skinJSON 提供 zip_url,一套皮肤一个压缩包。
|
|
|
|
|
|
/// - Zip 解压路径:AppGroup/Skins/<skinId>/...
|
|
|
|
|
|
/// * 图标:icons/<shortName>.png,例如 icons/key_a.png
|
|
|
|
|
|
/// * 背景:background.png(可选)
|
|
|
|
|
|
/// - skinJSON.key_icons 的 value 填写 Zip 内的图标“短文件名”(不含路径,可不含扩展名),例如 "key_a"
|
|
|
|
|
|
/// 应用时会被转换为相对 App Group 根目录的路径:Skins/<skinId>/icons/<shortName>.png
|
2025-11-18 20:53:47 +08:00
|
|
|
|
- (void)kb_applySkinUsingRemoteIcons:(NSDictionary *)skin completion:(KBSkinApplyCompletion)completion {
|
2025-11-25 18:54:53 +08:00
|
|
|
|
[KBSkinInstallBridge installRemoteSkinWithJSON:skin
|
|
|
|
|
|
completion:^(BOOL success, NSError * _Nullable error) {
|
|
|
|
|
|
if (completion) {
|
|
|
|
|
|
completion(success);
|
2025-11-19 20:16:19 +08:00
|
|
|
|
}
|
2025-11-25 18:54:53 +08:00
|
|
|
|
if (!success && error) {
|
|
|
|
|
|
NSLog(@"[KBSkinService] remote skin install failed: %@", error);
|
2025-11-19 20:16:19 +08:00
|
|
|
|
}
|
2025-11-25 18:54:53 +08:00
|
|
|
|
NSString *message = nil;
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
message = KBLocalized(@"已应用,切到键盘查看");
|
|
|
|
|
|
} else if ([error.domain isEqualToString:KBSkinBridgeErrorDomain] &&
|
|
|
|
|
|
error.code == KBSkinBridgeErrorContainerUnavailable) {
|
|
|
|
|
|
message = KBLocalized(@"无法访问共享容器,应用皮肤失败");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
message = KBLocalized(@"应用皮肤失败");
|
2025-11-19 20:16:19 +08:00
|
|
|
|
}
|
2025-11-25 18:54:53 +08:00
|
|
|
|
[KBHUD showInfo:message];
|
|
|
|
|
|
}];
|
2025-11-19 20:16:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 本地 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;
|
2025-12-11 16:18:00 +08:00
|
|
|
|
// if ([skin[@"key_icons"] isKindOfClass:NSDictionary.class]) {
|
|
|
|
|
|
// iconShortNames = skin[@"key_icons"];
|
|
|
|
|
|
// } else {
|
2025-11-20 14:27:57 +08:00
|
|
|
|
iconShortNames = [KBSkinInstallBridge defaultIconShortNames];
|
2025-12-11 16:18:00 +08:00
|
|
|
|
// }
|
2025-11-19 20:16:19 +08:00
|
|
|
|
|
|
|
|
|
|
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;
|
2025-11-19 15:39:47 +08:00
|
|
|
|
if (ext.length == 0) {
|
|
|
|
|
|
ext = @"zip";
|
|
|
|
|
|
} else {
|
2025-11-19 20:16:19 +08:00
|
|
|
|
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];
|
2025-11-19 15:39:47 +08:00
|
|
|
|
}
|
2025-11-19 20:16:19 +08:00
|
|
|
|
|
2025-11-19 15:39:47 +08:00
|
|
|
|
NSData *data = (path.length > 0) ? [NSData dataWithContentsOfFile:path] : nil;
|
2025-11-19 20:16:19 +08:00
|
|
|
|
|
|
|
|
|
|
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]) {
|
2025-11-19 15:39:47 +08:00
|
|
|
|
zipOK = NO;
|
|
|
|
|
|
dispatch_group_leave(group);
|
2025-11-19 20:16:19 +08:00
|
|
|
|
} 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);
|
|
|
|
|
|
}
|
2025-11-19 15:39:47 +08:00
|
|
|
|
}
|
2025-11-19 20:16:19 +08:00
|
|
|
|
}
|
2025-11-19 15:39:47 +08:00
|
|
|
|
}
|
2025-11-19 20:16:19 +08:00
|
|
|
|
#else
|
|
|
|
|
|
zipOK = NO;
|
|
|
|
|
|
#endif
|
2025-11-18 20:53:47 +08:00
|
|
|
|
|
2025-11-19 14:54:45 +08:00
|
|
|
|
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"];
|
2025-11-18 20:53:47 +08:00
|
|
|
|
}
|
2025-11-19 14:54:45 +08:00
|
|
|
|
NSString *relative = [NSString stringWithFormat:@"Skins/%@/icons/%@", skinId, fileName];
|
|
|
|
|
|
iconPathMap[identifier] = relative;
|
2025-11-18 20:53:47 +08:00
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
NSMutableDictionary *themeJSON = [skin mutableCopy];
|
2025-11-19 14:54:45 +08:00
|
|
|
|
themeJSON[@"id"] = skinId;
|
|
|
|
|
|
if (iconPathMap.count > 0) {
|
|
|
|
|
|
themeJSON[@"key_icons"] = iconPathMap.copy;
|
|
|
|
|
|
}
|
2025-11-18 20:53:47 +08:00
|
|
|
|
|
|
|
|
|
|
BOOL themeOK = [[KBSkinManager shared] applyThemeFromJSON:themeJSON];
|
2025-11-19 14:54:45 +08:00
|
|
|
|
|
|
|
|
|
|
NSData *bgData = [NSData dataWithContentsOfFile:bgPath];
|
2025-11-18 20:53:47 +08:00
|
|
|
|
BOOL ok = themeOK;
|
|
|
|
|
|
if (bgData.length > 0) {
|
|
|
|
|
|
ok = [[KBSkinManager shared] applyImageSkinWithData:bgData skinId:skinId name:name];
|
|
|
|
|
|
}
|
2025-11-19 14:54:45 +08:00
|
|
|
|
|
|
|
|
|
|
if (!zipOK && !hasCachedAssets) {
|
|
|
|
|
|
ok = NO;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 20:53:47 +08:00
|
|
|
|
if (completion) completion(ok);
|
2025-12-11 19:43:55 +08:00
|
|
|
|
if (ok) {
|
|
|
|
|
|
NSString *preview = [skin[@"preview"] isKindOfClass:NSString.class] ? skin[@"preview"] : nil;
|
|
|
|
|
|
[KBSkinInstallBridge recordInstalledSkinWithId:skinId
|
|
|
|
|
|
name:name
|
|
|
|
|
|
preview:preview
|
2025-12-11 20:40:49 +08:00
|
|
|
|
zipURL:zipName
|
|
|
|
|
|
themeJSON:themeJSON];
|
2025-12-11 19:43:55 +08:00
|
|
|
|
}
|
2025-11-18 20:53:47 +08:00
|
|
|
|
[KBHUD showInfo:(ok ? KBLocalized(@"已应用,切到键盘查看") : KBLocalized(@"应用皮肤失败"))];
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-11 20:40:49 +08:00
|
|
|
|
- (void)deleteSkinsWithIds:(NSArray<NSString *> *)skinIds
|
|
|
|
|
|
completion:(KBSkinDeleteCompletion)completion {
|
|
|
|
|
|
if (skinIds.count == 0) {
|
|
|
|
|
|
if (completion) {
|
|
|
|
|
|
NSError *error = [NSError errorWithDomain:KBSkinBridgeErrorDomain
|
|
|
|
|
|
code:KBSkinBridgeErrorInvalidPayload
|
|
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey: @"Invalid skin ids"}];
|
|
|
|
|
|
completion(NO, error);
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
|
|
|
|
NSArray<KBSkinDownloadRecord *> *beforeDesc = [KBSkinInstallBridge installedSkinRecords];
|
|
|
|
|
|
NSArray<KBSkinDownloadRecord *> *beforeAsc = beforeDesc.count > 0 ? [[[beforeDesc reverseObjectEnumerator] allObjects] copy] : @[];
|
|
|
|
|
|
NSString *currentId = [KBSkinManager shared].current.skinId ?: @"";
|
|
|
|
|
|
NSSet<NSString *> *deleteSet = [NSSet setWithArray:skinIds];
|
|
|
|
|
|
NSError *lastError = nil;
|
|
|
|
|
|
for (NSString *skinId in skinIds) {
|
|
|
|
|
|
if (skinId.length == 0) { continue; }
|
|
|
|
|
|
BOOL ok = [KBSkinInstallBridge removeInstalledSkinWithId:skinId error:&lastError];
|
|
|
|
|
|
if (!ok) { break; }
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!lastError && [deleteSet containsObject:currentId]) {
|
|
|
|
|
|
NSArray<KBSkinDownloadRecord *> *afterDesc = [KBSkinInstallBridge installedSkinRecords];
|
|
|
|
|
|
NSArray<KBSkinDownloadRecord *> *afterAsc = afterDesc.count > 0 ? [[[afterDesc reverseObjectEnumerator] allObjects] copy] : @[];
|
|
|
|
|
|
NSString *fallbackId = [self kb_fallbackSkinIdForCurrent:currentId
|
|
|
|
|
|
orderAsc:beforeAsc
|
|
|
|
|
|
deletedSet:deleteSet
|
|
|
|
|
|
remainingAsc:afterAsc];
|
|
|
|
|
|
if (fallbackId.length > 0) {
|
|
|
|
|
|
BOOL ok = [KBSkinInstallBridge applyInstalledSkinWithId:fallbackId error:&lastError];
|
|
|
|
|
|
if (!ok) {
|
|
|
|
|
|
if (lastError == nil) {
|
|
|
|
|
|
lastError = [NSError errorWithDomain:KBSkinBridgeErrorDomain
|
|
|
|
|
|
code:KBSkinBridgeErrorApplyFailed
|
|
|
|
|
|
userInfo:nil];
|
|
|
|
|
|
}
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
|
[[KBSkinManager shared] resetToDefault];
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
|
[[KBSkinManager shared] resetToDefault];
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
|
if (completion) completion(lastError == nil, lastError);
|
|
|
|
|
|
if (lastError) {
|
|
|
|
|
|
NSLog(@"[KBSkinService] delete skins failed: %@", lastError);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (NSString *)kb_fallbackSkinIdForCurrent:(NSString *)currentId
|
|
|
|
|
|
orderAsc:(NSArray<KBSkinDownloadRecord *> *)orderAsc
|
|
|
|
|
|
deletedSet:(NSSet<NSString *> *)deletedSet
|
|
|
|
|
|
remainingAsc:(NSArray<KBSkinDownloadRecord *> *)remainingAsc {
|
|
|
|
|
|
if (currentId.length == 0) { return nil; }
|
|
|
|
|
|
NSInteger idx = NSNotFound;
|
|
|
|
|
|
for (NSInteger i = 0; i < (NSInteger)orderAsc.count; i++) {
|
|
|
|
|
|
KBSkinDownloadRecord *record = orderAsc[i];
|
|
|
|
|
|
if ([record.skinId isEqualToString:currentId]) {
|
|
|
|
|
|
idx = i;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (idx != NSNotFound) {
|
|
|
|
|
|
for (NSInteger left = idx - 1; left >= 0; left--) {
|
|
|
|
|
|
KBSkinDownloadRecord *candidate = orderAsc[left];
|
|
|
|
|
|
if (candidate.skinId.length > 0 && ![deletedSet containsObject:candidate.skinId]) {
|
|
|
|
|
|
return candidate.skinId;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for (NSInteger right = idx + 1; right < (NSInteger)orderAsc.count; right++) {
|
|
|
|
|
|
KBSkinDownloadRecord *candidate = orderAsc[right];
|
|
|
|
|
|
if (candidate.skinId.length > 0 && ![deletedSet containsObject:candidate.skinId]) {
|
|
|
|
|
|
return candidate.skinId;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for (KBSkinDownloadRecord *record in remainingAsc) {
|
|
|
|
|
|
if (record.skinId.length > 0) {
|
|
|
|
|
|
return record.skinId;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 20:53:47 +08:00
|
|
|
|
@end
|