From 8f63741d8cbf65a399e0b69c43c267503cc29f12 Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Thu, 4 Dec 2025 16:59:59 +0800 Subject: [PATCH] 1 --- keyBoard/Class/Home/M/KBCharacter.h | 1 - keyBoard/Class/Home/VC/HomeHotVC.m | 38 +++++++++++++++++++++- keyBoard/Class/Home/VC/HomeRankContentVC.m | 38 +++++++++++++++++++++- keyBoard/Class/Me/VM/KBMyVM.h | 5 +++ keyBoard/Class/Me/VM/KBMyVM.m | 18 ++++++++-- 5 files changed, 94 insertions(+), 6 deletions(-) diff --git a/keyBoard/Class/Home/M/KBCharacter.h b/keyBoard/Class/Home/M/KBCharacter.h index 6e4daa0..e6afa94 100644 --- a/keyBoard/Class/Home/M/KBCharacter.h +++ b/keyBoard/Class/Home/M/KBCharacter.h @@ -41,7 +41,6 @@ NS_ASSUME_NONNULL_BEGIN /// 排名序号(1、2、3...) @property (nonatomic, assign) NSInteger rank; -/// 本地字段:是否已添加到键盘(仅客户端使用) @property (nonatomic, assign) BOOL added; @property (nonatomic, copy) NSString *emoji; diff --git a/keyBoard/Class/Home/VC/HomeHotVC.m b/keyBoard/Class/Home/VC/HomeHotVC.m index e781e08..5ab22af 100644 --- a/keyBoard/Class/Home/VC/HomeHotVC.m +++ b/keyBoard/Class/Home/VC/HomeHotVC.m @@ -16,6 +16,7 @@ #import "HomeRankDetailPopView.h" #import "LSTPopView.h" #import +#import "KBMyVM.h" // 用户人设变更通知 @interface HomeHotVC () @@ -53,9 +54,24 @@ // 请求排行榜数据 self.homeVM = [KBHomeVM new]; + [self kb_reloadRankListWithShowHUD:YES]; + + // 监听“用户人设删除”通知,用于同步更新加号按钮状态 + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(kb_onUserCharacterDeleted:) + name:KBUserCharacterDeletedNotification + object:nil]; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +/// 拉取首页排行榜数据 +- (void)kb_reloadRankListWithShowHUD:(BOOL)needShow { KBWeakSelf [self.homeVM fetchRankListWithParams:nil - needShow:YES + needShow:needShow completion:^(NSArray * _Nullable list, NSError * _Nullable error) { if (error) { // 错误提示已经在 VM 内部通过 HUD 处理,这里不再重复提示 @@ -104,6 +120,26 @@ }]; } +/// 收到“用户人设删除”通知:如果当前列表包含该 id,则刷新接口数据 +- (void)kb_onUserCharacterDeleted:(NSNotification *)note { + NSNumber *cid = note.userInfo[@"characterId"]; + if (!cid) { return; } + NSInteger targetId = cid.integerValue; + if (targetId <= 0) { return; } + + BOOL contains = NO; + for (KBCharacter *c in self.allCharacters) { + if (c.characterId.integerValue == targetId) { + contains = YES; + break; + } + } + if (!contains) { return; } + + // 刷新首页排行榜数据(不需要 HUD) + [self kb_reloadRankListWithShowHUD:NO]; +} + #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { diff --git a/keyBoard/Class/Home/VC/HomeRankContentVC.m b/keyBoard/Class/Home/VC/HomeRankContentVC.m index e7d0ec9..9606b43 100644 --- a/keyBoard/Class/Home/VC/HomeRankContentVC.m +++ b/keyBoard/Class/Home/VC/HomeRankContentVC.m @@ -14,6 +14,7 @@ #import "HomeRankCardCell.h" #import "KBHomeVM.h" #import "KBCharacter.h" +#import "KBMyVM.h" // 引入用户人设变更通知 @interface HomeRankContentVC () @property (nonatomic, copy, nullable) NSString *tagId; // 当前标签 id @@ -46,14 +47,29 @@ make.edges.equalTo(self.view); }]; + // 监听“用户人设删除”通知,用于同步更新加号按钮状态 + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(kb_onUserCharacterDeleted:) + name:KBUserCharacterDeletedNotification + object:nil]; + // 加载当前 tag 下的角色列表 [self loadCharactersForCurrentTag]; } +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + - (void)loadCharactersForCurrentTag { + [self kb_requestCharactersWithShowHUD:YES]; +} + +// 内部通用方法:根据 tag 请求角色列表 +- (void)kb_requestCharactersWithShowHUD:(BOOL)needShow { KBWeakSelf [self.homeVM fetchRankListByTagId:self.tagId - needShow:YES + needShow:needShow completion:^(NSArray * _Nullable list, NSError * _Nullable error) { if (error) { return; @@ -63,6 +79,26 @@ }]; } +/// 收到“用户人设删除”通知:如果当前列表包含该 id,则刷新接口数据 +- (void)kb_onUserCharacterDeleted:(NSNotification *)note { + NSNumber *cid = note.userInfo[@"characterId"]; + if (!cid) { return; } + NSInteger targetId = cid.integerValue; + if (targetId <= 0) { return; } + + BOOL contains = NO; + for (KBCharacter *c in self.characters) { + if (c.characterId.integerValue == targetId) { + contains = YES; + break; + } + } + if (!contains) { return; } + + // 后端会根据最新的人设状态返回 added 字段,刷新本页 UI + [self kb_requestCharactersWithShowHUD:NO]; +} + - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; diff --git a/keyBoard/Class/Me/VM/KBMyVM.h b/keyBoard/Class/Me/VM/KBMyVM.h index 9919940..0c065ad 100644 --- a/keyBoard/Class/Me/VM/KBMyVM.h +++ b/keyBoard/Class/Me/VM/KBMyVM.h @@ -11,6 +11,11 @@ NS_ASSUME_NONNULL_BEGIN +/// 用户人设发生变更(例如从“我的键盘”中删除)时的进程内通知。 +/// userInfo: +/// @"characterId" : NSNumber(NSInteger) 被删除或变更的人设 id +extern NSString * const KBUserCharacterDeletedNotification; + typedef void(^KBMyUserDetailCompletion)(KBUser *_Nullable user, NSError *_Nullable error); typedef void(^KBCharacterListCompletion)(NSArray *characterArray, NSError *_Nullable error); typedef void(^KBUpLoadAvatarCompletion)(BOOL success, NSError * _Nullable error); diff --git a/keyBoard/Class/Me/VM/KBMyVM.m b/keyBoard/Class/Me/VM/KBMyVM.m index 0f95735..0dd3df5 100644 --- a/keyBoard/Class/Me/VM/KBMyVM.m +++ b/keyBoard/Class/Me/VM/KBMyVM.m @@ -11,6 +11,8 @@ #import "KBUser.h" #import "KBAPI.h" +NSString * const KBUserCharacterDeletedNotification = @"KBUserCharacterDeletedNotification"; + @implementation KBMyVM - (void)fetchUserDetailWithCompletion:(KBMyUserDetailCompletion)completion { @@ -111,7 +113,6 @@ } NSDictionary *params = @{@"id": characterId}; - [KBHUD show]; [[KBNetworkManager shared] GET:API_CHARACTER_DEL_USER_CHARACTER parameters:params headers:nil @@ -119,9 +120,20 @@ completion:^(NSDictionary *jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) { - [KBHUD dismiss]; + BOOL success = (error == nil); + + if (success) { + // 通知 App 内其他页面(如 HomeRankContentVC / HomeHotVC)该人设已被删除 + NSDictionary *info = @{@"characterId": characterId ?: @0}; + dispatch_async(dispatch_get_main_queue(), ^{ + [[NSNotificationCenter defaultCenter] postNotificationName:KBUserCharacterDeletedNotification + object:nil + userInfo:info]; + }); + } + if (completion) { - completion(error == nil, error); + completion(success, error); } }]; }