OfficialNoticeController.m 3.2 KB
//
//  OfficialNoticeController.m
//  DreamSleep
//
//  Created by peter on 2022/10/11.
//

#import "OfficialNoticeController.h"
#import "OfficialNotiCell.h"
#import "OfficialNotiViewModel.h"

@interface OfficialNoticeController () <UITableViewDelegate>
@property (nonatomic, strong) DSDataSource *dataSource;
@property (nonatomic, strong) UITableView *listView;
@end

@implementation OfficialNoticeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"官方通知";
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    
    [self.dataSource addDataArray:@[]];
    [self.listView.mj_header beginRefreshing];
}

- (void)fireHandler {
    [super fireHandler];
    [self.listView.mj_header beginRefreshing];
}

- (void)getListDataRequest {
    [OfficialNotiViewModel queryUserOfficeMessagesWithCompletion:^(OfficialNotiViewModel * _Nonnull viewModel) {
        [self.listView.mj_header endRefreshing];
        
        if (viewModel.resCode == DSResCodeSuccess) {
            if (viewModel.listArr.count == 0) {
                [self updateDefalutView:DefaultTypeEmpty info:@""];
            } else {
                [self.dataSource addDataArray:viewModel.listArr];
            }
            [self.listView reloadData];
        } else {
            [self updateDefalutView:DefaultTypeNet info:viewModel.errMessage];
        }
    }];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OfficialNotiModel *model = self.dataSource.dataArray[indexPath.row];
    return [model cellHeight];
}

#pragma mark - lazy
- (DSDataSource *)dataSource {
    if (!_dataSource) {
        CellConfigureBlock cellBlock = ^(OfficialNotiCell * cell, OfficialNotiModel * model, NSIndexPath * indexPath) {
            cell.officialNotiModel = model;
        };
        _dataSource = [[DSDataSource alloc] initWithIdentifier:NSStringFromClass([OfficialNotiCell class]) datas:@[] isSection:NO configureBlock:cellBlock];
        
        UITableView *listView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        listView.separatorStyle = UITableViewCellSeparatorStyleNone;
        listView.delegate = self;
        listView.dataSource = _dataSource;
        listView.showsVerticalScrollIndicator = NO;
        listView.backgroundColor = DSClearColor;
        listView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [listView registerClass:[OfficialNotiCell class] forCellReuseIdentifier:NSStringFromClass([OfficialNotiCell class])];
        listView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];
        listView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 24 + Bottom_SafeArea_Height)];
        
        [self.view addSubview:listView];
        self.listView = listView;
        WS(weakSelf);
        self.listView.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
            [weakSelf getListDataRequest];
        }];
        
        [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _dataSource;
}

@end