CNLiveServiceSearchVC.m 16.9 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
//
//  CNLiveServiceSearchVC.m
//  CNLiveCServiceModule
//
//  Created by CNLive on 2021/11/24.
//

#import "CNLiveServiceSearchVC.h"
#import "CNLiveMainSearch.h"
#import "CNLiveSearchModel.h"
#import "CNLiveServicePresent.h"
#import "CNLiveSearchListCell.h"
#import "CNLiveServiceHistorySearch.h"
#import "CNLiveSearchHotsCell.h"
#import "CNLiveSearchHistoryCell.h"
#import "CNLiveSearchHotsFooterView.h"
#import "CNLiveSearchHeaderView.h"

@interface CNLiveServiceSearchVC ()<UITableViewDelegate,UITableViewDataSource, UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (nonatomic, strong) CNLiveMainSearch *searchView;//搜索框
@property (nonatomic, strong) UIButton *cancelBtn; //取消按钮
@property (nonatomic, strong) NSArray *histories;
@property (nonatomic, strong) UITableView *listTableView;//搜索结果列表

@property (nonatomic, strong) UICollectionView *listCollectionView;//热搜/搜索历史列表
@property (nonatomic, strong) NSArray *hDataSources;//包含搜索历史和热搜的数组
@end

static NSString *SearchListCellID = @"SearchListCellID";
static NSString *HotListCellID = @"HotListCellID";
static NSString *HistoryListCellID = @"HistoryListCellID";
static NSString *HotHeaderID = @"HotHeaderID";
static NSString *HistoryFootID = @"HistoryFootID";

@implementation CNLiveServiceSearchVC
+ (instancetype)serviceSearchVC:(NSArray *)array
{
    CNLiveServiceSearchVC *search = [[CNLiveServiceSearchVC alloc] init];
    search.hots = array;
    [[BaseAppDelegate sharedAppDelegate] pushViewController:search];
    return search;
}
- (void)searchRequese{
    if (self.page == 1) {
        self.dataListArr = @[];
        [self.listTableView reloadData];
    }
    if (![CNLiveNetworking isNetworking]) {
        [QMUITips showError:@"网络未连接" inView:self.view hideAfterDelay:2];
        return;
    }
    [QMUITips showLoadingInView:self.view];
    weakself
    [CNLiveServicePresent searchServiceList:_searchStr page:[NSString stringWithFormat:@"%ld",_page] result:^(NSArray<CNLiveSearchModel *> * _Nonnull dataArray, BOOL isLastPage) {
        weakSelf.listCollectionView.hidden = YES;
        [QMUITips hideAllTipsInView:self.view];
        strongself
        if (!dataArray||dataArray.count <= 0) {
            if (self.dataListArr.count > 0) {
                return;
            }
            [self showEmptyViewWithImage:[UIImage loadCServiceBundle:@"service_noMsg"] text:@"暂无内容" detailText:nil buttonTitle:NULL buttonAction:@selector(searchRequese)];
            self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
            [self setCNAPIErrorEmptyView];
        }else{
            [QMUITips hideAllTipsInView:self.view];
            [self removeEmptyView];
            self.listTableView.mj_footer.hidden = NO;
            if (isLastPage) {
                [self.listTableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.listTableView.mj_footer endRefreshing];
            }
            self.listTableView.hidden = NO;
            NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.dataListArr];
            for (id object in dataArray) {
                [array addObject:object];
            }
            self.dataListArr = array.copy;
            [self.listTableView reloadData];
        }
    } error:^{
        [QMUITips hideAllTipsInView:self.view];
    }];
}
-(void)removeEmptyView
{
    [self.emptyView removeFromSuperview];
    self.emptyView = nil;
}

