You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/02/15 20:12:04 UTC

[3/4] ios commit: [CB-2276] Move Splashscreen logic out of CDVViewController

[CB-2276] Move Splashscreen logic out of CDVViewController


Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/c5d3c00f
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/c5d3c00f
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/c5d3c00f

Branch: refs/heads/master
Commit: c5d3c00fc0207ac31df494a92449d19a301af32f
Parents: fa03d93
Author: Andrew Grieve <ag...@chromium.org>
Authored: Fri Feb 15 13:32:34 2013 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Fri Feb 15 13:48:51 2013 -0500

----------------------------------------------------------------------
 CordovaLib/Classes/CDVSplashScreen.h   |    6 +-
 CordovaLib/Classes/CDVSplashScreen.m   |  158 ++++++++++++++++++++++--
 CordovaLib/Classes/CDVViewController.h |    7 +-
 CordovaLib/Classes/CDVViewController.m |  177 +++------------------------
 4 files changed, 170 insertions(+), 178 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/c5d3c00f/CordovaLib/Classes/CDVSplashScreen.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVSplashScreen.h b/CordovaLib/Classes/CDVSplashScreen.h
index b0d8615..a0868a0 100644
--- a/CordovaLib/Classes/CDVSplashScreen.h
+++ b/CordovaLib/Classes/CDVSplashScreen.h
@@ -20,7 +20,11 @@
 #import <Foundation/Foundation.h>
 #import "CDVPlugin.h"
 
-@interface CDVSplashScreen : CDVPlugin {}
+@interface CDVSplashScreen : CDVPlugin {
+    UIActivityIndicatorView* _activityView;
+    UIImageView* _imageView;
+    UIView* _parentView;
+}
 
 - (void)show:(CDVInvokedUrlCommand*)command;
 - (void)hide:(CDVInvokedUrlCommand*)command;

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/c5d3c00f/CordovaLib/Classes/CDVSplashScreen.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVSplashScreen.m b/CordovaLib/Classes/CDVSplashScreen.m
index 24c0a34..f4fda5f 100644
--- a/CordovaLib/Classes/CDVSplashScreen.m
+++ b/CordovaLib/Classes/CDVSplashScreen.m
@@ -18,31 +18,167 @@
  */
 
 #import "CDVSplashScreen.h"
-#import "CDVViewController.h"
+
+#define kSplashScreenStateShow 0
+#define kSplashScreenStateHide 1
+
+#define kSplashScreenDurationDefault 2.0f
 
 @implementation CDVSplashScreen
 
-- (void)__show:(BOOL)show
+- (void)pluginInitialize
 {
-    // Legacy support - once deprecated classes removed, clean this up
-    id <UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageDidLoad) name:CDVPageDidLoadNotification object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onOrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
 
