2
0

WFCDiagnoseViewController.m 5.5 KB

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