CNLiveAllServiceVC.m
10.6 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
//
// CNLiveAllServiceVC.m
// CNLiveCServiceModule
//
// Created by CNLive on 2021/11/29.
//
#import "CNLiveAllServiceVC.h"
#import "CNLiveServiceSearchVC.h"
#import "CNLiveSearchHotsCell.h"
#import "CNLiveSearchHeaderView.h"
#import "CNLiveServicePresent.h"
#import "CNLIveCategoryCell.h"
static NSString *HotListCellID = @"CNLiveSearchHotsCell";
static NSString *HotHeaderID = @"CNLiveSearchHeaderView";
static NSString *CNCategoryCellID = @"CNLIveCategoryCell";
static NSString *HistoryFootID = @"UICollectionFooterView";
@interface CNLiveAllServiceVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) CNLiveNavigationBar *navigationBar;
@property (nonatomic, strong) UICollectionView *listCollectionView;//热搜/搜索历史列表
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSString *serviceId;
@property (nonatomic, assign) NSInteger page;
@end
@implementation CNLiveAllServiceVC
+ (instancetype)allServiceVCWithId:(NSString *)serviceId title:(nonnull NSString *)title
{
CNLiveAllServiceVC *vc = [[CNLiveAllServiceVC alloc] initWithServiceId:serviceId];
vc.title = title;
[[BaseAppDelegate sharedAppDelegate] pushViewController:vc withBackTitle:@""];
return vc;
}
- (void)requestService{
if (self.page == 1) {
self.dataSource = @[];
[self.listCollectionView reloadData];
}
if (![CNLiveNetworking isNetworking]) {
self.listCollectionView.mj_footer.hidden = YES;
if (self.dataSource.count>0) {
return;
}
[self showEmptyViewWithImage:[UIImage loadCServiceBundle:@"service_noNet"] text:@"网络开小差了,刷新试试" detailText:nil buttonTitle:@" 刷新 " buttonAction:@selector(requestService)];
self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
[self setCNAPIErrorEmptyView];
return;
}
[QMUITips showLoadingInView:self.view];
weakself
[CNLiveServicePresent loadAllServiceWithServiceId:_serviceId page:[NSString stringWithFormat:@"%ld",_page] result:^(NSArray * _Nonnull dataArray, BOOL isLastPage) {
strongself
[QMUITips hideAllTipsInView:self.view];
[self.listCollectionView.mj_footer endRefreshing];
self.listCollectionView.mj_footer.hidden = NO;
if (isLastPage) {
[self.listCollectionView.mj_footer endRefreshingWithNoMoreData];
}
[self removeEmptyView];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.dataSource];
for (id object in dataArray) {
[array addObject:object];
}
self.dataSource = array.copy;
[self.listCollectionView reloadData];
}error:^{
strongself
[QMUITips hideAllTipsInView:self.view];
if (self.dataSource.count > 0) {
return;
}
[self.listCollectionView.mj_footer endRefreshing];
self.listCollectionView.mj_footer.hidden = YES;
[self showEmptyViewWithImage:[UIImage loadCServiceBundle:@"service_failure"] text:@"加载失败,请点击重试" detailText:nil buttonTitle:@" 重试 " buttonAction:@selector(requestService)];
self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
[self setCNAPIErrorEmptyView];
}];
}
-(void)removeEmptyView
{
[self.emptyView removeFromSuperview];
self.emptyView = nil;
}
- (instancetype)initWithServiceId:(NSString *)serviceId
{
self = [super init];
if (self) {
_serviceId = serviceId;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
_page = 1;
[self requestService];
self.view.backgroundColor = kWhiteColor;
[self.view addSubview:self.navigationBar];
[self.view addSubview:self.listCollectionView];
// Do any additional setup after loading the view.
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.listCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.navigationBar.mas_bottom).offset(0);
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.mas_bottomLayoutGuide);
}];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
CNLiveSearchModel *model1 = [CNLiveSearchModel mj_objectWithKeyValues:model.lists[indexPath.row]];
[CNLiveServicePresent usedServiceWithServingId:model1.id];
[CNLiveCServiceManagerBridge jumpServiceController:model1.id type:model1.type to:model1.to shopType:model1.shopType controller:self];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
CNLiveSearchHotsModel *model = self.dataSource[section];
return model.lists.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return self.dataSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
if (_serviceId &&![_serviceId isEqualToString:@""]&&indexPath.section != 0){
CNLIveCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNCategoryCellID forIndexPath:indexPath];
cell.model = [CNLiveSearchModel mj_objectWithKeyValues:model.lists[indexPath.row]];
return cell;
}else{
CNLiveSearchHotsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HotListCellID forIndexPath:indexPath];
cell.model = [CNLiveSearchModel mj_objectWithKeyValues:model.lists[indexPath.row]];
return cell;
}
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqualToString:@"UICollectionElementKindSectionHeader"]) {
CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
CNLiveSearchHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HotHeaderID forIndexPath:indexPath];
if (_serviceId &&![_serviceId isEqualToString:@""]&&indexPath.section == 0) {
header.type = CNServiceHeaderTypeCategoryId;
}else{
header.type = CNServiceHeaderTypeService;
}
header.model = model;
return header;
}else
{
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HistoryFootID forIndexPath:indexPath];
footer.backgroundColor = CNLiveColorWithHexString(@"#F5F5F5");
return footer;
}
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
if (_serviceId &&![_serviceId isEqualToString:@""]&&indexPath.section != 0){
return CGSizeMake(collectionView.width, model.layout.height);
}
return CGSizeMake(model.layout.width, model.layout.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(5, 15, 10, 15);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(collectionView.width, 60);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
return CGSizeMake(self.view.width, 5);
}
#pragma mark - Setting&&Getting
- (UICollectionView *)listCollectionView
{
if (!_listCollectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_listCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_listCollectionView.backgroundColor = kWhiteColor;
_listCollectionView.collectionViewLayout = layout;
_listCollectionView.delegate = self;
_listCollectionView.dataSource = self;
_listCollectionView.showsVerticalScrollIndicator = NO;
[_listCollectionView registerClass:[CNLiveSearchHotsCell class] forCellWithReuseIdentifier:HotListCellID];
[_listCollectionView registerClass:[CNLiveSearchHeaderView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:HotHeaderID];
[_listCollectionView registerClass:[CNLIveCategoryCell class] forCellWithReuseIdentifier:CNCategoryCellID];
[_listCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionFooter" withReuseIdentifier:HistoryFootID];
weakself
MJRefreshAutoNormalFooter *loadFooter = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
strongself
self.page ++;
[self requestService];
}];
_listCollectionView.mj_footer = loadFooter;
}
return _listCollectionView;
}
#pragma mark - Navigation
- (CNLiveNavigationBar *)navigationBar {
if (!_navigationBar) {
_navigationBar = [CNLiveNavigationBar navigationBar];
_navigationBar.title.text = self.title;
weakself
_navigationBar.onClickLeftButton = ^{
strongself
[self.navigationController popViewControllerAnimated:YES];
};
if ([self.title isEqualToString:@"全部服务"]) {
[_navigationBar.right setTitleColor:CNLiveColorWithHexString(@"#F5A623") forState:UIControlStateNormal];
_navigationBar.rightImage = [UIImage loadCServiceBundle:@"service_search"];
_navigationBar.onClickRightButton = ^{
[CNLiveServiceSearchVC serviceSearchVC:nil];
};
}
}
return _navigationBar;
}
@end