Skip to content

Commit

Permalink
Merge pull request #193 from Nfactor26/async-methods-for-wizard-and-s…
Browse files Browse the repository at this point in the history
…tagedsmartscreen

Async methods for Wizard and StagedSmartScreen
  • Loading branch information
Nfactor26 authored Dec 11, 2022
2 parents 27017f6 + 0e378f9 commit 73cd72c
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ public bool CanTryProcessStage
get => IsValid;
}

public override bool TryProcessStage(out string errorDescription)
{
errorDescription = string.Empty;
public override async Task<bool> TryProcessStage()
{
if(Validate())
{
this.PrefabName = this.PrefabName.Trim();
this.prefabProject.Namespace = $"{Constants.PrefabNameSpacePrefix}.{this.PrefabName.Replace(' ', '.')}";
logger.Information($"Prefab name is : {PrefabName}. Moving to next screen");
return true;
return await Task.FromResult(true);
}
return false;
return await Task.FromResult(false);
}

public override object GetProcessedResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
using Pixel.Automation.Editor.Core.Interfaces;
using Pixel.Scripting.Editor.Core.Contracts;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Pixel.Automation.AppExplorer.ViewModels.PrefabBuilder
Expand Down Expand Up @@ -62,12 +57,11 @@ public PrefabDataModelBuilderViewModel(PrefabProject prefabProject, ICodeGenerat
currentDataModel = rootEntity.EntityManager.Arguments;
}

public override bool TryProcessStage(out string errorDescription)
public override async Task<bool> TryProcessStage()
{
ClearErrors("");
if(!RequiredProperties.Any(a => a.IsRequired))
{
errorDescription = string.Empty;
logger.Information("None of the properties were marked as required proerties");
}

Expand Down Expand Up @@ -101,14 +95,14 @@ public override bool TryProcessStage(out string errorDescription)
classBuilder.AddProperty(property.PropertyName, property.PropertyType);
classBuilder.AddAttribute(property.PropertyName,typeof(ParameterUsage), new List<KeyValuePair<string, object>>() { new KeyValuePair<string, object>("", $"ParameterUsage.{property.Usage}") });
}
if (!classBuilder.HasErrors(out errorDescription))
if (!classBuilder.HasErrors(out string errorDescription))
{
this.generatedCode = classBuilder.GetGeneratedCode();
logger.Information($"Created prefab data model class : {Constants.PrefabDataModelName} with {RequiredProperties.Count()} properties");
return true;
return await Task.FromResult(true);
}
AddOrAppendErrors("", errorDescription);
return false;
return await Task.FromResult(false);
}

private void MirrorTargetType(Type targetType)
Expand Down Expand Up @@ -196,12 +190,12 @@ private void MarkRequiredProperties()
}


public override void OnPreviousScreen()
public override async Task OnPreviousScreen()
{
ClearErrors("");
RequiredProperties.Clear();
arguments.Clear();
base.OnPreviousScreen();
await base.OnPreviousScreen();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public PrefabDataModelEditorViewModel(PrefabProject prefabProject, IPrefabFileSy
this.prefabFileSystem = projectFileSystem;
}

public override bool TryProcessStage(out string errorDescription)
public override async Task<bool> TryProcessStage()
{
try
{
Expand All @@ -40,17 +40,15 @@ public override bool TryProcessStage(out string errorDescription)
compilationResult.SaveAssemblyToDisk(this.prefabFileSystem.TempDirectory);
dataModelAssembly = Assembly.LoadFrom(Path.Combine(this.prefabFileSystem.TempDirectory, compilationResult.OutputAssemblyName));
logger.Information($"Loaded prefab assembly : {compilationResult.OutputAssemblyName}");
errorDescription = string.Empty;
return true;
return await Task.FromResult(true);
}

}
catch (Exception ex)
{
logger.Error(ex, ex.Message);
errorDescription = ex.Message;
AddOrAppendErrors("", errorDescription);
return false;
logger.Error(ex, ex.Message);
AddOrAppendErrors("", ex.Message);
return await Task.FromResult(false);
}

}
Expand Down Expand Up @@ -93,16 +91,16 @@ string GetDataModelFileContent()
}
}

public override void OnFinished()
public override async Task OnFinished()
{
CleanUp();
base.OnPreviousScreen();
await base.OnPreviousScreen();
}

public override void OnCancelled()
public override async Task OnCancelled()
{
CleanUp();
base.OnCancelled();
await base.OnCancelled();
}

private void CleanUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,19 @@ public PrefabScriptsImporterViewModel(PrefabProject prefabToolBoxItem, Entity ro

}

public override bool TryProcessStage(out string errorDescription)
public override async Task<bool> TryProcessStage()
{
try
{
UpdateScriptFilePath();
ClearErrors("");
errorDescription = string.Empty;
return true;
ClearErrors("");
return await Task.FromResult(true);
}
catch (Exception ex)
{
logger.Error(ex, ex.Message);
errorDescription = ex.Message;
logger.Error(ex, ex.Message);
AddOrAppendErrors("", ex.Message);
return false;
return await Task.FromResult(true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,24 @@ public override object GetProcessedResult()
}

/// <inheritdoc/>
public override bool TryProcessStage(out string errorDescription)
public override async Task<bool> TryProcessStage()
{
this.ScriptEditor.Deactivate();
errorDescription = String.Empty;
return true;
return await Task.FromResult(true);
}

/// <inheritdoc/>
public override void OnFinished()
public override async Task OnFinished()
{
DisposeEditor();
base.OnFinished();
await base.OnFinished();
}

/// <inheritdoc/>
public override void OnCancelled()
public override async Task OnCancelled()
{
DisposeEditor();
base.OnCancelled();
await base.OnCancelled();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,35 +140,34 @@ public void PickOutputMappingScriptFile()
}

/// <inheritdoc/>
public override bool TryProcessStage(out string errorDescription)
public override async Task<bool> TryProcessStage()
{
//TODO : Can we make this asyc ?
if(this.SelectedVersion != null && !dropTarget.ComponentCollection.Any(a => a.Model.Equals(prefabEntity)))
{
UpdatePrefabReferences();
UpdateControlReferences();
await UpdatePrefabReferencesAsync();
await UpdateControlReferencesAsync();
dropTarget.AddComponent(prefabEntity);
this.CanChangeVersion = false;
logger.Information("Added version {0} of {1} to {2}.", this.SelectedVersion, this.prefabProject, this.dropTarget);
}
errorDescription = String.Empty;
}
return true;
}

/// <summary>
/// When adding a Prefab, make an entry of the Prefab in to the Prefab References file of the automation Process if it doesn't already exist.
/// </summary>
private void UpdatePrefabReferences()
private async Task UpdatePrefabReferencesAsync()
{
_ = this.projectReferenceManager.AddPrefabReferenceAsync(new PrefabReference() { ApplicationId = prefabProject.ApplicationId, PrefabId = prefabProject.PrefabId, Version = this.SelectedVersion });
await this.projectReferenceManager.AddPrefabReferenceAsync(new PrefabReference() { ApplicationId = prefabProject.ApplicationId, PrefabId = prefabProject.PrefabId, Version = this.SelectedVersion });
logger.Debug($"Updated prefab refrences file.");
}

/// <summary>
/// A Prefab might use one or more control. When adding a Prefab to an automation process, we need to update the Control References file
/// of the automation process to include details of controls being used by the Prefab.
/// </summary>
private void UpdateControlReferences()
private async Task UpdateControlReferencesAsync()
{
//Load control references file for prefab project
this.prefabFileSystem.Initialize(this.prefabProject, this.SelectedVersion);
Expand All @@ -184,7 +183,7 @@ private void UpdateControlReferences()
{
if (!projectControlReferences.HasReference(controlReference.ControlId))
{
_ = this.projectReferenceManager.AddControlReferenceAsync(controlReference);
await this.projectReferenceManager.AddControlReferenceAsync(controlReference);
}
}
logger.Debug($"Updated control refrences file.");
Expand Down
11 changes: 6 additions & 5 deletions src/Pixel.Automation.Editor.Core/Interfaces/IStagedScreen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Caliburn.Micro;
using System.Threading.Tasks;

namespace Pixel.Automation.Editor.Core
{
Expand All @@ -10,19 +11,19 @@ public interface IStagedScreen : IScreen

bool IsValid { get; }

bool TryProcessStage(out string errorDescription);
Task<bool> TryProcessStage();

object GetProcessedResult();

bool Validate();

void OnNextScreen();
Task OnNextScreen();

void OnPreviousScreen();
Task OnPreviousScreen();

void OnCancelled();
Task OnCancelled();

void OnFinished();
Task OnFinished();

}
}
85 changes: 43 additions & 42 deletions src/Pixel.Automation.Editor.Core/StagedSmartScreen.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
namespace Pixel.Automation.Editor.Core
{
public abstract class StagedSmartScreen : SmartScreen, IStagedScreen
{
public IStagedScreen NextScreen
{
get; set;
}

public IStagedScreen PreviousScreen
{
get; set;
}

public virtual bool IsValid
{
get
{
return !HasErrors;
}
}
using System.Threading.Tasks;

namespace Pixel.Automation.Editor.Core;

public abstract object GetProcessedResult();
public abstract class StagedSmartScreen : SmartScreen, IStagedScreen
{
public IStagedScreen NextScreen
{
get; set;
}

public virtual void OnCancelled()
{
}
public IStagedScreen PreviousScreen
{
get; set;
}

public virtual void OnFinished()
public virtual bool IsValid
{
get
{

return !HasErrors;
}
}

public virtual void OnNextScreen()
{

}
public abstract object GetProcessedResult();

public virtual void OnPreviousScreen()
{

}
public virtual async Task OnCancelled()
{
await Task.CompletedTask;
}

public virtual async Task OnFinished()
{
await Task.CompletedTask;
}

public abstract bool TryProcessStage(out string errorDescription);

public virtual bool Validate()
{
return IsValid;
}
public virtual async Task OnNextScreen()
{
await Task.CompletedTask;
}

public virtual async Task OnPreviousScreen()
{
await Task.CompletedTask;
}

public virtual async Task<bool> TryProcessStage()
{
return await Task.FromResult(true);
}

public virtual bool Validate()
{
return IsValid;
}
}
Loading

0 comments on commit 73cd72c

Please sign in to comment.