YXAddFamilyViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // YXAddFamilyViewController.m
  3. // Temperature
  4. //
  5. // Created by TC on 2025/3/23.
  6. //
  7. #import "YXAddFamilyViewController.h"
  8. #import "YXAddressViewController.h"
  9. #import "YXDefaultRoomModel.h"
  10. #import "RDSRootControl.h"
  11. @interface YXAddFamilyViewController ()<UITableViewDataSource,UITableViewDelegate>
  12. @property (weak, nonatomic) UIView *contentView;
  13. @property (weak, nonatomic) UITableView *tableView;
  14. @property (weak, nonatomic) UITextField *nameTextField;
  15. @property (weak, nonatomic) UILabel *adTextLabel;
  16. @property (strong, nonatomic) NSMutableArray *familyArray;
  17. @property (nonatomic,copy) NSString *province;
  18. @property (nonatomic,copy) NSString *city;
  19. @property (nonatomic,copy) NSString *area;
  20. @property (nonatomic,copy) NSString *address;
  21. @end
  22. @implementation YXAddFamilyViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  26. self.navigationItem.title = @"新建家庭";
  27. self.familyArray = [[NSMutableArray alloc]initWithCapacity:5];
  28. [self createTableView];
  29. [self getDefaultRooms];
  30. }
  31. -(void)getDefaultRooms
  32. {
  33. RDS_WEAKSELF(weakSelf)
  34. [RDSDemoApiHelper rds_getDefaultRoomsSuccess:^(id responseObject) {
  35. if ([responseObject[@"code"] intValue] == 9999) {
  36. [RDSRootControl shareControl].isLoginSuccess = NO;
  37. TheDataManager.token = @"";
  38. }
  39. if ([responseObject[@"code"] intValue] == 0) {
  40. NSDictionary *dataDic = responseObject[@"data"];
  41. NSArray *list = dataDic[@"list"];
  42. weakSelf.familyArray = [YXDefaultRoomModel mj_objectArrayWithKeyValuesArray:list];
  43. [weakSelf.tableView reloadData];
  44. } else{
  45. [RDSHudShower showBottomToast:responseObject[@"message"]];
  46. }
  47. } failure:^(NSError *error) {
  48. }];
  49. }
  50. -(void)createTableView
  51. {
  52. UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped];
  53. tableView.delegate = self;
  54. tableView.dataSource = self;
  55. tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  56. tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  57. [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"addFamilyCellID"];
  58. [self.view addSubview:tableView];
  59. self.tableView = tableView;
  60. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.top.equalTo(@10);
  62. make.right.equalTo(@-15);
  63. make.left.equalTo(@15);
  64. make.bottom.equalTo(@(-kSafeBottomHeight));
  65. }];
  66. tableView.layer.cornerRadius = 8;
  67. tableView.clipsToBounds = YES;
  68. UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  69. [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
  70. [saveBtn addTarget:self action:@selector(saveBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  71. [saveBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
  72. saveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  73. saveBtn.backgroundColor = [UIColor colorWithHexString:@"#267AFF"];
  74. [self.view addSubview:saveBtn];
  75. [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.height.equalTo(@45);
  77. make.left.equalTo(@20);
  78. make.right.equalTo(@-20);
  79. make.bottom.equalTo(@(-kSafeBottomHeight));
  80. }];
  81. saveBtn.layer.cornerRadius = 22.5;
  82. [self createTableHeaderView];
  83. [self.tableView setEditing:YES animated:YES];
  84. }
  85. -(void)saveBtnAction:(UIButton *)btn
  86. {
  87. if (self.nameTextField.text.length == 0) {
  88. [RDSHudShower showBottomToast:@"家庭名称不能为空"];
  89. return;
  90. }
  91. NSMutableArray *roomsArray = [[NSMutableArray alloc]initWithCapacity:5];
  92. for (YXDefaultRoomModel *model in self.familyArray) {
  93. [roomsArray addObject:model.name];
  94. }
  95. RDS_WEAKSELF(weakSelf)
  96. [RDSDemoApiHelper rds_addHomeWithName:self.nameTextField.text province:self.province city:self.city district:self.area address:self.address rooms:roomsArray success:^(id responseObject) {
  97. if ([responseObject[@"code"] intValue] == 9999) {
  98. [RDSRootControl shareControl].isLoginSuccess = NO;
  99. TheDataManager.token = @"";
  100. }
  101. if ([responseObject[@"code"] intValue] == 0) {
  102. [RDSHudShower showBottomToast:@"创建成功"];
  103. [weakSelf popViewControllerAnimated:YES];
  104. } else{
  105. [RDSHudShower showBottomToast:responseObject[@"message"]];
  106. }
  107. } failure:^(NSError *error) {
  108. }];
  109. }
  110. -(void)createTableHeaderView
  111. {
  112. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 180)];
  113. headerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  114. UIView *bgView = [[UIView alloc]init];
  115. bgView.backgroundColor = [UIColor whiteColor];
  116. [headerView addSubview:bgView];
  117. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  118. make.top.equalTo(@15);
  119. make.right.equalTo(@0);
  120. make.left.equalTo(@0);
  121. make.height.equalTo(@110);
  122. }];
  123. bgView.layer.cornerRadius = 8;
  124. UILabel *nameLabel = [[UILabel alloc]init];
  125. nameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  126. nameLabel.font = [UIFont systemFontOfSize:15];
  127. nameLabel.text = @"家庭名称";
  128. nameLabel.font = [UIFont boldSystemFontOfSize:15];
  129. [bgView addSubview:nameLabel];
  130. [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.top.equalTo(@0);
  132. make.left.equalTo(@15);
  133. make.height.equalTo(@55);
  134. make.width.equalTo(@80);
  135. }];
  136. UITextField *nameTextField = [[UITextField alloc]init];
  137. nameTextField.placeholder = @"请输入家庭名称(20字以内)";
  138. nameTextField.font = [UIFont systemFontOfSize:15];
  139. nameTextField.textAlignment = NSTextAlignmentRight;
  140. [bgView addSubview:nameTextField];
  141. [nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
  142. make.top.equalTo(@0);
  143. make.right.equalTo(@-15);
  144. make.height.equalTo(@55);
  145. make.left.equalTo(nameLabel.mas_right).offset(5);
  146. }];
  147. self.nameTextField = nameTextField;
  148. UIView *lineView = [[UIView alloc]init];
  149. lineView.backgroundColor = [UIColor colorWithHexString:@"#F2F5FA"];
  150. [bgView addSubview:lineView];
  151. [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  152. make.top.equalTo(nameLabel.mas_bottom);
  153. make.right.equalTo(@-15);
  154. make.left.equalTo(@15);
  155. make.height.equalTo(@1);
  156. }];
  157. UILabel *adNameLabel = [[UILabel alloc]init];
  158. adNameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  159. adNameLabel.font = [UIFont systemFontOfSize:15];
  160. adNameLabel.text = @"家庭地址";
  161. adNameLabel.font = [UIFont boldSystemFontOfSize:15];
  162. [bgView addSubview:adNameLabel];
  163. [adNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  164. make.top.equalTo(lineView.mas_bottom);
  165. make.left.equalTo(@15);
  166. make.height.equalTo(@55);
  167. make.width.equalTo(@80);
  168. }];
  169. UIImageView *imgView = [[UIImageView alloc]init];
  170. imgView.image = [UIImage imageNamed:@"home_right_arrow"];
  171. [bgView addSubview:imgView];
  172. [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  173. make.centerY.equalTo(adNameLabel.mas_centerY);
  174. make.right.equalTo(@-15);
  175. make.width.equalTo(@9);
  176. make.height.equalTo(@15);
  177. }];
  178. UILabel *adTextLabel = [[UILabel alloc]init];
  179. adTextLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  180. adTextLabel.font = [UIFont systemFontOfSize:15];
  181. adTextLabel.text = @"请选择地址";
  182. adTextLabel.textAlignment = NSTextAlignmentRight;
  183. [bgView addSubview:adTextLabel];
  184. [adTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  185. make.top.equalTo(lineView.mas_bottom);
  186. make.right.equalTo(imgView.mas_left).offset(-10);
  187. make.height.equalTo(@55);
  188. make.left.equalTo(adNameLabel.mas_right).offset(5);
  189. }];
  190. adTextLabel.userInteractionEnabled = YES;
  191. self.adTextLabel = adTextLabel;
  192. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toAddressAction)];
  193. [adTextLabel addGestureRecognizer:tap];
  194. UILabel *listLabel = [[UILabel alloc]init];
  195. listLabel.textColor = [UIColor colorWithHexString:@"#666666"];
  196. listLabel.font = [UIFont systemFontOfSize:15];
  197. listLabel.text = @"房间列表";
  198. [headerView addSubview:listLabel];
  199. [listLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  200. make.top.equalTo(bgView.mas_bottom);
  201. make.left.equalTo(@15);
  202. make.height.equalTo(@55);
  203. make.width.equalTo(@150);
  204. }];
  205. UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  206. [addBtn setTitle:@"新增房间" forState:UIControlStateNormal];
  207. [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  208. [addBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal];
  209. addBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  210. [headerView addSubview:addBtn];
  211. [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  212. make.top.equalTo(bgView.mas_bottom);
  213. make.right.equalTo(@-15);
  214. make.height.equalTo(@55);
  215. make.width.equalTo(@80);
  216. }];
  217. self.tableView.tableHeaderView = headerView;
  218. }
  219. -(void)addBtnAction:(UIButton *)btn
  220. {
  221. [self alertWithText:@"房间名称"];
  222. }
  223. -(void)alertWithText:(NSString *)text
  224. {
  225. RDS_WEAKSELF(weakSelf)
  226. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:text message:nil preferredStyle:UIAlertControllerStyleAlert];
  227. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  228. UITextField *textFeild = [alertController.textFields objectAtIndex:0];
  229. NSLog(@"%@",textFeild.text);
  230. YXDefaultRoomModel *newModel = [[YXDefaultRoomModel alloc]init];
  231. newModel.name = textFeild.text;
  232. [weakSelf.familyArray addObject:newModel];
  233. [weakSelf.tableView reloadData];
  234. }];
  235. [alertController addAction:sureAction];
  236. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  237. }];
  238. [alertController addAction:cancelAction];
  239. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  240. textField.placeholder = [NSString stringWithFormat:@"请输入%@",text];
  241. }];
  242. [self presentViewController:alertController animated:YES completion:nil];
  243. }
  244. -(void)toAddressAction
  245. {
  246. RDS_WEAKSELF(weakSelf)
  247. YXAddressViewController *vc = [[YXAddressViewController alloc]init];
  248. [self.navigationController pushViewController:vc animated:YES];
  249. vc.sureBtnClick = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull area, NSString * _Nonnull address) {
  250. weakSelf.province = province;
  251. weakSelf.city = city;
  252. weakSelf.area = area;
  253. weakSelf.address = address;
  254. weakSelf.adTextLabel.text = [NSString stringWithFormat:@"%@%@%@%@",province,city,area,address];
  255. };
  256. }
  257. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  258. {
  259. return 1;
  260. }
  261. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  262. {
  263. return self.familyArray.count;
  264. }
  265. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  266. {
  267. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addFamilyCellID" forIndexPath:indexPath];
  268. YXDefaultRoomModel *model = self.familyArray[indexPath.row];
  269. cell.textLabel.text = model.name;
  270. return cell;
  271. }
  272. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  273. {
  274. }
  275. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  276. {
  277. UIView *headerView = [[UIView alloc]init];
  278. return headerView;
  279. }
  280. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  281. {
  282. UIView *footerView = [[UIView alloc]init];
  283. return footerView;
  284. }
  285. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  286. {
  287. return 0.01;
  288. }
  289. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  290. {
  291. return 0.01;
  292. }
  293. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  294. {
  295. return 65;
  296. }
  297. #pragma mark - edit
  298. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  299. {
  300. return YES;
  301. }
  302. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  303. {
  304. return UITableViewCellEditingStyleDelete;
  305. }
  306. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  307. {
  308. if (editingStyle == UITableViewCellEditingStyleDelete) {
  309. [self.familyArray removeObjectAtIndex:indexPath.row];
  310. [tableView reloadData];
  311. }
  312. }
  313. /*
  314. #pragma mark - Navigation
  315. // In a storyboard-based application, you will often want to do a little preparation before navigation
  316. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  317. // Get the new view controller using [segue destinationViewController].
  318. // Pass the selected object to the new view controller.
  319. }
  320. */
  321. @end