-    if ([delegate respondsToSelector:@selector(viewController)]) {
-        id vc = [delegate performSelector:@selector(viewController)];
-        if ([vc isKindOfClass:[CDVViewController class]]) {
-            show ? [((CDVViewController*)vc)showSplashScreen] : [((CDVViewController*)vc)hideSplashScreen];
-        }
+    if (self.useSplashScreen) {
+        [self show:nil];
     }
 }
 
 - (void)show:(CDVInvokedUrlCommand*)command
 {
-    [self __show:YES];
+    [self updateSplashScreenWithState:kSplashScreenStateShow];
 }
 
 - (void)hide:(CDVInvokedUrlCommand*)command
 {
-    [self __show:NO];
+    [self updateSplashScreenWithState:kSplashScreenStateHide];
+}
+
+- (BOOL)useSplashScreen
+{
+    id ret = self.commandDelegate.settings[@"ShowSplashScreen"];
+
+    // if value is missing, default to yes
+    return (ret == nil) || [ret boolValue];
+}
+
+- (void)pageDidLoad
+{
+    id autoHideSplashScreenValue = [self.commandDelegate.settings objectForKey:@"AutoHideSplashScreen"];
+
+    // if value is missing, default to yes
+    if ((autoHideSplashScreenValue == nil) || [autoHideSplashScreenValue boolValue]) {
+        [self hide:nil];
+    }
+}
+
+- (void)onOrientationWillChange:(NSNotification*)notification
+{
+    if (_imageView != nil) {
+        UIInterfaceOrientation orientation = [notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] intValue];
+        [self updateSplashImageForOrientation:orientation];
+    }
+}
+
+- (void)createViews
+{
+    /*
+     * The Activity View is the top spinning throbber in the status/battery bar. We init it with the default Grey Style.
+     *
+     *     whiteLarge = UIActivityIndicatorViewStyleWhiteLarge
+     *     white      = UIActivityIndicatorViewStyleWhite
+     *     gray       = UIActivityIndicatorViewStyleGray
+     *
+     */
+    NSString* topActivityIndicator = [self.commandDelegate.settings objectForKey:@"TopActivityIndicator"];
+    UIActivityIndicatorViewStyle topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
+
+    if ([topActivityIndicator isEqualToString:@"whiteLarge"]) {
+        topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhiteLarge;
+    } else if ([topActivityIndicator isEqualToString:@"white"]) {
+        topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
+    } else if ([topActivityIndicator isEqualToString:@"gray"]) {
+        topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
+    }
+
+    _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:topActivityIndicatorStyle];
+    _activityView.tag = 2;
+    _activityView.center = self.viewController.view.center;
+    [_activityView startAnimating];
+
+    _imageView = [[UIImageView alloc] init];
+    [self.viewController.view addSubview:_imageView];
+    [self.viewController.view.superview addSubview:_activityView];
+    [self.viewController.view.superview layoutSubviews];
+}
+
+- (void)updateSplashImageForOrientation:(UIInterfaceOrientation)orientation
+{
+    // IPHONE (default)
+    NSString* imageName = @"Default";
+
+    if (CDV_IsIPhone5()) {
+        imageName = [imageName stringByAppendingString:@"-568h"];
+    } else if (CDV_IsIPad()) {
+        // set default to portrait upside down
+        imageName = @"Default-Portrait"; // @"Default-PortraitUpsideDown.png";
+
+        if (orientation == UIInterfaceOrientationLandscapeLeft) {
+            imageName = @"Default-Landscape.png"; // @"Default-LandscapeLeft.png";
+        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
+            imageName = @"Default-Landscape.png"; // @"Default-LandscapeRight.png";
+        }
+    }
+
+    _imageView.image = [UIImage imageNamed:imageName];
+    _imageView.frame = CGRectMake(0, 0, _imageView.image.size.width, _imageView.image.size.height);
+}
+
+- (void)updateSplashScreenWithState:(int)state
+{
+    float toAlpha = state == kSplashScreenStateShow ? 1.0f : 0.0f;
+    BOOL hidden = state == kSplashScreenStateShow ? NO : YES;
+
+    id fadeSplashScreenValue = [self.commandDelegate.settings objectForKey:@"FadeSplashScreen"];
+    id fadeSplashScreenDuration = [self.commandDelegate.settings objectForKey:@"FadeSplashScreenDuration"];
+
+    float fadeDuration = fadeSplashScreenDuration == nil ? kSplashScreenDurationDefault : [fadeSplashScreenDuration floatValue];
+
+    if ((fadeSplashScreenValue == nil) || ![fadeSplashScreenValue boolValue]) {
+        fadeDuration = 0;
+    }
+    if (hidden && (_imageView == nil)) {
+        return;
+    } else if (_imageView == nil) {
+        [self createViews];
+        fadeDuration = 0;
+    }
+
+    if (!hidden) {
+        [self updateSplashImageForOrientation:self.viewController.interfaceOrientation];
+    }
+
+    if (fadeDuration == 0) {
+        [_imageView setHidden:hidden];
+        [_activityView setHidden:hidden];
+    } else {
+        if (state == kSplashScreenStateShow) {
+            // reset states
+            [_imageView setHidden:NO];
+            [_activityView setHidden:NO];
+            [_imageView setAlpha:0.0f];
+            [_activityView setAlpha:0.0f];
+        }
+
+        [UIView transitionWithView:self.viewController.view
+                          duration:fadeDuration
+                           options:UIViewAnimationOptionTransitionNone
+                        animations:^(void) {
+                [_imageView setAlpha:toAlpha];
+                [_activityView setAlpha:toAlpha];
+            }
+                        completion:^(BOOL finished) {
+                if (state == kSplashScreenStateHide) {
+                    // Clean-up resources.
+                    [_imageView removeFromSuperview];
+                    [_activityView removeFromSuperview];
+                    _imageView = nil;
+                    _activityView = nil;
+                }
+            }];
+    }
 }
 
 @end

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/c5d3c00f/CordovaLib/Classes/CDVViewController.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.h b/CordovaLib/Classes/CDVViewController.h
index e1e98be..553c628 100644
--- a/CordovaLib/Classes/CDVViewController.h
+++ b/CordovaLib/Classes/CDVViewController.h
@@ -45,9 +45,7 @@
 @property (nonatomic, readonly, strong) CDVWhitelist* whitelist; // readonly for public
 @property (nonatomic, readonly, assign) BOOL loadFromString;
 
