Merge pull request #943 from nriley/master

Fix stories intermittently not displaying; clean up story loading
This commit is contained in:
Samuel Clay 2016-08-23 18:55:59 -07:00 committed by GitHub
commit b177fa96b7

View file

@ -35,8 +35,7 @@
@interface StoryDetailViewController () @interface StoryDetailViewController ()
@property (nonatomic, strong) NSString *loadingHTML; @property (nonatomic, strong) NSString *fullStoryHTML;
@property (nonatomic, strong) NSURL *loadingURL;
@end @end
@ -330,6 +329,16 @@
[appDelegate hideShareView:NO]; [appDelegate hideShareView:NO];
} }
- (void)loadHTMLString:(NSString *)html {
static NSURL *baseURL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
baseURL = [NSBundle mainBundle].bundleURL;
});
[self.webView loadHTMLString:html baseURL:baseURL];
}
- (void)hideNoStoryMessage { - (void)hideNoStoryMessage {
self.noStoryMessage.hidden = YES; self.noStoryMessage.hidden = YES;
} }
@ -495,24 +504,19 @@
htmlBottom htmlBottom
]; ];
NSString *htmlString = [htmlTop stringByAppendingString:htmlBottom]; NSString *htmlTopAndBottom = [htmlTop stringByAppendingString:htmlBottom];
// NSLog(@"\n\n\n\nStory html (%@):\n\n\n%@\n\n\n", self.activeStory[@"story_title"], htmlContent); // NSLog(@"\n\n\n\nStory html (%@):\n\n\n%@\n\n\n", self.activeStory[@"story_title"], htmlContent);
NSString *path = [[NSBundle mainBundle] bundlePath]; self.hasStory = NO;
NSURL *baseURL = [NSURL fileURLWithPath:path]; self.fullStoryHTML = htmlContent;
self.loadingURL = baseURL;
self.loadingHTML = htmlContent;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
self.hasStory = YES;
// NSLog(@"Drawing Story: %@", [self.activeStory objectForKey:@"story_title"]); // NSLog(@"Drawing Story: %@", [self.activeStory objectForKey:@"story_title"]);
[self.webView setMediaPlaybackRequiresUserAction:NO]; [self.webView setMediaPlaybackRequiresUserAction:NO];
[self.webView loadHTMLString:htmlString baseURL:baseURL]; [self loadHTMLString:htmlTopAndBottom];
[appDelegate.storyPageControl setTextButton:self]; [appDelegate.storyPageControl setTextButton:self];
}); });
self.activeStoryId = [self.activeStory objectForKey:@"story_hash"]; self.activeStoryId = [self.activeStory objectForKey:@"story_hash"];
} }
@ -587,13 +591,12 @@
themeStyle = [NSString stringWithFormat:@"<link rel=\"stylesheet\" type=\"text/css\" href=\"storyDetailView%@.css\">", themeStyle]; themeStyle = [NSString stringWithFormat:@"<link rel=\"stylesheet\" type=\"text/css\" href=\"storyDetailView%@.css\">", themeStyle];
} }
NSURL *baseURL = [NSBundle mainBundle].bundleURL;
NSString *html = [NSString stringWithFormat:@"<html>" NSString *html = [NSString stringWithFormat:@"<html>"
"<head><link rel=\"stylesheet\" type=\"text/css\" href=\"storyDetailView.css\">%@</head>" // header string "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"storyDetailView.css\">%@</head>" // header string
"<body></body>" "<body></body>"
"</html>", themeStyle]; "</html>", themeStyle];
[self.webView loadHTMLString:html baseURL:baseURL]; [self loadHTMLString:html];
} }
- (NSString *)getHeader { - (NSString *)getHeader {
@ -1602,42 +1605,40 @@ shouldStartLoadWithRequest:(NSURLRequest *)request
} }
- (void)webViewDidStartLoad:(UIWebView *)webView { - (void)webViewDidStartLoad:(UIWebView *)webView {
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults]; if (!self.hasStory) // other Web page loads aren't visible
[self changeFontSize:[userPreferences stringForKey:@"story_font_size"]]; return;
[self changeLineSpacing:[userPreferences stringForKey:@"story_line_spacing"]];
} // DOM should already be set up here
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// self.webView.hidden = NO;
[self.activityIndicator stopAnimating];
if (self.loadingHTML) {
[self.webView loadHTMLString:self.loadingHTML baseURL:self.loadingURL];
self.loadingHTML = nil;
self.loadingURL = nil;
} else {
self.webView.hidden = NO;
[self.webView setNeedsDisplay];
}
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults]; NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
[self changeFontSize:[userPreferences stringForKey:@"story_font_size"]]; [self changeFontSize:[userPreferences stringForKey:@"story_font_size"]];
[self changeLineSpacing:[userPreferences stringForKey:@"story_line_spacing"]]; [self changeLineSpacing:[userPreferences stringForKey:@"story_line_spacing"]];
[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"]; [self.webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.activityIndicator stopAnimating];
if (!self.fullStoryHTML)
return; // if we're loading anything other than a full story, the view will be hidden
[self loadHTMLString:self.fullStoryHTML];
self.fullStoryHTML = nil;
self.hasStory = YES;
[MBProgressHUD hideHUDForView:self.view animated:YES];
if ([appDelegate.storiesCollection.activeFeedStories count] && if ([appDelegate.storiesCollection.activeFeedStories count] &&
self.activeStoryId && self.hasStory) { self.activeStoryId) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, .15 * NSEC_PER_SEC), dispatch_after(dispatch_time(DISPATCH_TIME_NOW, .15 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{ dispatch_get_main_queue(), ^{
[self checkTryFeedStory]; [self checkTryFeedStory];
}); });
} }
if ([[self.webView stringByEvaluatingJavaScriptFromString:@"document.readyState"] isEqualToString:@"complete"]) { [self scrollToLastPosition:YES];
[self scrollToLastPosition:YES];
} self.webView.hidden = NO;
[self.webView setNeedsDisplay];
} }
- (void)checkTryFeedStory { - (void)checkTryFeedStory {