FormsList.axaml.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Data.Converters;
  4. using Avalonia.Markup.Xaml;
  5. using Avalonia.Media;
  6. using Avalonia.Threading;
  7. using CommunityToolkit.Mvvm.ComponentModel;
  8. using CommunityToolkit.Mvvm.Input;
  9. using DynamicData.Binding;
  10. using InABox.Avalonia;
  11. using InABox.Avalonia.Components;
  12. using InABox.Avalonia.Converters;
  13. using InABox.Clients;
  14. using InABox.Core;
  15. using JetBrains.Annotations;
  16. using PRS.Avalonia.DigitalForms;
  17. using System;
  18. using System.ComponentModel;
  19. using System.Globalization;
  20. using System.Linq;
  21. using System.Threading.Tasks;
  22. using System.Windows.Input;
  23. namespace PRS.Avalonia.Components;
  24. public class FormsListBackgroundColorConverter : AbstractConverter<IDigitalFormInstanceShell, IBrush>
  25. {
  26. protected override IBrush? Convert(IDigitalFormInstanceShell? value, object? parameter = null)
  27. {
  28. if(value is null) return new SolidColorBrush(Colors.Transparent);
  29. return new SolidColorBrush(!value.Completed.IsEmpty()
  30. ? Colors.DimGray
  31. : !value.Started.IsEmpty()
  32. ? Colors.LightGreen
  33. : Colors.Coral);
  34. }
  35. }
  36. public class FormsListForegroundColorConverter : AbstractConverter<IDigitalFormInstanceShell, IBrush>
  37. {
  38. protected override IBrush? Convert(IDigitalFormInstanceShell? value, object? parameter = null)
  39. {
  40. if(value is null) return new SolidColorBrush(Colors.Transparent);
  41. return new SolidColorBrush(!value.Completed.IsEmpty()
  42. ? Colors.WhiteSmoke
  43. : !value.Started.IsEmpty()
  44. ? Colors.Black
  45. : Colors.Black);
  46. }
  47. }
  48. public class ExistingFormStatusConverter : AbstractConverter<IDigitalFormInstanceShell, string>
  49. {
  50. public static readonly ExistingFormStatusConverter Instance = new();
  51. protected override string? Convert(IDigitalFormInstanceShell? value, object? parameter = null)
  52. {
  53. if (value is null) return "";
  54. return !value.Completed.IsEmpty()
  55. ? $"{value.Completed:dd MMMM yy}"
  56. : !value.Started.IsEmpty()
  57. ? $"{value.Started:dd MMMM yy}"
  58. : $"{value.Created:dd MMMM yy}";
  59. }
  60. }
  61. public class FormsListSearchEventArgs(IDigitalFormInstanceShell shell)
  62. {
  63. public IDigitalFormInstanceShell Shell { get; set; } = shell;
  64. }
  65. public delegate bool FormsListSearchEvent(object sender, FormsListSearchEventArgs args);
  66. public partial class FormsList : UserControl
  67. {
  68. // public static readonly StyledProperty<bool> SeparateHistoryProperty =
  69. // AvaloniaProperty.Register<FormsList, bool>("SeparateHistory", true);
  70. public static readonly StyledProperty<string> AppliesToProperty =
  71. AvaloniaProperty.Register<FormsList, string>(nameof(AppliesTo), "");
  72. public static readonly StyledProperty<ICoreRepository?> ModelProperty =
  73. AvaloniaProperty.Register<FormsList, ICoreRepository?>(nameof(Model));
  74. public static readonly StyledProperty<ICommand?> FormClickedProperty =
  75. AvaloniaProperty.Register<FormsList, ICommand?>(nameof(FormClicked));
  76. public bool SeparateHistory { get; set; } = true;
  77. public string AppliesTo
  78. {
  79. get => GetValue(AppliesToProperty);
  80. set => SetValue(AppliesToProperty, value);
  81. }
  82. private bool SelectionMenuVisible
  83. {
  84. set => SelectionMenuButton.IsVisible = value;
  85. }
  86. private bool ShowIncomplete { get; set; } = true;
  87. public event FormsListSearchEvent? Search;
  88. public ICoreRepository? Model
  89. {
  90. get => GetValue(ModelProperty);
  91. set => SetValue(ModelProperty, value);
  92. }
  93. public ICommand? FormClicked
  94. {
  95. get => GetValue(FormClickedProperty);
  96. set => SetValue(FormClickedProperty, value);
  97. }
  98. public FormsList()
  99. {
  100. InitializeComponent();
  101. }
  102. // static FormsList()
  103. // {
  104. // SeparateHistoryProperty.Changed.AddClassHandler<FormsList>(SeparateHistory_Changed);
  105. // }
  106. // private static void SeparateHistory_Changed(FormsList list, AvaloniaPropertyChangedEventArgs args)
  107. // {
  108. // list._separateHistory = list.GetValue(SeparateHistoryProperty);
  109. // }
  110. private bool FilterShell(IShell shell)
  111. {
  112. if (shell is not IDigitalFormInstanceShell formShell) return false;
  113. return (!SeparateHistory || ShowIncomplete == (formShell.Completed == DateTime.MinValue))
  114. && (Search is null || Search.Invoke(this, new(formShell)));
  115. }
  116. private void TabStrip_SelectionChanged(object sender, SelectionChangedEventArgs e)
  117. {
  118. if (_tabList is null) return;
  119. ShowIncomplete = _tabList.SelectedIndex == 0;
  120. Model?.SelectNone();
  121. SelectionMenuVisible = false;
  122. }
  123. [RelayCommand]
  124. private void FormChecked(IDigitalFormInstanceShell form)
  125. {
  126. SelectionMenuVisible = Model?.SelectedItems.OfType<IShell>().Any() == true;
  127. }
  128. [RelayCommand]
  129. private void SelectionMenu(IDigitalFormInstanceShell form)
  130. {
  131. var menu = new ContextMenu();
  132. AvaloniaMenuItem.LoadMenuItems([
  133. new CoreMenuItem<IImage>("Complete Forms", null, CompleteForms, () => ShowIncomplete),
  134. new CoreMenuItem<IImage>("Re-open Forms", null, ReopenForms, () => !ShowIncomplete),
  135. new CoreMenuSeparator(),
  136. new CoreMenuItem<IImage>("Select All", null, SelectAll),
  137. new CoreMenuItem<IImage>("Select None", null, SelectNone),
  138. ], menu.Items);
  139. menu.Open(SelectionMenuButton);
  140. }
  141. private Task<bool> SelectNone()
  142. {
  143. Model?.SelectNone();
  144. return Task.FromResult(true);
  145. }
  146. private Task<bool> SelectAll()
  147. {
  148. Model?.SelectAll();
  149. return Task.FromResult(true);
  150. }
  151. private async Task<bool> ReopenForms()
  152. {
  153. if (Model is null) return true;
  154. var shells = Model.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
  155. foreach(var shell in shells)
  156. {
  157. shell.Completed = DateTime.MinValue;
  158. }
  159. await Model.SaveAsync("Re-opened on Mobile Device");
  160. Model.SelectNone();
  161. SelectionMenuVisible = false;
  162. await Model.RefreshAsync(true);
  163. return true;
  164. }
  165. private async Task<bool> CompleteForms()
  166. {
  167. if (Model is null) return true;
  168. var shells = Model.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
  169. foreach(var shell in shells)
  170. {
  171. shell.Completed = DateTime.Now;
  172. }
  173. await Model.SaveAsync("Completed on Mobile Device");
  174. Model.SelectNone();
  175. SelectionMenuVisible = false;
  176. await Model.RefreshAsync(true);
  177. return true;
  178. }
  179. public static async Task<DigitalFormShell?> SelectForm<TForm, TParent, TLink>(DigitalFormModel formModel)
  180. where TForm : EntityForm<TParent, TLink, TForm>
  181. where TParent : Entity, new()
  182. where TLink : EntityLink<TParent>, new()
  183. {
  184. return (await SelectionViewModel.ExecutePopup<DigitalFormShell>(model =>
  185. {
  186. model.AddFilters(formModel.AvailableFilters.Select(x => x.Name).NotNull());
  187. model.Columns.BeginUpdate()
  188. .Add(new AvaloniaDataGridTextColumn<DigitalFormShell>
  189. {
  190. Column = x => x.Code,
  191. Width = GridLength.Auto
  192. })
  193. .Add(new AvaloniaDataGridTextColumn<DigitalFormShell>
  194. {
  195. Column = x => x.Description,
  196. Width = GridLength.Star
  197. })
  198. .EndUpdate();
  199. }, args =>
  200. {
  201. formModel.SelectFilter(args.Filter);
  202. formModel.Search(x => x.AppliesTo == typeof(TParent).Name);
  203. return formModel.Refresh(args.Force);
  204. }))?.FirstOrDefault();
  205. }
  206. }