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

View File

@@ -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;
/// 上传头像

View File

@@ -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;