- (void)dealHotsAndHistories
{
    [CNLiveServicePresent dealHots:self.hots searchHistories:self.histories results:^(NSArray * _Nonnull dataArray) {
        self.hDataSources = dataArray;
        [self.listCollectionView reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    if (!self.fromAllSearch) {
        NSMutableArray *historSearch = [CNLiveServiceHistorySearch readHistorySearch];
        for (id obj in historSearch.copy) {
            if ([obj isKindOfClass:[NSString class]]) {
                NSDictionary *dic = @{
                    @"title":obj,
                };
                NSInteger index = [historSearch indexOfObject:obj];
                [historSearch replaceObjectAtIndex:index withObject:dic];
            }
        }
        self.histories = historSearch.copy;
        [self dealHotsAndHistories];
        [self.view addSubview:self.searchView];
        [self.view addSubview:self.cancelBtn];
        [self.view addSubview:self.listCollectionView];
    }
   
    [self.view addSubview:self.listTableView];
    // Do any additional setup after loading the view.
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if (self.fromAllSearch) {
        [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuide);
        }];
    }else{
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (self.mas_topLayoutGuide > 0) {
                make.top.equalTo(self.mas_topLayoutGuide);
            }else{
            make.top.equalTo(self.view).offset(35.5);
            }
            make.height.offset(36);
            make.right.equalTo(self.view).offset(-15);
        }];
        [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (self.mas_topLayoutGuide > 0) {
                make.top.equalTo(self.mas_topLayoutGuide);
            }else{
            make.top.equalTo(self.view).offset(35.5);
            }
            make.left.equalTo(self.view).offset(15);
            make.right.equalTo(self.cancelBtn.mas_left).offset(-15);
            make.height.offset(36);
        }];
        [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.searchView.mas_bottom).offset(10);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuide);
        }];
        [self.listCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.searchView.mas_bottom).offset(7);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuide);
        }];
    }
   
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!self.fromAllSearch) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignActive) name:UIApplicationDidEnterBackgroundNotification object:nil];
    }
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (!self.fromAllSearch) {
        [[NSNotificationCenter defaultCenter]  removeObserver:self];
        [CNLiveServiceHistorySearch saveHistorySearch:self.histories.mutableCopy];
    }
}
- (BOOL)preferredNavigationBarHidden
{
    return YES;
}
#pragma mark - Notification
- (void)resignActive{
    [CNLiveServiceHistorySearch saveHistorySearch:self.histories.mutableCopy];
}
#pragma mark - Action
- (void)cancelBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}
- (BOOL)shouldHideKeyboardWhenTouchInView:(UIView *)view
{
    return YES;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSearchModel *model = self.dataListArr[indexPath.row];
    [CNLiveCServiceManagerBridge jumpServiceController:model.id showAgreement:model.showAgreement agreementDesc:model.agreementDesc type:model.type to:model.to shopType:model.shopType controller:self];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataListArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSearchListCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchListCellID];
    CNLiveSearchModel *model = self.dataListArr[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSearchHotsModel *model = self.hDataSources[indexPath.section];
    if ([model.title isEqualToString:@"热度推荐"]) {
        CNLiveSearchModel *model1 = model.lists[indexPath.row];
        [CNLiveCServiceManagerBridge jumpServiceController:model1.id showAgreement:model1.showAgreement agreementDesc:model1.agreementDesc  type:model1.type to:model1.to shopType:model1.shopType controller:self];
    }else
    {
        CNLiveHistoryModel *model1 = model.lists[indexPath.row];
        self.searchStr = model1.title;
        NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.histories];
        [array removeObjectAtIndex:indexPath.row];
        NSDictionary *dic = @{
            @"title":_searchStr,
        };
        [array insertObject:dic atIndex:0];
        self.histories = array.copy;
        [self dealHotsAndHistories];
        self.searchView.searchField.text = self.searchStr;
        [self searchRequese];
    }
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    CNLiveSearchHotsModel *model = self.hDataSources[section];
    return model.lists.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.hDataSources.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSearchHotsModel *model = self.hDataSources[indexPath.section];
    if ([model.title isEqualToString:@"热度推荐"]) {
        CNLiveSearchHotsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HotListCellID forIndexPath:indexPath];
        cell.model = model.lists[indexPath.row];
        return cell;
    }else{
        CNLiveSearchHistoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HistoryListCellID forIndexPath:indexPath];
        cell.model = model.lists[indexPath.row];
        return cell;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:@"UICollectionElementKindSectionHeader"]) {
        CNLiveSearchHotsModel *model = self.hDataSources[indexPath.section];
        CNLiveSearchHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HotHeaderID forIndexPath:indexPath];
        header.model = model;
        return header;
    }else
    {
        CNLiveSearchHotsFooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HistoryFootID forIndexPath:indexPath];
        footer.clearBlock = ^{
            self.histories = @[];
            [self dealHotsAndHistories];
        };
        return footer;
    }
    
    
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSearchHotsModel *model = self.hDataSources[indexPath.section];
    return CGSizeMake(model.layout.width, model.layout.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 15, 10, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(collectionView.width, 40);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    CNLiveSearchHotsModel *model = self.hDataSources[section];
    if ([model.title isEqualToString:@"热度推荐"]) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(self.view.width, 40);
}
#pragma mark - Setting&&Getting

