Files
keyboard/keyBoard/Class/Home/VC/HomeHotVC.m

304 lines
11 KiB
Mathematica
Raw Normal View History

2025-11-06 14:02:22 +08:00
//
// HomeHotVC.m
// keyBoard
//
// Created by Mac on 2025/11/6.
//
#import "HomeHotVC.h"
2025-11-06 14:59:00 +08:00
//
#import "KBTopThreeView.h"
2025-12-03 18:49:18 +08:00
#import "KBHomeVM.h"
#import "KBCharacter.h"
2025-11-06 14:59:00 +08:00
#import "HomeHotCell.h"
2025-11-06 16:05:28 +08:00
#import "HomeRankVC.h"
2025-11-07 22:22:41 +08:00
#import "KBSearchVC.h"
2025-11-17 15:39:03 +08:00
#import "HomeRankDetailPopView.h"
#import "LSTPopView.h"
2025-11-06 14:59:00 +08:00
#import <HWPanModal/HWPanModal.h>
2025-12-04 16:59:59 +08:00
#import "KBMyVM.h" //
2025-12-04 19:12:34 +08:00
#import "KBHUD.h"
2025-12-03 18:49:18 +08:00
2025-11-06 14:59:00 +08:00
@interface HomeHotVC () <UITableViewDataSource, UITableViewDelegate>
//
@property (nonatomic, strong) KBTopThreeView *topThreeView;
2025-12-03 18:49:18 +08:00
/// VM
@property (nonatomic, strong) KBHomeVM *homeVM;
///
@property (nonatomic, copy) NSArray<KBCharacter *> *allCharacters;
/// 使 4
@property (nonatomic, copy) NSArray<KBCharacter *> *listCharacters;
2025-11-06 14:59:00 +08:00
@end
2025-11-06 14:02:22 +08:00
@interface HomeHotVC ()
@end
@implementation HomeHotVC
- (void)viewDidLoad {
[super viewDidLoad];
2025-11-06 14:59:00 +08:00
// UI
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// tableHeaderViewtableHeaderView
CGFloat headerH = 280.0;
self.topThreeView = [[KBTopThreeView alloc] initWithFrame:CGRectMake(0, 0, KB_SCREEN_WIDTH, headerH)];
self.tableView.tableHeaderView = self.topThreeView;
2025-12-03 18:49:18 +08:00
2025-12-04 19:12:34 +08:00
KBWeakSelf
//
self.topThreeView.onCenterPlusTapped = ^(KBCharacter * _Nullable character) {
[weakSelf kb_addUserCharacterFromTopViewWithCharacter:character];
};
self.topThreeView.onLeftPlusTapped = ^(KBCharacter * _Nullable character) {
[weakSelf kb_addUserCharacterFromTopViewWithCharacter:character];
};
self.topThreeView.onRightPlusTapped = ^(KBCharacter * _Nullable character) {
[weakSelf kb_addUserCharacterFromTopViewWithCharacter:character];
};
2025-12-03 18:49:18 +08:00
//
self.homeVM = [KBHomeVM new];
2025-12-04 16:59:59 +08:00
[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 {
2025-12-03 18:49:18 +08:00
KBWeakSelf
[self.homeVM fetchRankListWithParams:nil
2025-12-04 16:59:59 +08:00
needShow:needShow
2025-12-03 18:49:18 +08:00
completion:^(NSArray<KBCharacter *> * _Nullable list, NSError * _Nullable error) {
if (error) {
// VM HUD
return;
}
weakSelf.allCharacters = list ?: @[];
2025-12-04 19:12:34 +08:00
//
[weakSelf kb_refreshTopThreeView];
[weakSelf kb_refreshListCharacters];
2025-12-03 18:49:18 +08:00
[weakSelf.tableView reloadData];
// HomeHotVC PanModal
// 1) HomeHotVC VC HomeSheetVC
// VC hw_panModalSetNeedsLayoutUpdate
UIViewController *parent = weakSelf.parentViewController;
id target = nil;
if ([parent respondsToSelector:@selector(hw_panModalSetNeedsLayoutUpdate)]) {
target = parent;
} else {
// 2) HomeHotVC HWPanModalContentView KBPanModalView
// parentViewController nil view.superview
UIView *v = weakSelf.view;
while (v && ![v respondsToSelector:@selector(hw_panModalSetNeedsLayoutUpdate)]) {
v = v.superview;
}
if (v && [v respondsToSelector:@selector(hw_panModalSetNeedsLayoutUpdate)]) {
target = v;
}
}
if (target) {
[(id)target hw_panModalSetNeedsLayoutUpdate];
}
}];
2025-11-06 14:02:22 +08:00
}
2025-12-04 16:59:59 +08:00
/// 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) {
2025-12-04 19:12:34 +08:00
if (c.ID.integerValue == targetId) {
2025-12-04 16:59:59 +08:00
contains = YES;
break;
}
}
if (!contains) { return; }
// HUD
[self kb_reloadRankListWithShowHUD:NO];
}
2025-12-04 19:12:34 +08:00
/// allCharacters
- (void)kb_refreshTopThreeView {
NSInteger topCount = MIN(3, self.allCharacters.count);
if (topCount > 0) {
NSRange range = NSMakeRange(0, topCount);
NSArray<KBCharacter *> *topThree = [self.allCharacters subarrayWithRange:range];
[self.topThreeView configWithCharacters:topThree];
} else {
[self.topThreeView configWithCharacters:@[]];
}
}
/// allCharacters 4
- (void)kb_refreshListCharacters {
if (self.allCharacters.count > 3) {
NSRange range = NSMakeRange(3, self.allCharacters.count - 3);
self.listCharacters = [self.allCharacters subarrayWithRange:range];
} else {
self.listCharacters = @[];
}
}
/// +
- (void)kb_addUserCharacterFromTopViewWithCharacter:(KBCharacter * _Nullable)character {
if (!character || character.added) { return; }
NSString *cidStr = character.ID ?: @"";
if (cidStr.length == 0) { return; }
NSNumber *cid = @([cidStr integerValue]);
[self.homeVM addUserCharacterWithId:cid
completion:^(BOOL success, NSError * _Nullable error) {
if (!success) {
NSString *msg = error.localizedDescription ?: KBLocalized(@"Network error");
[KBHUD showInfo:msg];
return;
}
//
character.added = YES;
// UI
[self kb_refreshTopThreeView];
[self.tableView reloadData];
}];
}
2025-11-06 14:59:00 +08:00
#pragma mark - UITableViewDataSource
2025-11-06 14:02:22 +08:00
2025-11-06 14:59:00 +08:00
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
2025-12-03 18:49:18 +08:00
return self.listCharacters.count;
2025-11-06 14:02:22 +08:00
}
2025-11-06 14:59:00 +08:00
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HomeHotCell *cell = [tableView dequeueReusableCellWithIdentifier:HomeHotCell.reuseId forIndexPath:indexPath];
2025-12-03 18:49:18 +08:00
KBCharacter *item = self.listCharacters[indexPath.row];
2025-12-03 18:53:15 +08:00
// cell cell
cell.character = item;
2025-12-04 19:12:34 +08:00
KBWeakSelf
__weak typeof(cell) weakCell = cell;
cell.onTapAction = ^{
__strong typeof(weakSelf) self = weakSelf;
HomeHotCell *strongCell = weakCell;
if (!self || !strongCell) { return; }
NSIndexPath *current = [self.tableView indexPathForCell:strongCell];
if (!current) { return; }
if (current.row >= self.listCharacters.count) { return; }
KBCharacter *mc = self.listCharacters[current.row];
//
if (mc.added) { return; }
NSString *cidStr = mc.ID ?: @"";
if (cidStr.length == 0) { return; }
NSNumber *cid = @([cidStr integerValue]);
[self.homeVM addUserCharacterWithId:cid
completion:^(BOOL success, NSError * _Nullable error) {
if (!success) {
NSString *msg = error.localizedDescription ?: KBLocalized(@"Network error");
[KBHUD showInfo:msg];
return;
}
//
mc.added = YES;
NSMutableArray *m = [self.listCharacters mutableCopy];
[m replaceObjectAtIndex:current.row withObject:mc];
self.listCharacters = [m copy];
[self.tableView reloadRowsAtIndexPaths:@[current] withRowAnimation:UITableViewRowAnimationNone];
//
[self kb_refreshTopThreeView];
}];
};
2025-11-06 14:59:00 +08:00
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 84.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
2025-11-17 15:39:03 +08:00
// KBSearchVC *vc = [[KBSearchVC alloc] init];
2025-11-06 16:57:28 +08:00
// [self.navigationController pushViewController:vc animated:true];
2025-11-17 15:39:03 +08:00
// UINavigationController *nav = KB_CURRENT_NAV;
2025-11-10 13:27:26 +08:00
// [nav pushViewController:vc animated:true];
2025-11-06 16:57:28 +08:00
NSLog(@"===");
2025-12-03 18:49:18 +08:00
2025-11-17 15:39:03 +08:00
// ->
2025-12-03 18:49:18 +08:00
KBCharacter *c = self.listCharacters[indexPath.row];
2025-11-17 15:39:03 +08:00
//
CGFloat width = MIN(KB_SCREEN_WIDTH - 76, 420);
HomeRankDetailPopView *content = [[HomeRankDetailPopView alloc] initWithFrame:CGRectMake(0, 0, width, 460)];
2025-12-03 18:49:18 +08:00
NSString *title = c.characterName ?: @"High EQ";
NSString *people = c.download ?: @"Download: 1 Million";
2025-11-17 15:39:03 +08:00
NSString *desc = @"Be Neither Too Close\nNor Too Distant"; //
[content configWithAvatar:nil title:title download:people desc:desc];
//
LSTPopView *pop = [LSTPopView initWithCustomView:content
parentView:nil
popStyle:LSTPopStyleScale
dismissStyle:LSTDismissStyleScale];
pop.hemStyle = LSTHemStyleCenter; //
pop.bgColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
pop.isClickBgDismiss = YES; //
pop.cornerRadius = 0; // view
__weak typeof(pop) weakPop = pop;
content.saveHandler = ^{ [weakPop dismiss]; };
content.closeHandler = ^{ [weakPop dismiss]; };
[pop pop];
2025-11-06 14:59:00 +08:00
}
#pragma mark - Lazy
- (BaseTableView *)tableView {
if (!_tableView) {
// 使 BaseTableView
_tableView = [[BaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 线
_tableView.showsVerticalScrollIndicator = NO;
_tableView.contentInset = UIEdgeInsetsMake(8, 0, KB_SafeAreaBottom(), 0);
[_tableView registerClass:HomeHotCell.class forCellReuseIdentifier:HomeHotCell.reuseId];
}
return _tableView;
}
2025-11-06 14:02:22 +08:00
@end