Shell.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 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. _entity.PropertyCascaded -= _entity_PropertyChanged;
  49. _entity.PropertyCascaded += _entity_PropertyChanged;
  50. return _entity;
  51. }
  52. private void _entity_PropertyChanged(object? sender, PropertyChangedEventArgs e)
  53. {
  54. if(e.PropertyName is not null && Columns.FindShellProperty(e.PropertyName) is string property)
  55. {
  56. DoPropertyChanged(property);
  57. }
  58. }
  59. public TEntity Entity => CheckEntity();
  60. protected virtual void RowChanged()
  61. {
  62. }
  63. public bool IsChanged() => _entity?.IsChanged() ?? false;
  64. public void SyncRow()
  65. {
  66. Row.Table.FillRow(Row, Entity);
  67. }
  68. public virtual void Save(string auditmessage)
  69. {
  70. if (_entity != null)
  71. {
  72. new Client<TEntity>().Save(_entity, auditmessage);
  73. _entity.CommitChanges();
  74. SyncRow();
  75. }
  76. }
  77. public Task SaveAsync(string auditMessage)
  78. {
  79. return Task.Run(() => Save(auditMessage));
  80. }
  81. public void Cancel()
  82. {
  83. _entity?.CancelChanges();
  84. }
  85. private CoreRow _row = null!;
  86. public CoreRow Row
  87. {
  88. get => _row;
  89. set
  90. {
  91. _row = value;
  92. RowChanged();
  93. }
  94. }
  95. public TParent Parent { get; set; }
  96. ICoreRepository IShell.Parent => this.Parent;
  97. public Guid ID => Get<Guid>();
  98. public virtual string[] TextSearchValues() => [];
  99. public bool Match(string? text)
  100. {
  101. if (string.IsNullOrWhiteSpace(text))
  102. return true;
  103. foreach (var value in TextSearchValues())
  104. {
  105. if (value.Contains(text, StringComparison.InvariantCultureIgnoreCase))
  106. return true;
  107. }
  108. return false;
  109. }
  110. #region Row Get/Set Caching
  111. // We do this for three reasons:
  112. // 1. Rather than define properties in once class and columns in another,
  113. // we can define and link properties and columns in the one class,
  114. // using a _static_ constructor, which reduces complexity
  115. // 2. By caching based on the property name, we eliminate the need to convert
  116. // expressions to strings (expensive), while still retaining type-safety
  117. // 3. Using the Get/Set helper functions reduces code complexity when defining
  118. // shell properties, and distinguishes between data and calculated properties
  119. private static ShellColumns<TParent, TEntity>? _columns;
  120. public ShellColumns<TParent, TEntity> Columns
  121. {
  122. get
  123. {
  124. if (_columns == null)
  125. {
  126. _columns = new ShellColumns<TParent, TEntity>();
  127. _columns.Map(nameof(ID), x => x.ID);
  128. ConfigureColumns(_columns);
  129. }
  130. return _columns;
  131. }
  132. }
  133. protected abstract void ConfigureColumns(ShellColumns<TParent, TEntity> columns);
  134. protected virtual T Get<T>([CallerMemberName] string property = null)
  135. {
  136. if (_entity != null)
  137. {
  138. return (T)CoreUtils.GetPropertyValue(
  139. _entity,
  140. Columns[property]
  141. );
  142. }
  143. if (_row != null)
  144. {
  145. var col = _columns?.IndexOf(property);
  146. if (col != null)
  147. return Row.Get<T>(col.Value); //() Row.Values[];
  148. }
  149. return CoreUtils.GetDefault<T>();
  150. //return value != null ? (T)CoreUtils.ChangeType(value, typeof(T)) : CoreUtils.GetDefault<T>();
  151. }
  152. protected virtual void Set<T>(T value, bool donotify = true, [CallerMemberName] string property = null, params string[] notify)
  153. {
  154. CheckEntity();
  155. CoreUtils.SetPropertyValue(_entity, Columns[property], value);
  156. //Row.Values[Columns.IndexOf(property)] = value;
  157. // if (donotify)
  158. // {
  159. // DoPropertyChanged(property);
  160. // foreach (var _property in notify)
  161. // DoPropertyChanged(_property);
  162. // }
  163. }
  164. #endregion
  165. }