diff --git a/Shared/KBAPI.h b/Shared/KBAPI.h index 5dd57cc..ec32be3 100644 --- a/Shared/KBAPI.h +++ b/Shared/KBAPI.h @@ -33,6 +33,7 @@ #define KB_API_CHARACTER_DETAIL @"/character/detail" // 人设详情 #define KB_API_CHARACTER_LISTBYUSER @"/character/listByUser" // 用户人设列表 #define API_CHARACTER_UPDATE_USER_CHARTSORT @"/character/updateUserCharacterSort" // 更新用户人设排序 +#define API_CHARACTER_DEL_USER_CHARACTER @"/character/delUserCharacter" // 删除用户人设 diff --git a/keyBoard/Class/Me/VC/KBMyKeyBoardVC.m b/keyBoard/Class/Me/VC/KBMyKeyBoardVC.m index a5fc5ae..cc37686 100644 --- a/keyBoard/Class/Me/VC/KBMyKeyBoardVC.m +++ b/keyBoard/Class/Me/VC/KBMyKeyBoardVC.m @@ -74,6 +74,27 @@ static NSString * const kKBMyKeyboardCellId = @"kKBMyKeyboardCellId"; }]; // 使用后端真实数据初始化列表 + [self kb_reloadUserCharacters]; +} + +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + // 隐藏系统导航栏 +// [self.navigationController setNavigationBarHidden:YES animated:animated]; +} + +- (void)viewWillDisappear:(BOOL)animated { + [super viewWillDisappear:animated]; +// if (self.isMovingFromParentViewController || self.isBeingDismissed) { +// [self.navigationController setNavigationBarHidden:NO animated:animated]; +// } +} + + + +#pragma mark - Data + +- (void)kb_reloadUserCharacters { __weak typeof(self) weakSelf = self; [self.viewModel fetchCharacterListByUserWithCompletion:^(NSArray * _Nonnull characterArray, NSError * _Nullable error) { // 请求失败或无数据时,不再使用本地测试数据,直接清空展示 @@ -112,23 +133,6 @@ static NSString * const kKBMyKeyboardCellId = @"kKBMyKeyboardCellId"; }]; } -- (void)viewWillAppear:(BOOL)animated { - [super viewWillAppear:animated]; - // 隐藏系统导航栏 -// [self.navigationController setNavigationBarHidden:YES animated:animated]; -} - -- (void)viewWillDisappear:(BOOL)animated { - [super viewWillDisappear:animated]; -// if (self.isMovingFromParentViewController || self.isBeingDismissed) { -// [self.navigationController setNavigationBarHidden:NO animated:animated]; -// } -} - - - -#pragma mark - Data - #pragma mark - BMLongPressDragCellCollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.dataSourceArray.count; } @@ -148,6 +152,25 @@ static NSString * const kKBMyKeyboardCellId = @"kKBMyKeyboardCellId"; if (!self) { return; } NSIndexPath *tapIndexPath = [weakCV indexPathForCell:tappedCell]; if (!tapIndexPath) { return; } + + // 取出将要删除的人设 id + NSNumber *characterId = nil; + if (tapIndexPath.section < self.dataSourceArray.count) { + NSArray *section = self.dataSourceArray[tapIndexPath.section]; + if (tapIndexPath.item < section.count) { + NSDictionary *item = section[tapIndexPath.item]; + id cid = item[@"id"]; + if ([cid isKindOfClass:[NSNumber class]]) { + characterId = (NSNumber *)cid; + } else if ([cid isKindOfClass:[NSString class]]) { + NSString *cidStr = (NSString *)cid; + if (cidStr.length > 0) { + characterId = @([cidStr integerValue]); + } + } + } + } + [KBAlert confirmTitle:KBLocalized(@"Delete this tag?") message:KBLocalized(@"This action cannot be undone") ok:KBLocalized(@"Confirm") @@ -156,19 +179,37 @@ static NSString * const kKBMyKeyboardCellId = @"kKBMyKeyboardCellId"; cancelColor:[UIColor blackColor] completion:^(BOOL ok) { if (!ok) { return; } - // 更新数据源并删除 item - if (tapIndexPath.section < self.dataSourceArray.count) { - NSMutableArray *section = self.dataSourceArray[tapIndexPath.section]; - if (tapIndexPath.item < section.count) { - [section removeObjectAtIndex:tapIndexPath.item]; - [self.collectionView performBatchUpdates:^{ - [self.collectionView deleteItemsAtIndexPaths:@[tapIndexPath]]; - } completion:^(BOOL finished) { - // 删除后同步最新排序到服务端 - [self kb_updateUserCharacterSortWithShowHUD:NO]; - }]; + + // 若无法获取 id,仅做本地删除以保持 UI 一致 + if (!characterId) { + if (tapIndexPath.section < self.dataSourceArray.count) { + NSMutableArray *section = self.dataSourceArray[tapIndexPath.section]; + if (tapIndexPath.item < section.count) { + [section removeObjectAtIndex:tapIndexPath.item]; + [self.collectionView performBatchUpdates:^{ + [self.collectionView deleteItemsAtIndexPaths:@[tapIndexPath]]; + } completion:nil]; + } } + return; } + + // 调用删除接口,成功后刷新界面 + __weak typeof(self) weakSelf2 = self; + [self.viewModel deleteUserCharacterWithId:characterId + completion:^(BOOL success, NSError * _Nullable error) { + __strong typeof(weakSelf2) strongSelf = weakSelf2; + if (!strongSelf) { return; } + + if (!success) { + NSString *msg = error.localizedDescription ?: KBLocalized(@"Network error"); + [KBHUD showInfo:msg]; + return; + } + + // 重新拉取用户人设列表,刷新 UI + [strongSelf kb_reloadUserCharacters]; + }]; }]; }; return cell; diff --git a/keyBoard/Class/Me/VM/KBMyVM.h b/keyBoard/Class/Me/VM/KBMyVM.h index 1890c1b..9919940 100644 --- a/keyBoard/Class/Me/VM/KBMyVM.h +++ b/keyBoard/Class/Me/VM/KBMyVM.h @@ -16,6 +16,7 @@ typedef void(^KBCharacterListCompletion)(NSArray *characterArray, typedef void(^KBUpLoadAvatarCompletion)(BOOL success, NSError * _Nullable error); typedef void(^KBUpdateUserInfoCompletion)(BOOL success, NSError * _Nullable error); typedef void(^KBUpdateCharacterSortCompletion)(BOOL success, NSError * _Nullable error); +typedef void(^KBDeleteUserCharacterCompletion)(BOOL success, NSError * _Nullable error); @interface KBMyVM : NSObject @@ -27,6 +28,9 @@ typedef void(^KBUpdateCharacterSortCompletion)(BOOL success, NSError * _Nullable /// 更新用户人设排序 - (void)updateUserCharacterSortWithSortArray:(NSArray *)sortArray completion:(KBUpdateCharacterSortCompletion)completion; +/// 删除用户人设(/character/delUserCharacter) +- (void)deleteUserCharacterWithId:(NSNumber *)characterId + completion:(KBDeleteUserCharacterCompletion)completion; /// 上传头像 diff --git a/keyBoard/Class/Me/VM/KBMyVM.m b/keyBoard/Class/Me/VM/KBMyVM.m index cdfe735..0f95735 100644 --- a/keyBoard/Class/Me/VM/KBMyVM.m +++ b/keyBoard/Class/Me/VM/KBMyVM.m @@ -97,6 +97,35 @@ }]; } +/// 删除用户人设 +- (void)deleteUserCharacterWithId:(NSNumber *)characterId + completion:(KBDeleteUserCharacterCompletion)completion { + if (!characterId) { + if (completion) { + NSError *e = [NSError errorWithDomain:KBNetworkErrorDomain + code:KBNetworkErrorInvalidResponse + userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid parameter")}]; + completion(NO, e); + } + return; + } + + NSDictionary *params = @{@"id": characterId}; + [KBHUD show]; + [[KBNetworkManager shared] GET:API_CHARACTER_DEL_USER_CHARACTER + parameters:params + headers:nil + autoShowBusinessError:YES + completion:^(NSDictionary *jsonOrData, + NSURLResponse * _Nullable response, + NSError * _Nullable error) { + [KBHUD dismiss]; + if (completion) { + completion(error == nil, error); + } + }]; +} + /// 上传头像 - (void)upLoadAvatarWithData:(NSData *)avatarData completion:(KBUpLoadAvatarCompletion)completion{ KBWeakSelf;