WFCDiagnoseViewController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // WFCDiagnoseViewController.m
  3. // WildFireChat
  4. //
  5. // Created by Heavyrain Lee on 2019/11/11.
  6. // Copyright © 2019 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCDiagnoseViewController.h"
  9. #import "AFNetworking.h"
  10. #import "WFCConfig.h"
  11. #import "AppService.h"
  12. #import <WFChatUIKit/WFChatUIKit.h>
  13. @interface WFCDiagnoseViewController ()
  14. @property (nonatomic, strong)UIActivityIndicatorView *indicatorView;
  15. @property (nonatomic, strong)UILabel *resultLabel;
  16. @property (nonatomic, strong)UIButton *startButton;
  17. @property (nonatomic, strong)UIButton *uploadLogsButton;
  18. @end
  19. @implementation WFCDiagnoseViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.title = LocalizedString(@"Diagnose");
  23. if (@available(iOS 13.0, *)) {
  24. self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
  25. } else {
  26. self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  27. }
  28. self.indicatorView.color = [UIColor grayColor];
  29. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  30. self.indicatorView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/4 - 10);
  31. self.indicatorView.hidden = YES;
  32. [self.view addSubview:self.indicatorView];
  33. self.resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - 150, self.view.bounds.size.height/4-40, 300, 60)];
  34. self.resultLabel.textAlignment = NSTextAlignmentCenter;
  35. self.resultLabel.text = @"点击\"测试网络\"开始测试";
  36. self.resultLabel.numberOfLines = 0;
  37. [self.view addSubview:self.resultLabel];
  38. self.startButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - 80, self.view.bounds.size.height/2 - 20, 160, 40)];
  39. [self.startButton setTitle:@"测试网络" forState:UIControlStateNormal];
  40. [self.startButton setTitleColor:[WFCUConfigManager globalManager].naviTextColor forState:UIControlStateNormal];
  41. [self.startButton setBackgroundColor:[UIColor greenColor]];
  42. self.startButton.layer.masksToBounds = YES;
  43. self.startButton.layer.cornerRadius = 5.0;
  44. [self.startButton addTarget:self action:@selector(onStart:) forControlEvents:UIControlEventTouchDown];
  45. [self.view addSubview:self.startButton];
  46. self.uploadLogsButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - 80, self.view.bounds.size.height/2 + 40, 160, 40)];
  47. [self.uploadLogsButton setTitle:@"上传日志" forState:UIControlStateNormal];
  48. [self.uploadLogsButton setTitleColor:[WFCUConfigManager globalManager].naviTextColor forState:UIControlStateNormal];
  49. [self.uploadLogsButton setBackgroundColor:[UIColor redColor]];
  50. self.uploadLogsButton.layer.masksToBounds = YES;
  51. self.uploadLogsButton.layer.cornerRadius = 5.0;
  52. [self.uploadLogsButton addTarget:self action:@selector(onUploadLogs:) forControlEvents:UIControlEventTouchDown];
  53. [self.view addSubview:self.uploadLogsButton];
  54. }
  55. - (void)onStart:(id)sender {
  56. self.resultLabel.hidden = YES;
  57. self.indicatorView.hidden = NO;
  58. [self.indicatorView startAnimating];
  59. self.startButton.enabled = NO;
  60. self.uploadLogsButton.enabled = NO;
  61. NSDate *now = [[NSDate alloc] init];
  62. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  63. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
  64. __weak typeof(self)ws =self;
  65. [manager GET:[NSString stringWithFormat:@"http://%@%@", IM_SERVER_HOST, @"/api/version"] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  66. if ([responseObject isKindOfClass:[NSDictionary class]]) {
  67. double value = now.timeIntervalSinceNow;
  68. int duration = (int)((-value)*1000 + 0.5);
  69. [ws reportResult:[NSString stringWithFormat:@"测速成功,延时为%dms", duration]];
  70. } else {
  71. [ws reportResult:[NSString stringWithFormat:@"测速失败,无法识别服务器返回的数据:%@", responseObject]];
  72. }
  73. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  74. [ws reportResult:[NSString stringWithFormat:@"测速失败,错误原因:%@", error.localizedDescription]];
  75. }];
  76. }
  77. - (void)onUploadLogs:(id)sender {
  78. self.resultLabel.hidden = YES;
  79. self.indicatorView.hidden = NO;
  80. [self.indicatorView startAnimating];
  81. self.startButton.enabled = NO;
  82. self.uploadLogsButton.enabled = NO;
  83. __weak typeof(self)ws =self;
  84. [[AppService sharedAppService] uploadLogs:^{
  85. [ws reportResult:@"上传成功"];
  86. } error:^(NSString *errorMsg) {
  87. [ws reportResult:[NSString stringWithFormat:@"上传失败:%@", errorMsg]];
  88. }];
  89. }
  90. - (void)reportResult:(NSString *)text {
  91. dispatch_async(dispatch_get_main_queue(), ^{
  92. self.indicatorView.hidden = YES;
  93. self.resultLabel.hidden = NO;
  94. self.resultLabel.text = text;
  95. self.startButton.enabled = YES;
  96. self.uploadLogsButton.enabled = YES;
  97. });
  98. }
  99. /*
  100. #pragma mark - Navigation
  101. // In a storyboard-based application, you will often want to do a little preparation before navigation
  102. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  103. // Get the new view controller using [segue destinationViewController].
  104. // Pass the selected object to the new view controller.
  105. }
  106. */
  107. @end