Shell.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.ComponentModel;
  2. using System.Runtime.CompilerServices;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. namespace InABox.Avalonia
  6. {
  7. public abstract class Shell<TParent,TEntity> : INotifyPropertyChanged, IShell, IShell<TEntity>
  8. where TParent : ICoreRepository
  9. where TEntity : Entity, IPersistent, IRemotable, new()
  10. {
  11. private object? _tag;
  12. public object? Tag
  13. {
  14. get => _tag;
  15. set => SetProperty(ref _tag, value);
  16. }
  17. private bool _isSelected;
  18. public bool IsSelected
  19. {
  20. get => _isSelected;
  21. set
  22. {
  23. if(_isSelected != value)
  24. {
  25. _isSelected = value;
  26. DoPropertyChanged(nameof(IsSelected));
  27. }
  28. }
  29. }
  30. #region INotifyPropertyChanged
  31. public event PropertyChangedEventHandler? PropertyChanged;
  32. protected void DoPropertyChanged(string propertyName)
  33. {
  34. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  35. }
  36. protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
  37. {
  38. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  39. field = value;
  40. DoPropertyChanged(propertyName);
  41. return true;
  42. }
  43. #endregion
  44. private TEntity? _entity;
  45. private TEntity CheckEntity()
  46. {
  47. _entity ??= Row.ToObject<TEntity>();
  48. return _entity;
  49. }
  50. public TEntity Entity => CheckEntity();
  51. protected virtual void RowChanged()
  52. {
  53. }
  54. public bool IsChanged() => _entity?.IsChanged() ?? false;
  55. public virtual void Save(string auditmessage)
  56. {
  57. if (_entity != null)
  58. new Client<TEntity>().Save(_entity, auditmessage);
  59. }
  60. public void Cancel()
  61. {
  62. _entity?.CancelChanges();
  63. _entity = null;
  64. }
  65. private CoreRow _row = null!;
  66. public CoreRow Row
  67. {
  68. get => _row;
  69. set
  70. {
  71. _row = value;
  72. RowChanged();
  73. }
  74. }
  75. public TParent Parent { get; set; }
  76. ICoreRepository IShell.Parent => this.Parent;
  77. public Guid ID => Get<Guid>();
  78. public virtual string[] TextSearchValues() => [];
  79. public bool Match(string? text)
  80. {
  81. if (string.IsNullOrWhiteSpace(text))
  82. return true;
  83. foreach (var value in TextSearchValues())
  84. {
  85. if (value.Contains(text, StringComparison.InvariantCultureIgnoreCase))
  86. return true;
  87. }
  88. return false;
  89. }
  90. #region Row Get/Set Caching
  91. // We do this for three reasons:
  92. // 1. Rather than define properties in once class and columns in another,
  93. // we can define and link properties and columns in the one class,
  94. // using a _static_ constructor, which reduces complexity
  95. // 2. By caching based on the property name, we eliminate the need to convert
  96. // expressions to strings (expensive), while still retaining type-safety
  97. // 3. Using the Get/Set helper functions reduces code complexity when defining
  98. // shell properties, and distinguishes between data and calculated properties
  99. private static ShellColumns<TParent, TEntity>? _columns;
  100. public ShellColumns<TParent, TEntity> Columns
  101. {
  102. get
  103. {
  104. if (_columns == null)
  105. {
  106. _columns = new ShellColumns<TParent, TEntity>();
  107. _columns.Map(nameof(ID), x => x.ID);
  108. ConfigureColumns(_columns);
  109. }
  110. return _columns;
  111. }
  112. }
  113. protected abstract void ConfigureColumns(ShellColumns<TParent, TEntity> columns);
  114. protected virtual T Get<T>([CallerMemberName] string property = null)
  115. {
  116. if (_entity != null)
  117. {
  118. return (T)CoreUtils.GetPropertyValue(
  119. _entity,
  120. CoreUtils.GetFullPropertyName(Columns[property], ".")
  121. );
  122. }
  123. if (_row != null)
  124. {
  125. var col = _columns?.IndexOf(property);
  126. if (col != null)
  127. return Row.Get<T>(col.Value); //() Row.Values[];
  128. }
  129. return CoreUtils.GetDefault<T>();
  130. //return value != null ? (T)CoreUtils.ChangeType(value, typeof(T)) : CoreUtils.GetDefault<T>();
  131. }
  132. protected virtual void Set<T>(T value, bool notify = true, [CallerMemberName] string property = null)
  133. {
  134. CheckEntity();
  135. CoreUtils.SetPropertyValue(
  136. _entity,
  137. CoreUtils.GetFullPropertyName(Columns[property], "."),
  138. value
  139. );
  140. //Row.Values[Columns.IndexOf(property)] = value;
  141. if (notify)
  142. DoPropertyChanged(property);
  143. }
  144. #endregion
  145. }
  146. }