Skip to content

Commit afcd147

Browse files
committed
[Tabs] Only immediately reload if there was an error, otherwise delay 1 hour.
1 parent d6bd0ac commit afcd147

File tree

5 files changed

+96
-17
lines changed

5 files changed

+96
-17
lines changed

Artsy.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
49F45188176A71B50041A4B4 /* ARArtworkSetViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4917819E176A6B22001E751E /* ARArtworkSetViewController.m */; };
212212
5122D69A1A89E5F800DA2704 /* ARFairMapAnnotationViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5122D6991A89E5F800DA2704 /* ARFairMapAnnotationViewTests.m */; };
213213
513483551AADD0A10062E624 /* PSPDFKitImproveRecursiveDescription.m in Sources */ = {isa = PBXBuildFile; fileRef = 513483541AADD0A10062E624 /* PSPDFKitImproveRecursiveDescription.m */; };
214+
5144331B1AD68D8400FBBCE5 /* ARTopMenuInternalMobileWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5144331A1AD68D8400FBBCE5 /* ARTopMenuInternalMobileWebViewController.m */; };
214215
5174A03B1A859E2C006CD337 /* ARFairMapView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5174A03A1A859E2C006CD337 /* ARFairMapView.m */; };
215216
540262C618A0FAFB00844AE1 /* ARButtonWithImage.m in Sources */ = {isa = PBXBuildFile; fileRef = 540262C518A0FAFB00844AE1 /* ARButtonWithImage.m */; };
216217
54289FEE18AA7F4E00681E49 /* UINavigationController_InnermostTopViewControllerSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 54289FED18AA7F4E00681E49 /* UINavigationController_InnermostTopViewControllerSpec.m */; };
@@ -951,6 +952,8 @@
951952
49F0C67E17B972F200721244 /* ARSlideshowView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARSlideshowView.m; sourceTree = "<group>"; };
952953
5122D6991A89E5F800DA2704 /* ARFairMapAnnotationViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARFairMapAnnotationViewTests.m; sourceTree = "<group>"; };
953954
513483541AADD0A10062E624 /* PSPDFKitImproveRecursiveDescription.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PSPDFKitImproveRecursiveDescription.m; sourceTree = "<group>"; };
955+
514433191AD68D8400FBBCE5 /* ARTopMenuInternalMobileWebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ARTopMenuInternalMobileWebViewController.h; sourceTree = "<group>"; };
956+
5144331A1AD68D8400FBBCE5 /* ARTopMenuInternalMobileWebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARTopMenuInternalMobileWebViewController.m; sourceTree = "<group>"; };
954957
5174A0391A859E2C006CD337 /* ARFairMapView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ARFairMapView.h; sourceTree = "<group>"; };
955958
5174A03A1A859E2C006CD337 /* ARFairMapView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARFairMapView.m; sourceTree = "<group>"; };
956959
540262C418A0FAFB00844AE1 /* ARButtonWithImage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ARButtonWithImage.h; path = "Table View Cells/ARButtonWithImage.h"; sourceTree = "<group>"; };
@@ -3479,6 +3482,8 @@
34793482
60647C741A94F71600A45247 /* AREndOfLineInternalMobileWebViewController.m */,
34803483
60A309A61AC999DE000783C1 /* ARInternalShareValidator.h */,
34813484
60A309A71AC999DE000783C1 /* ARInternalShareValidator.m */,
3485+
514433191AD68D8400FBBCE5 /* ARTopMenuInternalMobileWebViewController.h */,
3486+
5144331A1AD68D8400FBBCE5 /* ARTopMenuInternalMobileWebViewController.m */,
34823487
);
34833488
name = "Web Browsing";
34843489
sourceTree = "<group>";
@@ -4370,6 +4375,7 @@
43704375
062C202116DD76C90095A7EC /* ARZoomView.m in Sources */,
43714376
E6A3500518AAEBFF0075398F /* NSKeyedUnarchiver+ErrorLogging.m in Sources */,
43724377
3CCCC89B18996DD4008015DD /* Post.m in Sources */,
4378+
5144331B1AD68D8400FBBCE5 /* ARTopMenuInternalMobileWebViewController.m in Sources */,
43734379
609F985F1AC1054D0079FE21 /* PartnerShowCoordinates.m in Sources */,
43744380
3CEE0B6818A16F9000FEA6E6 /* ArtsyAPI+Genes.m in Sources */,
43754381
60F1C51B17C11303000938F7 /* ARGeneViewController.m in Sources */,

