SiteManufacturing.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using InABox.Configuration;
  9. using InABox.Mobile;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. using LogType = InABox.Core.LogType;
  13. using SelectionChangedEventArgs = Syncfusion.XForms.Buttons.SelectionChangedEventArgs;
  14. namespace PRS.Mobile
  15. {
  16. [XamlCompilation(XamlCompilationOptions.Compile)]
  17. public partial class SiteManufacturing : SitePage
  18. {
  19. private JobShell _job;
  20. private ManufacturingPacketModel _packets;
  21. private SetoutModel _setouts;
  22. private SetoutDocumentModel _documents;
  23. public SiteManufacturing(JobShell job) : base(job)
  24. {
  25. _job = job;
  26. _packets = new ManufacturingPacketModel(App.Data,
  27. () => new Filter<ManufacturingPacket>(x=>x.SetoutLink.JobLink.ID).IsEqualTo(_job.ID),
  28. () => CoreRepository.CacheFileName<ManufacturingPacket>($"{_job.ID}")
  29. );
  30. _setouts = new SetoutModel(App.Data,
  31. () => new Filter<Setout>(x => x.JobLink.ID).IsEqualTo(_job.ID),
  32. () => CoreRepository.CacheFileName<Setout>($"{_job.ID}")
  33. );
  34. _documents = new SetoutDocumentModel(App.Data,
  35. () => new Filter<SetoutDocument>(x => x.EntityLink.ID)
  36. .InQuery<Setout>(new Filter<Setout>(x=>x.JobLink.ID).IsEqualTo(_job.ID),x=>x.ID),
  37. () => CoreRepository.CacheFileName<SetoutDocument>($"{_job.ID}")
  38. );
  39. InitializeComponent();
  40. ProgressVisible = true;
  41. Title = Job?.DisplayName ?? "Manufacturing";
  42. Packets.Packets = _packets;
  43. Packets.JobID = Job?.ID ?? CoreUtils.FullGuid;
  44. Packets.FactoryID = Guid.Empty;
  45. Setouts.Setouts = _setouts;
  46. Setouts.Packets = _packets;
  47. Setouts.Documents = _documents;
  48. Setouts.JobID = Job?.ID ?? CoreUtils.FullGuid;
  49. RefreshData(false, true);
  50. }
  51. private void RefreshData(bool force, bool async)
  52. {
  53. Task[] tasks = new Task[]
  54. {
  55. Task.Run(() => _packets.Refresh(force)),
  56. Task.Run(() => _setouts.Refresh(force)),
  57. Task.Run(() => _documents.Refresh(force))
  58. };
  59. if (async)
  60. Task.WhenAll(tasks).ContinueWith((_) => Device.BeginInvokeOnMainThread(RefreshScreen));
  61. else
  62. {
  63. Task.WaitAll(tasks);
  64. RefreshScreen();
  65. }
  66. }
  67. private void RefreshScreen()
  68. {
  69. Packets.Refresh();
  70. Setouts.Refresh();
  71. ProgressVisible = false;
  72. }
  73. private void UpdateFactoryButton()
  74. {
  75. var factory = App.Data.ManufacturingFactories.FirstOrDefault(x => x.ID == Packets.FactoryID);
  76. _factory.Text = factory != null
  77. ? factory.Name
  78. : "All Factories";
  79. }
  80. private View CreateFactorySelection(String caption, Action<ManufacturingFactoryShell> selected)
  81. {
  82. SelectionView selection = new SelectionView
  83. {
  84. VerticalOptions = LayoutOptions.Fill,
  85. HorizontalOptions = LayoutOptions.Fill,
  86. CanSearch = false
  87. };
  88. selection.Configure += (sender, args) =>
  89. {
  90. args.Columns
  91. .BeginUpdate()
  92. .Clear()
  93. .Add(new MobileGridTextColumn<ManufacturingFactoryShell>() { Column = x=>x.Name, Width = GridLength.Star, Caption = caption})
  94. .EndUpdate();
  95. };
  96. selection.Refresh += (sender, args) => App.Data.ManufacturingFactories.Refresh(false);
  97. selection.SelectionChanged += (sender, args) =>
  98. {
  99. selected(args.SelectedItems.FirstOrDefault() as ManufacturingFactoryShell);
  100. DismissPopup();
  101. };
  102. selection.AddButton("All Factories", () =>
  103. {
  104. selected(null);
  105. DismissPopup();
  106. });
  107. selection.Load();
  108. return selection;
  109. }
  110. private void _selectfactory_OnClicked(object sender, MobileButtonClickEventArgs args)
  111. {
  112. var selection = CreateFactorySelection("Select Factory", (factory) =>
  113. {
  114. Packets.FactoryID = factory?.ID ?? Guid.Empty;
  115. UpdateFactoryButton();
  116. RefreshData(false,true);
  117. });
  118. ShowPopup(() => selection);
  119. }
  120. }
  121. }