123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- //
- // XYRoomChatMessageViewController.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/9/23.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "XYRoomChatMessageViewController.h"
- #import "XYMessageMainViewModel.h"
- #import "XYMessageMainTableViewCell.h"
- #import "XYChatRoomDispatchCommitHallListView.h"
- #import "XYUserDynamicListViewController.h"
- #import "XYSystemMessageViewController.h"
- #import "XYUnReadBadgeView.h"
- @interface XYRoomChatMessageViewController ()<UITableViewDelegate, UITableViewDataSource>
- @property (nonatomic, strong) UIView *bgView;
- @property (nonatomic, strong) UIView *infoView;
- @property (nonatomic, strong) UIView *titleView;
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UIButton *systemButton;
- @property (nonatomic, strong) XYUnReadBadgeView *badgeLabel;
- @property (nonatomic, strong) UITableView *tableView;
- /**
- * 消息列表的视图模型
- * 视图模型能够协助消息列表界面实现数据的加载、移除、过滤等多种功能。替界面分摊部分的业务逻辑运算。
- */
- @property (nonatomic, strong) XYMessageMainViewModel *viewModel;
- @end
- @implementation XYRoomChatMessageViewController
- - (void)dealloc {
- [self clear];
- }
- #pragma mark — Public
- - (void)clear {
- [self.view removeFromSuperview];
- [self removeFromParentViewController];
- [NotificationCenter removeObserver:self];
- }
- - (void)show {
- self.view.hidden = NO;
- [self.view.superview bringSubviewToFront:self.view];
- [UIView animateWithDuration:.3 animations:^{
- self.infoView.frame = CGRectMake(self.infoView.f_x, STATUS_HEIGHT+340.0f, self.infoView.f_width, self.infoView.f_heigh);
- } completion:^(BOOL finished) {
- [self.viewModel loadConversation];
- }];
- }
- - (void)hide {
- [UIView animateWithDuration:.3 animations:^{
- self.infoView.frame = CGRectMake(self.infoView.f_x, SCREEN_HEIGHT, self.infoView.f_width, self.infoView.f_heigh);
- } completion:^(BOOL finished) {
- self.view.hidden = YES;
- }];
- }
- #pragma mark — UITableViewDelegate, UITableViewDataSource
- - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
- XYMessageMainTableViewCell *cell = [XYMessageMainTableViewCell cellWithTableView:tableView];
- [cell configureModel:self.viewModel.dataSource[indexPath.row]];
- return cell;
- }
- - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.viewModel.dataSource.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return 64.0f;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- XYMessageMainCellModel *model = self.viewModel.dataSource[indexPath.row];
- switch (model.messageType) {
- case Message_List_Type_Dispatch_Order:
- {
- // 消息Tab,点[系统消息]的次数
- [StatisticsManager event:@"news_systemmessage_click"];
-
- XYChatRoomDispatchCommitHallListView *controller = XYChatRoomDispatchCommitHallListView.new;
- [self.navigationController pushViewController:controller animated:YES];
- }
- break;
- case Message_List_Type_Dynamic:
- {
- XYUserDynamicListViewController *controller = XYUserDynamicListViewController.new;
- controller.isCloseScroll = YES;
- [self.navigationController pushViewController:controller animated:YES];
- }
- break;
- case Message_List_Type_Chat:
- {
- [self.navigationController pushViewController:[XYNewChatViewController createPrivateChatControllerWithConvId:model.convId] animated:YES];
- }
- break;
- default:
- break;
- }
- }
- #pragma mark — begin-------左滑删除-------
- // 判断是否可以编辑
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- if ((self.viewModel.fixedList.count-1) < indexPath.row) {
- // 固定cell不能编辑
- return YES;
- }
- return NO;
- }
- // 定义编辑样式
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
- return UITableViewCellEditingStyleDelete;
- }
- // 进入编辑模式,按下出现的编辑按钮后,进行删除操作
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- // 进行删除数据源的操作
- XYMessageMainCellModel *conv = self.viewModel.dataSource[indexPath.row];
- [self.viewModel removeData:conv];
- [tableView beginUpdates];
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
- [tableView endUpdates];
- }
- }
- // 修改编辑按钮的文字
- - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
- return kLocalizedString(@"删除");
- }
- #pragma mark — end-------左滑删除-------
- #pragma mark — Action
- - (void)systemButtonAction:(id)sender {
- XYSystemMessageViewController *controller = XYSystemMessageViewController.new;
- controller.isShowNavigation = YES;
- [self.navigationController pushViewController:controller animated:YES];
- }
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- self.keyboardEnable = NO;
- self.shouldAutoToolbarEnable = NO;
- }
- return self;
- }
- #pragma mark — Override
- - (void)bindViewModel {
- self.viewModel = [XYMessageMainViewModel new];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self setupUI];
-
- /// 登录状态改变
- [NotificationCenter addObserver:self selector:@selector(updateUserStatusNotifition:) name:USER_LOGIN_STATUS_NOTIFICATION object:nil];
- // 收到系统消息
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(onRefreshNotification:) name:RECEIVED_SYSTEM_MESSAGE_NOTIFICATION
- object:nil];
- // 消息阅读状态改变通知
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(onRefreshNotification:) name:MODIFY_MESSAGE_READ_STATUS_NOTIFICATION
- object:nil];
- @weakify(self)
- [self.viewModel setUpdateList:^{
- @strongify(self)
- [self.tableView reloadData];
-
- // 更新红点
- [self onRefreshNotification:nil];
- }];
- }
- - (void)updateUserStatusNotifition:(NSNotification *)info {
- // 先清空会话列表
- self.viewModel.dataList = [NSArray array];
- [self.viewModel loadConversation];
-
- // 更新红点
- [self onRefreshNotification:nil];
- }
- - (void)onRefreshNotification:(NSNotification *)info {
- NSInteger systemUnreadCount = [DataBase getSystemMessageUnreadNum];
- CGFloat badgeWidth = [self.badgeLabel setNum:systemUnreadCount];
- self.badgeLabel.frame = CGRectMake(self.titleView.f_width - 10.0f - badgeWidth, self.badgeLabel.f_y, badgeWidth, self.badgeLabel.f_heigh);
- }
- - (void)setupUI {
-
- self.view.backgroundColor = Color_Clear;
-
- [self.view addSubview:self.bgView];
- [self.view addSubview:self.infoView];
-
- [self.infoView addSubview:self.titleView];
- [self.titleView addSubview:self.titleLabel];
- [self.titleView addSubview:self.systemButton];
- [self.titleView addSubview:self.badgeLabel];
-
- [self.infoView addSubview:self.tableView];
-
- [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.titleView).offset(44.0f);
- make.right.equalTo(self.titleView).offset(-44.0f);
- make.centerY.equalTo(self.titleView);
- }];
-
- [self.badgeLabel addViewBorder:Color_Clear redian:12/2];
- }
- #pragma mark — Getter
- - (UIView *)bgView {
- if (!_bgView) {
- _bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
- _bgView.backgroundColor = Color_Clear;
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
- _bgView.userInteractionEnabled = YES;
- [_bgView addGestureRecognizer:tap];
- }
- return _bgView;
- }
- - (UIView *)infoView {
- if (!_infoView) {
- _infoView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-340.0f-STATUS_HEIGHT)];
- _infoView.backgroundColor = Color_White;
- [_infoView addViewBorderWithCorners:UIRectCornerTopLeft|UIRectCornerTopRight color:Color_Clear redian:8];
- // 添加下滑手势
- UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
- [downSwipe setDirection:UISwipeGestureRecognizerDirectionDown];
- _infoView.userInteractionEnabled = YES;
- [_infoView addGestureRecognizer:downSwipe];
- }
- return _infoView;
- }
- - (UIView *)titleView {
- if (!_titleView) {
- _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.infoView.f_width, 44.0f)];
- _titleView.backgroundColor = Color_Clear;
- }
- return _titleView;
- }
- - (UILabel *)titleLabel {
- if (!_titleLabel) {
- _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font_B(20)];
- _titleLabel.text = kLocalizedString(@"消息");
- _titleLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _titleLabel;
- }
- - (UITableView *)tableView {
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.titleView.f_top, self.infoView.f_width, self.infoView.f_heigh - self.titleView.f_top) style:UITableViewStylePlain];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.backgroundColor = Color_Clear;
- _tableView.tableFooterView = [UIView new];
- }
- return _tableView;
- }
- - (UIButton *)systemButton {
- if (_systemButton == nil) {
- _systemButton = [UIButton createButtonTextColor:Color_TextFont textFont:Font(14)];
- _systemButton.frame = CGRectMake(self.titleView.f_width - 5.0f - 70.0f, 0, 65.0f, 44.0f);
- [_systemButton setTitle:@"系统消息" forState:UIControlStateNormal];
- [_systemButton addTarget:self action:@selector(systemButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _systemButton;
- }
- - (XYUnReadBadgeView *)badgeLabel {
- if (!_badgeLabel) {
- _badgeLabel = [[XYUnReadBadgeView alloc] initWithFrame:CGRectMake(self.titleView.f_width - 10.0f - 8.0f, 10.0f, 12.0f, 12.0f)];
- _badgeLabel.unReadLabel.font = Font_B(8);
- _badgeLabel.unReadLabel.textColor = Color_White;
- _badgeLabel.viewHeight = 12.0f;
- }
- return _badgeLabel;
- }
- @end
|