-@property (nonatomic, readwrite, assign) BOOL useSplashScreen;
-@property (nonatomic, readonly, strong) IBOutlet UIActivityIndicatorView* activityView;
-@property (nonatomic, readonly, strong) UIImageView* imageView;
+@property (nonatomic, readwrite, assign) BOOL useSplashScreen CDV_DEPRECATED(2.5, "Replaced with ShowSplashScreen setting (or remove the SplashScreen plugin).");
 
 @property (nonatomic, readwrite, copy) NSString* wwwFolderName;
 @property (nonatomic, readwrite, copy) NSString* startPage;
@@ -73,7 +71,4 @@
 
 - (BOOL)URLisAllowed:(NSURL*)url;
 
-- (void)showSplashScreen;
-- (void)hideSplashScreen;
-
 @end

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/c5d3c00f/CordovaLib/Classes/CDVViewController.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.m b/CordovaLib/Classes/CDVViewController.m
index 8cda743..f6da41b 100644
--- a/CordovaLib/Classes/CDVViewController.m
+++ b/CordovaLib/Classes/CDVViewController.m
@@ -26,11 +26,6 @@
 
 #define degreesToRadian(x) (M_PI * (x) / 180.0)
 
-#define kSplashScreenStateShow 0
-#define kSplashScreenStateHide 1
-
-#define kSplashScreenDurationDefault 2.0f
-
 @interface CDVViewController () {
     NSInteger _userAgentLockToken;
     // Used to distinguish an iframe navigation from a top-level one.
@@ -47,8 +42,6 @@
 @property (nonatomic, readwrite, strong) NSArray* supportedOrientations;
 @property (nonatomic, readwrite, assign) BOOL loadFromString;
 
-@property (nonatomic, readwrite, strong) IBOutlet UIActivityIndicatorView* activityView;
-@property (nonatomic, readwrite, strong) UIImageView* imageView;
 @property (readwrite, assign) BOOL initialized;
 
 @property (atomic, strong) NSURL* openURL;
@@ -60,7 +53,6 @@
 @synthesize webView, supportedOrientations;
 @synthesize pluginObjects, pluginsMap, whitelist, startupPluginNames;
 @synthesize configParser, settings, loadFromString;
-@synthesize imageView, activityView, useSplashScreen;
 @synthesize wwwFolderName, startPage, initialized, openURL;
 @synthesize commandDelegate = _commandDelegate;
 @synthesize commandQueue = _commandQueue;
@@ -70,9 +62,6 @@
     if ((self != nil) && !self.initialized) {
         _commandQueue = [[CDVCommandQueue alloc] initWithViewController:self];
         _commandDelegate = [[CDVCommandDelegateImpl alloc] initWithViewController:self];
-        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
-        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedOrientationChange:)
-                                                     name:UIDeviceOrientationDidChangeNotification object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillTerminate:)
                                                      name:UIApplicationWillTerminateNotification object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillResignActive:)
