Skip to content

Instantly share code, notes, and snippets.

@trupin
Last active September 24, 2018 21:11
Show Gist options
  • Save trupin/bfe52017bcabbef41af927ad4d93c97e to your computer and use it in GitHub Desktop.
Save trupin/bfe52017bcabbef41af927ad4d93c97e to your computer and use it in GitHub Desktop.
#import "WSReviewViewController.h"
#import "Sample-Swift.h"
NS_ASSUME_NONNULL_BEGIN
@interface WSReviewViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) id<WSReviewViewControllerDependencyResolver> dependencies;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<Review *> *reviews;
@end
@implementation WSReviewViewController
- (instancetype)initWithDependencies:(id<WSReviewViewControllerDependencyResolver>)dependencies {
self = [super init];
if (self) {
self.dependencies = dependencies;
}
return self;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [UITableView new];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[ReviewTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ReviewTableViewCell class])];
_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.estimatedRowHeight = 140;
}
return _tableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Reviews";
self.view.backgroundColor = [UIColor whiteColor];
self.edgesForExtendedLayout = UIRectEdgeNone;
[self.view addSubview:self.tableView];
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[[self.tableView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[self.tableView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
[self.tableView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[self.tableView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]]];
[self.dependencies.reviewManager getReviewsFor:self.dependencies.movieID completion:^(ReviewPage * _Nullable page) {
if (!page) {
return;
}
self.reviews = page.results;
[self.tableView reloadData];
}];
}
#pragma pragma - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.reviews.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ReviewTableViewCell class]) forIndexPath:indexPath];
if ([cell isKindOfClass:[ReviewTableViewCell class]]) {
ReviewTableViewCell *reviewCell = (ReviewTableViewCell *)cell;
Review *review = self.reviews[indexPath.row];
ReviewTableViewCellViewModel *viewModel = [[ReviewTableViewCellViewModel alloc] initWithReview:review];
[reviewCell bindWithViewModel:viewModel];
}
return cell;
}
@end
NS_ASSUME_NONNULL_END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment