2
0

DeviceInfoViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // DeviceInfoViewController.m
  3. // WildFireChat
  4. //
  5. // Created by Tom Lee on 2020/5/1.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "DeviceInfoViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "MBProgressHUD.h"
  11. #import "AppService.h"
  12. #import <WFChatUIKit/WFChatUIKit.h>
  13. @interface DeviceInfoViewController ()
  14. @property(nonatomic, strong)UILabel *nameLabel;
  15. @property(nonatomic, strong)UILabel *idLabel;
  16. @property(nonatomic, strong)UILabel *tokenLabel;
  17. @property(nonatomic, strong)UILabel *ownerLabel;
  18. @end
  19. @implementation DeviceInfoViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = [UIColor whiteColor];
  23. CGFloat width = self.view.bounds.size.width;
  24. self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 120, width - 32, 20)];
  25. self.nameLabel.text = [NSString stringWithFormat:@"设备名称:%@", self.device.name];
  26. [self.view addSubview:self.nameLabel];
  27. self.idLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 148, width - 32, 20)];
  28. self.idLabel.text = [NSString stringWithFormat:@"设备ID:%@", self.device.deviceId];
  29. [self.view addSubview:self.idLabel];
  30. self.tokenLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 176, width - 32, 20)];
  31. self.tokenLabel.text = [NSString stringWithFormat:@"设备令牌:%@", self.device.token];
  32. [self.view addSubview:self.tokenLabel];
  33. CGRect bounds = self.view.bounds;
  34. UIButton *modifyBtn = [[UIButton alloc] initWithFrame:CGRectMake((bounds.size.width - 100 - 100 - 20)/2, (bounds.size.height - 20)/2, 100, 40)];
  35. [modifyBtn addTarget:self action:@selector(onModifyBtn:) forControlEvents:UIControlEventTouchDown];
  36. [modifyBtn setTitle:@"修改名称" forState:UIControlStateNormal];
  37. [modifyBtn setBackgroundColor:[UIColor redColor]];
  38. modifyBtn.layer.masksToBounds = YES;
  39. modifyBtn.layer.cornerRadius = 5.f;
  40. modifyBtn.tag = 0;
  41. UIButton *delBtn = [[UIButton alloc] initWithFrame:CGRectMake((bounds.size.width - 100 - 100 - 20)/2 + 100 + 20, (bounds.size.height - 20)/2, 100, 40)];
  42. [delBtn addTarget:self action:@selector(onDeleteBtn:) forControlEvents:UIControlEventTouchDown];
  43. [delBtn setTitle:@"删除设备" forState:UIControlStateNormal];
  44. [delBtn setBackgroundColor:[UIColor redColor]];
  45. delBtn.layer.masksToBounds = YES;
  46. delBtn.layer.cornerRadius = 5.f;
  47. delBtn.tag = 1;
  48. [self.view addSubview:modifyBtn];
  49. [self.view addSubview:delBtn];
  50. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送给我" style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  51. }
  52. - (void)onModifyBtn:(id)sender {
  53. WFCUGeneralModifyViewController *vc = [[WFCUGeneralModifyViewController alloc] init];
  54. vc.defaultValue = self.device.name;
  55. vc.titleText = @"修改设备名";
  56. __weak typeof(self) ws = self;
  57. vc.tryModify = ^(NSString *newValue, void (^result)(BOOL success)) {
  58. [[AppService sharedAppService] addDevice:newValue deviceId:ws.device.deviceId owner:ws.device.owners success:^(Device * _Nonnull device) {
  59. ws.device = device;
  60. ws.nameLabel.text = [NSString stringWithFormat:@"设备名称:%@", ws.device.name];
  61. result(YES);
  62. } error:^(int error_code) {
  63. result(NO);
  64. }];
  65. };
  66. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  67. [self.navigationController presentViewController:nav animated:YES completion:nil];
  68. }
  69. - (void)onDeleteBtn:(id)sender {
  70. __weak typeof(self) ws = self;
  71. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  72. hud.label.text = @"删除中...";
  73. [hud showAnimated:YES];
  74. [[AppService sharedAppService] delDevice:self.device.deviceId success:^(Device * _Nonnull device) {
  75. [hud hideAnimated:NO];
  76. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  77. hud.mode = MBProgressHUDModeText;
  78. hud.label.text = @"已删除";
  79. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  80. [hud hideAnimated:YES afterDelay:1.f];
  81. if (ws.navigationController.viewControllers.count >= 3) {
  82. [ws.navigationController popToViewController:[ws.navigationController.viewControllers objectAtIndex:ws.navigationController.viewControllers.count-3] animated:YES];
  83. }
  84. } error:^(int error_code) {
  85. [hud hideAnimated:NO];
  86. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  87. hud.mode = MBProgressHUDModeText;
  88. hud.label.text = @"删除失败";
  89. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  90. [hud hideAnimated:YES afterDelay:1.f];
  91. }];
  92. }
  93. - (void)onRightBtn:(id)sender {
  94. __weak typeof(self) ws = self;
  95. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  96. hud.label.text = @"发送中...";
  97. [hud showAnimated:YES];
  98. WFCCConversation *conv = [WFCCConversation conversationWithType:Single_Type target:[WFCCNetworkService sharedInstance].userId line:0];
  99. WFCCTextMessageContent *textContent = [WFCCTextMessageContent contentWith:[NSString stringWithFormat:@"DeviceName:%@, \nDeviceId:%@, \nToken:%@", self.device.name, self.device.deviceId, self.device.token]];
  100. [[WFCCIMService sharedWFCIMService] send:conv content:textContent success:^(long long messageUid, long long timestamp) {
  101. [hud hideAnimated:NO];
  102. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  103. hud.mode = MBProgressHUDModeText;
  104. hud.label.text = @"发送成功";
  105. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  106. [hud hideAnimated:YES afterDelay:1.f];
  107. } error:^(int error_code) {
  108. [hud hideAnimated:NO];
  109. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  110. hud.mode = MBProgressHUDModeText;
  111. hud.label.text = @"发送失败";
  112. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  113. [hud hideAnimated:YES afterDelay:1.f];
  114. }];
  115. }
  116. /*
  117. #pragma mark - Navigation
  118. // In a storyboard-based application, you will often want to do a little preparation before navigation
  119. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  120. // Get the new view controller using [segue destinationViewController].
  121. // Pass the selected object to the new view controller.
  122. }
  123. */
  124. @end