From a529f340c692a52d2ddd829e2feb79e230b1746f Mon Sep 17 00:00:00 2001 From: Nicholas Riley Date: Tue, 23 Aug 2016 20:52:34 -0400 Subject: [PATCH] Fix stories intermittently not displaying; clean up story loading. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Funnel all HTML loading through -loadHTMLString:. Cache base URL (which is always the same) in a static variable instead of computing it different ways and putting in an instance variable temporarily. Use more intention-revealing variable names. Only set hasStory when we’re actually loading the story itself. Only set the font size and line spacing once, in webViewDidStartLoad, because the DOM is always set up by then in my experience. Only bother to set font size and line spacing if we’re loading a (full) story. Show the Web view when a full story *starts* to load, as sometimes it may not complete but you still want to see a partially loaded story. (Fixes an intermittent issue with AnandTech; see https://getsatisfaction.com/newsblur/topics/anandtech-straight-up-doesnt-load-on-the-ios-app) --- .../ios/Classes/StoryDetailViewController.m | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/clients/ios/Classes/StoryDetailViewController.m b/clients/ios/Classes/StoryDetailViewController.m index 9acbe3ed2..ed33c1d27 100644 --- a/clients/ios/Classes/StoryDetailViewController.m +++ b/clients/ios/Classes/StoryDetailViewController.m @@ -35,8 +35,7 @@ @interface StoryDetailViewController () -@property (nonatomic, strong) NSString *loadingHTML; -@property (nonatomic, strong) NSURL *loadingURL; +@property (nonatomic, strong) NSString *fullStoryHTML; @end @@ -330,6 +329,16 @@ [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 { self.noStoryMessage.hidden = YES; } @@ -495,24 +504,19 @@ 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); - NSString *path = [[NSBundle mainBundle] bundlePath]; - NSURL *baseURL = [NSURL fileURLWithPath:path]; - - self.loadingURL = baseURL; - self.loadingHTML = htmlContent; + self.hasStory = NO; + self.fullStoryHTML = htmlContent; dispatch_async(dispatch_get_main_queue(), ^{ - self.hasStory = YES; // NSLog(@"Drawing Story: %@", [self.activeStory objectForKey:@"story_title"]); [self.webView setMediaPlaybackRequiresUserAction:NO]; - [self.webView loadHTMLString:htmlString baseURL:baseURL]; + [self loadHTMLString:htmlTopAndBottom]; [appDelegate.storyPageControl setTextButton:self]; }); - self.activeStoryId = [self.activeStory objectForKey:@"story_hash"]; } @@ -587,13 +591,12 @@ themeStyle = [NSString stringWithFormat:@"", themeStyle]; } - NSURL *baseURL = [NSBundle mainBundle].bundleURL; NSString *html = [NSString stringWithFormat:@"" "%@" // header string "" "", themeStyle]; - - [self.webView loadHTMLString:html baseURL:baseURL]; + + [self loadHTMLString:html]; } - (NSString *)getHeader { @@ -1602,42 +1605,40 @@ shouldStartLoadWithRequest:(NSURLRequest *)request } - (void)webViewDidStartLoad:(UIWebView *)webView { - NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults]; - [self changeFontSize:[userPreferences stringForKey:@"story_font_size"]]; - [self changeLineSpacing:[userPreferences stringForKey:@"story_line_spacing"]]; + if (!self.hasStory) // other Web page loads aren't visible + return; -} - -- (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]; + // DOM should already be set up here NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults]; [self changeFontSize:[userPreferences stringForKey:@"story_font_size"]]; [self changeLineSpacing:[userPreferences stringForKey:@"story_line_spacing"]]; [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] && - self.activeStoryId && self.hasStory) { + self.activeStoryId) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, .15 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [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 {