Shell.cs 6.0 KB

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