CNLiveAdvancedSearchViewController.m 14.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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
//
//  CNLiveAdvancedSearchViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/26.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNLiveAdvancedSearchViewController.h"

#import "PGDatePickManager.h"
#import "SelectedViewController.h"
#import "SearchResultViewController.h"

@interface CNLiveAdvancedSearchViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, PGDatePickerDelegate, SelectedProtocol>

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableDictionary *paramDict;
@property (nonatomic, strong) PGDatePickManager *dataManager;

@end

@implementation CNLiveAdvancedSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.paramDict = [NSMutableDictionary dictionary];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpNavigationBar];
    [self createUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelDatePicker) name:@"CancelDatePicker" object:nil];
}
#pragma mark - UI
- (void)setUpNavigationBar{
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.navigationBar.barTintColor = Color_Blue;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 44)];
    headerButton.layer.cornerRadius = 2.0;
    headerButton.layer.masksToBounds = true;
    [headerButton setImage:[UIImage imageNamed:@"cnlive_white_back"] forState:UIControlStateNormal];
    [headerButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];
//    self.navigationItem.title = @"分类检索";
    self.navigationItem.title = CustomStr(@"ClassificationSearch");

}

- (void)createUI {
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 50)];
    self.searchBar.delegate = self;
    self.searchBar.placeholder = @"请输入关键字,多个关键字用空格分隔";
    self.searchBar.barTintColor = [UIColor whiteColor];
    self.searchBar.searchBarStyle =UISearchBarStyleMinimal;
    
    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    searchField.layer.cornerRadius = 10.0f;
    searchField.layer.borderColor = HEXCOLOR(0xC4C5C8).CGColor;
    searchField.layer.borderWidth = 1;
    searchField.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.searchBar];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, MainScreenWidth, MainScreenHeight - 50)];
    self.tableView.backgroundColor = HEXCOLOR(0xF7F6F6);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 60)];
    
    UIImage *image = [UIImage imageNamed:@"cnlive_additional_screening"];
    UIImageView *img = [[UIImageView alloc] initWithImage:image];
    
    img.frame = CGRectMake(20, 30 - image.size.height / 2, image.size.width, image.size.height);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake( image.size.width + 30, 10, MainScreenWidth - 40, 40)];
    label.font = [UIFont systemFontOfSize:14];
//    label.text = @"附加筛选条件(可选项)";
    label.text = CustomStr(@"AdditionalFilterConditions(optional)");
    label.textAlignment = NSTextAlignmentLeft;
    [header addSubview:img];
    [header addSubview:label];
    self.tableView.tableHeaderView = header;
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 120)];
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    [left setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    left.backgroundColor = [UIColor blueColor];
    [left addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    left.frame = CGRectMake(61, 80, MainScreenWidth / 2 - 90, 41);
//    [left setTitle:@"筛选" forState:UIControlStateNormal];
    [left setTitle:CustomStr(@"Screening") forState:UIControlStateNormal];
    left.backgroundColor = HEXCOLOR(0x2A5AC2);
    left.titleLabel.font = [UIFont systemFontOfSize:15];
    left.layer.cornerRadius = 5;
    left.layer.masksToBounds = YES;
    [footer addSubview:left];
    
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    [right addTarget:self action:@selector(clearUp) forControlEvents:UIControlEventTouchUpInside];
    right.layer.borderColor = HEXCOLOR(0xA0A1A1).CGColor;
    right.layer.borderWidth = 1;
    [right setTitleColor:HEXCOLOR(0xA0A1A1) forState:UIControlStateNormal] ;
    right.titleLabel.font = [UIFont systemFontOfSize:15];
    right.layer.cornerRadius = 5;
    right.layer.masksToBounds = YES;
    [right setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    right.frame = CGRectMake(MainScreenWidth / 2 + 30, 80, MainScreenWidth / 2 - 90, 41);
//    [right setTitle:@"重置" forState:UIControlStateNormal];
    [right setTitle:CustomStr(@"Reset") forState:UIControlStateNormal];
    [footer addSubview:right];
    
    self.tableView.tableFooterView = footer;
}

- (void)searchAction {
    NSString *key = self.searchBar.text;
    if (key.length == 0) {
        [AlertViewTool showHUDAddedToView:self.view text:@"请输入关键字"];
        return;
    }
    
    [self searchApiWithKeywords:key];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    [searchBar endEditing:YES];
    [self searchAction];
}

- (void)searchApiWithKeywords:(NSString *)keywords {
    
    NSString *strUrl = @"http://app.phonemovie.cnlive.com/gxb/api/seniorProgramList";
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:self.paramDict];
    
    [param setObject:@"i" forKey:@"plat"];
    [param setObject:@"5.0" forKey:@"version"];
    [param setObject:CNLiveAppId forKey:@"appid"];
    [param setObject:@"spzl/djzw/" forKey:@"cmsColumnId"];
    [param setObject:keywords forKey:@"keywords"];
    
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
        return;
    }
    [AlertViewTool showHUDView];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:strUrl
       parameters:@{@"params" : [self convertToJsonData:param]}
         progress:^(NSProgress * _Nonnull uploadProgress) {}
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              // 请求成功,解析数据
              NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
              SearchResultViewController *result = [[SearchResultViewController alloc] init];
              result.dataDict = [NSDictionary dictionaryWithDictionary:dic];
              result.moreParam = [NSDictionary dictionaryWithDictionary:param];
              result.key = keywords;
              [AlertViewTool hideHUDView];
              [self.navigationController pushViewController:result animated:YES];
          }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              // 请求失败
              [AlertViewTool hideHUDView];
              [AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"ServerBusy")];
          }];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    
    [searchBar setShowsCancelButton:YES animated:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    
    [searchBar setShowsCancelButton:NO animated:YES];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    
    [searchBar endEditing:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [self.searchBar endEditing:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellID = @"Identifier";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        
    }
    cell.backgroundColor = HEXCOLOR(0xF7F6F6);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    NSString *key = @"";
    switch (indexPath.row) {
        case 0:
            key = @"time";
            break;
        case 1:
            key = @"mac";
            break;
        case 2:
            key = @"speaker";
            break;
        case 3:
            key = @"media";
        default:
            break;
    }
    NSString *detail = self.paramDict[key];
    cell.detailTextLabel.text = detail ? detail : CustomStr(@"Unlimited");
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.searchBar endEditing:YES];
    NSArray *dataArray;
    NSString *key = @"";
    switch (indexPath.row) {
        case 0:
        {
            [self showDatePicker];
            return;
        }
            break;
        case 1:
        {
            key = @"mac";
        }
            break;
        case 2:
        {
            key = @"speaker";
        }
            break;
        case 3:
        {
            key = @"media";
        }
            break;
            
        default:
            break;
    }
    
    SelectedViewController *select = [[SelectedViewController alloc] init];
    select.selectedDelegate = self;
    select.type = indexPath.row;
    NSString *text = self.paramDict[key];
    if (text.length > 0) {
        
        select.selectedText = text;
    }
    select.url = [NSString stringWithFormat:@"%@List", key];
    [self.navigationController pushViewController:select animated:YES];
}

