CommitListViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // CommitListViewController.m
  3. // FLEXample
  4. //
  5. // Created by Tanner on 3/11/20.
  6. // Copyright © 2020 Flipboard. All rights reserved.
  7. //
  8. #import "CommitListViewController.h"
  9. #import "FLEXample-Swift.h"
  10. #import <FLEX.h>
  11. @interface CommitListViewController ()
  12. @property (nonatomic) FLEXMutableListSection<Commit *> *commits;
  13. @property (nonatomic, readonly) NSMutableDictionary<NSString *, UIImage *> *avatars;
  14. @end
  15. @implementation CommitListViewController
  16. - (id)init {
  17. // Default style is grouped
  18. return [self initWithStyle:UITableViewStylePlain];
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. _avatars = [NSMutableDictionary new];
  23. self.title = @"FLEX Commit History";
  24. self.showsSearchBar = YES;
  25. self.navigationItem.rightBarButtonItem = [UIBarButtonItem
  26. itemWithTitle:@"FLEX" target:FLEXManager.sharedManager action:@selector(toggleExplorer)
  27. ];
  28. // Load and process commits
  29. NSString *commitsURL = @"https://api.github.com/repos/Flipboard/FLEX/commits";
  30. [self startDataTask:commitsURL completion:^(NSData *data, NSInteger statusCode, NSError *error) {
  31. if (statusCode == 200) {
  32. self.commits.list = [Commit commitsFrom:data];
  33. [self fadeInNewRows];
  34. } else {
  35. [FLEXAlert showAlert:@"Error" message:error.localizedDescription from:self];
  36. }
  37. }];
  38. }
  39. - (void)viewWillAppear:(BOOL)animated {
  40. [super viewWillAppear:animated];
  41. [self disableToolbar];
  42. }
  43. - (NSArray<FLEXTableViewSection *> *)makeSections {
  44. _commits = [FLEXMutableListSection list:@[]
  45. cellConfiguration:^(__kindof UITableViewCell *cell, Commit *commit, NSInteger row) {
  46. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  47. cell.textLabel.text = commit.firstLine;
  48. cell.detailTextLabel.text = commit.secondLine;
  49. cell.detailTextLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  50. // cell.textLabel.numberOfLines = 2;
  51. // cell.detailTextLabel.numberOfLines = 3;
  52. UIImage *avi = self.avatars[commit.committer.login];
  53. if (avi) {
  54. cell.imageView.image = avi;
  55. } else {
  56. cell.tag = commit.identifier;
  57. [self loadImage:commit.committer.avatarUrl completion:^(UIImage *image) {
  58. self.avatars[commit.committer.login] = image;
  59. if (cell.tag == commit.identifier) {
  60. cell.imageView.image = image;
  61. } else {
  62. [self.tableView reloadRowsAtIndexPaths:@[
  63. [NSIndexPath indexPathForRow:row inSection:0]
  64. ] withRowAnimation:UITableViewRowAnimationAutomatic];
  65. }
  66. }];
  67. }
  68. } filterMatcher:^BOOL(NSString *filterText, Commit *commit) {
  69. return [commit matchesWithQuery:filterText];
  70. }
  71. ];
  72. self.commits.selectionHandler = ^(__kindof UIViewController *host, Commit *commit) {
  73. [host.navigationController pushViewController:[
  74. FLEXObjectExplorerFactory explorerViewControllerForObject:commit
  75. ] animated:YES];
  76. };
  77. return @[self.commits];
  78. }
  79. // Empty sections are removed by default and we always want this one section
  80. - (NSArray<FLEXTableViewSection *> *)nonemptySections {
  81. return @[_commits];
  82. }
  83. - (void)fadeInNewRows {
  84. NSIndexSet *sections = [NSIndexSet indexSetWithIndex:0];
  85. [self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic];
  86. }
  87. - (void)loadImage:(NSString *)imageURL completion:(void(^)(UIImage *image))callback {
  88. [self startDataTask:imageURL completion:^(NSData *data, NSInteger statusCode, NSError *error) {
  89. if (statusCode == 200) {
  90. callback([UIImage imageWithData:data]);
  91. }
  92. }];
  93. }
  94. - (void)startDataTask:(NSString *)urlString completion:(void (^)(NSData *data, NSInteger statusCode, NSError *error))completionHandler {
  95. // return;
  96. NSURL *url = [NSURL URLWithString:urlString];
  97. [[NSURLSession.sharedSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  98. dispatch_async(dispatch_get_main_queue(), ^{
  99. NSInteger code = [(NSHTTPURLResponse *)response statusCode];
  100. completionHandler(data, code, error);
  101. });
  102. }] resume];
  103. }
  104. @end