Shell.cs 5.4 KB

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