XYRoomChatMessageViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // XYRoomChatMessageViewController.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/9/23.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "XYRoomChatMessageViewController.h"
  9. #import "XYMessageMainViewModel.h"
  10. #import "XYMessageMainTableViewCell.h"
  11. #import "XYChatRoomDispatchCommitHallListView.h"
  12. #import "XYUserDynamicListViewController.h"
  13. #import "XYSystemMessageViewController.h"
  14. #import "XYUnReadBadgeView.h"
  15. @interface XYRoomChatMessageViewController ()<UITableViewDelegate, UITableViewDataSource>
  16. @property (nonatomic, strong) UIView *bgView;
  17. @property (nonatomic, strong) UIView *infoView;
  18. @property (nonatomic, strong) UIView *titleView;
  19. @property (nonatomic, strong) UILabel *titleLabel;
  20. @property (nonatomic, strong) UIButton *systemButton;
  21. @property (nonatomic, strong) XYUnReadBadgeView *badgeLabel;
  22. @property (nonatomic, strong) UITableView *tableView;
  23. /**
  24. * 消息列表的视图模型
  25. * 视图模型能够协助消息列表界面实现数据的加载、移除、过滤等多种功能。替界面分摊部分的业务逻辑运算。
  26. */
  27. @property (nonatomic, strong) XYMessageMainViewModel *viewModel;
  28. @end
  29. @implementation XYRoomChatMessageViewController
  30. - (void)dealloc {
  31. [self clear];
  32. }
  33. #pragma mark — Public
  34. - (void)clear {
  35. [self.view removeFromSuperview];
  36. [self removeFromParentViewController];
  37. [NotificationCenter removeObserver:self];
  38. }
  39. - (void)show {
  40. self.view.hidden = NO;
  41. [self.view.superview bringSubviewToFront:self.view];
  42. [UIView animateWithDuration:.3 animations:^{
  43. self.infoView.frame = CGRectMake(self.infoView.f_x, STATUS_HEIGHT+340.0f, self.infoView.f_width, self.infoView.f_heigh);
  44. } completion:^(BOOL finished) {
  45. [self.viewModel loadConversation];
  46. }];
  47. }
  48. - (void)hide {
  49. [UIView animateWithDuration:.3 animations:^{
  50. self.infoView.frame = CGRectMake(self.infoView.f_x, SCREEN_HEIGHT, self.infoView.f_width, self.infoView.f_heigh);
  51. } completion:^(BOOL finished) {
  52. self.view.hidden = YES;
  53. }];
  54. }
  55. #pragma mark — UITableViewDelegate, UITableViewDataSource
  56. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  57. XYMessageMainTableViewCell *cell = [XYMessageMainTableViewCell cellWithTableView:tableView];
  58. [cell configureModel:self.viewModel.dataSource[indexPath.row]];
  59. return cell;
  60. }
  61. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  62. return self.viewModel.dataSource.count;
  63. }
  64. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  65. return 64.0f;
  66. }
  67. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  68. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  69. XYMessageMainCellModel *model = self.viewModel.dataSource[indexPath.row];
  70. switch (model.messageType) {
  71. case Message_List_Type_Dispatch_Order:
  72. {
  73. // 消息Tab,点[系统消息]的次数
  74. [StatisticsManager event:@"news_systemmessage_click"];
  75. XYChatRoomDispatchCommitHallListView *controller = XYChatRoomDispatchCommitHallListView.new;
  76. [self.navigationController pushViewController:controller animated:YES];
  77. }
  78. break;
  79. case Message_List_Type_Dynamic:
  80. {
  81. XYUserDynamicListViewController *controller = XYUserDynamicListViewController.new;
  82. controller.isCloseScroll = YES;
  83. [self.navigationController pushViewController:controller animated:YES];
  84. }
  85. break;
  86. case Message_List_Type_Chat:
  87. {
  88. [self.navigationController pushViewController:[XYNewChatViewController createPrivateChatControllerWithConvId:model.convId] animated:YES];
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. #pragma mark — begin-------左滑删除-------
  96. // 判断是否可以编辑
  97. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  98. if ((self.viewModel.fixedList.count-1) < indexPath.row) {
  99. // 固定cell不能编辑
  100. return YES;
  101. }
  102. return NO;
  103. }
  104. // 定义编辑样式
  105. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  106. return UITableViewCellEditingStyleDelete;
  107. }
  108. // 进入编辑模式,按下出现的编辑按钮后,进行删除操作
  109. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  110. if (editingStyle == UITableViewCellEditingStyleDelete) {
  111. // 进行删除数据源的操作
  112. XYMessageMainCellModel *conv = self.viewModel.dataSource[indexPath.row];
  113. [self.viewModel removeData:conv];
  114. [tableView beginUpdates];
  115. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
  116. [tableView endUpdates];
  117. }
  118. }
  119. // 修改编辑按钮的文字
  120. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
  121. return kLocalizedString(@"删除");
  122. }
  123. #pragma mark — end-------左滑删除-------
  124. #pragma mark — Action
  125. - (void)systemButtonAction:(id)sender {
  126. XYSystemMessageViewController *controller = XYSystemMessageViewController.new;
  127. controller.isShowNavigation = YES;
  128. [self.navigationController pushViewController:controller animated:YES];
  129. }
  130. - (instancetype)init
  131. {
  132. self = [super init];
  133. if (self) {
  134. self.keyboardEnable = NO;
  135. self.shouldAutoToolbarEnable = NO;
  136. }
  137. return self;
  138. }
  139. #pragma mark — Override
  140. - (void)bindViewModel {
  141. self.viewModel = [XYMessageMainViewModel new];
  142. }
  143. - (void)viewDidLoad {
  144. [super viewDidLoad];
  145. [self setupUI];
  146. /// 登录状态改变
  147. [NotificationCenter addObserver:self selector:@selector(updateUserStatusNotifition:) name:USER_LOGIN_STATUS_NOTIFICATION object:nil];
  148. // 收到系统消息
  149. [[NSNotificationCenter defaultCenter] addObserver:self
  150. selector:@selector(onRefreshNotification:) name:RECEIVED_SYSTEM_MESSAGE_NOTIFICATION
  151. object:nil];
  152. // 消息阅读状态改变通知
  153. [[NSNotificationCenter defaultCenter] addObserver:self
  154. selector:@selector(onRefreshNotification:) name:MODIFY_MESSAGE_READ_STATUS_NOTIFICATION
  155. object:nil];
  156. @weakify(self)
  157. [self.viewModel setUpdateList:^{
  158. @strongify(self)
  159. [self.tableView reloadData];
  160. // 更新红点
  161. [self onRefreshNotification:nil];
  162. }];
  163. }
  164. - (void)updateUserStatusNotifition:(NSNotification *)info {
  165. // 先清空会话列表
  166. self.viewModel.dataList = [NSArray array];
  167. [self.viewModel loadConversation];
  168. // 更新红点
  169. [self onRefreshNotification:nil];
  170. }
  171. - (void)onRefreshNotification:(NSNotification *)info {
  172. NSInteger systemUnreadCount = [DataBase getSystemMessageUnreadNum];
  173. CGFloat badgeWidth = [self.badgeLabel setNum:systemUnreadCount];
  174. self.badgeLabel.frame = CGRectMake(self.titleView.f_width - 10.0f - badgeWidth, self.badgeLabel.f_y, badgeWidth, self.badgeLabel.f_heigh);
  175. }
  176. - (void)setupUI {
  177. self.view.backgroundColor = Color_Clear;
  178. [self.view addSubview:self.bgView];
  179. [self.view addSubview:self.infoView];
  180. [self.infoView addSubview:self.titleView];
  181. [self.titleView addSubview:self.titleLabel];
  182. [self.titleView addSubview:self.systemButton];
  183. [self.titleView addSubview:self.badgeLabel];
  184. [self.infoView addSubview:self.tableView];
  185. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  186. make.left.equalTo(self.titleView).offset(44.0f);
  187. make.right.equalTo(self.titleView).offset(-44.0f);
  188. make.centerY.equalTo(self.titleView);
  189. }];
  190. [self.badgeLabel addViewBorder:Color_Clear redian:12/2];
  191. }
  192. #pragma mark — Getter
  193. - (UIView *)bgView {
  194. if (!_bgView) {
  195. _bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  196. _bgView.backgroundColor = Color_Clear;
  197. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
  198. _bgView.userInteractionEnabled = YES;
  199. [_bgView addGestureRecognizer:tap];
  200. }
  201. return _bgView;
  202. }
  203. - (UIView *)infoView {
  204. if (!_infoView) {
  205. _infoView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-340.0f-STATUS_HEIGHT)];
  206. _infoView.backgroundColor = Color_White;
  207. [_infoView addViewBorderWithCorners:UIRectCornerTopLeft|UIRectCornerTopRight color:Color_Clear redian:8];
  208. // 添加下滑手势
  209. UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
  210. [downSwipe setDirection:UISwipeGestureRecognizerDirectionDown];
  211. _infoView.userInteractionEnabled = YES;
  212. [_infoView addGestureRecognizer:downSwipe];
  213. }
  214. return _infoView;
  215. }
  216. - (UIView *)titleView {
  217. if (!_titleView) {
  218. _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.infoView.f_width, 44.0f)];
  219. _titleView.backgroundColor = Color_Clear;
  220. }
  221. return _titleView;
  222. }
  223. - (UILabel *)titleLabel {
  224. if (!_titleLabel) {
  225. _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font_B(20)];
  226. _titleLabel.text = kLocalizedString(@"消息");
  227. _titleLabel.textAlignment = NSTextAlignmentCenter;
  228. }
  229. return _titleLabel;
  230. }
  231. - (UITableView *)tableView {
  232. if (!_tableView) {
  233. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.titleView.f_top, self.infoView.f_width, self.infoView.f_heigh - self.titleView.f_top) style:UITableViewStylePlain];
  234. _tableView.delegate = self;
  235. _tableView.dataSource = self;
  236. _tableView.backgroundColor = Color_Clear;
  237. _tableView.tableFooterView = [UIView new];
  238. }
  239. return _tableView;
  240. }
  241. - (UIButton *)systemButton {
  242. if (_systemButton == nil) {
  243. _systemButton = [UIButton createButtonTextColor:Color_TextFont textFont:Font(14)];
  244. _systemButton.frame = CGRectMake(self.titleView.f_width - 5.0f - 70.0f, 0, 65.0f, 44.0f);
  245. [_systemButton setTitle:@"系统消息" forState:UIControlStateNormal];
  246. [_systemButton addTarget:self action:@selector(systemButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  247. }
  248. return _systemButton;
  249. }
  250. - (XYUnReadBadgeView *)badgeLabel {
  251. if (!_badgeLabel) {
  252. _badgeLabel = [[XYUnReadBadgeView alloc] initWithFrame:CGRectMake(self.titleView.f_width - 10.0f - 8.0f, 10.0f, 12.0f, 12.0f)];
  253. _badgeLabel.unReadLabel.font = Font_B(8);
  254. _badgeLabel.unReadLabel.textColor = Color_White;
  255. _badgeLabel.viewHeight = 12.0f;
  256. }
  257. return _badgeLabel;
  258. }
  259. @end