DBBodyPreviewViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // The MIT License
  2. //
  3. // Copyright (c) 2016 Dariusz Bukowski
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "DBBodyPreviewViewController.h"
  23. typedef NS_ENUM(NSUInteger, DBBodyPreviewViewControllerViewState) {
  24. DBBodyPreviewViewControllerViewStateLoading,
  25. DBBodyPreviewViewControllerViewStateShowingText,
  26. DBBodyPreviewViewControllerViewStateShowingImage,
  27. };
  28. @interface DBBodyPreviewViewController ()
  29. @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
  30. @property (weak, nonatomic) IBOutlet UITextView *textView;
  31. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  32. @end
  33. @implementation DBBodyPreviewViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. }
  37. - (void)configureWithRequestModel:(DBRequestModel *)requestModel mode:(DBBodyPreviewViewControllerMode)mode {
  38. self.title = mode == DBBodyPreviewViewControllerModeRequest ? @"Request body" : @"Response body";
  39. [self setViewState:DBBodyPreviewViewControllerViewStateLoading animated:YES];
  40. DBRequestModelBodyType bodyType = mode == DBBodyPreviewViewControllerModeRequest ? requestModel.requestBodyType : requestModel.responseBodyType;
  41. void (^completion)(NSData *) = ^void(NSData *data) {
  42. if (bodyType == DBRequestModelBodyTypeImage) {
  43. self.imageView.image = [UIImage imageWithData:data];
  44. [self setViewState:DBBodyPreviewViewControllerViewStateShowingImage animated:YES];
  45. } else {
  46. NSString *dataString;
  47. if (bodyType == DBRequestModelBodyTypeJSON) {
  48. NSError *error;
  49. NSJSONSerialization *jsonSerialization = [NSJSONSerialization JSONObjectWithData:data ?: [NSData data]
  50. options:NSJSONReadingAllowFragments
  51. error:&error];
  52. if (error) {
  53. dataString = @"Unable to read the data.";
  54. } else {
  55. data = [NSJSONSerialization dataWithJSONObject:jsonSerialization options:NSJSONWritingPrettyPrinted error:nil];
  56. dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  57. dataString = [dataString stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
  58. }
  59. } else {
  60. NSString *UTF8DecodedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  61. if (UTF8DecodedString == nil) {
  62. NSMutableString *mutableDataString = [NSMutableString stringWithCapacity:data.length * 2];
  63. const unsigned char *dataBytes = [data bytes];
  64. for (NSInteger index = 0; index < data.length; index++) {
  65. [mutableDataString appendFormat:@"%02x", dataBytes[index]];
  66. }
  67. dataString = [mutableDataString copy];
  68. } else {
  69. dataString = UTF8DecodedString;
  70. }
  71. }
  72. self.textView.text = dataString;
  73. [self setViewState:DBBodyPreviewViewControllerViewStateShowingText animated:YES];
  74. }
  75. };
  76. if (mode == DBBodyPreviewViewControllerModeRequest) {
  77. [requestModel readRequestBodyWithCompletion:completion];
  78. } else {
  79. [requestModel readResponseBodyWithCompletion:completion];
  80. }
  81. }
  82. - (void)setViewState:(DBBodyPreviewViewControllerViewState)state animated:(BOOL)animated {
  83. [UIView animateWithDuration:animated ? 0.35 : 0.0 animations:^{
  84. self.activityIndicator.alpha = 0.0;
  85. self.textView.alpha = 0.0;
  86. self.imageView.alpha = 0.0;
  87. switch (state) {
  88. case DBBodyPreviewViewControllerViewStateLoading: {
  89. [self.activityIndicator startAnimating];
  90. self.activityIndicator.alpha = 1.0;
  91. break;
  92. }
  93. case DBBodyPreviewViewControllerViewStateShowingText:
  94. self.textView.alpha = 1.0;
  95. break;
  96. case DBBodyPreviewViewControllerViewStateShowingImage:
  97. self.imageView.alpha = 1.0;
  98. }
  99. } completion:^(BOOL finished) {
  100. if (state != DBBodyPreviewViewControllerViewStateLoading) {
  101. [self.activityIndicator stopAnimating];
  102. }
  103. }];
  104. }
  105. @end