CommunityView.m
10.3 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
// CommunityView.m
// DreamSleep
//
// Created by peter on 2022/9/20.
//
#import "CommunityView.h"
#import "OfficialMessageCell.h"
#import "InformCancelAlertView.h"
@interface CommunityView () <UITableViewDelegate, UITableViewDataSource, InformCancelAlertViewDelegate>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *listView;
@property (nonatomic, strong) ComListViewModel *comListVM;
@property (nonatomic, strong) InformCancelAlertView *informCancelView;
@end
@implementation CommunityView
#pragma mark - 初始化
- (instancetype)initWithDelegate:(id<CommunityViewDelegate>)delegate comListViewModel:(ComListViewModel *)comListVM {
if (self = [super init]) {
self.delegate = delegate;
self.comListVM = comListVM;
self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
[self addSubview:self.listView];
UIButton *publishBtn = [UIButton new];
[publishBtn setImage:[UIImage imageNamed:@"ic_fabu"] forState:UIControlStateNormal];
publishBtn.imageView.dk_alphaPicker = DKAlphaPickerWithAlphas(1.0, .5, 1);
[publishBtn addTarget:self action:@selector(publishAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:publishBtn];
[publishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self).offset(-15);
make.bottom.equalTo(self).offset(-45);
}];
}
return self;
}
- (void)layoutSubviews {
self.listView.frame = CGRectMake(15, 0, self.width - 30, self.height);
}
#pragma mark - public
- (void)updateCommunityMoments:(BOOL)loadMore {
[self endRefreshing:loadMore];
[self.listView reloadData];
}
- (void)endRefreshing:(BOOL)loadMore {
if (loadMore) {
[self.listView.mj_footer endRefreshing];
} else {
[self.listView.mj_header endRefreshing];
[self.listView.mj_footer resetNoMoreData];
}
}
- (void)endRefreshingWithNoMoreData {
[self.listView.mj_footer endRefreshingWithNoMoreData];
}
- (void)showInformCancelView {
[self.informCancelView display];
}
- (void)updateListView {
[self.listView reloadData];
}
- (void)notiRefresh {
[self.listView.mj_header beginRefreshing];
}
#pragma mark - Action
- (void)tapAction:(UITapGestureRecognizer *)tapGR {
if (self.delegate && [self.delegate respondsToSelector:@selector(tapCommunityHeaderModule:)]) {
[self.delegate tapCommunityHeaderModule:tapGR.view.tag];
}
}
- (void)publishAction {
if (self.delegate && [self.delegate respondsToSelector:@selector(publishLogicDeal)]) {
[self.delegate publishLogicDeal];
}
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.comListVM.listArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.comListVM.isOfficial && indexPath.section == 0) {
OfficialMessageModel *model = self.comListVM.listArr[0];
return [model cellHeight];
} else {
ComDynModel *model = self.comListVM.listArr[indexPath.section];
return [model cellHeight];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return section == self.comListVM.listArr.count - 1 ? (24 + Bottom_SafeArea_Height) : 12;;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WS(weakSelf);
if (self.comListVM.isOfficial && indexPath.section == 0) {
OfficialMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OfficialMessageCell class]) forIndexPath:indexPath];
OfficialMessageModel *model = self.comListVM.listArr[0];
cell.model = model;
cell.showAllContentBlock = ^{
[weakSelf.comListVM showOfficialAllContent];
[weakSelf.listView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[weakSelf.listView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
};
return cell;
} else {
ComDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ComDynamicCell class])];
if (!cell) {
cell = [[ComDynamicCell alloc] initWithCellType:DynModelTypeCom style:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([ComDynamicCell class])];
}
ComDynModel *model = self.comListVM.listArr[indexPath.section];
cell.model = model;
__weak ComDynamicCell * weakCell = (ComDynamicCell *)cell;
cell.tapInformBlock = ^{
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didTapInformItem:)]) {
[weakSelf.delegate didTapInformItem:model.dynamicID];
}
};
cell.tapLikeBlock = ^{
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didTapLikeItem:cell:)]) {
[weakSelf.delegate didTapLikeItem:model cell:weakCell];
}
};
cell.tapRemarkBlock = ^{
[weakSelf tableView:tableView didSelectRowAtIndexPath:indexPath];
};
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.comListVM.isOfficial && indexPath.section == 0) { return; }
ComDynModel *model = self.comListVM.listArr[indexPath.section];
if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemWithModel:)]) {
[self.delegate didSelectItemWithModel:model];
}
}
#pragma mark - InformCancelAlertViewDelegate
- (void)tapInformBtn {
if (self.delegate && [self.delegate respondsToSelector:@selector(jumpToReportPage)]) {
[self.delegate jumpToReportPage];
}
}
#pragma mark - lazy
- (UITableView *)listView {
if (!_listView) {
_listView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_listView.delegate = self;
_listView.dataSource = self;
_listView.backgroundColor = DSClearColor;
_listView.showsVerticalScrollIndicator = NO;
_listView.separatorStyle = UITableViewCellSeparatorStyleNone;
_listView.tableHeaderView = self.headView;
_listView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
[_listView registerClass:[OfficialMessageCell class] forCellReuseIdentifier:NSStringFromClass([OfficialMessageCell class])];
_listView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];;
WS(weakSelf);
_listView.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getDynamicListRequest:)]) {
[weakSelf.delegate getDynamicListRequest:NO];
}
}];
[_listView.mj_header beginRefreshing];
_listView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getDynamicListRequest:)]) {
[weakSelf.delegate getDynamicListRequest:YES];
}
}];
}
return _listView;
}
- (UIView *)headView {
if (!_headView) {
CGFloat aroundMargin = 15;
CGFloat middleSpace = 12;
CGFloat articleScale = 146.0 / (kBaseW - 2*aroundMargin - middleSpace);
CGFloat totalW = kScreenWidth - 2*aroundMargin - middleSpace;
CGFloat articleW = articleScale * totalW;
CGFloat evaluationW = totalW * (1 - articleScale);
CGFloat articleH = 80 * articleW / 146;
_headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.listView.width, articleH + 2*aroundMargin)];
_headView.backgroundColor = DSClearColor;
UIImageView *articleIV = [self plateWithFrame:CGRectMake(0, 15, articleW, articleH) title:@"#睡眠文章" desc:@"精选海量好文" imgName:@"btn_shequ_shuimianwenzhang" nightImgName:@"dk_btn_shequ_shuimianwenzhang" tag:1];
[self plateWithFrame:CGRectMake(articleW + 12, articleIV.y, evaluationW, articleH) title:@"#人气测评" desc:@"玩转趣味测试" imgName:@"btn_shequ_ceping" nightImgName:@"dk_btn_shequ_ceping" tag:2];
}
return _headView;
}
- (InformCancelAlertView *)informCancelView {
if (!_informCancelView) {
_informCancelView = [[InformCancelAlertView alloc] initWithDelegate:self];
}
return _informCancelView;
}
- (UIImageView *)plateWithFrame:(CGRect)frame title:(NSString *)title desc:(NSString *)desc imgName:(NSString *)imgName nightImgName:(NSString *)nightImgName tag:(NSInteger)tag {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
imgView.dk_imagePicker = DKImagePickerWithNames(imgName, nightImgName, imgName);
imgView.tag = tag;
imgView.userInteractionEnabled = YES;
[self.headView addSubview:imgView];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[imgView addGestureRecognizer:tapGR];
UILabel *titleLab = [UILabel labWithText:title textColor:DSWhite font:BoldFont(16)];
[imgView addSubview:titleLab];
UILabel *descLab = [UILabel labWithText:desc textColor:ColorFromHexA(0xFFFFFF, .5) font:SysFont(14)];
[imgView addSubview:descLab];
[titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(imgView).offset(15);
make.height.equalTo(@22);
}];
[descLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(titleLab);
make.bottom.equalTo(imgView).offset(-15);
}];
return imgView;
}
@end