DeviceTableViewController.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. self.tableView.delegate = self;
  24. self.tableView.dataSource = self;
  25. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  26. [self.tableView reloadData];
  27. [self.view addSubview:self.tableView];
  28. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  29. }
  30. - (void)viewDidAppear:(BOOL)animated {
  31. [super viewDidAppear:animated];
  32. __weak typeof(self) ws = self;
  33. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  34. hud.label.text = @"获取中...";
  35. [hud showAnimated:YES];
  36. [[AppService sharedAppService] getMyDevices:^(NSArray<Device *> * _Nonnull devices) {
  37. [hud hideAnimated:NO];
  38. ws.devices = devices;
  39. [ws.tableView reloadData];
  40. } error:^(int error_code) {
  41. [hud hideAnimated:NO];
  42. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  43. hud.mode = MBProgressHUDModeText;
  44. hud.label.text = @"获取失败";
  45. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  46. [hud hideAnimated:YES afterDelay:1.f];
  47. }];
  48. }
  49. - (void)onRightBtn:(id)sender {
  50. CreateDeviceViewController *vc = [[CreateDeviceViewController alloc] init];
  51. [self.navigationController pushViewController:vc animated:YES];
  52. }
  53. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  54. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  55. if (cell == nil) {
  56. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  57. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  58. }
  59. Device *device = [self.devices objectAtIndex:indexPath.row];
  60. cell.textLabel.text = device.name;
  61. return cell;
  62. }
  63. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  64. return self.devices.count;
  65. }
  66. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  67. Device *device = [self.devices objectAtIndex:indexPath.row];
  68. DeviceViewController *vc = [[DeviceViewController alloc] init];
  69. vc.device = device;
  70. [self.navigationController pushViewController:vc animated:YES];
  71. }
  72. @end