ExistingForms.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.Mobile;
  12. using Xamarin.Forms;
  13. using Xamarin.Forms.Xaml;
  14. using XF.Material.Forms.UI;
  15. using XF.Material.Forms.UI.Dialogs;
  16. using SelectionChangedEventArgs = Syncfusion.XForms.Buttons.SelectionChangedEventArgs;
  17. namespace PRS.Mobile
  18. {
  19. public class ExistingFormStatusConverter : IValueConverter
  20. {
  21. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  22. {
  23. if (value is IDigitalFormInstanceShell shell)
  24. {
  25. return !shell.Completed.IsEmpty()
  26. ? $"Completed"
  27. : !shell.Started.IsEmpty()
  28. ? "In Progress"
  29. : $"Not Started";
  30. }
  31. return "";
  32. }
  33. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. }
  38. public class ExistingFormBackgroundColorConverter : IValueConverter
  39. {
  40. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  41. {
  42. if (value is IDigitalFormInstanceShell shell)
  43. {
  44. return String.IsNullOrWhiteSpace(shell.Data)
  45. ? Color.Coral
  46. : shell.Completed.IsEmpty()
  47. ? Color.LightGreen
  48. : Color.DimGray;
  49. }
  50. return Color.Transparent;
  51. }
  52. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  53. {
  54. throw new NotImplementedException();
  55. }
  56. }
  57. public class ExistingFormForegroundColorConverter : IValueConverter
  58. {
  59. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  60. {
  61. if (value is IDigitalFormInstanceShell shell)
  62. {
  63. return String.IsNullOrWhiteSpace(shell.Data)
  64. ? Color.Black
  65. : shell.Completed.IsEmpty()
  66. ? Color.Black
  67. : Color.WhiteSmoke;
  68. }
  69. return Color.Transparent;
  70. }
  71. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  72. {
  73. throw new NotImplementedException();
  74. }
  75. }
  76. public class ExistingFormRefreshEventArgs : CancelEventArgs
  77. {
  78. }
  79. public delegate void ExistingFormRefreshEvent(object sender, ExistingFormRefreshEventArgs args);
  80. public class ExistingFormSearchEventArgs : EventArgs
  81. {
  82. public IDigitalFormInstanceShell Shell { get; private set; }
  83. public ExistingFormSearchEventArgs(IDigitalFormInstanceShell shell)
  84. {
  85. Shell = shell;
  86. }
  87. }
  88. public delegate bool ExistingFormSearchEvent(object sender, ExistingFormSearchEventArgs args);
  89. public class ExistingFormTappedEventArgs : EventArgs
  90. {
  91. public IDigitalFormInstanceShell Shell { get; private set; }
  92. public ExistingFormTappedEventArgs(IDigitalFormInstanceShell shell)
  93. {
  94. Shell = shell;
  95. }
  96. }
  97. public delegate void ExistingFormTappedEvent(object sender, ExistingFormTappedEventArgs args);
  98. [XamlCompilation(XamlCompilationOptions.Compile)]
  99. public partial class ExistingForms
  100. {
  101. private String _currentfilter = "";
  102. public ICoreRepository DataModel
  103. {
  104. get => _model.DataModel;
  105. set => _model.DataModel = value;
  106. }
  107. public String AppliesTo
  108. {
  109. get => _model.AppliesTo;
  110. set => _model.AppliesTo = value;
  111. }
  112. public Func<ICoreRepository, IDigitalFormInstanceShell[]> Property
  113. {
  114. get => _model.Property;
  115. set => _model.Property = value;
  116. }
  117. public bool SeparateHistory
  118. {
  119. get => _model.SeparateHistory;
  120. set => _model.SeparateHistory = value;
  121. }
  122. public event ExistingFormRefreshEvent RefreshRequested;
  123. public event ExistingFormSearchEvent Search;
  124. public event ExistingFormTappedEvent FormTapped;
  125. // Event to handle new / existing form selection
  126. public ExistingForms()
  127. {
  128. InitializeComponent();
  129. }
  130. public void RefreshData(bool force, bool async)
  131. {
  132. if (async)
  133. DataModel?.Refresh(force, Refresh);
  134. else
  135. {
  136. DataModel?.Refresh(true);
  137. Refresh();
  138. }
  139. }
  140. public void Refresh()
  141. {
  142. var forms = Property(DataModel);
  143. Device.BeginInvokeOnMainThread(() =>
  144. {
  145. _forms.LastUpdated = DataModel.LastUpdated;
  146. _forms.ItemsSource = forms.Where(FilterShell).ToList();
  147. });
  148. }
  149. private bool _showincomplete = true;
  150. private bool FilterShell(IDigitalFormInstanceShell shell)
  151. {
  152. if (_model.SeparateHistory)
  153. {
  154. bool shellincomplete = shell.Completed.IsEmpty();
  155. if (shellincomplete != _showincomplete)
  156. return false;
  157. }
  158. bool bOK =
  159. String.IsNullOrWhiteSpace(_currentfilter)
  160. || (shell.FormCode.ToUpper().Contains(_currentfilter.ToUpper())
  161. || shell.FormDescription.ToUpper().Contains(_currentfilter.ToUpper()));
  162. bOK = bOK
  163. && (Search?.Invoke(this, new ExistingFormSearchEventArgs(shell)) ?? true);
  164. return bOK;
  165. }
  166. private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
  167. {
  168. _currentfilter = args.Text;
  169. Refresh();
  170. }
  171. private void _digitalforms_OnRefresh(object sender, MobileListRefreshEventArgs args)
  172. {
  173. var newargs = new ExistingFormRefreshEventArgs() { Cancel = false };
  174. RefreshRequested?.Invoke(this, newargs);
  175. if (!newargs.Cancel)
  176. RefreshData(true,false);
  177. }
  178. private void AddForm_Tapped(object sender, EventArgs e)
  179. {
  180. var selector = new NewForms() { AppliesTo = this.AppliesTo };
  181. selector.ItemSelected += CreateNewForm;
  182. Navigation.PushAsync(selector);
  183. }
  184. private async void CreateNewForm(object sender, NewFormSelectedArgs args)
  185. {
  186. await MaterialDialog.Instance.AlertAsync("Not Implemented Yet!");
  187. return;
  188. }
  189. private async void Form_Clicked(object sender, EventArgs e)
  190. {
  191. if ((sender is MobileCard card) && (card.BindingContext is IDigitalFormInstanceShell shell))
  192. FormTapped?.Invoke(this, new ExistingFormTappedEventArgs(shell));
  193. else
  194. await MaterialDialog.Instance.AlertAsync("Tapped Item is not a Digital Form", "ERROR");
  195. }
  196. public void CreateNewForm(String appliesto, Func<IDigitalFormInstanceShell> createform, Guid parentid, Action refresh)
  197. {
  198. var selector = new NewForms() { AppliesTo = appliesto };
  199. selector.ItemSelected += (sender, args) =>
  200. {
  201. var instance = createform();
  202. instance.ParentID = parentid;
  203. instance.FormID = args.Form.ID;
  204. instance.FormCode = args.Form.Code;
  205. instance.FormDescription = args.Form.Description;
  206. instance.Save("Created on Mobile Device");
  207. Dispatcher.BeginInvokeOnMainThread(refresh);
  208. };
  209. Navigation.PushAsync(selector);
  210. }
  211. public void EditForm<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
  212. where TParent : Entity, IRemotable, IPersistent, new()
  213. where TParentLink : EntityLink<TParent>, new()
  214. where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
  215. {
  216. var form = new Client<TForm>()
  217. .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID))
  218. .Rows
  219. .Select(x => x.ToObject<AssignmentForm>())
  220. .FirstOrDefault();
  221. var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
  222. model.LoadItems(parent, form, null);
  223. var host = new DigitalFormHost(model);
  224. host.OnClosing += (o, args) =>
  225. {
  226. if (args.Changed)
  227. {
  228. //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  229. }
  230. };
  231. Navigation.PushAsync(host);
  232. }
  233. public async void EditFormAsync<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
  234. where TParent : Entity, IRemotable, IPersistent, new()
  235. where TParentLink : EntityLink<TParent>, new()
  236. where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
  237. {
  238. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  239. {
  240. var form = new Client<TForm>()
  241. .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID))
  242. .Rows
  243. .Select(x => x.ToObject<AssignmentForm>())
  244. .FirstOrDefault();
  245. var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
  246. model.LoadItems(parent, form, null);
  247. var host = new DigitalFormHost(model);
  248. host.OnClosing += (o, args) =>
  249. {
  250. if (args.Changed)
  251. {
  252. //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  253. }
  254. };
  255. Navigation.PushAsync(host);
  256. }
  257. }
  258. private void _tabStrip_OnSelectionChanged(object sender, EventArgs e)
  259. {
  260. _showincomplete = _tabStrip.SelectedItem?.Index == 0;
  261. Refresh();
  262. }
  263. }
  264. }