@@ -187,6 +176,19 @@
     self.pluginObjects = [[NSMutableDictionary alloc] initWithCapacity:20];
 }
 
+- (BOOL)useSplashScreen
+{
+    id ret = self.settings[@"ShowSplashScreen"];
+
+    // if value is missing, default to yes
+    return (ret == nil) || [ret boolValue];
+}
+
+- (void)setUseSplashScreen:(BOOL)value
+{
+    self.settings[@"ShowSplashScreen"] = [NSNumber numberWithBool:value];
+}
+
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad
 {
@@ -321,6 +323,9 @@
         [self getCommandInstance:pluginName];
     }
 
+    // TODO: Remove this explicit instantiation once we move to cordova-CLI.
+    [self getCommandInstance:@"splashscreen"];
+
     // /////////////////
     [CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
             _userAgentLockToken = lockToken;
@@ -518,7 +523,7 @@
 }
 
 /**
- Called when the webview finishes loading.  This stops the activity view and closes the imageview
+ Called when the webview finishes loading.  This stops the activity view.
  */
 - (void)webViewDidFinishLoad:(UIWebView*)theWebView
 {
@@ -536,14 +541,6 @@
      */
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
 
-    id autoHideSplashScreenValue = [self.settings objectForKey:@"AutoHideSplashScreen"];
-    // if value is missing, default to yes
-    if ((autoHideSplashScreenValue == nil) || [autoHideSplashScreenValue boolValue]) {
-        [self hideSplashScreen];
-        [self.view.superview bringSubviewToFront:self.webView];
-    }
-    [self didRotateFromInterfaceOrientation:(UIInterfaceOrientation)[[UIDevice currentDevice] orientation]];
-
     // The .onNativeReady().fire() will work when cordova.js is already loaded.
     // The _nativeReady = true; is used when this is run before cordova.js is loaded.
     NSString* nativeReady = @"try{cordova.require('cordova/channel').onNativeReady.fire();}catch(e){window._nativeReady = true;}";
@@ -676,146 +673,6 @@
     return basePath;
 }
 
