Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Save to Pocket' feature. #24

Merged
merged 1 commit into from
Nov 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions HackerNews.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions HackerNews/ARChromeActivity/ARChromeActivity.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


#import "ARChromeActivity.h"
#import "HNPostURL.h"

@implementation ARChromeActivity {
NSURL *_activityURL;
Expand All @@ -23,7 +24,7 @@ @implementation ARChromeActivity {

- (void)commonInit {
_callbackSource = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleName"];
_activityTitle = @"Chrome";
_activityTitle = @"Open in Chrome";
}

- (id)init {
Expand Down Expand Up @@ -52,11 +53,12 @@ - (NSString *)activityType {
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
return [[activityItems lastObject] isKindOfClass:[NSURL class]] && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome-x-callback://"]];
return [[activityItems lastObject] isKindOfClass:[HNPostURL class]] && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome-x-callback://"]];
}

- (void)prepareWithActivityItems:(NSArray *)activityItems {
_activityURL = [activityItems lastObject];
// Note: Shouldn't we use the original URL here? Not the readibility one.
_activityURL = ((HNPostURL*)[activityItems lastObject]).mobileFriendlyAbsoluteURL;
}

- (void)performActivity {
Expand Down
14 changes: 14 additions & 0 deletions HackerNews/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
#import "IIViewDeckController.h"
#import "NavigationDeckViewController.h"
#import "HNManager.h"
#import "PocketAPI.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Register in the Pocket API.
[[PocketAPI sharedAPI] setConsumerKey:@"20118-9a164e727c7246cdc440dcab"];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Set Pro & Start HNManager Session
Expand Down Expand Up @@ -84,4 +88,14 @@ - (void)applicationWillTerminate:(UIApplication *)application
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([[PocketAPI sharedAPI] handleOpenURL:url]) {
return YES;
} else {
// Handle other url-schemes here.
return NO;
}
}

@end
13 changes: 13 additions & 0 deletions HackerNews/DRPocketActivity/DRPocketActivity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// DRPocketActivity.h
// HackerNews
//
// Created by Daniel Rosado on 09/11/13.
// Copyright (c) 2013 Benjamin Gordon. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DRPocketActivity : UIActivity

@end
66 changes: 66 additions & 0 deletions HackerNews/DRPocketActivity/DRPocketActivity.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// DRPocketActivity.m
// HackerNews
//
// Created by Daniel Rosado on 09/11/13.
// Copyright (c) 2013 Benjamin Gordon. All rights reserved.
//

#import "PocketAPI.h"
#import "DRPocketActivity.h"
#import "SVProgressHUD.h"
#import "LoadingHUDViewController.h"

@implementation DRPocketActivity {
NSURL *_URL;
}

- (NSString *)activityType {
return NSStringFromClass([self class]);
}

- (NSString *)activityTitle {
return NSLocalizedStringFromTable(@"Save to Pocket", @"DRPocketActivity", nil);
}

- (UIImage *)activityImage {
return [UIImage imageNamed:@"PocketActivity.png"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
for (id activityItem in activityItems) {
if ([activityItem isKindOfClass:[NSURL class]] && [[UIApplication sharedApplication] canOpenURL:activityItem]) {
return YES;
}
}

return NO;
}

- (void)prepareWithActivityItems:(NSArray *)activityItems {
for (id activityItem in activityItems) {
if ([activityItem isKindOfClass:[NSURL class]]) {
_URL = ((NSURL*)activityItem).absoluteURL;
}
}
}

- (UIViewController *)activityViewController
{
[self performSelector:@selector(saveToPocket) withObject:nil afterDelay:0];
return [[LoadingHUDViewController alloc] initWithLoadingStatus:@"Saving..."];
}

- (void)saveToPocket {
[[PocketAPI sharedAPI] saveURL:_URL handler: ^(PocketAPI *API, NSURL *URL, NSError *error) {
BOOL activityCompletedSuccessfully = error ? NO : YES;

double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self activityDidFinish:activityCompletedSuccessfully];
});
}];
}

@end
15 changes: 15 additions & 0 deletions HackerNews/DRPocketActivity/LoadingHUDViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// LoadingHUDViewController.h
// HackerNews
//
// Created by Daniel Rosado on 09/11/13.
// Copyright (c) 2013 Benjamin Gordon. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LoadingHUDViewController : UIViewController

- (instancetype)initWithLoadingStatus:(NSString*)string;

@end
36 changes: 36 additions & 0 deletions HackerNews/DRPocketActivity/LoadingHUDViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// LoadingHUDViewController.m
// HackerNews
//
// Created by Daniel Rosado on 09/11/13.
// Copyright (c) 2013 Benjamin Gordon. All rights reserved.
//

#import "LoadingHUDViewController.h"
#import "SVProgressHUD.h"

@implementation LoadingHUDViewController
{
NSString *_loadingString;
}

- (instancetype)initWithLoadingStatus:(NSString*)string
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
_loadingString = string;
}

return self;
}

