drumkit-old/DrumKit/UI/LoadingPage.xaml.cs

104 lines
3.3 KiB
C#
Raw Normal View History

2013-11-18 18:06:17 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2013-11-18 18:11:53 +00:00
using System.Threading.Tasks;
2013-11-18 18:06:17 +00:00
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace DrumKit
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class LoadingPage : Page
{
public LoadingPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
2013-11-18 18:11:53 +00:00
LoadStuff(e.Parameter);
2013-11-18 18:06:17 +00:00
}
2013-11-18 18:11:53 +00:00
private async Task DoFactoryReset()
2013-11-18 18:06:17 +00:00
{
2013-11-18 18:11:53 +00:00
DataController.Dispose();
await DataController.FactoryReset();
}
2013-11-18 18:06:17 +00:00
2013-11-18 18:11:53 +00:00
/// <summary>
/// Performs the loading of the application stuff.
/// </summary>
/// <param name="arg"></param>
private async void LoadStuff(object arg)
{
bool error = false;
DataController.ProgressChanged += DataController_ProgressChanged;
2013-11-18 18:06:17 +00:00
2013-11-18 18:11:53 +00:00
// Application restart?
if (arg is string && (string) arg == "restart")
DataController.Dispose();
2013-11-18 18:06:17 +00:00
2013-11-18 18:11:53 +00:00
// Factory reset?
if (arg is string && (string) arg == "reset")
await DoFactoryReset();
// Perform initializations
try {
await DataController.Initialize();
2013-11-18 18:06:17 +00:00
Frame.Navigate(typeof(MainPage));
}
// Error handling
2013-11-18 18:11:53 +00:00
catch (Exception ex) {
error = true;
2013-11-18 18:06:17 +00:00
Log.Error("Failure during loading!");
Log.Except(ex);
}
2013-11-18 18:11:53 +00:00
if (error) {
var dialog = new Windows.UI.Popups.MessageDialog("A problem occurred, and the application could not be loaded.", "An error occurred!");
dialog.Commands.Add(new Windows.UI.Popups.UICommand("Close", new Windows.UI.Popups.UICommandInvokedHandler(UICommandCloseHandler)));
dialog.CancelCommandIndex = dialog.DefaultCommandIndex = 0;
await dialog.ShowAsync();
}
}
/// <summary>
/// Application failed to load
/// </summary>
private async void UICommandCloseHandler(Windows.UI.Popups.IUICommand cmd)
{
await System.Threading.Tasks.Task.Delay(1000);
Windows.ApplicationModel.Core.CoreApplication.Exit();
}
/// <summary>
/// Progress event
/// </summary>
void DataController_ProgressChanged(object sender, KeyValuePair<int, string> e)
{
progressBar.Value = e.Key;
textLoading.Text = e.Value;
2013-11-18 18:06:17 +00:00
}
}
}