-- (void)updateSplashScreenSpinner
-{
-    /*
-     * The Activity View is the top spinning throbber in the status/battery bar. We init it with the default Grey Style.
-     *
-     *     whiteLarge = UIActivityIndicatorViewStyleWhiteLarge
-     *     white      = UIActivityIndicatorViewStyleWhite
-     *     gray       = UIActivityIndicatorViewStyleGray
-     *
-     */
-    NSString* topActivityIndicator = [self.settings objectForKey:@"TopActivityIndicator"];
-    UIActivityIndicatorViewStyle topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
-
-    if ([topActivityIndicator isEqualToString:@"whiteLarge"]) {
-        topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhiteLarge;
-    } else if ([topActivityIndicator isEqualToString:@"white"]) {
-        topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
-    } else if ([topActivityIndicator isEqualToString:@"gray"]) {
-        topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
-    }
-
-    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:topActivityIndicatorStyle];
-    self.activityView.tag = 2;
-
-    id showSplashScreenSpinnerValue = [self.settings objectForKey:@"ShowSplashScreenSpinner"];
-    // backwards compatibility - if key is missing, default to true
-    if ((showSplashScreenSpinnerValue == nil) || [showSplashScreenSpinnerValue boolValue]) {
-        [self.view.superview addSubview:self.activityView];
-    }
-
-    self.activityView.center = self.view.center;
-    [self.activityView startAnimating];
-
-    [self.view.superview layoutSubviews];
-}
-
-- (void)updateSplashImageForOrientation:(UIInterfaceOrientation)orientation
-{
-    if (self.imageView == nil) {
-        self.imageView = [[UIImageView alloc] init];
-        [self.view addSubview:self.imageView];
-    }
-
-    // IPHONE (default)
-    NSString* imageName = @"Default";
-
-    if (CDV_IsIPhone5()) {
-        imageName = [imageName stringByAppendingString:@"-568h"];
-    } else if (CDV_IsIPad()) {
-        // set default to portrait upside down
-        imageName = @"Default-Portrait"; // @"Default-PortraitUpsideDown.png";
-
-        if (orientation == UIInterfaceOrientationLandscapeLeft) {
-            imageName = @"Default-Landscape.png"; // @"Default-LandscapeLeft.png";
-        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
-            imageName = @"Default-Landscape.png"; // @"Default-LandscapeRight.png";
-        }
-    }
-
-    self.imageView.image = [UIImage imageNamed:imageName];
-    self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
-}
-
-- (void)showSplashScreen
-{
-    [self updateSplashScreenWithState:kSplashScreenStateShow];
-}
-
-- (void)hideSplashScreen
-{
-    [self updateSplashScreenWithState:kSplashScreenStateHide];
-}
-
-- (void)updateSplashScreenWithState:(int)state
-{
-    float toAlpha = state == kSplashScreenStateShow ? 1.0f : 0.0f;
-    BOOL hidden = state == kSplashScreenStateShow ? NO : YES;
-
-    id fadeSplashScreenValue = [self.settings objectForKey:@"FadeSplashScreen"];
-    id fadeSplashScreenDuration = [self.settings objectForKey:@"FadeSplashScreenDuration"];
-
-    // if value is missing, default to 2.0f
-    float fadeDuration = fadeSplashScreenDuration == nil ? kSplashScreenDurationDefault : [fadeSplashScreenDuration floatValue];
-
-    // if value is missing, default to no fade
-    if ((fadeSplashScreenValue == nil) || ![fadeSplashScreenValue boolValue]) {
-        [self.imageView setHidden:hidden];
-        [self.activityView setHidden:hidden];
-    } else {
-        if (state == kSplashScreenStateShow) {
-            // reset states
-            [self.imageView setHidden:NO];
-            [self.activityView setHidden:NO];
-            [self.imageView setAlpha:0.0f];
-            [self.activityView setAlpha:0.0f];
-        }
-
-        [UIView transitionWithView:self.view
-                          duration:fadeDuration
-                           options:UIViewAnimationOptionTransitionNone
-                        animations:^(void) {
-                [self.imageView setAlpha:toAlpha];
-                [self.activityView setAlpha:toAlpha];
-            }
-                        completion:^(BOOL finished) {
-                if (state == kSplashScreenStateHide) {
-                    // reset states
-                    [self.imageView setHidden:YES];
-                    [self.activityView setHidden:YES];
-                    [self.imageView setAlpha:1.0f];
-                    [self.activityView setAlpha:1.0f];
-                }
-            }];
-    }
-}
-
-- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
-{
-    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
-
-    if (self.useSplashScreen && (self.imageView != nil)) {
-        [self updateSplashImageForOrientation:toInterfaceOrientation];
-    }
-}
-
-BOOL gSplashScreenShown = NO;
-- (void)receivedOrientationChange:(NSNotification*)notification
-{
-    if (self.imageView == nil) {
-        gSplashScreenShown = YES;
-        if (self.useSplashScreen) {
-            [self updateSplashImageForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
-        }
-    }
-
-    if (self.activityView == nil) {
-        [self updateSplashScreenSpinner];
-    }
-}
-
 #pragma mark CordovaCommands
 
 - (void)registerPlugin:(CDVPlugin*)plugin withClassName:(NSString*)className