WFCUUserMessageListViewController.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // WFCUUserMessageListViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2020/8/19.
  6. // Copyright © 2020 wildfirechat. All rights reserved.
  7. //
  8. #import "WFCUUserMessageListViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUUtilities.h"
  11. @interface WFCUUserMessageListViewController () <UITableViewDelegate, UITableViewDataSource>
  12. @property(nonatomic, strong)UITableView *tableView;
  13. @property(nonatomic, strong)NSMutableArray<WFCCMessage *> *messages;
  14. @property(nonatomic)bool isLoading;
  15. @end
  16. @implementation WFCUUserMessageListViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.userId refresh:NO];
  20. if (userInfo.displayName.length) {
  21. self.title = [NSString stringWithFormat:@"%@ 的消息", userInfo.displayName];
  22. }
  23. self.messages = [[[WFCCIMService sharedWFCIMService] getUserMessages:self.userId conversation:self.conversation contentTypes:nil from:0 count:20] mutableCopy];
  24. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
  25. if (@available(iOS 15, *)) {
  26. self.tableView.sectionHeaderTopPadding = 0;
  27. }
  28. self.tableView.delegate = self;
  29. self.tableView.dataSource = self;
  30. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  31. [self.view addSubview:self.tableView];
  32. [self.tableView reloadData];
  33. }
  34. - (void)loadMore {
  35. if (!self.messages.count) {
  36. return;
  37. }
  38. NSArray *moreMsg = [[WFCCIMService sharedWFCIMService] getUserMessages:self.userId conversation:self.conversation contentTypes:nil from:[self.messages lastObject].messageId count:20];
  39. [self.messages addObjectsFromArray:moreMsg];
  40. [self.tableView reloadData];
  41. self.isLoading = NO;
  42. }
  43. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  44. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  45. if (!cell) {
  46. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  47. }
  48. WFCCMessage *msg = self.messages[indexPath.row];
  49. cell.textLabel.text = [msg.content digest:msg];
  50. cell.detailTextLabel.text = [WFCUUtilities formatTimeDetailLabel:msg.serverTime];
  51. return cell;
  52. }
  53. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  54. return self.messages.count;
  55. }
  56. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  57. CGPoint offset = scrollView.contentOffset;
  58. CGRect bounds = scrollView.bounds;
  59. CGSize size = scrollView.contentSize;
  60. UIEdgeInsets inset = scrollView.contentInset;
  61. CGFloat scrollViewHeight = bounds.size.height;
  62. CGFloat currentOffset = offset.y + scrollViewHeight - inset.bottom;
  63. CGFloat maximumOffset = size.height;
  64. CGFloat minSpace = 5;
  65. CGFloat maxSpace = 10;
  66. bool isNeedLoadMore = false;
  67. //上拉加载更多
  68. //tableview 的 content的高度 小于 tableview的高度
  69. if(scrollViewHeight < maximumOffset){
  70. CGFloat space = maximumOffset - currentOffset;
  71. if(space < minSpace){
  72. isNeedLoadMore = true;
  73. }
  74. }
  75. if(!self.isLoading && isNeedLoadMore){
  76. self.isLoading = true;
  77. NSLog(@"-->加载更多数据");
  78. [self loadMore];
  79. }
  80. }
  81. @end