ifish/Ifish/controllers/FishTinkController/maincontroller/IfishDeviceControlNameBaseC...

253 lines
8.7 KiB
Objective-C

//
// IfishDeviceControlNameBaseController.m
// Ifish
//
// Created by imac on 2017/6/13.
// Copyright © 2017年 lianlian. All rights reserved.
//
#import "IfishDeviceControlNameBaseController.h"
#import "RuSunChangeNameTitleViewCell.h"
#define RUSUN_DEVICENAME 100001
#import "MBProgressHUD.h"
@interface IfishDeviceControlNameBaseController ()
@property(nonatomic,strong)MBProgressHUD *HUD;
@end
//除零控之外所有设备名称 ,控制按钮名称 。可修改 睿芯独立设备单独 参见RunSunChangeNameViewController
@implementation IfishDeviceControlNameBaseController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addTitleViewWithTitle:@"修改名称"];
UIButton *tureButton = [UIButton buttonWithType:UIButtonTypeCustom];
tureButton.frame = CGRectMake(kScreenSize.width - 100, 10, 60, 60);
[tureButton setTitle:@"确定" forState:UIControlStateNormal];
[tureButton addTarget: self action: @selector(sureAction) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithCustomView:tureButton];
self.navigationItem.rightBarButtonItem=right;
self.name = self.device.showName;
[self creatTab];
}
#pragma mark sureAction
-(void)sureAction{}
-(void)showGressView{
_HUD=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
-(void)hideGressView{
[_HUD hide:YES];
}
-(void)creatTab{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStyleGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.scrollEnabled = YES;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.backgroundColor = RGB(242, 242, 242);
[self.view addSubview:self.tableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section==0) {
return 1;
}
return 0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==0) {
return [self baseViewLoadDeviceNameCell:indexPath intable:tableView];
}
return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 10;
}
-(UITableViewCell*)baseViewLoadDeviceNameCell:(NSIndexPath *)indexPath intable:(UITableView *)tableView
{
RuSunChangeNameTitleViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"RuSunChangeNameTitleViewCell"];
if (cell==nil) {
cell= [[[NSBundle mainBundle]loadNibNamed:@"RuSunChangeNameTitleViewCell" owner:self options:nil]lastObject];
}
cell.tag = RUSUN_DEVICENAME;
cell.deviceName.text = self.name;
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section==1) {
UIView *holdView = [[UIView alloc] init];
holdView.frame =CGRectMake(0, 0, kScreenSize.width, 10);
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redact_line"]];
img.frame = CGRectMake(19, 0, 12, 10);
[holdView addSubview:img];
return holdView;
}
return nil;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//设备名
-(void)upLoadDeviceName{
[self showGressView];
RuSunChangeNameTitleViewCell*cell = [self.view viewWithTag:RUSUN_DEVICENAME];
[cell.deviceName resignFirstResponder];
if (self.name!=0) {
AFHTTPRequestOperationManager*mannager=[AFHTTPRequestOperationManager manager];
mannager.responseSerializer=[AFHTTPResponseSerializer serializer];
NSMutableDictionary * para = [NSMutableDictionary dictionary];
NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
NSString *priId=self.device.deviceId;
;
NSString*userId=self.device.userId;
[para setValue:priId forKey:@"priId.deviceId"];
[para setValue:userId forKey:@"priId.userId"];
[para setValue: cell.deviceName.text forKey:@"showName"];
if (cell.deviceName.text.length <= 6) {
NSString*url=kUpdateDeviceUser;
if ([self.device.type isEqualToString:DECICE_TYPE_XUANDUO3F])
{
url=[NSString stringWithFormat:@"%@/api/user/updatePetDeviceUser.do",JIEKOUPORT];
}
[mannager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"result:%@",resultDic[@"result"]);
[self hideGressView];
if ([resultDic[@"result"] isEqualToString:@"100"]) {
[self.view makeToast:@"修改成功"];
[[NSNotificationCenter defaultCenter]postNotificationName:@"updateUserInfobyValidation" object:nil];
NSString *strName =[NSString stringWithFormat:@"%@",cell.deviceName.text];
NSMutableArray *newDeviceArr =[[NSMutableArray alloc] init];
if (self.NameWithBlock) {
self.NameWithBlock(strName);
}
[cell.deviceName resignFirstResponder];
//修改数据库中对应设备名称
for (DeviceModel *model in deviceArry) {
if ([model.deviceId isEqual:self.device.deviceId]) {
model.showName = strName;
}
[newDeviceArr addObject:model];
}
[[DataCenter defaultDtacenter] setValue:newDeviceArr forKey:@"deviceInfo"];
[self performSelector:@selector(popLastView) withObject:self afterDelay:1.6];
}else if ([resultDic[@"result"] isEqualToString:@"101"]){
[self showTitle:@"" messsage:@"获取失败"];
}else if ([resultDic[@"result"] isEqualToString:@"301"]){
[self showTitle:@"" messsage:@"请求验证失败,请重新登陆"];
}else if ([resultDic[@"result"] isEqualToString:@"302"]){
[self showTitle:@"" messsage:@"请求被舍弃,未执行"];
}else if ([resultDic[@"result"] isEqualToString:@"207"]){
[self showTitle:@"" messsage:@"设备已解绑请推出后台重新登录"];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self hideGressView];
[self showTitle:@"" messsage:@"请求异常"];
}];
}else{
[self hideGressView];
[self showTitle:@"" messsage:@"名称不能大于6个字"];
}
}else{
[self hideGressView];
[self showTitle:@"" messsage:@"设备名称不能为空"];
}
}
-(void)popLastView{
[self.navigationController popViewControllerAnimated:YES];
}
/*
#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