LogikalGrid.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.Logikal;
  5. using InABox.WPF;
  6. namespace PRSDesktop;
  7. public delegate void LogikalResponseEvent(LogikalResponse response);
  8. public delegate LogikalClient LogikalClientEvent(object sender);
  9. public abstract class LogikalGrid<T> : DynamicItemsListGrid<T> where T : BaseObject, new()
  10. {
  11. public LogikalClient? Client { get; set; }
  12. public event LogikalClientEvent ClientRequired;
  13. public event LogikalResponseEvent? ResponseReceived;
  14. protected void NotifyResponseReceived(LogikalResponse response)
  15. {
  16. if (ResponseReceived != null)
  17. {
  18. Dispatcher.BeginInvoke(
  19. () =>
  20. {
  21. ResponseReceived?.Invoke(response);
  22. });
  23. }
  24. }
  25. public LogikalGrid()
  26. {
  27. Refresh(true, false);
  28. }
  29. protected override void DoReconfigure(DynamicGridOptions options)
  30. {
  31. base.DoReconfigure(options);
  32. options.FilterRows = true;
  33. options.HideDatabaseFilters = true;
  34. }
  35. protected virtual void BeforeGet()
  36. {
  37. }
  38. protected abstract void DoGet(LogikalClient client, IProgress<string> progress);
  39. protected virtual void AfterGet()
  40. {
  41. Refresh(false, true);
  42. }
  43. public void Get()
  44. {
  45. Client ??= ClientRequired?.Invoke(this);
  46. Client ??= new LogikalClient();
  47. BeforeGet();
  48. Progress.ShowModal("Please wait", progress =>
  49. {
  50. if (Client.Ready)
  51. {
  52. progress.Report("Retrieving Data");
  53. DoGet(Client, progress);
  54. }
  55. else
  56. {
  57. progress.Report("Connecting to Logikal");
  58. Client.Connect()
  59. .Always(NotifyResponseReceived)
  60. .Success<LogikalConnectResponse>(c =>
  61. {
  62. progress.Report("Logging In");
  63. Client.Login()
  64. .Always(NotifyResponseReceived)
  65. .Success<LogikalLoginResponse>(l =>
  66. {
  67. progress.Report("Retrieving Data");
  68. DoGet(Client, progress);
  69. });
  70. });
  71. }
  72. });
  73. AfterGet();
  74. }
  75. }