MessageNotiView.m 6.0 KB
//
//  MessageNotiView.m
//  DreamSleep
//
//  Created by peter on 2022/10/10.
//

#import "MessageNotiView.h"
#import "MessageOfficialNotiCell.h"
#import "MessageComReplyCell.h"
#import "TextInputAlertView.h"

static int secHeaderHeight = 52;
static int secFooterHeight = 8;

@interface MessageNotiView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) MessageCenterViewModel *messageCenterVM;
@property (nonatomic, strong) TextInputAlertView *textInputAlertView;
@end

@implementation MessageNotiView

- (instancetype)initWithDelegate:(id<MessageNotiViewDelegate>)messageDelegate viewModel:(MessageCenterViewModel *)vm {
    if (self = [super initWithFrame:CGRectZero style:UITableViewStyleGrouped]) {
        self.messageDelegate = messageDelegate;
        self.messageCenterVM = vm;
        
        self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, DarkColor, DSWhite);
        
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerClass:[MessageOfficialNotiCell class] forCellReuseIdentifier:NSStringFromClass([MessageOfficialNotiCell class])];
        [self registerClass:[MessageComReplyCell class] forCellReuseIdentifier:NSStringFromClass([MessageComReplyCell class])];
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 24 + Bottom_SafeArea_Height)];
    }
    return self;
}

#pragma mark - public
- (void)dismissKeyBoard {
    [self.textInputAlertView dismiss];
}

- (void)refreshReadStatusView:(NSIndexPath *)indexPath {
    [self reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.messageCenterVM.messageListArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionArr = self.messageCenterVM.messageListArr[section];
    return sectionArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 77;
    } else {
        NSArray *userNotiList = self.messageCenterVM.messageListArr[indexPath.section];
        MessageComReplyModel *model = userNotiList[indexPath.row];
        return [model cellHeight];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? CGFLOAT_MIN : secHeaderHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
    header.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, DarkColor, DSWhite);
    
    if (section == 0) {
        header.height = 0;
    } else {
        header.height = secHeaderHeight;
        
        NSArray *userNotiList = self.messageCenterVM.messageListArr[section];
        UILabel *totalReplyCountLab = [UILabel labWithFont:BoldFont(15)];
        totalReplyCountLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
        totalReplyCountLab.origin = CGPointMake(15, 15);
        totalReplyCountLab.text = userNotiList.count ? @"评论和回复" : @"暂无评论回复";
        [totalReplyCountLab sizeToFit];
        [header addSubview:totalReplyCountLab];
    }
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == self.messageCenterVM.messageListArr.count - 1 ? CGFLOAT_MIN : secFooterHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, section == self.messageCenterVM.messageListArr.count - 1 ? CGFLOAT_MIN : secFooterHeight)];
    footer.dk_backgroundColorPicker = DKColorPickerWithColors(BGColor, AlertDarkColor, DSWhite);
    return footer;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MessageOfficialNotiCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MessageOfficialNotiCell class]) forIndexPath:indexPath];
        NSArray *officialNotiList = self.messageCenterVM.messageListArr[indexPath.section];
        cell.officialModel = officialNotiList[indexPath.row];
        return cell;
    } else {
        WS(weakSelf);
        MessageComReplyCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MessageComReplyCell class]) forIndexPath:indexPath];
        NSArray *userNotiList = self.messageCenterVM.messageListArr[indexPath.section];
        MessageComReplyModel *model = userNotiList[indexPath.row];
        cell.comReplyModel = model;
        cell.tapReplyBtnBlock = ^{
            [weakSelf.textInputAlertView showReplyTextAlertWithCommentUser:model.nickName model:model];
        };
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.messageDelegate && [self.messageDelegate respondsToSelector:@selector(didSelectRowAtIndexPath:)]) {
        [self.messageDelegate didSelectRowAtIndexPath:indexPath];
    }
}

#pragma mark - lazy
- (TextInputAlertView *)textInputAlertView {
    if (!_textInputAlertView) {
        WS(weakSelf);
        _textInputAlertView = [[TextInputAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _textInputAlertView.tapReplyBlock = ^(NSString * _Nonnull content, id  _Nonnull model) {
            if (weakSelf.messageDelegate && [weakSelf.messageDelegate respondsToSelector:@selector(replyContent:userMessageModel:)]) {
                [weakSelf.messageDelegate replyContent:content userMessageModel:model];
            }
        };
    }
    return _textInputAlertView;
}

@end