You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2015/03/10 22:27:48 UTC

[3/3] cordova-plugin-splashscreen git commit: [WP8] code cleanup, minor refactors, comments to clarify some stuff.

[WP8] code cleanup, minor refactors, comments to clarify some stuff.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-splashscreen/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-splashscreen/commit/6b1e72c6
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-splashscreen/tree/6b1e72c6
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-splashscreen/diff/6b1e72c6

Branch: refs/heads/master
Commit: 6b1e72c6312f1c449d15a11be445b858c6689d6a
Parents: 98edfeb
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Tue Mar 10 14:27:29 2015 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Tue Mar 10 14:27:29 2015 -0700

----------------------------------------------------------------------
 src/wp/SplashScreen.cs | 196 +++++++++++++++++++++-----------------------
 1 file changed, 92 insertions(+), 104 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-splashscreen/blob/6b1e72c6/src/wp/SplashScreen.cs
----------------------------------------------------------------------
diff --git a/src/wp/SplashScreen.cs b/src/wp/SplashScreen.cs
index 0eaaaac..e8ef4c0 100644
--- a/src/wp/SplashScreen.cs
+++ b/src/wp/SplashScreen.cs
@@ -40,14 +40,24 @@ namespace WPCordovaClassLib.Cordova.Commands
     /// </summary>
     public class SplashScreen : BaseCommand
     {
-        private readonly Preferences preferences = new Preferences();
         private Popup popup;
 
+        // Time until we dismiss the splashscreen
+        private int prefDelay = 3000; 
+
+        // Whether we hide it by default
+        private bool prefAutoHide = true;
+
+        // Path to image to use
+        private string prefImagePath = "SplashScreenImage.jpg";
+
+        // static because autodismiss is only ever applied once, at app launch
+        // subsequent page loads should not cause the SplashScreen to be shown.
         private static bool WasShown = false;
 
         public SplashScreen()
         {
-            this.LoadPreferencesFromConfiguration();
+            LoadConfigPrefs();
 
             Image SplashScreen = new Image()
             {
@@ -56,7 +66,8 @@ namespace WPCordovaClassLib.Cordova.Commands
                 Stretch = Stretch.Fill
             };
 
-            var imageResource = Application.GetResourceStream(preferences.SplashScreenImage);
+            Uri imagePath = new Uri(prefImagePath, UriKind.RelativeOrAbsolute);
+            var imageResource = Application.GetResourceStream(imagePath);
             if (imageResource != null)
             {
                 BitmapImage splash_image = new BitmapImage();
@@ -65,36 +76,26 @@ namespace WPCordovaClassLib.Cordova.Commands
             }
 
             // Instansiate the popup and set the Child property of Popup to SplashScreen
-            this.popup = new Popup() { IsOpen = false, Child = SplashScreen };
-            // Orient the popup accordingly
-            this.popup.HorizontalAlignment = HorizontalAlignment.Stretch;
-            this.popup.VerticalAlignment = VerticalAlignment.Center;
+            popup = new Popup() { IsOpen = false, 
+                                  Child = SplashScreen,
+                                  HorizontalAlignment = HorizontalAlignment.Stretch,
+                                  VerticalAlignment = VerticalAlignment.Center
+
+            };
         }
 
         public override void OnInit()
         {
-            // we only want to autoload the first time a page is loaded.
-            if (!WasShown)
+            // we only want to autoload on the first page load.
+            // but OnInit is called for every page load.
+            if (!SplashScreen.WasShown)
             {
-                WasShown = true;
-                this.show();
+                SplashScreen.WasShown = true;
+                show();
             }
         }
 
-        private static void GetPreference(XDocument document, string preferenceName, Action<string> action)
-        {
-            var attribute = from results in document.Descendants()
-                            where (string)results.Attribute("name") == preferenceName
-                            select (string)results.Attribute("value");
-
-            var value = attribute.FirstOrDefault();
-            if (!string.IsNullOrWhiteSpace(value))
-            {
-                action(value);
-            }      
-        }
-
-        private void LoadPreferencesFromConfiguration()
+        private void LoadConfigPrefs()
         {
             StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
             if (streamInfo != null)
@@ -102,118 +103,105 @@ namespace WPCordovaClassLib.Cordova.Commands
                 using (StreamReader sr = new StreamReader(streamInfo.Stream))
                 {
                     //This will Read Keys Collection for the xml file
-                    XDocument document = XDocument.Parse(sr.ReadToEnd());
+                    XDocument configFile = XDocument.Parse(sr.ReadToEnd());
 
-                    GetPreference(document, "AutoHideSplashScreen", value =>
-                    {
-                        var autoHideSplashScreen = false;
-                        if (bool.TryParse(value, out autoHideSplashScreen))
-                        {
-                            preferences.AutoHideSplashScreen = autoHideSplashScreen;
-                        }
-                    });
-
-                    GetPreference(document, "SplashScreenDelay", value =>
+                    string configAutoHide = configFile.Descendants()
+                                        .Where(x => (string)x.Attribute("name") == "AutoHideSplashScreen")
+                                        .Select(x => (string)x.Attribute("value"))
+                                        .FirstOrDefault();
+                    bool.TryParse(configAutoHide, out prefAutoHide);
+
+
+                    string configDelay = configFile.Descendants()
+                                      .Where(x => (string)x.Attribute("name") == "SplashScreenDelay")
+                                      .Select(x => (string)x.Attribute("value"))
+                                      .FirstOrDefault();
+                    int.TryParse(configDelay, out prefDelay);
+
+                    string configImage = configFile.Descendants()
+                                        .Where(x => (string)x.Attribute("name") == "SplashScreen")
+                                        .Select(x => (string)x.Attribute("value"))
+                                        .FirstOrDefault();
+
+                    if (!String.IsNullOrEmpty(configImage))
                     {
-                        var splashScreenDelay = 0;
-                        if (int.TryParse(value, out splashScreenDelay))
-                        {
-                            preferences.SplashScreenDelay = splashScreenDelay;
-                        }
-                    });
-
-                    GetPreference(document, "SplashScreen", value => preferences.SplashScreenImage = new Uri(value, UriKind.Relative));
+                        prefImagePath = configImage;
+                    }
                 }
             }
         }
 
         public void show(string options = null)
         {
-            Deployment.Current.Dispatcher.BeginInvoke(() =>
+
+            if (!popup.IsOpen)    
             {
-                if (this.popup.IsOpen)
+                Deployment.Current.Dispatcher.BeginInvoke(() =>
                 {
-                    return;
-                }
+                    popup.Child.Opacity = 0;
 
-                this.popup.Child.Opacity = 0;
+                    Storyboard story = new Storyboard();
+                    DoubleAnimation animation = new DoubleAnimation()
+                                                    {
+                                                        From = 0.0,
+                                                        To = 1.0,
+                                                        Duration = new Duration(TimeSpan.FromSeconds(0.2))
+                                                    };
 
-                Storyboard story = new Storyboard();
-                DoubleAnimation animation;
-                animation = new DoubleAnimation();
-                animation.From = 0.0;
-                animation.To = 1.0;
-                animation.Duration = new Duration(TimeSpan.FromSeconds(0.2));
+                    Storyboard.SetTarget(animation, popup.Child);
+                    Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
+                    story.Children.Add(animation);
 
-                Storyboard.SetTarget(animation, popup.Child);
-                Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
-                story.Children.Add(animation);
+                    story.Begin();
 
-                Debug.WriteLine("Fading the splash screen in");
+                    popup.IsOpen = true;
 
-                story.Begin();
-
-                this.popup.IsOpen = true;
-
-                if (this.preferences.AutoHideSplashScreen)
-                {
-                    this.StartAutoHideTimer();
-                }
-            });
+                    if (prefAutoHide)
+                    {
+                        StartAutoHideTimer();
+                    }
+                });
+            }
         }
 
         public void hide(string options = null)
         {
-            Deployment.Current.Dispatcher.BeginInvoke(() =>
+            if (popup.IsOpen)
             {
-                if (!popup.IsOpen)
+                Deployment.Current.Dispatcher.BeginInvoke(() =>
                 {
-                    return;
-                }
 
-                popup.Child.Opacity = 1.0;
+                    popup.Child.Opacity = 1.0;
 
-                Storyboard story = new Storyboard();
-                DoubleAnimation animation;
-                animation = new DoubleAnimation();
-                animation.From = 1.0;
-                animation.To = 0.0;
-                animation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
+                    Storyboard story = new Storyboard();
+                    DoubleAnimation animation = new DoubleAnimation()
+                                                    {
+                                                        From = 1.0,
+                                                        To = 0.0,
+                                                        Duration = new Duration(TimeSpan.FromSeconds(0.4))
+                                                    };
 
-                Storyboard.SetTarget(animation, popup.Child);
-                Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
-                story.Children.Add(animation);
-                story.Completed += (object sender, EventArgs e) =>
-                {
-                    popup.IsOpen = false;
-                };
-                story.Begin();
-            });
+                    Storyboard.SetTarget(animation, popup.Child);
+                    Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
+                    story.Children.Add(animation);
+                    story.Completed += (object sender, EventArgs e) =>
+                    {
+                        popup.IsOpen = false;
+                    };
+                    story.Begin();
+                });
+            }
         }
 
         private void StartAutoHideTimer()
         {
-            var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(preferences.SplashScreenDelay) };
+            var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(prefDelay) };
             timer.Tick += (object sender, EventArgs e) =>
             {
-                this.hide();
+                hide();
                 timer.Stop();
             };
             timer.Start();
         }
-
-        private class Preferences
-        {
-            public bool AutoHideSplashScreen { get; set; }
-            public Uri SplashScreenImage { get; set; }
-            public int SplashScreenDelay { get; set; }
-
-            public Preferences()
-            {
-                this.SplashScreenDelay = 3000;
-                this.AutoHideSplashScreen = true;
-                this.SplashScreenImage = new Uri("SplashScreenImage.jpg", UriKind.Relative);
-            }
-        }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org