1
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user