// // YXMyDeviceViewController.m // Temperature // // Created by TC on 2025/2/22. // #import "YXMyDeviceViewController.h" #import "YXMyDeviceTableViewCell.h" #import "YXMyDeviceModel.h" #import "YXDeviceInfoViewController.h" #import #import "YXMyDeviceModel.h" #import "RDSRootControl.h" @interface YXMyDeviceViewController () @property (weak, nonatomic) UITableView *tableView; @property (copy, nonatomic) NSArray *devicesArray; @property (weak, nonatomic) UIButton *menuBtn; @property (weak, nonatomic) UILabel *numLabel; @property (copy, nonatomic) NSString *homeID; @property (strong, nonatomic) UILabel *emptyLabel; @end @implementation YXMyDeviceViewController - (void)viewDidLoad { [super viewDidLoad]; // 隐藏导航栏 self.fd_prefersNavigationBarHidden = YES; self.navigationItem.title = @"设备耗材"; self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; [self createTableView]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self p_getHomeList]; } - (void)p_getHomeList { [RDSHomeModel rds_getHomeListIsAdmin:YES finished:^(NSError * _Nullable error) { [self getDeviceConsumables:TheDataManager.current_home_id]; self.homeID = TheDataManager.current_home_id; }]; } -(void)getDeviceConsumables:(NSString *)homeId { RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_getDeviceConsumablesWithHomeId:homeId Success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { NSDictionary *dataDic = responseObject[@"data"]; NSDictionary *devices = dataDic[@"devices"]; NSMutableArray *tempArray = [YXMyDeviceModel mj_objectArrayWithKeyValuesArray:devices]; for (YXMyDeviceModel *model in tempArray) { model.consumables = [YXConsumablesModel mj_objectArrayWithKeyValuesArray:model.consumables]; } weakSelf.devicesArray = tempArray; if (weakSelf.devicesArray.count == 0) { weakSelf.emptyLabel.hidden = NO; }else{ weakSelf.emptyLabel.hidden = YES; } NSNumber *enough = dataDic[@"enough"]; NSString *desc = dataDic[@"desc"]; if ([enough integerValue] == 1) { self.numLabel.textColor = [UIColor colorWithHexString:@"111111"]; }else{ self.numLabel.textColor = [UIColor redColor]; } self.numLabel.text = desc; [weakSelf.tableView reloadData]; } else{ [RDSHudShower showErrorWithStatus:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)resetDeviceConsumablesWithId:(NSString *)deviceId { RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_resetDeviceConsumablesWithId:deviceId success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower showSuccessWithStatus:@"重置成功"]; [weakSelf getDeviceConsumables:weakSelf.homeID]; } else{ [RDSHudShower showErrorWithStatus:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(UILabel *)emptyLabel { if (_emptyLabel == nil) { _emptyLabel = [[UILabel alloc]init]; _emptyLabel.text = @"暂无数据"; _emptyLabel.font = [UIFont systemFontOfSize:20]; _emptyLabel.textColor = [UIColor colorWithHexString:@"333333"]; _emptyLabel.textAlignment = NSTextAlignmentCenter; _emptyLabel.hidden = YES; [self.view addSubview:_emptyLabel]; [_emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(@200); make.centerX.equalTo(@0); make.height.equalTo(@50); make.centerY.equalTo(@0); }]; } return _emptyLabel; } -(void)createTableView { UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped]; tableView.delegate = self; tableView.dataSource = self; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [tableView registerNib:[UINib nibWithNibName:@"YXMyDeviceTableViewCell" bundle:nil] forCellReuseIdentifier:@"myDeviceCellID"]; tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; tableView.insetsContentViewsToSafeArea = false; [self.view addSubview:tableView]; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@0); make.right.equalTo(@0); make.left.equalTo(@0); make.bottom.equalTo(@0); }]; self.tableView = tableView; [self createTableHeaderView]; } -(void)createTableHeaderView { UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 190)]; headerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; self.tableView.tableHeaderView = headerView; UIImageView *bgImgView = [[UIImageView alloc]init]; bgImgView.image = [UIImage imageNamed:@"my_top_bg"]; [headerView addSubview:bgImgView]; [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.left.right.equalTo(@0); }]; UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [backBtn setImage:[UIImage imageNamed:@"home_left_arrow"] forState:UIControlStateNormal]; [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [headerView addSubview:backBtn]; [backBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@60); make.left.equalTo(@15); make.width.equalTo(@20); make.height.equalTo(@20); }]; UILabel *titleLabel = [[UILabel alloc]init]; titleLabel.textColor = [UIColor colorWithHexString:@"#1E1E1E"]; titleLabel.font = [UIFont boldSystemFontOfSize:18]; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.text = @"设备耗材"; [headerView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(backBtn.mas_centerY); make.centerX.equalTo(@0); make.width.equalTo(@150); make.height.equalTo(@30); }]; UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [menuBtn setTitle:TheDataManager.current_home_name forState:UIControlStateNormal]; menuBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18]; [menuBtn setTitleColor:[UIColor colorWithHexString:@"#1E1E1E"] forState:UIControlStateNormal]; [menuBtn addTarget:self action:@selector(menuBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [headerView addSubview:menuBtn]; [menuBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(@15); make.top.equalTo(backBtn.mas_bottom).offset(30); // make.width.equalTo(@150); make.height.equalTo(@30); }]; self.menuBtn = menuBtn; UIImageView *imgView = [[UIImageView alloc]init]; imgView.image = [UIImage imageNamed:@"home_top_arrow"]; [headerView addSubview:imgView]; [imgView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(menuBtn.mas_centerY); make.left.equalTo(menuBtn.mas_right).offset(2); make.width.equalTo(@25); make.height.equalTo(@18); }]; UILabel *numLabel = [[UILabel alloc]init]; numLabel.textColor = [UIColor colorWithHexString:@"#999999"]; numLabel.font = [UIFont systemFontOfSize:14]; [headerView addSubview:numLabel]; [numLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(menuBtn.mas_bottom).offset(10); make.left.equalTo(@15); make.right.equalTo(@-15); make.height.equalTo(@30); }]; self.numLabel = numLabel; } #pragma mark - 右上角按钮 -(void)backBtnAction:(UIButton *)button { [self.navigationController popViewControllerAnimated:YES]; } - (void)menuBtnAction:(UIButton *)button { NSMutableArray *titleArray = [[NSMutableArray alloc]initWithCapacity:5]; for (int i = 0; i < TheDataManager.homes.count; i++) { RDSHomeModel *model = TheDataManager.homes[i]; [titleArray addObject:model.name]; } [YBPopupMenu showRelyOnView:button titles:titleArray icons:nil menuWidth:130 otherSettings:^(YBPopupMenu *popupMenu) { popupMenu.delegate = self; popupMenu.animationManager.duration = 0.25; popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"]; }]; } #pragma mark - YPPopupMenuDelegate - (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index { RDSHomeModel *model = TheDataManager.homes[index]; [self getDeviceConsumables:model.record_id]; self.homeID = model.record_id; [self.menuBtn setTitle:model.name forState:UIControlStateNormal]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.devicesArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { YXMyDeviceModel *deviceModel = self.devicesArray[section]; return deviceModel.consumables.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { YXMyDeviceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myDeviceCellID" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; YXMyDeviceModel *deviceModel = self.devicesArray[indexPath.section]; YXConsumablesModel *model = deviceModel.consumables[indexPath.row]; if (indexPath.row == deviceModel.consumables.count - 1) { cell.lineView.hidden = YES; }else{ cell.lineView.hidden = NO; } if ([model.user_rate floatValue] >= 0) { [cell.circleBar setProgress:[model.user_rate floatValue] animated:YES]; } cell.titleLabel.text = model.consumable_name; cell.detailLabel.text = [NSString stringWithFormat:@"已使用%@天",model.used_time]; RDS_WEAKSELF(weakSelf) cell.resetClick = ^(UITableViewCell * cell) { [weakSelf resetDeviceConsumablesWithId:model.record_id]; }; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)]; headerBgView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; UIView *headerView = [[UIView alloc]init]; headerView.backgroundColor = [UIColor whiteColor]; [headerBgView addSubview:headerView]; [headerView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@0); make.left.equalTo(@15); make.right.equalTo(@-15); make.bottom.equalTo(@0); }]; UIImageView *imgView = [[UIImageView alloc]init]; imgView.image = [UIImage imageNamed:@"home_device_1"]; [headerView addSubview:imgView]; [imgView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(@0); make.left.equalTo(@15); make.width.equalTo(@40); make.height.equalTo(@40); }]; UILabel *titleLabel = [[UILabel alloc]init]; titleLabel.textColor = [UIColor colorWithHexString:@"#333333"]; titleLabel.font = [UIFont boldSystemFontOfSize:16]; [headerView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(imgView.mas_top); make.left.equalTo(imgView.mas_right).offset(15); make.right.equalTo(@-15); make.height.equalTo(@20); }]; UILabel *textLabel = [[UILabel alloc]init]; textLabel.textColor = [UIColor colorWithHexString:@"#999999"]; textLabel.font = [UIFont systemFontOfSize:13]; [headerView addSubview:textLabel]; [textLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleLabel.mas_bottom); make.left.equalTo(imgView.mas_right).offset(15); make.right.equalTo(@-15); make.height.equalTo(@20); }]; UIView *lineView = [[UIView alloc]init]; lineView.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"]; [headerBgView addSubview:lineView]; [lineView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(@30); make.right.equalTo(@-30); make.height.equalTo(@1); make.bottom.equalTo(@-1); }]; YXMyDeviceModel *deviceModel = self.devicesArray[section]; if (deviceModel.consumables.count == 0) { lineView.hidden = YES; }else{ lineView.hidden = NO; } titleLabel.text = deviceModel.device_name; textLabel.text = deviceModel.device_type_name; return headerBgView; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 10)]; footerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; return footerView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 80; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 85; } /* #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