Permalink
Browse files

add theme switcher for gist

  • Loading branch information...
1 parent b71c064 commit ef2154ddb029132609ee2e208a27384e4a3ae0a2 @trivektor committed Mar 24, 2013
@@ -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
@@ -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;
}
@@ -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
@@ -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];
@@ -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 }
@@ -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;

0 comments on commit ef2154d

Please sign in to comment.