[WP8] code cleanup, minor refactors, comments to clarify some stuff.
This commit is contained in:
parent
98edfeb73e
commit
6b1e72c631
@ -40,14 +40,24 @@ namespace WPCordovaClassLib.Cordova.Commands
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SplashScreen : BaseCommand
|
public class SplashScreen : BaseCommand
|
||||||
{
|
{
|
||||||
private readonly Preferences preferences = new Preferences();
|
|
||||||
private Popup popup;
|
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;
|
private static bool WasShown = false;
|
||||||
|
|
||||||
public SplashScreen()
|
public SplashScreen()
|
||||||
{
|
{
|
||||||
this.LoadPreferencesFromConfiguration();
|
LoadConfigPrefs();
|
||||||
|
|
||||||
Image SplashScreen = new Image()
|
Image SplashScreen = new Image()
|
||||||
{
|
{
|
||||||
@ -56,7 +66,8 @@ namespace WPCordovaClassLib.Cordova.Commands
|
|||||||
Stretch = Stretch.Fill
|
Stretch = Stretch.Fill
|
||||||
};
|
};
|
||||||
|
|
||||||
var imageResource = Application.GetResourceStream(preferences.SplashScreenImage);
|
Uri imagePath = new Uri(prefImagePath, UriKind.RelativeOrAbsolute);
|
||||||
|
var imageResource = Application.GetResourceStream(imagePath);
|
||||||
if (imageResource != null)
|
if (imageResource != null)
|
||||||
{
|
{
|
||||||
BitmapImage splash_image = new BitmapImage();
|
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
|
// Instansiate the popup and set the Child property of Popup to SplashScreen
|
||||||
this.popup = new Popup() { IsOpen = false, Child = SplashScreen };
|
popup = new Popup() { IsOpen = false,
|
||||||
// Orient the popup accordingly
|
Child = SplashScreen,
|
||||||
this.popup.HorizontalAlignment = HorizontalAlignment.Stretch;
|
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||||
this.popup.VerticalAlignment = VerticalAlignment.Center;
|
VerticalAlignment = VerticalAlignment.Center
|
||||||
|
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnInit()
|
public override void OnInit()
|
||||||
{
|
{
|
||||||
// we only want to autoload the first time a page is loaded.
|
// we only want to autoload on the first page load.
|
||||||
if (!WasShown)
|
// but OnInit is called for every page load.
|
||||||
|
if (!SplashScreen.WasShown)
|
||||||
{
|
{
|
||||||
WasShown = true;
|
SplashScreen.WasShown = true;
|
||||||
this.show();
|
show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GetPreference(XDocument document, string preferenceName, Action<string> action)
|
private void LoadConfigPrefs()
|
||||||
{
|
|
||||||
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()
|
|
||||||
{
|
{
|
||||||
StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
|
StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
|
||||||
if (streamInfo != null)
|
if (streamInfo != null)
|
||||||
@ -102,83 +103,83 @@ namespace WPCordovaClassLib.Cordova.Commands
|
|||||||
using (StreamReader sr = new StreamReader(streamInfo.Stream))
|
using (StreamReader sr = new StreamReader(streamInfo.Stream))
|
||||||
{
|
{
|
||||||
//This will Read Keys Collection for the xml file
|
//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 =>
|
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 autoHideSplashScreen = false;
|
prefImagePath = configImage;
|
||||||
if (bool.TryParse(value, out autoHideSplashScreen))
|
|
||||||
{
|
|
||||||
preferences.AutoHideSplashScreen = autoHideSplashScreen;
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
GetPreference(document, "SplashScreenDelay", value =>
|
|
||||||
{
|
|
||||||
var splashScreenDelay = 0;
|
|
||||||
if (int.TryParse(value, out splashScreenDelay))
|
|
||||||
{
|
|
||||||
preferences.SplashScreenDelay = splashScreenDelay;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
GetPreference(document, "SplashScreen", value => preferences.SplashScreenImage = new Uri(value, UriKind.Relative));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void show(string options = null)
|
public void show(string options = null)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (!popup.IsOpen)
|
||||||
{
|
{
|
||||||
Deployment.Current.Dispatcher.BeginInvoke(() =>
|
Deployment.Current.Dispatcher.BeginInvoke(() =>
|
||||||
{
|
{
|
||||||
if (this.popup.IsOpen)
|
popup.Child.Opacity = 0;
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.popup.Child.Opacity = 0;
|
|
||||||
|
|
||||||
Storyboard story = new Storyboard();
|
Storyboard story = new Storyboard();
|
||||||
DoubleAnimation animation;
|
DoubleAnimation animation = new DoubleAnimation()
|
||||||
animation = new DoubleAnimation();
|
{
|
||||||
animation.From = 0.0;
|
From = 0.0,
|
||||||
animation.To = 1.0;
|
To = 1.0,
|
||||||
animation.Duration = new Duration(TimeSpan.FromSeconds(0.2));
|
Duration = new Duration(TimeSpan.FromSeconds(0.2))
|
||||||
|
};
|
||||||
|
|
||||||
Storyboard.SetTarget(animation, popup.Child);
|
Storyboard.SetTarget(animation, popup.Child);
|
||||||
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
|
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
|
||||||
story.Children.Add(animation);
|
story.Children.Add(animation);
|
||||||
|
|
||||||
Debug.WriteLine("Fading the splash screen in");
|
|
||||||
|
|
||||||
story.Begin();
|
story.Begin();
|
||||||
|
|
||||||
this.popup.IsOpen = true;
|
popup.IsOpen = true;
|
||||||
|
|
||||||
if (this.preferences.AutoHideSplashScreen)
|
if (prefAutoHide)
|
||||||
{
|
{
|
||||||
this.StartAutoHideTimer();
|
StartAutoHideTimer();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void hide(string options = null)
|
public void hide(string options = null)
|
||||||
|
{
|
||||||
|
if (popup.IsOpen)
|
||||||
{
|
{
|
||||||
Deployment.Current.Dispatcher.BeginInvoke(() =>
|
Deployment.Current.Dispatcher.BeginInvoke(() =>
|
||||||
{
|
{
|
||||||
if (!popup.IsOpen)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
popup.Child.Opacity = 1.0;
|
popup.Child.Opacity = 1.0;
|
||||||
|
|
||||||
Storyboard story = new Storyboard();
|
Storyboard story = new Storyboard();
|
||||||
DoubleAnimation animation;
|
DoubleAnimation animation = new DoubleAnimation()
|
||||||
animation = new DoubleAnimation();
|
{
|
||||||
animation.From = 1.0;
|
From = 1.0,
|
||||||
animation.To = 0.0;
|
To = 0.0,
|
||||||
animation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
|
Duration = new Duration(TimeSpan.FromSeconds(0.4))
|
||||||
|
};
|
||||||
|
|
||||||
Storyboard.SetTarget(animation, popup.Child);
|
Storyboard.SetTarget(animation, popup.Child);
|
||||||
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
|
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
|
||||||
@ -190,30 +191,17 @@ namespace WPCordovaClassLib.Cordova.Commands
|
|||||||
story.Begin();
|
story.Begin();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void StartAutoHideTimer()
|
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) =>
|
timer.Tick += (object sender, EventArgs e) =>
|
||||||
{
|
{
|
||||||
this.hide();
|
hide();
|
||||||
timer.Stop();
|
timer.Stop();
|
||||||
};
|
};
|
||||||
timer.Start();
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user