add an activity indicator to the preview view controller

This commit is contained in:
Sami Samhuri 2015-05-09 18:12:01 -07:00
parent 0c9c8164e4
commit c4dfcb9039
2 changed files with 24 additions and 15 deletions

View file

@ -321,18 +321,23 @@ wanted with as little input and thought as possible.</string>
<rect key="frame" x="0.0" y="0.0" width="600" height="536"/> <rect key="frame" x="0.0" y="0.0" width="600" height="536"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<webView contentMode="scaleToFill" restorationIdentifier="Web View" translatesAutoresizingMaskIntoConstraints="NO" id="0n2-Ma-NtP"> <webView hidden="YES" contentMode="scaleToFill" restorationIdentifier="Web View" translatesAutoresizingMaskIntoConstraints="NO" id="0n2-Ma-NtP">
<rect key="frame" x="0.0" y="0.0" width="600" height="536"/> <rect key="frame" x="0.0" y="0.0" width="600" height="536"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<connections> <connections>
<outlet property="delegate" destination="ixd-IL-hNy" id="Alk-Fw-YWo"/> <outlet property="delegate" destination="ixd-IL-hNy" id="Alk-Fw-YWo"/>
</connections> </connections>
</webView> </webView>
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" hidesWhenStopped="YES" animating="YES" style="whiteLarge" translatesAutoresizingMaskIntoConstraints="NO" id="zNM-N6-tdC">
<rect key="frame" x="282" y="250" width="37" height="37"/>
</activityIndicatorView>
</subviews> </subviews>
<color key="backgroundColor" red="0.1333333333" green="0.1333333333" blue="0.1333333333" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> <color key="backgroundColor" red="0.1333333333" green="0.1333333333" blue="0.1333333333" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints> <constraints>
<constraint firstAttribute="centerX" secondItem="zNM-N6-tdC" secondAttribute="centerX" id="AgQ-RY-Y6c"/>
<constraint firstAttribute="centerX" secondItem="0n2-Ma-NtP" secondAttribute="centerX" id="Iad-IJ-UKT"/> <constraint firstAttribute="centerX" secondItem="0n2-Ma-NtP" secondAttribute="centerX" id="Iad-IJ-UKT"/>
<constraint firstAttribute="centerY" secondItem="0n2-Ma-NtP" secondAttribute="centerY" id="cwx-9Q-nmc"/> <constraint firstAttribute="centerY" secondItem="0n2-Ma-NtP" secondAttribute="centerY" id="cwx-9Q-nmc"/>
<constraint firstAttribute="centerY" secondItem="zNM-N6-tdC" secondAttribute="centerY" id="e25-K8-rET"/>
<constraint firstItem="0n2-Ma-NtP" firstAttribute="height" secondItem="d1U-C6-XQQ" secondAttribute="height" id="eZP-iJ-reC"/> <constraint firstItem="0n2-Ma-NtP" firstAttribute="height" secondItem="d1U-C6-XQQ" secondAttribute="height" id="eZP-iJ-reC"/>
<constraint firstItem="0n2-Ma-NtP" firstAttribute="width" secondItem="d1U-C6-XQQ" secondAttribute="width" id="myh-gl-cb6"/> <constraint firstItem="0n2-Ma-NtP" firstAttribute="width" secondItem="d1U-C6-XQQ" secondAttribute="width" id="myh-gl-cb6"/>
</constraints> </constraints>
@ -342,6 +347,7 @@ wanted with as little input and thought as possible.</string>
</navigationItem> </navigationItem>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics" statusBarStyle="lightContent"/> <simulatedStatusBarMetrics key="simulatedStatusBarMetrics" statusBarStyle="lightContent"/>
<connections> <connections>
<outlet property="indicatorView" destination="zNM-N6-tdC" id="aBl-qZ-1YS"/>
<outlet property="webView" destination="0n2-Ma-NtP" id="4yz-yf-YCI"/> <outlet property="webView" destination="0n2-Ma-NtP" id="4yz-yf-YCI"/>
</connections> </connections>
</viewController> </viewController>
@ -480,6 +486,6 @@ wanted with as little input and thought as possible.</string>
</scene> </scene>
</scenes> </scenes>
<inferredMetricsTieBreakers> <inferredMetricsTieBreakers>
<segue reference="6S0-TO-JiA"/> <segue reference="Tll-UG-LXB"/>
</inferredMetricsTieBreakers> </inferredMetricsTieBreakers>
</document> </document>

View file

@ -11,6 +11,7 @@
@interface PreviewViewController () <UIWebViewDelegate> @interface PreviewViewController () <UIWebViewDelegate>
@property (nonatomic, weak) IBOutlet UIWebView *webView; @property (nonatomic, weak) IBOutlet UIWebView *webView;
@property (nonatomic, weak) IBOutlet UIActivityIndicatorView *indicatorView;
@end @end
@ -22,40 +23,42 @@
// UIWebView restores its request so we just have to reload it // UIWebView restores its request so we just have to reload it
if (!self.initialRequest && self.webView.request) { if (!self.initialRequest && self.webView.request) {
[self.webView reload]; [self.webView reload];
[self.indicatorView startAnimating];
return; return;
} }
if (self.initialRequest) { if (self.initialRequest) {
if (self.promise) { PMKPromise *p = self.promise ?: [PMKPromise promiseWithValue:nil];
__weak typeof(self) welf = self; __weak typeof(self) welf = self;
self.promise.then(^{ p.then(^{
typeof(self) self = welf; typeof(self) self = welf;
[self.webView loadRequest:self.initialRequest]; [self.webView loadRequest:self.initialRequest];
}).finally(^{ [self.indicatorView startAnimating];
typeof(self) self = welf; }).finally(^{
self.promise = nil; typeof(self) self = welf;
}); self.promise = nil;
return; });
}
[self.webView loadRequest:self.initialRequest];
return; return;
} }
} }
- (void)setInitialRequest:(NSURLRequest *)initialRequest { - (void)setInitialRequest:(NSURLRequest *)initialRequest {
_initialRequest = initialRequest; _initialRequest = initialRequest;
[self.webView loadHTMLString:@"<!doctype html><html><head><title></title></head><body></body></html>" baseURL:nil]; self.webView.hidden = YES;
} }
#pragma mark - UIWebViewDelegate methods #pragma mark - UIWebViewDelegate methods
- (void)webViewDidFinishLoad:(UIWebView *)webView { - (void)webViewDidFinishLoad:(UIWebView *)webView {
self.webView.hidden = NO;
[self.indicatorView stopAnimating];
if ([webView.request.URL isEqual:self.initialRequest.URL]) { if ([webView.request.URL isEqual:self.initialRequest.URL]) {
self.initialRequest = nil; self.initialRequest = nil;
} }
} }
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[self.indicatorView stopAnimating];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
__weak typeof(self) welf = self; __weak typeof(self) welf = self;
[alertController addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {