This commit is contained in:
2025-12-11 20:40:49 +08:00
parent 577b749198
commit 35597f89ca
7 changed files with 198 additions and 26 deletions

View File

@@ -279,10 +279,102 @@
[KBSkinInstallBridge recordInstalledSkinWithId:skinId
name:name
preview:preview
zipURL:zipName];
zipURL:zipName
themeJSON:themeJSON];
}
[KBHUD showInfo:(ok ? KBLocalized(@"已应用,切到键盘查看") : KBLocalized(@"应用皮肤失败"))];
});
}
- (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;
}
@end