using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.Core.Helpers;
using Tango.BL.Entities;
using Tango.Logging;
using Tango.MachineStudio.Common.Modules;
using Tango.MachineStudio.Common.Navigation;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.Common.StudioApplication;
using Tango.SharedUI;
using Tango.BL;
using Tango.MachineStudio.Common.EventLogging;
using Tango.BL.Enumerations;
using Tango.MachineStudio.UI.TFS;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.Update;
using Tango.Core.DI;
using Tango.Settings;
using Tango.Core;
namespace Tango.MachineStudio.UI.ViewModels
{
///
/// Represents the Machine Studio loading view, view model.
///
///
public class LoadingViewVM : StudioViewModelInternal
{
private INotificationProvider _notificationProvider;
private TeamFoundationServiceExtendedClient _tfs;
private INavigationManager _navigationManager;
private IStudioModuleLoader _studioModuleLoader;
private IEventLogger _eventLogger;
private LogManager logManager = LogManager.Default;
public IStudioApplicationManager ApplicationManager { get; set; }
private bool _isLoading;
public bool IsLoading
{
get { return _isLoading; }
set { _isLoading = value; RaisePropertyChangedAuto(); }
}
private String _status;
public String Status
{
get { return _status; }
set { _status = value; RaisePropertyChangedAuto(); }
}
///
/// Initializes a new instance of the class.
///
/// The navigation manager.
/// The studio module loader.
/// The notification provider.
public LoadingViewVM(IStudioApplicationManager applicationManager, INavigationManager navigationManager, IStudioModuleLoader studioModuleLoader, INotificationProvider notificationProvider, IEventLogger eventLogger, TeamFoundationServiceExtendedClient teamFoundationClient)
{
Status = "Loading, please wait...";
_tfs = teamFoundationClient;
_eventLogger = eventLogger;
ApplicationManager = applicationManager;
_navigationManager = navigationManager;
_studioModuleLoader = studioModuleLoader;
_notificationProvider = notificationProvider;
}
public override void OnApplicationStarted()
{
base.OnApplicationStarted();
Load();
}
///
/// Load application modules.
///
private void Load()
{
IsLoading = true;
ThreadsHelper.StartStaThread(() =>
{
try
{
try
{
Status = "Checking for critical updates...";
LogManager.Log("Checking for forced update...");
var service = UpdateServiceHelper.GetUpdateServiceChannel();
var client = service.CreateChannel();
CheckForUpdatesResponse response = client.CheckForUpdates(new CheckForUpdatesRequest()
{
Email = "ForceUpdate",
Password = "ForceUpdate",
Version = ApplicationManager.Version,
});
if (response.IsUpdateAvailable && response.ForcedUpdate)
{
LogManager.Log("Forced update found, Navigating to update view!");
InvokeUI(() =>
{
if (_notificationProvider.ShowQuestion("Machine Studio has detected a critical update which must be installed in order for the application to run properly. Do you wish to download and install this update?"))
{
TangoMessenger.Default.Send(new Messages.ForcedUpdateMessage() { UpdateResponse = response });
_navigationManager.NavigateTo(NavigationView.UpdateView);
}
else
{
ApplicationManager.ShutDown();
}
IsLoading = false;
});
return;
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error checking for forced update!");
}
try
{
Status = "Connecting to Team Foundation Services...";
_tfs.Initialize();
Thread.Sleep(500);
}
catch (Exception ex)
{
LogManager.Log(ex, "Could not initialize Team Foundation Service client.");
}
Status = "Loading, please wait...";
ObservablesEntitiesAdapter.Instance.Initialize();
_eventLogger.Log(EventTypes.ApplicationStarted, "Application Started!");
Status = "Starting application...";
InvokeUI(() =>
{
_studioModuleLoader.LoadModules();
_navigationManager.NavigateTo(NavigationView.LoginView);
IsLoading = false;
});
}
catch (Exception ex)
{
logManager.Log(ex);
InvokeUINow(() =>
{
if (_notificationProvider.ShowQuestion("An error occurred while trying to connect to Twine database." + Environment.NewLine + "Would you like to try again?"))
{
Load();
}
else
{
ApplicationManager.ShutDown();
}
});
}
});
}
}
}