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
|
||||
|
||||
@@ -15,6 +15,7 @@ typedef void(^KBMyUserDetailCompletion)(KBUser *_Nullable user, NSError *_Nullab
|
||||
typedef void(^KBCharacterListCompletion)(NSArray<KBCharacter *> *characterArray, NSError *_Nullable error);
|
||||
typedef void(^KBUpLoadAvatarCompletion)(BOOL success, NSError * _Nullable error);
|
||||
typedef void(^KBUpdateUserInfoCompletion)(BOOL success, NSError * _Nullable error);
|
||||
typedef void(^KBUpdateCharacterSortCompletion)(BOOL success, NSError * _Nullable error);
|
||||
|
||||
@interface KBMyVM : NSObject
|
||||
|
||||
@@ -24,7 +25,8 @@ typedef void(^KBUpdateUserInfoCompletion)(BOOL success, NSError * _Nullable erro
|
||||
/// 用户人设列表(/character/listByUser)
|
||||
- (void)fetchCharacterListByUserWithCompletion:(KBCharacterListCompletion)completion;
|
||||
/// 更新用户人设排序
|
||||
|
||||
- (void)updateUserCharacterSortWithSortArray:(NSArray<NSNumber *> *)sortArray
|
||||
completion:(KBUpdateCharacterSortCompletion)completion;
|
||||
|
||||
|
||||
/// 上传头像
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#import "AppDelegate.h"
|
||||
#import "KBNetworkManager.h"
|
||||
#import "KBUser.h"
|
||||
#import "KBAPI.h"
|
||||
|
||||
@implementation KBMyVM
|
||||
|
||||
@@ -72,6 +73,30 @@
|
||||
}];
|
||||
}
|
||||
|
||||
/// 更新用户人设排序
|
||||
- (void)updateUserCharacterSortWithSortArray:(NSArray<NSNumber *> *)sortArray
|
||||
completion:(KBUpdateCharacterSortCompletion)completion {
|
||||
// 构造请求体:{"sort": [id1, id2, ...]}
|
||||
NSMutableDictionary *params = [NSMutableDictionary dictionary];
|
||||
if (sortArray.count > 0) {
|
||||
params[@"sort"] = sortArray;
|
||||
} else {
|
||||
params[@"sort"] = @[];
|
||||
}
|
||||
|
||||
[[KBNetworkManager shared] POST:API_CHARACTER_UPDATE_USER_CHARTSORT
|
||||
jsonBody:params
|
||||
headers:nil
|
||||
autoShowBusinessError:true
|
||||
completion:^(NSDictionary * _Nullable json,
|
||||
NSURLResponse * _Nullable response,
|
||||
NSError * _Nullable error) {
|
||||
if (completion) {
|
||||
completion(error == nil, error);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
/// 上传头像
|
||||
- (void)upLoadAvatarWithData:(NSData *)avatarData completion:(KBUpLoadAvatarCompletion)completion{
|
||||
KBWeakSelf;
|
||||
|
||||
Reference in New Issue
Block a user