This commit is contained in:
2025-12-04 16:18:43 +08:00
parent c9863cd353
commit 231f7f8c13
3 changed files with 94 additions and 7 deletions

View File

@@ -83,16 +83,25 @@ static NSString * const kKBMyKeyboardCellId = @"kKBMyKeyboardCellId";
return;
}
// KBCharacter 使 {emoji, title}
// KBCharacter 使 {emoji, title, id}
NSMutableArray<NSDictionary *> *section = [NSMutableArray arrayWithCapacity:characterArray.count];
for (KBCharacter *c in characterArray) {
NSString *emoji = c.emoji ?: @"";
NSString *title = c.characterName ?: @"";
NSString *identifier = c.characterId ?: @"";
// emoji
if (emoji.length == 0 && title.length == 0) {
continue;
}
[section addObject:@{@"emoji": emoji, @"title": title}];
NSMutableDictionary *item = [NSMutableDictionary dictionary];
item[@"emoji"] = emoji;
item[@"title"] = title;
if (identifier.length > 0) {
// 便 sort
NSInteger cid = identifier.integerValue;
item[@"id"] = @(cid);
}
[section addObject:item];
}
weakSelf.dataSourceArray = [NSMutableArray array];
@@ -154,7 +163,10 @@ static NSString * const kKBMyKeyboardCellId = @"kKBMyKeyboardCellId";
[section removeObjectAtIndex:tapIndexPath.item];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:@[tapIndexPath]];
} completion:nil];
} completion:^(BOOL finished) {
//
[self kb_updateUserCharacterSortWithShowHUD:NO];
}];
}
}
}];
@@ -190,9 +202,57 @@ static NSString * const kKBMyKeyboardCellId = @"kKBMyKeyboardCellId";
#pragma mark - Actions
- (void)onSave {
//
NSLog(@"保存顺序: %@", self.dataSourceArray);
[KBHUD showInfo:KBLocalized(@"Saved")];
//
[self kb_updateUserCharacterSortWithShowHUD:YES];
}
/// dataSourceArray sort
- (NSArray<NSNumber *> *)kb_currentSortArray {
NSMutableArray<NSNumber *> *result = [NSMutableArray array];
for (NSArray *section in self.dataSourceArray) {
for (NSDictionary *item in section) {
id cid = item[@"id"];
if ([cid isKindOfClass:[NSNumber class]]) {
[result addObject:cid];
} else if ([cid isKindOfClass:[NSString class]]) {
NSString *cidStr = (NSString *)cid;
if (cidStr.length > 0) {
NSInteger value = cidStr.integerValue;
[result addObject:@(value)];
}
}
}
}
return result;
}
/// VM
- (void)kb_updateUserCharacterSortWithShowHUD:(BOOL)showHUD {
NSArray<NSNumber *> *sortArray = [self kb_currentSortArray];
if (showHUD) {
[KBHUD show];
}
__weak typeof(self) weakSelf = self;
[self.viewModel updateUserCharacterSortWithSortArray:sortArray
completion:^(BOOL success, NSError * _Nullable error) {
__strong typeof(weakSelf) self = weakSelf;
if (!self) { return; }
if (showHUD) {
[KBHUD dismiss];
}
if (!success && error) {
NSString *msg = error.localizedDescription ?: KBLocalized(@"Network error");
[KBHUD showInfo:msg];
return;
}
if (showHUD) {
[KBHUD showSuccess:KBLocalized(@"Saved")];
}
}];
}
#pragma mark - Lazy UI