Skip to content

Commit

Permalink
add theme switcher for gist
Browse files Browse the repository at this point in the history
  • Loading branch information
trivektor committed Mar 24, 2013
1 parent b71c064 commit ef2154d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
Binary file not shown.
8 changes: 6 additions & 2 deletions Gitos-iPad/Controllers/GistRawFileViewController.h
Expand Up @@ -7,16 +7,20 @@
//

#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#import "GistFile.h"

@interface GistRawFileViewController : UIViewController <UIWebViewDelegate, NSURLConnectionDelegate>
@interface GistRawFileViewController : UIViewController <UIWebViewDelegate, NSURLConnectionDelegate, UIActionSheetDelegate>

@property (nonatomic, weak) IBOutlet UIWebView *fileWebView;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) GistFile *gistFile;
@property (nonatomic, strong) NSString *theme;
@property (nonatomic, strong) NSArray *themes;
@property (nonatomic, strong) UIActionSheet *themesOptions;

- (void)performHouseKeepingTasks;
- (void)fetchRawFile;
- (void)switchTheme;
- (NSString *)encodeHtmlEntities:(NSString *)rawHtmlString;

@end
47 changes: 37 additions & 10 deletions Gitos-iPad/Controllers/GistRawFileViewController.m
Expand Up @@ -14,13 +14,15 @@ @interface GistRawFileViewController ()

@implementation GistRawFileViewController

@synthesize fileWebView;
@synthesize fileWebView, theme, themes, themesOptions, hud;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
themes = @[@"prettify.css", @"desert.css", @"sunburst.css", @"sons-of-obsidian.css", @"doxy.css"];
theme = [themes objectAtIndex:0];
}
return self;
}
Expand All @@ -30,30 +32,40 @@ - (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self performHouseKeepingTasks];
NSURL *fileUrl = [NSURL URLWithString:[self.gistFile getRawUrl]];
NSURLRequest *fileRequest = [NSURLRequest requestWithURL:fileUrl];
NSURLConnection *fileConnection = [NSURLConnection connectionWithRequest:fileRequest delegate:self];
[fileConnection start];
[self fetchRawFile];
}

- (void)performHouseKeepingTasks
{
[self.navigationItem setTitle:[self.gistFile getName]];
self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.hud.mode = MBProgressHUDAnimationFade;
self.hud.labelText = @"Loading";

hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDAnimationFade;
hud.labelText = LOADING_MESSAGE;
}

- (void)fetchRawFile
{
NSURL *fileUrl = [NSURL URLWithString:[self.gistFile getRawUrl]];
NSURLRequest *fileRequest = [NSURLRequest requestWithURL:fileUrl];
NSURLConnection *fileConnection = [NSURLConnection connectionWithRequest:fileRequest delegate:self];
[fileConnection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
UIBarButtonItem *switchThemeButton = [[UIBarButtonItem alloc] initWithTitle:@"Switch Theme" style:UIBarButtonItemStyleBordered target:self action:@selector(switchTheme)];

self.navigationItem.rightBarButtonItem = switchThemeButton;

NSString *rawFilePath = [[NSBundle mainBundle] pathForResource:@"raw_file" ofType:@"html"];
NSString *rawFileContent = [NSString stringWithContentsOfFile:rawFilePath encoding:NSUTF8StringEncoding error:nil];
NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *htmlString = [NSString stringWithFormat:rawFileContent, [self encodeHtmlEntities:content]];
NSString *htmlString = [NSString stringWithFormat:rawFileContent, theme, [self encodeHtmlEntities:content]];
[fileWebView loadHTMLString:htmlString baseURL:baseURL];
[fileWebView loadHTMLString:htmlString baseURL:baseURL];
[self.hud hide:YES];
[hud hide:YES];
}

- (NSString *)encodeHtmlEntities:(NSString *)rawHtmlString
Expand All @@ -68,6 +80,21 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
[self.hud hide:YES];
}

- (void)switchTheme
{
themesOptions = [[UIActionSheet alloc] initWithTitle:@"Choose Themes" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Default", @"Desert", @"Sunburst", @"Sons Of Obsidian", @"Doxy", nil];
themesOptions.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[themesOptions showInView:[UIApplication sharedApplication].keyWindow];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != themesOptions.cancelButtonIndex) {
theme = [themes objectAtIndex:buttonIndex];
[self fetchRawFile];
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
Expand Down
5 changes: 1 addition & 4 deletions Gitos-iPad/Html/prettify_themes/sons-of-obsidian.css
Expand Up @@ -5,7 +5,7 @@
* http://CodeTunnel.com/blog/post/71/google-code-prettify-obsidian-theme
*/

html, body { margin: 0; padding: 0 }
html, body { margin: 0; padding: 0; background: #444 }

pre, code { display: block; margin: 0 }
pre { padding: 10px }
Expand Down Expand Up @@ -64,9 +64,6 @@ ol.linenums
margin-top: 0;
margin-bottom: 0;
}
.prettyprint {
background: #444;
}
li.L0, li.L1, li.L2, li.L3, li.L4, li.L5, li.L6, li.L7, li.L8, li.L9
{
color: #555;
Expand Down

0 comments on commit ef2154d

Please sign in to comment.