Artsy/Classes/View Controllers/ARInternalMobileWebViewController.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ - (UIEdgeInsets)webViewScrollIndicatorsInsets;
1212
@interface ARInternalMobileWebViewController() <UIAlertViewDelegate, TSMiniWebBrowserDelegate>
1313
@property (nonatomic, assign) BOOL loaded;
1414
@property (nonatomic, readonly, strong) ARInternalShareValidator *shareValidator;
15-
1615
@end
1716

1817
@implementation ARInternalMobileWebViewController
@@ -95,7 +94,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)aWebView
9594
{
9695
[super webViewDidFinishLoad:aWebView];
9796
[self hideLoading];
98-
_loaded = YES;
97+
self.loaded = YES;
9998
}
10099

101100
- (void)hideLoading
@@ -125,7 +124,7 @@ - (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *
125124
} else if ([ARRouter isInternalURL:request.URL] && ([request.URL.path isEqual:@"/log_in"] || [request.URL.path isEqual:@"/sign_up"])) {
126125
// hijack AJAX requests
127126
if ([User isTrialUser]) {
128-
[ARTrialController presentTrialWithContext:ARTrialContextNotTrial fromTarget:self selector:@selector(reload)];
127+
[ARTrialController presentTrialWithContext:ARTrialContextNotTrial fromTarget:self selector:@selector(userDidSignUp)];
129128
}
130129
return NO;
131130
}
@@ -135,7 +134,7 @@ - (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *
135134

136135
// A full reload, not just a webView.reload, which only refreshes the view without re-requesting data.
137136

138-
- (void)reload
137+
- (void)userDidSignUp
139138
{
140139
[self.webView loadRequest:[self requestWithURL:self.currentURL]];
141140
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#import "ARInternalMobileWebViewController.h"
2+
3+
@interface ARTopMenuInternalMobileWebViewController : ARInternalMobileWebViewController
4+
@property (nonatomic, readonly) BOOL shouldBeReloaded;
5+
- (void)reload;
6+
@end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#import "ARTopMenuInternalMobileWebViewController.h"
2+
3+
#define MAX_AGE 3600 // 1 hour
4+
5+
@interface ARTopMenuInternalMobileWebViewController ()
6+
@property (nonatomic, assign) BOOL hasSuccessfullyLoadedLastRequest;
7+
@property (nonatomic, strong) NSDate *lastRequestLoadedAt;
8+
@end
9+
10+
@implementation ARTopMenuInternalMobileWebViewController
11+
12+
- (void)reload;
13+
{
14+
[self loadURL:self.currentURL];
15+
}
16+
17+
// If the currently visible view is the root webview, reload it. This ensures that an existing view hierachy isn't
18+
// thrown out every time the user changes tabs, but that the user also has a way to effectively ‘reload’ a webview.
19+
// This is needed because there could have been a connectivity/server error at the time of loading and also because
20+
// content needs to be refreshable.
21+
//
22+
- (BOOL)shouldBeReloaded;
23+
{
24+
return !self.hasSuccessfullyLoadedLastRequest || (self.isCurrentlyVisibleViewController && self.isContentStale);
25+
}
26+
27+
- (BOOL)isCurrentlyVisibleViewController;
28+
{
29+
return self.navigationController.visibleViewController == self;
30+
}
31+
32+
- (BOOL)isContentStale;
33+
{
34+
return self.lastRequestLoadedAt.timeIntervalSinceNow < -MAX_AGE;
35+
}
36+
37+
#pragma mark - Overrides
38+
39+
- (instancetype)initWithURL:(NSURL *)url;
40+
{
41+
if ((self = [super initWithURL:url])) {
42+
_hasSuccessfullyLoadedLastRequest = YES;
43+
}
44+
return self;
45+
}
46+
47+
- (void)loadURL:(NSURL *)url
48+
{
49+
self.lastRequestLoadedAt = nil;
50+
[super loadURL:url];
51+
}
52+
53+
- (void)webViewDidFinishLoad:(UIWebView *)webView;
54+
{
55+
[super webViewDidFinishLoad:webView];
56+
57+
NSCachedURLResponse *urlResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
58+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)urlResponse.response;
59+
NSInteger statusCode = httpResponse.statusCode;
60+
self.hasSuccessfullyLoadedLastRequest = statusCode >= 200 && statusCode < 300;
61+
62+
if (self.hasSuccessfullyLoadedLastRequest) {
63+
self.lastRequestLoadedAt = [NSDate date];
64+
}
65+
}
66+
67+
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
68+
{
69+
[super webView:webView didFailLoadWithError:error];
70+
self.hasSuccessfullyLoadedLastRequest = NO;
71+
}
72+
73+
@end

Artsy/Classes/View Controllers/ARTopMenuNavigationDataSource.m

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@
44
#import "ARFavoritesViewController.h"
55
#import "ARAppSearchViewController.h"
66
#import "ARHeroUnitsNetworkModel.h"
7-
#import "ARInternalMobileWebViewController.h"
7+
#import "ARTopMenuInternalMobileWebViewController.h"
88
#import <SDWebImage/SDWebImagePrefetcher.h>
99

1010
static ARNavigationController *
11-
InternalWebViewNavigationControllerWithPath(NSString *path) {
11+
WebViewNavigationControllerWithPath(NSString *path) {
1212
NSURL *URL = [NSURL URLWithString:path];
13-
ARInternalMobileWebViewController *viewController = [[ARInternalMobileWebViewController alloc] initWithURL:URL];
13+
ARTopMenuInternalMobileWebViewController *viewController = [[ARTopMenuInternalMobileWebViewController alloc] initWithURL:URL];
1414
return [[ARNavigationController alloc] initWithRootViewController:viewController];
1515
}
1616

17-
// If the currently visible view is the root webview, reload it. This ensures that an existing view hierachy isn't
18-
// thrown out every time the user changes tabs, but that the user also has a way to effectively ‘reload’ a webview.
19-
// This is needed because there could have been a connectivity/server error at the time of loading and also because
20-
// content needs to be refreshable.
21-
//
2217
static ARNavigationController *
2318
RefreshedWebViewNavigationController(ARNavigationController *navigationController) {
2419
NSArray *viewControllers = navigationController.viewControllers;
25-
ARInternalMobileWebViewController *rootViewController = (ARInternalMobileWebViewController *)viewControllers[0];
26-
if (navigationController.visibleViewController == rootViewController) {
27-
[rootViewController loadURL:rootViewController.currentURL];
20+
ARTopMenuInternalMobileWebViewController *viewController = (ARTopMenuInternalMobileWebViewController *)viewControllers[0];
21+
if (viewController.shouldBeReloaded) {
22+
[viewController reload];
2823
}
2924
return navigationController;
3025
}
@@ -59,13 +54,13 @@ - (instancetype)init
5954
_showFeedViewController.heroUnitDatasource = [[ARHeroUnitsNetworkModel alloc] init];
6055
_feedNavigationController = [[ARNavigationController alloc] initWithRootViewController:_showFeedViewController];
6156

62-
_showsNavigationController = InternalWebViewNavigationControllerWithPath(@"/shows");
57+
_showsNavigationController = WebViewNavigationControllerWithPath(@"/shows");
6358

6459
_browseViewController = [[ARBrowseViewController alloc] init];
6560
_browseViewController.networkModel = [[ARBrowseNetworkModel alloc] init];
6661
_browseNavigationController = [[ARNavigationController alloc] initWithRootViewController:_browseViewController];
6762

68-
_magazineNavigationController = InternalWebViewNavigationControllerWithPath(@"/magazine");
63+
_magazineNavigationController = WebViewNavigationControllerWithPath(@"/magazine");
6964

7065
return self;
7166
}

0 commit comments

Comments
 (0)