FLEXCookiesTableViewController.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // FLEXCookiesTableViewController.m
  3. // FLEX
  4. //
  5. // Created by Rich Robinson on 19/10/2015.
  6. // Copyright © 2015 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXCookiesTableViewController.h"
  9. #import "FLEXObjectExplorerFactory.h"
  10. #import "FLEXUtility.h"
  11. @interface FLEXCookiesTableViewController ()
  12. @property (nonatomic, readonly) NSArray<NSHTTPCookie *> *cookies;
  13. @property (nonatomic) NSString *headerTitle;
  14. @end
  15. @implementation FLEXCookiesTableViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. NSSortDescriptor *nameSortDescriptor = [[NSSortDescriptor alloc]
  19. initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)
  20. ];
  21. _cookies = [NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies
  22. sortedArrayUsingDescriptors:@[nameSortDescriptor]
  23. ];
  24. self.title = @"Cookies";
  25. [self updateHeaderTitle];
  26. }
  27. - (void)updateHeaderTitle {
  28. self.headerTitle = [NSString stringWithFormat:@"%@ cookies", @(self.cookies.count)];
  29. // TODO update header title here when we can search cookies
  30. }
  31. - (NSHTTPCookie *)cookieForRowAtIndexPath:(NSIndexPath *)indexPath {
  32. return self.cookies[indexPath.row];
  33. }
  34. #pragma mark - Table View Data Source
  35. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  36. return self.cookies.count;
  37. }
  38. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  39. static NSString *CellIdentifier = @"Cell";
  40. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  41. if (!cell) {
  42. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  43. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  44. cell.detailTextLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  45. cell.detailTextLabel.textColor = UIColor.grayColor;
  46. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  47. }
  48. NSHTTPCookie *cookie = [self cookieForRowAtIndexPath:indexPath];
  49. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", cookie.name, cookie.value];
  50. cell.detailTextLabel.text = cookie.domain;
  51. return cell;
  52. }
  53. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  54. return self.headerTitle;
  55. }
  56. #pragma mark - Table View Delegate
  57. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  58. NSHTTPCookie *cookie = [self cookieForRowAtIndexPath:indexPath];
  59. UIViewController *cookieViewController = (UIViewController *)[FLEXObjectExplorerFactory explorerViewControllerForObject:cookie];
  60. [self.navigationController pushViewController:cookieViewController animated:YES];
  61. }
  62. #pragma mark - FLEXGlobalsEntry
  63. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  64. return @"🍪 Cookies";
  65. }
  66. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  67. return [self new];
  68. }
  69. @end