// // WiFiListViewController.m // tongxin // // Created by ecell on 2020/10/30. // Copyright © 2020 xTT. All rights reserved. // #import "WiFiListViewController.h" #import "WIFIModel.h" #import "TableCell.h" #import "SVProgressHUD.h" @interface WiFiListViewController () @property (strong, nonatomic) UITableView *myTableView; @property (strong, nonatomic) WIFIModel *model; @property (strong, nonatomic) NSMutableArray *wifiArr; @property (strong, nonatomic) id observe; @end @implementation WiFiListViewController - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self loadData]; } - (void)viewDidLoad { [super viewDidLoad]; WEAKSELF _wifiArr = [NSMutableArray new]; [self initUI]; _observe = [[NSNotificationCenter defaultCenter] addObserverForName:AccountMessageNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) { NSDictionary *dic = note.object; NSDictionary *msgDic = dic[@"message"]; NSString * msgType = msgDic[@"type"]; if(!msgType || !msgDic){ return ; } if([msgType isEqualToString:@"wifi"]) { NSArray *wifi = msgDic[@"wifi"]; NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *evaluatedObject, NSDictionary * _Nullable bindings) { NSString *name = evaluatedObject[@"ssid"]; if ([name isEqualToString:@""]) { return NO; } else { return YES; } }]; NSArray *filterArray = [wifi filteredArrayUsingPredicate:predicate]; [weakSelf.wifiArr removeAllObjects]; [weakSelf.wifiArr addObjectsFromArray:filterArray]; [weakSelf.myTableView reloadData]; [weakSelf dismissHUD]; } xLog(@"%@", dic); }]; } - (void)loadData{ WEAKSELF [WIFIModel getWIFIDataListSuccess:^(WIFIModel * _Nonnull model) { weakSelf.model = model; [weakSelf.myTableView reloadData]; [weakSelf showHUD]; } failure:^(NSError * _Nonnull error) { [weakSelf showHUD]; }]; } - (void)showHUD { [UICommon MessageSuccessText:@"正在检索附近wifi,请保持手机与设备处于同一网络环境" isImg:NO]; [self performSelector:@selector(dismissHUD) withObject:nil afterDelay:15.0]; } - (void)dismissHUD { [UICommon HidenLoading]; } - (void)initUI{ self.title = @"WIFI列表"; _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-getRectNavAndStatusHight) style:UITableViewStylePlain]; _myTableView.backgroundColor = tabViewBG; _myTableView.delegate = self; _myTableView.dataSource = self; [self.view addSubview:_myTableView]; _myTableView.bounces = NO; //禁止弹跳 _myTableView.showsHorizontalScrollIndicator = NO; _myTableView.showsVerticalScrollIndicator = NO; //去线 UIView *view = [UIView new]; view.backgroundColor = [UIColor clearColor]; [_myTableView setTableFooterView:view]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.wifiArr.count; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) { return 0.01; } return 15 * FIX_SCREEN; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 49; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary *wifi = self.wifiArr[indexPath.row]; TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell"]; if (!cell) { cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TextFieldCell"]; cell.accessoryType = UITableViewCellAccessoryNone; cell.textLabel.text = wifi[@"ssid"]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *wifi = self.wifiArr[indexPath.row]; [[NSNotificationCenter defaultCenter] postNotificationName:@"select_wifi" object:wifi]; [self.navigationController popViewControllerAnimated:YES]; NSLog(@"wifi = %@", wifi); } - (void) viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; if (_observe) { [[NSNotificationCenter defaultCenter] removeObserver:_observe]; } [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismissHUD) object:nil]; xLog(@"===removeObserver====") } @end