DynamicDashboard.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using InABox.Core;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace InABox.Wpf.Dashboard;
  10. public class DynamicDashboard
  11. {
  12. public DynamicDashboardDataComponent DataComponent { get; set; } = new();
  13. [JsonIgnore]
  14. public IDynamicDashboardDataPresenter? DataPresenter { get; set; }
  15. #region Serialization
  16. private string _presenterProperties = "";
  17. [JsonProperty(Order = 1, PropertyName = "PresenterProperties")]
  18. private string PresenterProperties
  19. {
  20. get => DataPresenter is not null ? Serialization.Serialize(DataPresenter.Properties) : "";
  21. set
  22. {
  23. _presenterProperties = value;
  24. }
  25. }
  26. [JsonProperty(Order = 2, PropertyName = "PresenterType")]
  27. private Type? PresenterType
  28. {
  29. get => DataPresenter?.GetType();
  30. set
  31. {
  32. if(value is null)
  33. {
  34. DataPresenter = null;
  35. }
  36. else
  37. {
  38. DataPresenter = Activator.CreateInstance(value) as IDynamicDashboardDataPresenter;
  39. var propertiesType = value.GetInterfaceDefinition(typeof(IDynamicDashboardDataPresenter<>))!.GenericTypeArguments[0];
  40. DataPresenter!.Properties = Serialization.Deserialize(propertiesType, _presenterProperties)
  41. ?? Activator.CreateInstance(propertiesType)!;
  42. }
  43. }
  44. }
  45. #endregion
  46. }
  47. public static class DynamicDashboardUtils
  48. {
  49. private static Type[]? _presenterTypes;
  50. private static Dictionary<Type, Type>? _presenterEditorTypes;
  51. private static bool _loaded;
  52. [MemberNotNull(nameof(_presenterTypes), nameof(_presenterEditorTypes))]
  53. private static void LoadTypes()
  54. {
  55. if(!_loaded)
  56. {
  57. var presenters = new List<Type>();
  58. var presenterEditors = new Dictionary<Type, Type>();
  59. foreach(var entity in CoreUtils.Entities)
  60. {
  61. if (entity.HasInterface<IDynamicDashboardDataPresenter>())
  62. {
  63. presenters.Add(entity);
  64. }
  65. else if(entity.GetInterfaceDefinition(typeof(IDynamicDashboardDataPresenterEditor<,>)) is Type editor)
  66. {
  67. presenterEditors[editor.GenericTypeArguments[0]] = entity;
  68. }
  69. }
  70. _presenterTypes = presenters.ToArray();
  71. _presenterEditorTypes = presenterEditors;
  72. _loaded = true;
  73. }
  74. }
  75. public static IEnumerable<Type> GetPresenterTypes()
  76. {
  77. LoadTypes();
  78. return _presenterTypes;
  79. }
  80. public static bool TryGetPresenterEditor(Type presenterType, [NotNullWhen(true)] out Type? editor)
  81. {
  82. LoadTypes();
  83. return _presenterEditorTypes.TryGetValue(presenterType, out editor);
  84. }
  85. public static Type? GetPresenterEditor(Type presenterType)
  86. {
  87. LoadTypes();
  88. return _presenterEditorTypes.GetValueOrDefault(presenterType);
  89. }
  90. private static JsonSerializerSettings SerializationSettings()
  91. {
  92. var settings = Serialization.CreateSerializerSettings();
  93. settings.TypeNameHandling = TypeNameHandling.Auto;
  94. return settings;
  95. }
  96. public static string Serialize(DynamicDashboard data)
  97. {
  98. return JsonConvert.SerializeObject(data, typeof(DynamicDashboard), SerializationSettings());
  99. }
  100. public static DynamicDashboard Deserialize(string json)
  101. {
  102. return JsonConvert.DeserializeObject<DynamicDashboard>(json, SerializationSettings())!;
  103. }
  104. }