ArticleController.m
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// 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 ? 15 : 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])];
UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];
articleTV.tableFooterView = footView;
[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