// // WiFiViewController.m // tongxin // // Created by WeiChaoZheng on 2019/1/5. // Copyright © 2019年 xTT. All rights reserved. // #import "WiFiViewController.h" #import "WIFIModel.h" #import "TableCell.h" #import "WiFiListViewController.h" @interface WiFiViewController () @property (strong, nonatomic) UITableView *myTableView; @property (strong, nonatomic) WIFIModel *model; /// 弱引用的 密码框 @property (weak, nonatomic) TextFieldCell *pwdTextField; @property (nonatomic, weak) id observe; @end @implementation WiFiViewController @synthesize myDataSource = _myDataSource; -(NSMutableArray *)myDataSource{ if(!_myDataSource){ _myDataSource = [NSMutableArray arrayWithArray:@[@"WiFi名称",@"密码"]]; } return _myDataSource; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //[self loadData]; } - (void)viewDidLoad { [super viewDidLoad]; [self initUI]; WEAKSELF _observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"select_wifi" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) { NSDictionary *wifi = note.object; if(!weakSelf.model) weakSelf.model = [WIFIModel new]; weakSelf.model.name = wifi[@"ssid"]; [weakSelf.myTableView reloadData]; xLog(@"==============%@", wifi); }]; [self loadData]; } - (void)loadData { WEAKSELF [WIFIModel getWIFIDataSuccess:^(WIFIModel * _Nonnull model) { weakSelf.model = model; [weakSelf.myTableView reloadData]; } failure:^(NSError * _Nonnull error) { }]; } - (void)initUI{ self.title = @"设备WIFI"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)]; _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; //去线 //_myTableView.separatorStyle = UITableViewCellSelectionStyleNone; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.myDataSource.count; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) { return 0.01; } return 15 * FIX_SCREEN; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 10 + 70 + 10; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake((SWIDTH - 310 * FIX_SCREEN) / 2, 10, 310 * FIX_SCREEN, 70)]; lable.text = @"注:若添加的Wi-Fi没有密码直接保存即可,设备不支持使用5G Wi-Fi"; lable.numberOfLines = 0; lable.font = [UIFont systemFontOfSize:14]; lable.textColor = [UIColor lightGrayColor]; [view addSubview:lable]; return view; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 49; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.row]; WEAKSELF TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell"]; if (!cell) { cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TextFieldCell"]; cell.accessoryType = UITableViewCellAccessoryNone; cell.textLabel.text = title; if ([title isEqualToString:@"WiFi名称"]) { cell.textField.placeholder = @"输入WiFi名称"; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(0, 0, 25, 25); [btn setImage:[UIImage imageNamed:@"icon_wifi_scanning"] forState:0]; [btn setSelected:YES]; [btn addTarget:self action:@selector(selectWiFiListAction:) forControlEvents:UIControlEventTouchUpInside]; cell.accessoryView = btn; }else{ cell.textField.placeholder = @"输入密码"; cell.textField.keyboardType = UIKeyboardTypeASCIICapable; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(0, 0, 25, 25); [btn setImage:[UIImage imageNamed:@"icon_password_show"] forState:0]; [btn setImage:[UIImage imageNamed:@"icon_password_hidden"] forState:UIControlStateSelected]; [btn setSelected:YES]; cell.textField.secureTextEntry = YES; [btn addTarget:self action:@selector(showOrHiddenPWDAction:) forControlEvents:UIControlEventTouchUpInside]; cell.accessoryView = btn; self.pwdTextField = cell; } } if(_model){ if ([title isEqualToString:@"WiFi名称"]) { cell.textField.text = _model.name; }else{ cell.textField.text = _model.password; } } cell.block = ^(NSString *sender, TextFieldCell *cell) { if ([cell isEqual:weakSelf.pwdTextField]) { weakSelf.model.password = sender; }else{ weakSelf.model.name = sender; } cell.textField.text = sender; }; return cell; } - (IBAction)showOrHiddenPWDAction:(UIButton *)sender { [sender setSelected:!sender.isSelected]; self.pwdTextField.textField.secureTextEntry = sender.isSelected; } - (IBAction)selectWiFiListAction:(UIButton *)sender { WiFiListViewController *vc = [WiFiListViewController new]; [self.navigationController pushViewController:vc animated:YES]; NSLog(@"selectWiFiListAction"); } - (void)saveAction:(id)sender{ WEAKSELF if(self.model.password.length != 0 && self.model.password.length < 8){ [UICommon MessageErrorText:@"请输入正确的WiFi密码"]; return; } if(self.model.name.length == 0){ [UICommon MessageErrorText:@"请输入WiFi名称"]; return; } [self.model postWIFIDataSuccess:^(id _Nonnull responseObject) { xLog(@"保存WIFI数据成功"); [UICommon MessageSuccessText:@"设备WiFi设置成功" isImg:YES]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UICommon HidenLoading]; [weakSelf.navigationController popViewControllerAnimated:YES]; }); } failure:^(NSError * _Nonnull error) { }]; } - (void)dealloc { if (_observe) { [[NSNotificationCenter defaultCenter] removeObserver:_observe]; } xLog(@"WiFiViewController dealloc"); } @end