CreateDeviceViewController.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // CreateDeviceViewController.m
  3. // WildFireChat
  4. //
  5. // Created by Tom Lee on 2020/5/1.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "CreateDeviceViewController.h"
  9. #import "AppService.h"
  10. #import "MBProgressHUD.h"
  11. @interface CreateDeviceViewController () <UITextFieldDelegate>
  12. @property (nonatomic, strong)UITextField *deviceNameField;
  13. @property (nonatomic, strong)UITextField *deviceIdField;
  14. @property (nonatomic, strong)UIButton *createBtn;
  15. @end
  16. @implementation CreateDeviceViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  20. CGFloat width = self.view.frame.size.width;
  21. self.deviceNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 120, width - 32, 24)];
  22. self.deviceNameField.placeholder = @"请输入设备名称";
  23. self.deviceNameField.delegate = self;
  24. self.deviceNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
  25. [self.deviceNameField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
  26. [self.view addSubview:self.deviceNameField];
  27. self.deviceIdField = [[UITextField alloc] initWithFrame:CGRectMake(16, 160, width - 32, 24)];
  28. self.deviceIdField.placeholder = @"请输入设备ID,如果为空,系统会自动生成";
  29. self.deviceIdField.clearButtonMode = UITextFieldViewModeWhileEditing;
  30. [self.view addSubview:self.deviceIdField];
  31. self.createBtn = [[UIButton alloc] initWithFrame:CGRectMake((width - 160)/2, self.view.bounds.size.height/2 - 20, 160, 40)];
  32. [self.createBtn setTitle:@"创建设备" forState:UIControlStateNormal];
  33. [self.createBtn addTarget:self action:@selector(onCreateBtn:) forControlEvents:UIControlEventTouchDown];
  34. self.createBtn.enabled = NO;
  35. [self.createBtn setBackgroundColor:[UIColor greenColor]];
  36. [self.view addSubview:self.createBtn];
  37. }
  38. - (void)onCreateBtn:(id)sender {
  39. self.createBtn.enabled = NO;
  40. __weak typeof(self) ws = self;
  41. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  42. hud.label.text = @"创建中...";
  43. [hud showAnimated:YES];
  44. [[AppService sharedAppService] addDevice:self.deviceNameField.text deviceId:self.deviceIdField.text owner:@[[WFCCNetworkService sharedInstance].userId] success:^(Device * _Nonnull device) {
  45. [hud hideAnimated:NO];
  46. [ws.navigationController popViewControllerAnimated:YES];
  47. } error:^(int error_code) {
  48. [hud hideAnimated:NO];
  49. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  50. hud.mode = MBProgressHUDModeText;
  51. hud.label.text = @"创建失败";
  52. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  53. [hud hideAnimated:YES afterDelay:1.f];
  54. NSLog(@"Create device error!!!!");
  55. ws.createBtn.enabled = YES;
  56. }];
  57. }
  58. - (void)textDidChange:(id<UITextInput>)textInput {
  59. self.createBtn.enabled = textInput.hasText;
  60. }
  61. @end