Skip to content

Commit

Permalink
UIWebView now loads module select in GenericViewController.
Browse files Browse the repository at this point in the history
  • Loading branch information
oalders committed Jun 10, 2011
1 parent 64e7e86 commit 86f9d4c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
5 changes: 4 additions & 1 deletion iCPAN/DetailViewController.h
Expand Up @@ -7,6 +7,7 @@
//

#import <UIKit/UIKit.h>
#import "Module.h"

@class GenericViewController;

Expand All @@ -18,13 +19,15 @@

@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;

@property (nonatomic, retain) NSManagedObject *detailItem;
@property (nonatomic, retain) Module *detailItem;

@property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;

@property (nonatomic, assign) IBOutlet GenericViewController *genericViewController;

@property (nonatomic, retain) IBOutlet UIWebView *webView;

@property (nonatomic, retain) NSString *moduleFile;


@end
72 changes: 55 additions & 17 deletions iCPAN/DetailViewController.m
Expand Up @@ -12,6 +12,8 @@

#import "iCPANAppDelegate.h"

#import "Module.h"

@interface DetailViewController ()
@property (nonatomic, retain) UIPopoverController *popoverController;
- (void)configureView;
Expand All @@ -29,14 +31,14 @@ @implementation DetailViewController

@synthesize genericViewController=_genericViewController;

@synthesize webView;
@synthesize webView, moduleFile;

#pragma mark - Managing the detail item

/*
When setting the detail item, update the view and dismiss the popover controller if it's showing.
*/
- (void)setDetailItem:(NSManagedObject *)managedObject
- (void)setDetailItem:(Module *)managedObject
{
if (_detailItem != managedObject) {
[_detailItem release];
Expand All @@ -62,16 +64,24 @@ - (void)configureView
// Basically, we'll initiate the page load here, but we'll write the page to disk later
// This method will only ever be called when the user selects a module from the table
// in the GenericView
NSLog(@"looking for: %@", [self.detailItem valueForKey:@"name"]);
//iCPANAppDelegate *del = [[UIApplication sharedApplication] delegate];
NSString *name = [self.detailItem valueForKey:@"name"];
NSLog(@"looking for: %@", name);
iCPANAppDelegate *del = [[UIApplication sharedApplication] delegate];

name = [name stringByReplacingOccurrencesOfString:@"::" withString:@"-"];
NSLog(@"looking for: %@", name);
name = [name stringByAppendingString:@".html"];
NSLog(@"looking for: %@", name);

NSString *fullPath = [[del cpanpod] stringByAppendingString:name];
NSLog(@"looking for path: %@", fullPath);

//NSURL *url = [[del applicationDocumentsDirectory] URLByAppendingPathComponent:@"pod"];
//url = [url URLByAppendingPathComponent:[self.detailItem valueForKey:@"name"]];
NSURL *url = [[NSURL alloc] initWithString:[self.detailItem valueForKey:@"name"]];
NSURL *url = [NSURL fileURLWithPath:fullPath];

//NSLog(@"url: %@", url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];

}

- (void)viewWillAppear:(BOOL)animated
Expand Down Expand Up @@ -103,35 +113,59 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface

#pragma mark - Loading webView

- (void)viewDidLoad {

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

//iCPANAppDelegate *del = [[UIApplication sharedApplication] delegate];

//Module *selectedModule = self.detailItem;
//NSLog(@"selected %@", self.detailItem);
//NSString *podPath = [del.cpanpod stringByAppendingString:[selectedModule path]];
//NSURL *url = [NSURL fileURLWithPath:podPath];

//NSLog(@"url: %@", url);
//NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//[webView loadRequest:requestObj];

// allow users to pinch/zoom. also scales the page by default
webView.scalesPageToFit = YES;


//[[NSUserDefaults standardUserDefaults] setValue:appDelegate.selectedModule.name forKey:@"last_module"];

// Override point for customization after application launch
[super viewDidLoad];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

NSLog(@"webvew shouldstartloadwithrequest");

iCPANAppDelegate *del = [[UIApplication sharedApplication] delegate];

NSURL *url = [request URL];
NSString *path = [url relativePath];

NSLog(@"relativePath: %@", [url relativePath]);
NSLog(@"absoluteString: %@", [url absoluteString]);
NSLog(@"baseURL: %@", [url baseURL]);
NSLog(@"baseURL: %@", [url baseURL]);

// This will always be the case when coming directly from the GenericViewController
if (!path) {
path = [url absoluteString];
}

if ([[url absoluteString] rangeOfString:@"http://"].location == NSNotFound ) {

NSLog(@"Offline page view ------------------------------------------");
// This is an offline page view. We need to handle all of the details.
//
// //path = [path stringByReplacingOccurrencesOfString:[[del cpanpod] absoluteString] withString:@""];
path = [path stringByReplacingOccurrencesOfString:[del cpanpod] withString:@""];
path = [path stringByReplacingOccurrencesOfString:@"-" withString:@"::"];
path = [path stringByReplacingOccurrencesOfString:@".html" withString:@""];

NSLog(@"module to search for: %@", path);
// remove next line once we try to follow links in webView
path = [url absoluteString];
//path = [url absoluteString];

NSManagedObjectContext *moc = [del managedObjectContext];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
Expand All @@ -154,9 +188,9 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
if ( results.count > 0 ) {

Module *module = [results objectAtIndex:0];
//NSLog(@"results for single module search %@", module.name );
NSLog(@"results for single module search %@", module.name );

//NSLog(@"This is a local URL");
NSLog(@"This is a local URL");

self.title = module.name;
//self.currentlyViewing = module.name;
Expand All @@ -176,12 +210,16 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)

if ( ![[NSFileManager defaultManager] fileExistsAtPath:podPath] ) {
NSLog(@"pod path: %@", del.cpanpod);
NSLog(@"pod will be written to: %@", podPath);
NSLog(@"pod will be written to xxxxxxxx: %@", podPath);
NSData* pod_data = [module.pod dataUsingEncoding:NSUTF8StringEncoding];
[pod_data writeToFile:podPath atomically:YES];
}
else {
NSLog(@"page exists at %@", podPath);
}
}
else {
NSLog(@"module not found: %@", path);
self.navigationItem.rightBarButtonItem = nil;
self.title = @"404: Page Not Found";
}
Expand Down
2 changes: 1 addition & 1 deletion iCPAN/iCPANAppDelegate.m
Expand Up @@ -245,7 +245,7 @@ - (NSArray *)getRecentlyViewed {
- (NSString *)cpanpod {
//NSLog(@"docs dir %@", [self applicationDocumentsDirectory]);
//return [self applicationDocumentsDirectory];
return [[self docDir] stringByAppendingPathComponent:@"cpanpod"];
return [[self docDir] stringByAppendingString:@"/cpanpod/"];
}

- (NSString *) docDir {
Expand Down

0 comments on commit 86f9d4c

Please sign in to comment.