// // TrainerViewController.m // NewsBlur // // Created by Samuel Clay on 12/24/12. // Copyright (c) 2012 NewsBlur. All rights reserved. // #import "TrainerViewController.h" #import "StringHelper.h" @implementation TrainerViewController @synthesize closeButton; @synthesize webView; @synthesize navBar; @synthesize appDelegate; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.appDelegate = [NewsBlurAppDelegate sharedAppDelegate]; navBar.tintColor = UIColorFromRGB(0x183353); } - (void)viewWillAppear:(BOOL)animated { [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects: [[UIMenuItem alloc] initWithTitle:@"👎 Hide" action:@selector(changeTitle:)], [[UIMenuItem alloc] initWithTitle:@"👍 Focus" action:@selector(changeTitle:)], nil]]; UILabel *titleLabel = (UILabel *)[appDelegate makeFeedTitle:appDelegate.activeFeed]; titleLabel.shadowColor = UIColorFromRGB(0x306070); navBar.topItem.titleView = titleLabel; NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [self.webView loadHTMLString:[self makeTrainerSections] baseURL:baseURL]; } - (void)viewDidAppear:(BOOL)animated { } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[UIMenuController sharedMenuController] setMenuItems:nil]; } #pragma mark - #pragma mark Story layout - (NSString *)makeTrainerSections { NSString *storyAuthor = [self makeAuthor]; NSString *storyTags = [self makeTags]; NSString *storyTitle = [self makeTitle]; NSString *storyPublisher = [self makePublisher]; int contentWidth = self.view.frame.size.width; NSString *contentWidthClass; if (contentWidth > 700) { contentWidthClass = @"NB-ipad-wide"; } else if (contentWidth > 480) { contentWidthClass = @"NB-ipad-narrow"; } else { contentWidthClass = @"NB-iphone"; } // set up layout values based on iPad/iPhone NSString *headerString = [NSString stringWithFormat:@ "" "", contentWidth]; NSString *footerString = [NSString stringWithFormat:@ "" "" ""]; NSString *htmlString = [NSString stringWithFormat:@ "" "%@" // header string "" "
" "
%@
" "
%@
" "
%@
" "
%@
" "
" "%@" // footer "" "", headerString, contentWidthClass, storyTitle, storyAuthor, storyTags, storyPublisher, footerString ]; // NSLog(@"\n\n\n\nhtmlString:\n\n\n%@\n\n\n", htmlString); return htmlString; } - (NSString *)makeAuthor { NSString *feedId = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"story_feed_id"]]; NSString *storyAuthor = @""; if ([[appDelegate.activeStory objectForKey:@"story_authors"] class] != [NSNull class] && [[appDelegate.activeStory objectForKey:@"story_authors"] length]) { NSString *author = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"story_authors"]]; if (author && [author class] != [NSNull class]) { int authorScore = [[[[appDelegate.activeClassifiers objectForKey:feedId] objectForKey:@"authors"] objectForKey:author] intValue]; storyAuthor = [NSString stringWithFormat:@"
" "
Story Authors
" "
" " " "
" "
", author, authorScore > 0 ? @"NB-story-author-positive" : authorScore < 0 ? @"NB-story-author-negative" : @"", [self makeClassifier:author withType:@"Author"]]; } } return storyAuthor; } - (NSString *)makeTags { NSString *feedId = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"story_feed_id"]]; NSString *storyTags = @""; if ([appDelegate.activeStory objectForKey:@"story_tags"]) { NSArray *tagArray = [appDelegate.activeStory objectForKey:@"story_tags"]; if ([tagArray count] > 0) { NSMutableArray *tagStrings = [NSMutableArray array]; for (NSString *tag in tagArray) { int tagScore = [[[[appDelegate.activeClassifiers objectForKey:feedId] objectForKey:@"tags"] objectForKey:tag] intValue]; NSString *tagHtml = [NSString stringWithFormat:@"
" "
%@
" "
", tag, tagScore > 0 ? @"NB-story-tag-positive" : tagScore < 0 ? @"NB-story-tag-negative" : @"", [self makeClassifier:tag withType:@"Tag"]]; [tagStrings addObject:tagHtml]; } storyTags = [NSString stringWithFormat:@"
" "
Story Tags
" "
" " " "
" "
", [tagStrings componentsJoinedByString:@""]]; } } return storyTags; } - (NSString *)makePublisher { NSString *feedId = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"story_feed_id"]]; NSString *feedTitle = [[appDelegate.dictFeeds objectForKey:feedId] objectForKey:@"feed_title"]; NSString *storyPublisher = [NSString stringWithFormat:@"
" "
Publisher
" "
" " %@" "
" "%@", titleScore > 0 ? @"positive" : titleScore < 0 ? @"negative" : @"", titleClassifier]]; } } NSString *titleTrainer = [NSString stringWithFormat:@"
" "
Story Title
" "
" "
Tap and hold the title below
" "
%@
" "
" "
", storyTitle]; return titleTrainer; } - (NSString *)makeClassifier:(NSString *)classifierName withType:(NSString *)classifierType { NSString *classifier = [NSString stringWithFormat:@"" "
" "
" "
" "
" "" "
", classifierType, classifierType, classifierName]; return classifier; } #pragma mark - #pragma mark Actions - (IBAction)doCloseDialog:(id)sender { [appDelegate.trainerViewController dismissModalViewControllerAnimated:YES]; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(changeTitle:)) { return YES; } else { return NO; } } - (void)changeTitle:(id)sender { NSLog(@"changeTitle: %@", sender); } @end @implementation TrainerWebView - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(changeTitle:)) { return YES; } else { return NO; } } - (void)changeTitle:(id)sender { NewsBlurAppDelegate *appDelegate = [NewsBlurAppDelegate sharedAppDelegate]; [appDelegate.trainerViewController changeTitle:sender]; } @end