- (void)selected:(NSString *)selectedStr type:(NSInteger)type {
    
    NSString *key = @"";
    switch (type) {
        case 1:
            key = @"mac";
            break;
        case 2:
            key = @"speaker";
            break;
        case 3:
            key = @"media";
        default:
            break;
    }
    if (selectedStr.length > 0) {
        
        [self.paramDict setObject:selectedStr forKey:key];
        
    }else {
        
        [self.paramDict removeObjectForKey:key];
    }
    
    [self.tableView reloadData];
    
}

-(void)showDatePicker {
    
    [self presentViewController:self.dataManager animated:NO completion:nil];
    
}

- (PGDatePickManager *)dataManager {
    
    if (!_dataManager) {
        
        _dataManager = [[PGDatePickManager alloc]init];
        _dataManager.style = PGDatePickerType3;
        PGDatePicker *datePicker = _dataManager.datePicker;
        datePicker.minimumDate = [NSDate setYear:2016];
        datePicker.maximumDate = [NSDate date];
        datePicker.delegate = self;
        datePicker.datePickerType = PGDatePickerType2;
        datePicker.datePickerMode = PGDatePickerModeYearAndMonthUnlimit;
        
        //设置线条的颜色
        datePicker.lineBackgroundColor = [UIColor clearColor];
        //设置选中行的字体颜色
        datePicker.textColorOfSelectedRow = HEXCOLOR(0x2A5AC2);
        //设置未选中行的字体颜色
        datePicker.textColorOfOtherRow = [UIColor grayColor];
        //设置取消按钮的字体颜色
        _dataManager.cancelButtonTextColor = HEXCOLOR(0xA0A1A1);
        
        _dataManager.isShadeBackgroud = true;
        //设置取消按钮的字
        _dataManager.cancelButtonText = CustomStr(@"Clear");
        //设置取消按钮的字体大小
        _dataManager.cancelButtonFont = [UIFont boldSystemFontOfSize:17];
        //设置确定按钮的字体颜色
        _dataManager.confirmButtonTextColor = HEXCOLOR(0x2A5AC2);
        //设置确定按钮的字
        _dataManager.confirmButtonText = CustomStr(@"Sure");
        //设置确定按钮的字体大小
        _dataManager.confirmButtonFont = [UIFont boldSystemFontOfSize:17];
    }
    
    return _dataManager;
    
}

- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSString *dateString = @"";
    if (dateComponents.month == 0) {
        dateString = [NSString stringWithFormat:@"%ld年", (long)dateComponents.year];
    }else
    {
        dateString = [NSString stringWithFormat:@"%ld年%ld月", (long)dateComponents.year, dateComponents.month];
    }
    [self.paramDict setValue:dateString forKey:@"time"];
    [self.tableView reloadData];
}

- (void)cancelDatePicker {
    
    [self.paramDict removeObjectForKey:@"time"];
    [self.tableView reloadData];
    
}

- (void)clearUp {
    
    self.paramDict = [NSMutableDictionary dictionary];
    [self.tableView reloadData];
}


- (NSArray *)titleArray {
//    return @[@"时间", @"部门", @"发言人", @"主要媒体"];
    return @[CustomStr(@"Time"), CustomStr(@"Department"), CustomStr(@"Spokesman"), CustomStr(@"MajorMedia")];

}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)convertToJsonData:(NSDictionary *)dict
{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",error);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    //    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return mutStr;
    
}


#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