ArticleController.m 4.7 KB
//
//  ArticleController.m
//  DreamSleep
//
//  Created by peter on 2022/9/20.
//

#import "ArticleController.h"
#import "ArticleRequestModel.h"
#import "ArticleCell.h"

@interface ArticleController () <UITableViewDelegate>
@property (nonatomic, strong) ArticleRequestModel *articleVM;
@property (nonatomic, strong) UITableView *articleView;
@property (nonatomic, strong) DSDataSource *dataSource;
@end

@implementation ArticleController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"睡眠文章";
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    self.articleVM = [[ArticleRequestModel alloc] init];
    [self.dataSource addDataArray:self.articleVM.articleArr];
}

- (void)getArticleListRequest:(BOOL)loadMore {    
    [self.articleVM querySsmianKnowledgeListWithLoadMore:loadMore completion:^(ArticleRequestModel * _Nonnull requestModel) {
        [self endRefreshing:loadMore];

        if (requestModel.resCode == DSResCodeSuccess) {
            if (requestModel.articleArr.count == 0) {
                [self updateDefalutView:DefaultTypeEmpty info:@""];
                [self.articleView reloadData];
                return;
            }
            
            if (requestModel.latestDataList.count == 0) {
                if (loadMore) {
                    [self.articleView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                [self.articleVM updateOffset];
                [self.dataSource addDataArray:self.articleVM.articleArr];
                [self.articleView reloadData];
            }
        } else {
            if (requestModel.articleArr.count) {
                [DSProgressHUD showToast:requestModel.errMessage];
            } else {
                [self updateDefalutView:DefaultTypeNet info:requestModel.errMessage];
            }
        }
    }];
}

- (void)endRefreshing:(BOOL)loadMore {
    if (loadMore) {
        [self.articleView.mj_footer endRefreshing];
    } else {
        [self.articleView.mj_header endRefreshing];
        [self.articleView.mj_footer resetNoMoreData];
    }
}

- (void)fireHandler {
    [super fireHandler];
    [self getArticleListRequest:NO];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == self.dataSource.dataArray.count - 1 ? (24 + Bottom_SafeArea_Height) : 12;;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ArticleModel *model = self.dataSource.dataArray[indexPath.section];
    [self.navigationController pushViewController:[[DsWebController alloc] initWithTitle:model.title link:model.url] animated:YES];
}

#pragma mark - lazy
- (DSDataSource *)dataSource {
    if (!_dataSource) {
        CellConfigureBlock cellBlock = ^(ArticleCell * cell, ArticleModel * model, NSIndexPath * indexPath) {
            cell.model = model;
        };
        _dataSource = [[DSDataSource alloc] initWithIdentifier:NSStringFromClass([ArticleCell class]) datas:@[] isSection:YES configureBlock:cellBlock];
        
        UITableView *articleTV = [[UITableView alloc] initWithFrame:CGRectMake(15, 0, kScreenWidth - 30, kScreenHeight - kTopHeight(0)) style:UITableViewStyleGrouped];
        articleTV.separatorStyle = UITableViewCellSeparatorStyleNone;
        articleTV.delegate = self;
        articleTV.dataSource = _dataSource;
        articleTV.showsVerticalScrollIndicator = NO;
        articleTV.backgroundColor = DSClearColor;
        articleTV.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [articleTV registerClass:[ArticleCell class] forCellReuseIdentifier:NSStringFromClass([ArticleCell class])];
        articleTV.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];;
        [self.view addSubview:articleTV];
        self.articleView = articleTV;
        
        WS(weakSelf);
        articleTV.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
            [weakSelf getArticleListRequest:NO];
        }];
        [articleTV.mj_header beginRefreshing];
        
        articleTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getArticleListRequest:YES];
        }];
    }
    return _dataSource;
}

@end