- (void)loadView
{
UIView *vw = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
vw.backgroundColor = [UIColor clearColor];
self.view = vw;

[SVProgressHUD showWithStatus:_loadingString maskType:SVProgressHUDMaskTypeGradient];
}

@end
Binary file added HackerNews/DRPocketActivity/PocketActivity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added HackerNews/DRPocketActivity/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions HackerNews/HNPostURL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// HNPostURL.h
// HackerNews
//
// Created by Daniel Rosado on 09/11/13.
// Copyright (c) 2013 Benjamin Gordon. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface HNPostURL : NSURL

- (id)initWithString:(NSString *)URLString andMobileFriendlyString:(NSString*)mFriendlyURLString;

@property (nonatomic, readonly, strong) NSString *mobileFriendlyAbsoluteString;
@property (nonatomic, readonly, strong) NSURL *mobileFriendlyAbsoluteURL;

@end
44 changes: 44 additions & 0 deletions HackerNews/HNPostURL.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// HNPostURL.m
// HackerNews
//
// Created by Daniel Rosado on 09/11/13.
// Copyright (c) 2013 Benjamin Gordon. All rights reserved.
//

#import "HNPostURL.h"

@interface HNPostURL ()

@property (nonatomic, readwrite, strong) NSString *mobileFriendlyAbsoluteString;
@property (nonatomic, readwrite, strong) NSURL *mobileFriendlyAbsoluteURL;

@end


@implementation HNPostURL

#pragma mark - Designated initializer
- (id)initWithString:(NSString *)URLString andMobileFriendlyString:(NSString*)mFriendlyURLString
{
self = [super initWithString:URLString];
if (self) {
_mobileFriendlyAbsoluteString = mFriendlyURLString;
_mobileFriendlyAbsoluteURL = [[NSURL alloc] initWithString:mFriendlyURLString];
}

return self;
}

#pragma mark - Mobile friendly getters
- (NSString*)mobileFriendlyAbsoluteString
{
return _mobileFriendlyAbsoluteString;
}

- (NSURL*)mobileFriendlyAbsoluteURL
{
return _mobileFriendlyAbsoluteURL;
}

@end
11 changes: 11 additions & 0 deletions HackerNews/HackerNews-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.getpocket.sdk</string>
<key>CFBundleURLSchemes</key>
<array>
<string>pocketapp20118</string>
</array>
</dict>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
Expand Down
42 changes: 38 additions & 4 deletions HackerNews/LinksViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
#import "Helpers.h"
#import "ARChromeActivity.h"
#import "TUSafariActivity.h"
#import "DRPocketActivity/DRPocketActivity.h"
#import "KGStatusBar.h"
#import "SVProgressHUD.h"
#import "HNPostURL.h"

@interface LinksViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *LinkWebView;
Expand Down Expand Up @@ -109,14 +112,22 @@ - (void)upvoteCurrentPost {

#pragma mark - Share
- (void)didClickShare {
NSURL *urlToShare = self.LinkWebView.request.URL;
NSArray *activityItems = @[ urlToShare ];

NSArray *activityItems = @[[self buildPostURL]];

ARChromeActivity *chromeActivity = [[ARChromeActivity alloc] init];
TUSafariActivity *safariActivity = [[TUSafariActivity alloc] init];
NSArray *applicationActivities = @[ safariActivity, chromeActivity ];
DRPocketActivity *pocketActivity = [[DRPocketActivity alloc] init];
NSArray *applicationActivities = @[ pocketActivity, safariActivity, chromeActivity ];

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityController.completionHandler = ^(NSString *activityType, BOOL completed) {
if ([activityType isEqualToString:pocketActivity.activityType]) {
completed
? [SVProgressHUD showSuccessWithStatus:@"Saved to pocket"]
: [SVProgressHUD showErrorWithStatus:@"Unable to save to pocket"];
}
};
[self presentViewController:activityController animated:YES completion:nil];
}

Expand All @@ -127,6 +138,14 @@ - (void)didClickComment {
}
}

- (HNPostURL *)buildPostURL
{
NSString *readabilityURLString = self.LinkWebView.request.URL.absoluteString;
NSString *originalPostUrlString = self.Post.UrlString;
return [[HNPostURL alloc] initWithString:originalPostUrlString andMobileFriendlyString:readabilityURLString];
}


#pragma mark - Change Readability
- (void)didChangeReadability:(NSNotification *)notification {
self.Readability = [[NSUserDefaults standardUserDefaults] boolForKey:@"Readability"];
Expand Down Expand Up @@ -155,4 +174,19 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.indicator removeFromSuperview];
}


#pragma mark - Activity result HUD
- (void)showActivityResultHUDWithText:(NSString*)text {
[self showActivityResultHUDWithText:text andDismissAfter:0];
}
- (void)showActivityResultHUDWithText:(NSString*)text andDismissAfter:(NSTimeInterval)seconds {

[SVProgressHUD showWithStatus:text];
double delayInSeconds = seconds;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[SVProgressHUD dismiss];
});
}

@end
Loading