AuthInspectorViewController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. @"authentication.accessToken",
  33. @"authentication.accessTokenExpirationDate",
  34. @"authentication.refreshToken",
  35. @"authentication.idToken",
  36. @"grantedScopes",
  37. @"userID",
  38. @"serverAuthCode",
  39. @"profile.email",
  40. @"profile.name",
  41. ];
  42. }
  43. return self;
  44. }
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero
  48. style:UITableViewStyleGrouped];
  49. tableView.delegate = self;
  50. tableView.dataSource = self;
  51. tableView.frame = self.view.bounds;
  52. [self.view addSubview:tableView];
  53. }
  54. - (void)viewDidLayoutSubviews {
  55. if (self.view.subviews.count) {
  56. ((UIView *)self.view.subviews[0]).frame = self.view.bounds;
  57. }
  58. }
  59. #pragma mark - UITableViewDataSource
  60. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  61. return (NSInteger)[_keyPaths count];
  62. }
  63. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  64. return [self contentForSectionHeader:section];
  65. }
  66. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  67. return 1;
  68. }
  69. - (UITableViewCell *)tableView:(UITableView *)tableView
  70. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  71. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReusableCellIdentifier];
  72. if (!cell) {
  73. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  74. reuseIdentifier:kReusableCellIdentifier];
  75. }
  76. cell.textLabel.font = [UIFont systemFontOfSize:kTableViewCellFontSize];
  77. cell.textLabel.numberOfLines = 0;
  78. cell.textLabel.text = [self contentForRowAtIndexPath:indexPath];
  79. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  80. return cell;
  81. }
  82. #pragma mark - UITableViewDelegate
  83. - (void)tableView:(UITableView *)tableView
  84. willDisplayHeaderView:(UIView *)view
  85. forSection:(NSInteger)section {
  86. // The default header view capitalizes the title, which we don't want (because it's the key path).
  87. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  88. ((UITableViewHeaderFooterView *)view).textLabel.text = [self contentForSectionHeader:section];
  89. }
  90. }
  91. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  92. return [self heightForTableView:tableView content:[self contentForSectionHeader:section]]
  93. - (section ? kTableViewCellPadding : 0); // to remove the extra padding in later sections.
  94. }
  95. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  96. return [self heightForTableView:tableView content:[self contentForRowAtIndexPath:indexPath]];
  97. }
  98. #pragma mark - Helpers
  99. - (NSString *)contentForSectionHeader:(NSInteger)section {
  100. return _keyPaths[section];
  101. }
  102. - (NSString *)contentForRowAtIndexPath:(NSIndexPath *)indexPath {
  103. NSString *keyPath = _keyPaths[indexPath.section];
  104. return [[GIDSignIn.sharedInstance.currentUser valueForKeyPath:keyPath] description];
  105. }
  106. - (CGFloat)heightForTableView:(UITableView *)tableView content:(NSString *)content {
  107. CGSize constraintSize =
  108. CGSizeMake(tableView.frame.size.width - 2 * kTableViewCellPadding, kVeryTallConstraint);
  109. CGSize size;
  110. UIFont *font = [UIFont systemFontOfSize:kTableViewCellFontSize];
  111. NSDictionary *attributes = @{ NSFontAttributeName : font };
  112. size = [content boundingRectWithSize:constraintSize
  113. options:NSStringDrawingUsesLineFragmentOrigin
  114. attributes:attributes
  115. context:NULL].size;
  116. return size.height + kTableViewCellPadding;
  117. }
  118. @end