using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.BL; using Tango.BL.Builders; using Tango.BL.Entities; using Tango.MachineStudio.Common; using Tango.MachineStudio.Common.Notifications; namespace Tango.MachineStudio.RML.ViewModels { public class MainViewVM : StudioViewModel { private INotificationProvider _notification; private ObservablesContext _rmls_context; private ObservablesContext _active_context; private ObservableCollection _rmls; public ObservableCollection Rmls { get { return _rmls; } set { _rmls = value; RaisePropertyChangedAuto(); } } private Rml _selectedRML; public Rml SelectedRML { get { return _selectedRML; } set { _selectedRML = value; RaisePropertyChangedAuto(); OnSelectedRmlChanged(); } } private Rml _activeRML; public Rml ActiveRML { get { return _activeRML; } set { _activeRML = value; RaisePropertyChangedAuto(); } } public MainViewVM(INotificationProvider notificationProvider) { _notification = notificationProvider; } public override void OnApplicationReady() { LoadRmls(); } private void LoadRmls() { using (_rmls_context = ObservablesContext.CreateDefault()) { Rmls = _rmls.ToList().ToObservableCollection(); } } private void OnSelectedRmlChanged() { if (SelectedRML != null) { LoadActiveRML(SelectedRML.Guid); } else { UnloadActiveRML(); } } private void UnloadActiveRML() { ActiveRML = null; } private async void LoadActiveRML(String guid) { using (_notification.PushTaskItem("Loading RML...")) { if (_active_context != null) { _active_context.Dispose(); } _active_context = ObservablesContext.CreateDefault(); ActiveRML = await new RmlBuilder(_active_context) .Set(guid) .WithAllParametersGroup() .WithCCT() .WithLiquidFactors() .BuildAsync(); } } } }