Skip to content

Commit

Permalink
App Store links
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Khamsing committed Jul 27, 2016
1 parent 3615cff commit 3c4bade
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 47 deletions.
File renamed without changes
Binary file added Assets/0.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added Assets/3.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added Assets/4.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Super basic iOS app to browse [`open-source-ios-apps`](https://github.com/dkhamsing/open-source-ios-apps) :octocat:

![](Assets/0.png)
![](Assets/0.2.png)

## Getting Started

Expand Down
188 changes: 142 additions & 46 deletions Xcode/OSIA/DataViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ @interface DataViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *dataSource;

@property (nonatomic, strong) NSURL *appStoreLink;

@end

@implementation DataViewController
Expand All @@ -25,20 +27,11 @@ @implementation DataViewController

static NSString * const itemCellId = @"itemCellId";

static NSString * const kSwiftMarker = @" Swift ";
static NSString * const kAppStoreMarker = @" App Store ";

/*
- (instancetype)initWithData:(Data *)data;
{
self = [super init];
if (!self)
return nil;
self.data = data;
return self;
}
*/
static NSString * const kCodeMarker = @"`";

static NSString * const kSwiftMarker = @" Swift ";

- (void)viewDidLoad;
{
Expand Down Expand Up @@ -96,6 +89,91 @@ - (void)setup;
self.tableView.delegate = self;
}

#pragma mark Private

- (NSAttributedString *)attributedStringForItem:(NSDictionary *)item;
{
NSString *title = item[@"title"];

NSString *description = item[@"description"];
description = [description stringByReplacingOccurrencesOfString:kCodeMarker withString:@""];

NSString *subtitle = description;

NSArray *tags = item[@"tags"];

BOOL isSwift = [tags containsObject:@"swift"];
BOOL inAppStore = [item.allKeys containsObject:@"itunes"];

BOOL condition = isSwift || inAppStore;
if (condition) {
if (description)
description = [description stringByAppendingString:@"\n"];
else
description = @"";
}

if (isSwift) {
NSString *swift = [NSString stringWithFormat:@" %@", kSwiftMarker];
description = [description stringByAppendingString:swift];
}

if (inAppStore) {
NSString *addition = [NSString stringWithFormat:@" %@", kAppStoreMarker];
description = [description stringByAppendingString:addition];
}

NSString *text = title;
if (description)
text = [title stringByAppendingFormat:@"\n%@", description];

NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:text];

{
UIColor *color = self.swiftColor;
NSDictionary *attributes = @{
NSBackgroundColorAttributeName:color,
NSForegroundColorAttributeName:[UIColor whiteColor]
};
NSRange range = [text rangeOfString:kSwiftMarker];
[attributed addAttributes:attributes range:range];
}

{
UIColor *color = [UIColor lightGrayColor];
NSDictionary *attributes = @{
NSBackgroundColorAttributeName:color,
NSForegroundColorAttributeName:[UIColor whiteColor]
};

NSRange range = [text rangeOfString:kAppStoreMarker];
[attributed addAttributes:attributes range:range];
}

if (description)
{
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 5;
NSDictionary *attributes = @{
NSParagraphStyleAttributeName:style,
NSFontAttributeName:[UIFont systemFontOfSize:12]
};
NSRange range = [text rangeOfString:description];
[attributed addAttributes:attributes range:range];
}

if (subtitle)
{
NSDictionary *attributes = @{
NSForegroundColorAttributeName:[UIColor grayColor]
};
NSRange range = [text rangeOfString:subtitle];
[attributed addAttributes:attributes range:range];
}

return attributed;
}

- (UIColor *)swiftColor;
{
UIColor *swiftColor = [UIColor colorWithRed:230/255.f green:78/255.f blue:54/255.f alpha:1];
Expand All @@ -104,6 +182,30 @@ - (UIColor *)swiftColor;

#pragma mark Table view

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
id item = self.dataSource[indexPath.row];

return [item isKindOfClass:[NSDictionary class]] ?
({


CGSize size = self.view.bounds.size;
size.width -= (20 * 2);

NSAttributedString *attributed = [self attributedStringForItem:item];

CGRect rect = [attributed boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin context:nil];

CGFloat subtitle = rect.size.height;

CGFloat pad = 15;
CGFloat total = subtitle + pad * 2;
total;
}):
50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return self.dataSource.count;
Expand All @@ -115,37 +217,12 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

if ([item isKindOfClass:[NSDictionary class]]) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:itemCellId];
if (!cell)
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:itemCellId];
cell.textLabel.numberOfLines = 0;
}

cell.textLabel.text = ({
NSString *title = item[@"title"];
title;
});

cell.detailTextLabel.attributedText = ({
NSString *description = item[@"description"];
NSArray *tags = item[@"tags"];
if ([tags containsObject:@"swift"]) {
NSString *swift = [NSString stringWithFormat:@" %@", kSwiftMarker];
description = [description stringByAppendingString:swift];
}

NSMutableAttributedString *attributed = description?
[[NSMutableAttributedString alloc] initWithString:description]:
nil;

if (attributed) {
UIColor *swiftColor = self.swiftColor;
NSDictionary *attributes = @{
NSBackgroundColorAttributeName:swiftColor,
NSForegroundColorAttributeName:[UIColor whiteColor]};
NSRange range = [description rangeOfString:kSwiftMarker];
[attributed addAttributes:attributes range:range];
}

attributed;
});
cell.textLabel.attributedText = [self attributedStringForItem:item];

return cell;
}
Expand All @@ -172,13 +249,27 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
id item = self.dataSource[indexPath.row];

if ([item isKindOfClass:[NSDictionary class]]) {
NSDictionary *app = (NSDictionary *)item;


NSString *key = @"itunes";
if ([app.allKeys containsObject:key]) {
NSString *appStoreLink = app[@"itunes"];
self.appStoreLink = [NSURL URLWithString:appStoreLink];
}
else
self.appStoreLink = nil;

NSString *source = item[@"source"];
NSURL *url = [NSURL URLWithString:source];

SFSafariViewController *safariController = [[SFSafariViewController alloc] initWithURL:url];
safariController.title = item[@"title"];

[self.navigationController pushViewController:safariController animated:YES];
SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:url];
safariViewController.title = item[@"title"];

if (self.appStoreLink)
safariViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"App Store" style:UIBarButtonItemStylePlain target:self action:@selector(openAppStoreLink)];

[self.navigationController pushViewController:safariViewController animated:YES];
return;
}

Expand All @@ -188,4 +279,9 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[self.navigationController pushViewController:controller animated:YES];
}

- (void)openAppStoreLink;
{
[[UIApplication sharedApplication] openURL:self.appStoreLink];
}

@end

0 comments on commit 3c4bade

Please sign in to comment.