JXCommentViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //
  2. // JXCommentViewController.m
  3. // AICity
  4. //
  5. // Created by TogetherWatch on 2025-10-20.
  6. //
  7. #import "JXCommentViewController.h"
  8. #import "JXAPIService.h"
  9. #import "JXCommentContent.h"
  10. #import "CommentItemCell.h"
  11. #import "CommentRpItemCell.h"
  12. #import "CommentFooterView.h"
  13. // Cell复用标识符
  14. static NSString * const kCommentCellIdentifier = @"JXCommentCell";
  15. @interface JXCommentViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
  16. #pragma mark - UI组件
  17. @property (nonatomic, strong) UIView *headerView;
  18. @property (nonatomic, strong) UILabel *titleLabel;
  19. @property (nonatomic, strong) UIButton *closeButton;
  20. @property (nonatomic, strong) UITableView *tableView;
  21. @property (nonatomic, strong) UIRefreshControl *refreshControl;
  22. @property (nonatomic, strong) UIView *inputContainerView;
  23. @property (nonatomic, strong) UITextField *inputTextField;
  24. @property (nonatomic, strong) UIButton *submitButton;
  25. #pragma mark - 数据
  26. @property (nonatomic, copy) NSString *dramaId;
  27. @property (nonatomic, strong) NSMutableArray<JXCommentContent *> *commentList;
  28. @property (nonatomic, assign) NSInteger currentPage;
  29. @property (nonatomic, assign) BOOL isLoading;
  30. @property (nonatomic, assign) BOOL hasMoreData;
  31. @property (strong, nonatomic) UIView *emptyView;
  32. // 回复相关
  33. @property (nonatomic, assign) long long replyToCommentId;
  34. @property (nonatomic, copy) NSString *replyToUserName;
  35. @end
  36. @implementation JXCommentViewController
  37. #pragma mark - 初始化
  38. - (instancetype)initWithDramaId:(NSString *)dramaId {
  39. self = [super init];
  40. if (self) {
  41. _dramaId = dramaId;
  42. _currentPage = 1;
  43. _hasMoreData = YES;
  44. _commentList = [NSMutableArray array];
  45. _replyToCommentId = 0;
  46. }
  47. return self;
  48. }
  49. #pragma mark - 生命周期
  50. - (void)viewDidLoad {
  51. [super viewDidLoad];
  52. self.view.backgroundColor = [UIColor whiteColor];
  53. [self setupUI];
  54. [self loadComments];
  55. // 监听键盘
  56. [[NSNotificationCenter defaultCenter] addObserver:self
  57. selector:@selector(keyboardWillShow:)
  58. name:UIKeyboardWillShowNotification
  59. object:nil];
  60. [[NSNotificationCenter defaultCenter] addObserver:self
  61. selector:@selector(keyboardWillHide:)
  62. name:UIKeyboardWillHideNotification
  63. object:nil];
  64. }
  65. - (void)dealloc {
  66. [[NSNotificationCenter defaultCenter] removeObserver:self];
  67. }
  68. #pragma mark - UI设置
  69. - (void)setupUI {
  70. // 顶部标题栏
  71. [self setupHeader];
  72. // 评论列表
  73. [self setupTableView];
  74. // 底部输入框
  75. [self setupInputContainer];
  76. }
  77. - (void)setupHeader {
  78. self.headerView = [[UIView alloc] init];
  79. self.headerView.backgroundColor = [UIColor whiteColor];
  80. [self.view addSubview:self.headerView];
  81. // 标题
  82. self.titleLabel = [[UILabel alloc] init];
  83. self.titleLabel.text = @"全部评论";
  84. self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
  85. self.titleLabel.textColor = [UIColor colorWithRed:1/255.0 green:1/255.0 blue:1/255.0 alpha:1];
  86. [self.headerView addSubview:self.titleLabel];
  87. // 关闭按钮
  88. self.closeButton = [UIButton buttonWithType:UIButtonTypeSystem];
  89. [self.closeButton setImage:[UIImage imageNamed:@"关闭-1"] forState:UIControlStateNormal];
  90. self.closeButton.tintColor = [UIColor grayColor];
  91. [self.closeButton addTarget:self action:@selector(handleClose) forControlEvents:UIControlEventTouchUpInside];
  92. [self.headerView addSubview:self.closeButton];
  93. // 约束
  94. self.headerView.translatesAutoresizingMaskIntoConstraints = NO;
  95. self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  96. self.closeButton.translatesAutoresizingMaskIntoConstraints = NO;
  97. [NSLayoutConstraint activateConstraints:@[
  98. [self.headerView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
  99. [self.headerView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  100. [self.headerView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  101. [self.headerView.heightAnchor constraintEqualToConstant:50],
  102. [self.titleLabel.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],
  103. [self.titleLabel.leadingAnchor constraintEqualToAnchor:self.headerView.leadingAnchor constant:16],
  104. [self.titleLabel.heightAnchor constraintEqualToConstant:50],
  105. [self.titleLabel.widthAnchor constraintEqualToConstant:200],
  106. [self.closeButton.trailingAnchor constraintEqualToAnchor:self.headerView.trailingAnchor constant:-8],
  107. [self.closeButton.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],
  108. [self.closeButton.widthAnchor constraintEqualToConstant:50],
  109. [self.closeButton.heightAnchor constraintEqualToConstant:50]
  110. ]];
  111. }
  112. - (void)setupTableView {
  113. self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  114. self.tableView.delegate = self;
  115. self.tableView.dataSource = self;
  116. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  117. self.tableView.backgroundColor = [UIColor whiteColor];
  118. [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass(CommentItemCell.class) bundle:nil] forCellReuseIdentifier:NSStringFromClass(CommentItemCell.class)];
  119. [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass(CommentRpItemCell.class) bundle:nil] forCellReuseIdentifier:NSStringFromClass(CommentRpItemCell.class)];
  120. [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass(CommentFooterView.class) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass(CommentFooterView.class)];
  121. [self.view addSubview:self.tableView];
  122. // 下拉刷新
  123. self.refreshControl = [[UIRefreshControl alloc] init];
  124. [self.refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  125. [self.tableView addSubview:self.refreshControl];
  126. // 约束
  127. self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
  128. [NSLayoutConstraint activateConstraints:@[
  129. [self.tableView.topAnchor constraintEqualToAnchor:self.headerView.bottomAnchor],
  130. [self.tableView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  131. [self.tableView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  132. [self.tableView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:-70]
  133. ]];
  134. }
  135. - (void)setupInputContainer {
  136. self.inputContainerView = [[UIView alloc] init];
  137. self.inputContainerView.backgroundColor = UIColor.whiteColor;
  138. [self.view addSubview:self.inputContainerView];
  139. // 输入框
  140. self.inputTextField = [[UITextField alloc] init];
  141. self.inputTextField.placeholder = @"说点什么...";
  142. self.inputTextField.backgroundColor = RGBACOLOR(241, 242, 244, 1);
  143. self.inputTextField.layer.cornerRadius = 20;
  144. self.inputTextField.returnKeyType = UIReturnKeySend;
  145. self.inputTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 0)];
  146. self.inputTextField.leftViewMode = UITextFieldViewModeAlways;
  147. self.inputTextField.delegate = self;
  148. [self.inputTextField addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
  149. [self.inputContainerView addSubview:self.inputTextField];
  150. // 约束
  151. self.inputContainerView.translatesAutoresizingMaskIntoConstraints = NO;
  152. self.inputTextField.translatesAutoresizingMaskIntoConstraints = NO;
  153. [NSLayoutConstraint activateConstraints:@[
  154. [self.inputContainerView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  155. [self.inputContainerView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  156. [self.inputContainerView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:-safebottom],
  157. [self.inputContainerView.heightAnchor constraintEqualToConstant:50],
  158. [self.inputTextField.leadingAnchor constraintEqualToAnchor:self.inputContainerView.leadingAnchor constant:16],
  159. [self.inputTextField.rightAnchor constraintEqualToAnchor:self.inputContainerView.rightAnchor constant:-16],
  160. [self.inputTextField.centerYAnchor constraintEqualToAnchor:self.inputContainerView.centerYAnchor],
  161. [self.inputTextField.heightAnchor constraintEqualToConstant:40],
  162. ]];
  163. }
  164. #pragma mark - 数据加载
  165. - (void)loadComments {
  166. if (self.isLoading) {
  167. return;
  168. }
  169. self.isLoading = YES;
  170. [[JXAPIService sharedService] getCommentsWithDramaId:self.dramaId
  171. page:self.currentPage
  172. pageSize:20
  173. success:^(id response) {
  174. self.isLoading = NO;
  175. [self.refreshControl endRefreshing];
  176. NSDictionary *data = response[@"data"];
  177. NSArray *list = data[@"list"];
  178. // long long totalCount = [data[@"total"] longLongValue];
  179. // 更新标题
  180. self.titleLabel.text = [NSString stringWithFormat:@"全部评论"];
  181. if (list.count == 0) {
  182. self.hasMoreData = NO;
  183. return;
  184. }
  185. // 转换为模型
  186. NSMutableArray *models = [NSMutableArray array];
  187. for (NSDictionary *dict in list) {
  188. JXCommentContent *comment = [[JXCommentContent alloc] initWithDictionary:dict];
  189. [models addObject:comment];
  190. }
  191. if (self.currentPage == 1) {
  192. self.commentList = models;
  193. } else {
  194. [self.commentList addObjectsFromArray:models];
  195. }
  196. self.emptyView.hidden = NO;
  197. [self.tableView reloadData];
  198. } failure:^(NSError *error) {
  199. self.isLoading = NO;
  200. [self.refreshControl endRefreshing];
  201. NSLog(@"[JXComment] 加载评论失败: %@", error.localizedDescription);
  202. }];
  203. }
  204. - (void)handleRefresh {
  205. self.currentPage = 1;
  206. self.hasMoreData = YES;
  207. [self loadComments];
  208. }
  209. - (void)loadMoreComments {
  210. if (!self.hasMoreData || self.isLoading) {
  211. return;
  212. }
  213. self.currentPage++;
  214. [self loadComments];
  215. }
  216. #pragma mark - UITableViewDataSource
  217. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  218. return self.commentList.count;
  219. }
  220. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  221. JXCommentContent *comment = self.commentList[section];
  222. return comment.isOpen ? comment.reply_count + 1 : 1;
  223. }
  224. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  225. if (indexPath.row == 0) {
  226. CommentItemCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(CommentItemCell.class) forIndexPath:indexPath];
  227. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  228. JXCommentContent *comment = self.commentList[indexPath.section];
  229. cell.labelT.text = comment.userName.length ? comment.userName : @"用户001";
  230. cell.labelC.text = comment.content;
  231. cell.labelL.text = [NSString stringWithFormat:@"%lld",comment.likeCount];
  232. if (comment.isLiked) {
  233. cell.imgL.image = [UIImage imageNamed:@"icon-2.2(1)"];
  234. }else{
  235. cell.imgL.image = [UIImage imageNamed:@"icon-2.2 1"];
  236. }
  237. cell.likeClicked = ^{
  238. [self handleLikeComment:comment];
  239. };
  240. cell.huifuClicked = ^{
  241. [self handleReplyComment:comment];
  242. };
  243. return cell;
  244. }
  245. CommentRpItemCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(CommentRpItemCell.class) forIndexPath:indexPath];
  246. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  247. // JXCommentContent *comment = self.commentList[indexPath.row];
  248. //
  249. // // 临时简单展示(后续可创建自定义Cell)
  250. // cell.textLabel.numberOfLines = 0;
  251. // NSString *likeText = comment.isLiked ? @"❤️" : @"🤍";
  252. // cell.textLabel.text = [NSString stringWithFormat:@"%@ · %@\n%@\n%@ %lld 💬 %lld",
  253. // comment.userName,
  254. // [comment formattedTime],
  255. // comment.content,
  256. // likeText,
  257. // comment.likeCount,
  258. // comment.replyCount];
  259. return cell;
  260. }
  261. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  262. CommentFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(CommentFooterView.class)];
  263. JXCommentContent *comment = self.commentList[section];
  264. header.btnOpen.selected = comment.isOpen;
  265. header.btnClicked = ^{
  266. comment.isOpen = !comment.isOpen;
  267. [self.tableView reloadData];
  268. };
  269. return header;
  270. }
  271. #pragma mark - UITableViewDelegate
  272. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  273. JXCommentContent *comment = self.commentList[section];
  274. return comment.reply_count > 0 ? 0 : 40;
  275. }
  276. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  277. return UITableViewAutomaticDimension;
  278. }
  279. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  280. return 80;
  281. }
  282. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  283. // [tableView deselectRowAtIndexPath:indexPath animated:YES];
  284. return;
  285. if (indexPath.row == 0) {
  286. JXCommentContent *comment = self.commentList[indexPath.row];
  287. // 显示操作菜单(点赞、回复)
  288. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
  289. message:nil
  290. preferredStyle:UIAlertControllerStyleActionSheet];
  291. // 点赞
  292. NSString *likeTitle = comment.isLiked ? @"取消点赞" : @"点赞";
  293. [alert addAction:[UIAlertAction actionWithTitle:likeTitle
  294. style:UIAlertActionStyleDefault
  295. handler:^(UIAlertAction *action) {
  296. [self handleLikeComment:comment];
  297. }]];
  298. // 回复
  299. [alert addAction:[UIAlertAction actionWithTitle:@"回复"
  300. style:UIAlertActionStyleDefault
  301. handler:^(UIAlertAction *action) {
  302. [self handleReplyComment:comment];
  303. }]];
  304. [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
  305. [self presentViewController:alert animated:YES completion:nil];
  306. }
  307. }
  308. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  309. // 预加载
  310. CGFloat offsetY = scrollView.contentOffset.y;
  311. CGFloat contentHeight = scrollView.contentSize.height;
  312. CGFloat scrollViewHeight = scrollView.bounds.size.height;
  313. if (offsetY + scrollViewHeight >= contentHeight - 200) {
  314. // [self loadMoreComments];
  315. }
  316. }
  317. #pragma mark - 评论交互
  318. - (void)handleLikeComment:(JXCommentContent *)comment {
  319. [[JXAPIService sharedService] likeCommentWithCommentId:comment.commentId
  320. success:^(id response) {
  321. // 更新UI
  322. BOOL isLiked = [response[@"data"][@"isLiked"] boolValue];
  323. long long likeCount = [response[@"data"][@"likeCount"] longLongValue];
  324. comment.isLiked = isLiked;
  325. comment.likeCount = likeCount;
  326. [self.tableView reloadData];
  327. } failure:^(NSError *error) {
  328. NSLog(@"[JXComment] 点赞评论失败: %@", error.localizedDescription);
  329. }];
  330. }
  331. - (void)handleReplyComment:(JXCommentContent *)comment {
  332. self.replyToCommentId = comment.commentId;
  333. self.replyToUserName = comment.userName;
  334. self.inputTextField.placeholder = [NSString stringWithFormat:@"回复 @%@:", comment.userName];
  335. [self.inputTextField becomeFirstResponder];
  336. }
  337. - (void)textFieldDidChange {
  338. NSString *text = self.inputTextField.text;
  339. self.submitButton.enabled = (text.length > 0);
  340. }
  341. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  342. if (textField.text.length == 0) {
  343. [MBProgressHUD showMessage:@"请输入评论"];
  344. return NO;
  345. }
  346. [self handleSubmit];
  347. return YES;
  348. }
  349. - (void)handleSubmit {
  350. NSString *content = self.inputTextField.text;
  351. if (content.length == 0) {
  352. return;
  353. }
  354. if (self.replyToCommentId > 0) {
  355. // 回复评论
  356. [[JXAPIService sharedService] replyCommentWithCommentId:self.replyToCommentId
  357. content:content
  358. success:^(id response) {
  359. NSLog(@"[JXComment] 回复成功");
  360. [self resetInput];
  361. [self handleRefresh];
  362. } failure:^(NSError *error) {
  363. NSLog(@"[JXComment] 回复失败: %@", error.localizedDescription);
  364. }];
  365. } else {
  366. // 提交新评论
  367. [[JXAPIService sharedService] submitCommentWithDramaId:self.dramaId
  368. content:content
  369. success:^(id response) {
  370. NSLog(@"[JXComment] 评论提交成功");
  371. [self resetInput];
  372. [self handleRefresh];
  373. } failure:^(NSError *error) {
  374. NSLog(@"[JXComment] 评论提交失败: %@", error.localizedDescription);
  375. }];
  376. }
  377. }
  378. - (void)resetInput {
  379. self.inputTextField.text = @"";
  380. self.inputTextField.placeholder = @"说点什么...";
  381. self.submitButton.enabled = NO;
  382. self.replyToCommentId = 0;
  383. self.replyToUserName = nil;
  384. [self.inputTextField resignFirstResponder];
  385. }
  386. #pragma mark - 键盘处理
  387. - (void)keyboardWillShow:(NSNotification *)notification {
  388. NSDictionary *userInfo = notification.userInfo;
  389. CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  390. CGFloat keyboardHeight = keyboardFrame.size.height;
  391. [UIView animateWithDuration:0.3 animations:^{
  392. self.inputContainerView.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);
  393. self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
  394. }];
  395. }
  396. - (void)keyboardWillHide:(NSNotification *)notification {
  397. [UIView animateWithDuration:0.3 animations:^{
  398. self.inputContainerView.transform = CGAffineTransformIdentity;
  399. self.tableView.contentInset = UIEdgeInsetsZero;
  400. }];
  401. }
  402. #pragma mark - 关闭
  403. - (void)handleClose {
  404. [self dismissViewControllerAnimated:YES completion:nil];
  405. }
  406. @end