DeviceTableViewController.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // DeviceTableViewController.m
  3. // WildFireChat
  4. //
  5. // Created by Tom Lee on 2020/5/1.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "DeviceTableViewController.h"
  9. #import "DeviceViewController.h"
  10. #import "CreateDeviceViewController.h"
  11. #import "Device.h"
  12. #import "AppService.h"
  13. #import "MBProgressHUD.h"
  14. @interface DeviceTableViewController () <UITableViewDataSource, UITableViewDelegate>
  15. @property(nonatomic, strong)UITableView *tableView;
  16. @property(nonatomic, strong)NSArray<Device *> *devices;
  17. @end
  18. @implementation DeviceTableViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.title = @"我的设备";
  22. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
  23. if (@available(iOS 15, *)) {
  24. self.tableView.sectionHeaderTopPadding = 0;
  25. }
  26. self.tableView.delegate = self;
  27. self.tableView.dataSource = self;
  28. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  29. [self.tableView reloadData];
  30. [self.view addSubview:self.tableView];
  31. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  32. }
  33. - (void)viewDidAppear:(BOOL)animated {
  34. [super viewDidAppear:animated];
  35. __weak typeof(self) ws = self;
  36. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  37. hud.label.text = @"获取中...";
  38. [hud showAnimated:YES];
  39. [[AppService sharedAppService] getMyDevices:^(NSArray<Device *> * _Nonnull devices) {
  40. [hud hideAnimated:NO];
  41. ws.devices = devices;
  42. [ws.tableView reloadData];
  43. } error:^(int error_code) {
  44. [hud hideAnimated:NO];
  45. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  46. hud.mode = MBProgressHUDModeText;
  47. hud.label.text = @"获取失败";
  48. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  49. [hud hideAnimated:YES afterDelay:1.f];
  50. }];
  51. }
  52. - (void)onRightBtn:(id)sender {
  53. CreateDeviceViewController *vc = [[CreateDeviceViewController alloc] init];
  54. [self.navigationController pushViewController:vc animated:YES];
  55. }
  56. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  57. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  58. if (cell == nil) {
  59. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  60. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  61. }
  62. Device *device = [self.devices objectAtIndex:indexPath.row];
  63. cell.textLabel.text = device.name;
  64. return cell;
  65. }
  66. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  67. return self.devices.count;
  68. }
  69. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  70. Device *device = [self.devices objectAtIndex:indexPath.row];
  71. DeviceViewController *vc = [[DeviceViewController alloc] init];
  72. vc.device = device;
  73. [self.navigationController pushViewController:vc animated:YES];
  74. }
  75. @end