- (CNLiveMainSearch *)searchView{
    if (!_searchView) {
        _searchView = [[CNLiveMainSearch alloc] initWithType:CNSearchViewShowTypeSearch];
        weakself
        _searchView.searchBlock = ^(NSString * _Nonnull searchStr) {
            [weakSelf.view endEditing:YES];
           weakSelf.page = 1;
            weakSelf.searchStr = searchStr;
            [weakSelf searchRequese];
            if (![weakSelf isSearchEd:searchStr]) {
                NSMutableArray *array = [NSMutableArray arrayWithArray:weakSelf.histories];
                NSDictionary *dic = @{
                    @"title":searchStr,
                };
                [array insertObject:dic atIndex:0];
                weakSelf.histories = array.copy;
                [weakSelf dealHotsAndHistories];
            }
           
        };
        _searchView.clearBlock = ^{
            if (weakSelf.listCollectionView.isHidden) {
                weakSelf.listCollectionView.hidden = NO;
            }
            if (!weakSelf.listTableView.isHidden) {
                weakSelf.listTableView.hidden = YES;
            }
            if (weakSelf.emptyView) {
                [weakSelf removeEmptyView];
            }
        };
    }
    return _searchView;
}
- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:CNLiveColorWithHexString(@"#00CD1E") forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = UIFontCNMake(14);
        _cancelBtn.backgroundColor = kClearColor;
        [_cancelBtn sizeToFit];
        [ _cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
- (UITableView *)listTableView
{
    if (!_listTableView) {
        _listTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        [_listTableView registerClass:[CNLiveSearchListCell class] forCellReuseIdentifier:SearchListCellID];
        weakself
        _listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            strongself
            self.page ++;
            [self searchRequese];
        }];
        _listTableView.hidden = YES;
    }
    return _listTableView;
}
- (UICollectionView *)listCollectionView
{
    if (!_listCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionFootersPinToVisibleBounds = YES;
        layout.sectionHeadersPinToVisibleBounds = YES;
        _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:[CNLiveSearchHistoryCell class] forCellWithReuseIdentifier:HistoryListCellID];
        [_listCollectionView registerClass:[CNLiveSearchHeaderView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:HotHeaderID];
        [_listCollectionView registerClass:[CNLiveSearchHotsFooterView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionFooter" withReuseIdentifier:HistoryFootID];
    }
    return _listCollectionView;
}

- (BOOL)isSearchEd:(NSString *)string
{
    for (NSDictionary *dic in self.histories) {
        if ([dic[@"title"] isEqualToString:string]) {
            return YES;
        }
    }
    return NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end