Created
May 31, 2016 08:42
-
-
Save InstaRobot/bca0e58b665ab40f97e3c4d1fb71270d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppsViewController.h | |
// Clean for VK | |
// | |
// Created by VITALIY PODOLSKIY on 01/10/15. | |
// Copyright © 2015 VITALIY PODOLSKIY. All rights reserved. | |
// | |
#import "BaseViewController.h" | |
@interface AppsViewController : BaseViewController | |
@property (nonatomic, strong) NSString *developerURL; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppsViewController.m | |
// Clean for VK | |
// | |
// Created by VITALIY PODOLSKIY on 01/10/15. | |
// Copyright © 2015 VITALIY PODOLSKIY. All rights reserved. | |
// | |
#import "AppsViewController.h" | |
#import "AppsTableViewCell.h" | |
static NSString * const kCellIdentifier = @"appsCell"; | |
@interface AppsViewController () | |
@property (weak, nonatomic) IBOutlet UITableView *tableView; | |
@property (nonatomic, strong) NSMutableArray *apps; | |
@end | |
@implementation AppsViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.developerURL = DEVELOPER_URL; | |
[self setupTableView]; | |
self.apps = [NSMutableArray array]; | |
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.developerURL]]; | |
[NSURLConnection sendAsynchronousRequest:request | |
queue:[NSOperationQueue mainQueue] | |
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { | |
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data | |
options:0 | |
error:nil]; | |
for (NSDictionary *app in JSON[@"results"]) { | |
NSString *name = app[@"trackName"]; | |
if (name) { | |
[self.apps addObject:app]; | |
} | |
} | |
[self.tableView reloadData]; | |
}]; | |
} // viewDidLoad | |
#pragma mark - TableView Delegate | |
- (void)setupTableView { | |
self.tableView.backgroundColor = [UIColor clearColor]; | |
UINib *nib = [UINib nibWithNibName:@"AppsTableViewCell" | |
bundle:nil]; | |
[self.tableView registerNib:nib | |
forCellReuseIdentifier:kCellIdentifier]; | |
} // setupTableView | |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { | |
return 100; | |
} // heightForRowAtIndexPath | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { | |
return 1; | |
} // numberOfSectionsInTableView | |
- (NSInteger)tableView:(UITableView *)tableView | |
numberOfRowsInSection:(NSInteger)section { | |
if (self.apps.count == 0) { | |
return 0; | |
} | |
else { | |
return self.apps.count; | |
} | |
} // numberOfRowsInSection | |
- (UITableViewCell *)tableView:(UITableView *)tableView | |
cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
AppsTableViewCell *cell = (AppsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; | |
if (!cell) { | |
cell = [[AppsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault | |
reuseIdentifier:kCellIdentifier]; | |
} | |
if (self.apps.count == 0) { | |
cell.appTitleLabel.text = @"Loading"; | |
} | |
else { | |
NSDictionary *app = self.apps[indexPath.row]; | |
NSString *ratingCount = app[@"userRatingCount"]; | |
NSString *currentRating = app[@"averageUserRating"]; | |
NSString *starString = @""; | |
NSString *primaryGenreName = app[@"primaryGenreName"]; | |
cell.appTitleLabel.text = [app[@"trackName"] componentsSeparatedByString:@"-"][0]; | |
if (ratingCount == nil) { | |
ratingCount = @"0"; | |
} | |
if (currentRating) { | |
NSNumber *starRating = @(ceil(currentRating.doubleValue)); | |
for (NSInteger i = 0; i < 5; i++) { | |
if ([starRating intValue] - i > 0) { | |
starString = [starString stringByAppendingString:@"★"]; | |
} | |
else { | |
starString = [starString stringByAppendingString:@"☆"]; | |
} | |
} | |
starString = [starString stringByAppendingString:@" "]; | |
} | |
NSString *appRewiews = NSLocalizedString(@"reviews", nil); | |
NSMutableParagraphStyle *ratingParagraphStyle = [[NSParagraphStyle defaultParagraphStyle]mutableCopy]; | |
ratingParagraphStyle.alignment = NSTextAlignmentLeft; | |
NSMutableAttributedString *rating = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ %@ %@", starString, ratingCount, appRewiews]]; | |
NSUInteger strLength = starString.length; | |
if (strLength > 0) { | |
[rating addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Menlo-Regular" size:20.0] range:NSMakeRange(0,5)]; | |
[rating addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:1.000 green:0.961 blue:0.000 alpha:1.000] range:NSMakeRange(0,5)]; | |
} | |
[rating addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Menlo-Regular" size:15.0] range:NSMakeRange(6,strLength)]; | |
[rating addAttribute:NSParagraphStyleAttributeName value:ratingParagraphStyle range:NSMakeRange(0,strLength)]; | |
[rating addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] range:NSMakeRange(6,strLength)]; | |
cell.appStarLabel.attributedText = rating; | |
cell.appStarLabel.backgroundColor = [UIColor clearColor]; | |
[cell.appStarLabel sizeToFit]; | |
NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", app[@"artworkUrl512"]]]; | |
cell.imageAppIcon.imageURL = imageUrl; | |
cell.appDescLabel.text = [NSString stringWithFormat:@"%@ - %@", NSLocalizedString(@"primaryGenreName", nil), primaryGenreName]; | |
} | |
cell.backgroundColor = [UIColor clearColor]; | |
return cell; | |
} // cellForRowAtIndexPath | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
[tableView deselectRowAtIndexPath:indexPath | |
animated:YES]; | |
AppsTableViewCell *cell = (AppsTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; | |
[UIView animateWithDuration:0.2 | |
animations:^{ | |
cell.cellGradient.backgroundColor = [UIColor colorWithRed:0.271 | |
green:0.408 | |
blue:0.573 | |
alpha:0.4]; | |
} completion:^(BOOL finished) { | |
cell.cellGradient.backgroundColor = [UIColor clearColor]; | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | |
}); | |
}]; | |
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.apps[indexPath.row][@"trackViewUrl"]]]; | |
} // didSelectRowAtIndexPath | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment