UserService moved out of the HiNc core package; Hi.Sample keeps its own copy under Sample.Common so the DemoSessionMessage #ShowStepPresent region (a live docfx code snippet referenced by two app-anatomy pages) still compiles. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
161 lines
4.8 KiB
C#
161 lines
4.8 KiB
C#
using Hi.Common;
|
|
using Hi.Common.XmlUtils;
|
|
using Hi.Licenses;
|
|
using Hi.MachiningSteps;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace Sample.Common;
|
|
|
|
/// <summary>
|
|
/// User Service.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Sample-owned copy, kept so the <see cref="DemoSessionMessage"/> step-present
|
|
/// demo still compiles. Each GUI/APP keeps its own version; the core engine
|
|
/// package (HiNc) no longer carries this GUI service type.
|
|
/// </remarks>
|
|
public class UserService : IDisposable
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the application configuration.
|
|
/// </summary>
|
|
public UserConfig UserConfig { get; set; } = new UserConfig();
|
|
/// <summary>
|
|
/// Gets or sets the path to the application configuration file.
|
|
/// </summary>
|
|
public string UserConfigPath { get; set; }
|
|
/// <summary>
|
|
/// Gets whether physics features are enabled based on configuration and license.
|
|
/// </summary>
|
|
public bool EnablePhysics =>
|
|
UserConfig.ShowPhysicsOptions && IsPhysicsLicensed;
|
|
/// <summary>
|
|
/// Gets whether advanced physics features are licensed.
|
|
/// </summary>
|
|
public bool IsPhysicsLicensed => License.IsLoggedIn(AuthFeature.AdvancedPhysics);
|
|
|
|
//runtime properties are not in the category of canonical IO.
|
|
#region Runtime Property
|
|
private readonly ILogger _logger;
|
|
LooseRunner SaveConfigLooseRunner;
|
|
/// <summary>
|
|
/// Gets or sets the currently selected item in the application.
|
|
/// </summary>
|
|
public object SelectedItem { get; set; }
|
|
private bool disposedValue;
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="UserService"/> class.
|
|
/// </summary>
|
|
/// <param name="logger">The logger instance.</param>
|
|
public UserService(ILogger logger) { _logger = logger; SaveConfigLooseRunner = new LooseRunner(logger); }
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="UserService"/> class with the specified configuration.
|
|
/// </summary>
|
|
/// <param name="appConfig">The application configuration.</param>
|
|
/// <param name="logger">The logger instance.</param>
|
|
public UserService(UserConfig appConfig, ILogger logger) : this(logger) { UserConfig = appConfig; }
|
|
|
|
/// <summary>
|
|
/// Saves the user configuration to the file specified by <see cref="UserConfigPath"/>.
|
|
/// </summary>
|
|
public void SaveUserConfig()
|
|
{
|
|
try
|
|
{
|
|
if (UserConfigPath != null)
|
|
UserConfig.MakeXmlSourceToFile(UserConfigPath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.LogError(ex, "{Message}", ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Schedules a loose save of the user configuration using a LooseRunner.
|
|
/// </summary>
|
|
public void LooseSaveUserConfig()
|
|
{
|
|
SaveConfigLooseRunner.TryRun(token => SaveUserConfig());
|
|
}
|
|
|
|
#region step present
|
|
/// <summary>
|
|
/// Gets or sets additional step presentation access configurations.
|
|
/// </summary>
|
|
public Dictionary<string, PresentAccess> AdditionalStepPresentAccess { get; set; } = new Dictionary<string, PresentAccess>();
|
|
/// <summary>
|
|
/// StepPresentAccessDictionary.
|
|
/// Read only.
|
|
/// </summary>
|
|
public Dictionary<string, PresentAccess> StepPresentAccessDictionary
|
|
{
|
|
get
|
|
{
|
|
var originalPresentAccessDictionary = typeof(MachiningStep).GetProperties()
|
|
.Where(p => p.GetCustomAttribute<PresentAttribute>() != null)
|
|
.ToDictionary(prop => prop.Name, prop =>
|
|
new PresentAccess(prop.GetCustomAttribute<PresentAttribute>(),
|
|
step => prop.GetValue(step)));
|
|
var presentAccessDictionary = new Dictionary<string, PresentAccess>(
|
|
AdditionalStepPresentAccess.Concat(originalPresentAccessDictionary));
|
|
return presentAccessDictionary;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Candidate Step Present Key List for display.
|
|
/// Read only.
|
|
/// </summary>
|
|
public List<string> CandidateStepPresentKeyList => StepPresentAccessDictionary.Keys.ToList();
|
|
/// <summary>
|
|
/// StepPresentAccessList for display.
|
|
/// Read only.
|
|
/// </summary>
|
|
public List<KeyValuePair<string, PresentAccess>> DisplayedStepPresentAccessList
|
|
{
|
|
get
|
|
{
|
|
var displayedStepPresentKeyList = UserConfig.DisplayedStepPresentKeyList;
|
|
List<KeyValuePair<string, PresentAccess>> dst
|
|
= new List<KeyValuePair<string, PresentAccess>>(
|
|
displayedStepPresentKeyList.Count);
|
|
var stepPresentAccessDictionary = StepPresentAccessDictionary;
|
|
foreach (var key in displayedStepPresentKeyList)
|
|
{
|
|
if (stepPresentAccessDictionary.TryGetValue(
|
|
key, out var access))
|
|
{
|
|
dst.Add(new KeyValuePair<string, PresentAccess>(key, access));
|
|
}
|
|
}
|
|
return dst;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <inheritdoc/>
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (!disposedValue)
|
|
{
|
|
if (disposing)
|
|
{
|
|
SaveConfigLooseRunner.Dispose();
|
|
}
|
|
disposedValue = true;
|
|
}
|
|
}
|
|
/// <inheritdoc/>
|
|
public void Dispose()
|
|
{
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
Dispose(disposing: true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|