AuthInspectorViewController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "AuthInspectorViewController.h"
  17. @import GoogleSignIn;
  18. static NSString * const kReusableCellIdentifier = @"AuthInspectorCell";
  19. static CGFloat const kVeryTallConstraint = 10000.f;
  20. static CGFloat const kTableViewCellFontSize = 16.f;
  21. static CGFloat const kTableViewCellPadding = 22.f;
  22. @interface AuthInspectorViewController () <UITableViewDataSource, UITableViewDelegate>
  23. @end
  24. @implementation AuthInspectorViewController {
  25. // Key-paths for the GIDSignIn instance to inspect.
  26. NSArray *_keyPaths;
  27. }
  28. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  29. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  30. if (self) {
  31. _keyPaths = @[
  32. @"accessToken.tokenString",
  33. @"accessToken.expirationDate",
  34. @"refreshToken.tokenString",
  35. @"idToken.tokenString",
  36. @"grantedScopes",
  37. @"userID",
  38. @"profile.email",
  39. @"profile.name",
  40. ];
  41. }
  42. return self;
  43. }
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero
  47. style:UITableViewStyleGrouped];
  48. tableView.delegate = self;
  49. tableView.dataSource = self;
  50. tableView.frame = self.view.bounds;
  51. [self.view addSubview:tableView];
  52. }
  53. - (void)viewDidLayoutSubviews {
  54. if (self.view.subviews.count) {
  55. ((UIView *)self.view.subviews[0]).frame = self.view.bounds;
  56. }
  57. }
  58. #pragma mark - UITableViewDataSource
  59. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  60. return (NSInteger)[_keyPaths count];
  61. }
  62. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  63. return [self contentForSectionHeader:section];
  64. }
  65. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  66. return 1;
  67. }
  68. - (UITableViewCell *)tableView:(UITableView *)tableView
  69. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  70. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReusableCellIdentifier];
  71. if (!cell) {
  72. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  73. reuseIdentifier:kReusableCellIdentifier];
  74. }
  75. cell.textLabel.font = [UIFont systemFontOfSize:kTableViewCellFontSize];
  76. cell.textLabel.numberOfLines = 0;
  77. cell.textLabel.text = [self contentForRowAtIndexPath:indexPath];
  78. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  79. return cell;
  80. }
  81. #pragma mark - UITableViewDelegate
  82. - (void)tableView:(UITableView *)tableView
  83. willDisplayHeaderView:(UIView *)view
  84. forSection:(NSInteger)section {
  85. // The default header view capitalizes the title, which we don't want (because it's the key path).
  86. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  87. ((UITableViewHeaderFooterView *)view).textLabel.text = [self contentForSectionHeader:section];
  88. }
  89. }
  90. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  91. return [self heightForTableView:tableView content:[self contentForSectionHeader:section]]
  92. - (section ? kTableViewCellPadding : 0); // to remove the extra padding in later sections.
  93. }
  94. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  95. return [self heightForTableView:tableView content:[self contentForRowAtIndexPath:indexPath]];
  96. }
  97. #pragma mark - Helpers
  98. - (NSString *)contentForSectionHeader:(NSInteger)section {
  99. return _keyPaths[section];
  100. }
  101. - (NSString *)contentForRowAtIndexPath:(NSIndexPath *)indexPath {
  102. NSString *keyPath = _keyPaths[indexPath.section];
  103. return [[GIDSignIn.sharedInstance.currentUser valueForKeyPath:keyPath] description];
  104. }
  105. - (CGFloat)heightForTableView:(UITableView *)tableView content:(NSString *)content {
  106. CGSize constraintSize =
  107. CGSizeMake(tableView.frame.size.width - 2 * kTableViewCellPadding, kVeryTallConstraint);
  108. CGSize size;
  109. UIFont *font = [UIFont systemFontOfSize:kTableViewCellFontSize];
  110. NSDictionary *attributes = @{ NSFontAttributeName : font };
  111. size = [content boundingRectWithSize:constraintSize
  112. options:NSStringDrawingUsesLineFragmentOrigin
  113. attributes:attributes
  114. context:NULL].size;
  115. return size.height + kTableViewCellPadding;
  116. }
  117. @end