123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- //
- // CommitListViewController.m
- // FLEXample
- //
- // Created by Tanner on 3/11/20.
- // Copyright © 2020 Flipboard. All rights reserved.
- //
- #import "CommitListViewController.h"
- #import "FLEXample-Swift.h"
- #import <FLEX.h>
- @interface CommitListViewController ()
- @property (nonatomic) FLEXMutableListSection<Commit *> *commits;
- @property (nonatomic, readonly) NSMutableDictionary<NSString *, UIImage *> *avatars;
- @end
- @implementation CommitListViewController
- - (id)init {
- // Default style is grouped
- return [self initWithStyle:UITableViewStylePlain];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- _avatars = [NSMutableDictionary new];
-
- self.title = @"FLEX Commit History";
- self.showsSearchBar = YES;
- self.navigationItem.rightBarButtonItem = [UIBarButtonItem
- flex_itemWithTitle:@"FLEX" target:FLEXManager.sharedManager action:@selector(toggleExplorer)
- ];
-
- // Load and process commits
- NSString *commitsURL = @"https://api.github.com/repos/Flipboard/FLEX/commits";
- [self startDataTask:commitsURL completion:^(NSData *data, NSInteger statusCode, NSError *error) {
- if (statusCode == 200) {
- self.commits.list = [Commit commitsFrom:data];
- [self fadeInNewRows];
- } else {
- [FLEXAlert showAlert:@"Error"
- message:error.localizedDescription ?: @(statusCode).stringValue
- from:self
- ];
- }
- }];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self disableToolbar];
- }
- - (NSArray<FLEXTableViewSection *> *)makeSections {
- _commits = [FLEXMutableListSection list:@[]
- cellConfiguration:^(__kindof UITableViewCell *cell, Commit *commit, NSInteger row) {
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- cell.textLabel.text = commit.firstLine;
- cell.detailTextLabel.text = commit.secondLine;
- cell.detailTextLabel.lineBreakMode = NSLineBreakByTruncatingTail;
- // cell.textLabel.numberOfLines = 2;
- // cell.detailTextLabel.numberOfLines = 3;
-
- UIImage *avi = self.avatars[commit.committer.login];
- if (avi) {
- cell.imageView.image = avi;
- } else {
- cell.tag = commit.identifier;
- [self loadImage:commit.committer.avatarUrl completion:^(UIImage *image) {
- self.avatars[commit.committer.login] = image;
- if (cell.tag == commit.identifier) {
- cell.imageView.image = image;
- } else {
- [self.tableView reloadRowsAtIndexPaths:@[
- [NSIndexPath indexPathForRow:row inSection:0]
- ] withRowAnimation:UITableViewRowAnimationAutomatic];
- }
- }];
- }
- } filterMatcher:^BOOL(NSString *filterText, Commit *commit) {
- return [commit matchesWithQuery:filterText];
- }
- ];
-
- self.commits.selectionHandler = ^(__kindof UIViewController *host, Commit *commit) {
- [host.navigationController pushViewController:[
- FLEXObjectExplorerFactory explorerViewControllerForObject:commit
- ] animated:YES];
- };
-
- return @[self.commits];
- }
- // Empty sections are removed by default and we always want this one section
- - (NSArray<FLEXTableViewSection *> *)nonemptySections {
- return @[_commits];
- }
- - (void)fadeInNewRows {
- NSIndexSet *sections = [NSIndexSet indexSetWithIndex:0];
- [self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic];
- }
- - (void)loadImage:(NSString *)imageURL completion:(void(^)(UIImage *image))callback {
- [self startDataTask:imageURL completion:^(NSData *data, NSInteger statusCode, NSError *error) {
- if (statusCode == 200) {
- callback([UIImage imageWithData:data]);
- }
- }];
- }
- - (void)startDataTask:(NSString *)urlString completion:(void (^)(NSData *data, NSInteger statusCode, NSError *error))completionHandler {
- // return;
- NSURL *url = [NSURL URLWithString:urlString];
- [[NSURLSession.sharedSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
- dispatch_async(dispatch_get_main_queue(), ^{
- NSInteger code = [(NSHTTPURLResponse *)response statusCode];
-
- completionHandler(data, code, error);
- });
- }] resume];
- }
- @end
|