ExistingForms.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.Mobile;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. using XF.Material.Forms.UI.Dialogs;
  13. namespace PRS.Mobile
  14. {
  15. public class ExistingFormStatusConverter : IValueConverter
  16. {
  17. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  18. {
  19. if (value is IDigitalFormInstanceShell shell)
  20. {
  21. return !shell.Completed.IsEmpty()
  22. ? $"{shell.Completed:dd MMMM yy}"
  23. : !shell.Started.IsEmpty()
  24. ? $"{shell.Started:dd MMMM yy}"
  25. : $"{shell.Created:dd MMMM yy}";
  26. }
  27. return "";
  28. }
  29. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  30. {
  31. throw new NotImplementedException();
  32. }
  33. }
  34. public class ExistingFormBackgroundColorConverter : IValueConverter
  35. {
  36. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  37. {
  38. if (value is IDigitalFormInstanceShell shell)
  39. {
  40. return !shell.Completed.IsEmpty()
  41. ? Color.DimGray
  42. : !shell.Started.IsEmpty()
  43. ? Color.LightGreen
  44. : Color.Coral;
  45. }
  46. return Color.Transparent;
  47. }
  48. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  49. {
  50. throw new NotImplementedException();
  51. }
  52. }
  53. public class ExistingFormForegroundColorConverter : IValueConverter
  54. {
  55. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  56. {
  57. if (value is IDigitalFormInstanceShell shell)
  58. {
  59. return !shell.Completed.IsEmpty()
  60. ? Color.WhiteSmoke
  61. : !shell.Started.IsEmpty()
  62. ? Color.Black
  63. : Color.Black;
  64. }
  65. return Color.Transparent;
  66. }
  67. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  68. {
  69. throw new NotImplementedException();
  70. }
  71. }
  72. public class ExistingFormRefreshEventArgs : CancelEventArgs
  73. {
  74. }
  75. public delegate void ExistingFormRefreshEvent(object sender, ExistingFormRefreshEventArgs args);
  76. public class ExistingFormSearchEventArgs : EventArgs
  77. {
  78. public IDigitalFormInstanceShell Shell { get; private set; }
  79. public ExistingFormSearchEventArgs(IDigitalFormInstanceShell shell)
  80. {
  81. Shell = shell;
  82. }
  83. }
  84. public delegate bool ExistingFormSearchEvent(object sender, ExistingFormSearchEventArgs args);
  85. public class ExistingFormTappedEventArgs : EventArgs
  86. {
  87. public IDigitalFormInstanceShell Shell { get; private set; }
  88. public ExistingFormTappedEventArgs(IDigitalFormInstanceShell shell)
  89. {
  90. Shell = shell;
  91. }
  92. }
  93. public delegate void ExistingFormTappedEvent(object sender, ExistingFormTappedEventArgs args);
  94. [XamlCompilation(XamlCompilationOptions.Compile)]
  95. public partial class ExistingForms
  96. {
  97. private String _currentfilter = "";
  98. public ICoreRepository DataModel
  99. {
  100. get => _model.DataModel;
  101. set => _model.DataModel = value;
  102. }
  103. public String AppliesTo
  104. {
  105. get => _model.AppliesTo;
  106. set => _model.AppliesTo = value;
  107. }
  108. public Func<ICoreRepository, IDigitalFormInstanceShell[]> Property
  109. {
  110. get => _model.Property;
  111. set => _model.Property = value;
  112. }
  113. public bool SeparateHistory
  114. {
  115. get => _model.SeparateHistory;
  116. set => _model.SeparateHistory = value;
  117. }
  118. public event ExistingFormRefreshEvent RefreshRequested;
  119. public event ExistingFormSearchEvent Search;
  120. public event ExistingFormTappedEvent FormTapped;
  121. // Event to handle new / existing form selection
  122. public ExistingForms()
  123. {
  124. InitializeComponent();
  125. }
  126. public void RefreshData(bool force, bool async)
  127. {
  128. if (async)
  129. DataModel?.Refresh(force, Refresh);
  130. else
  131. {
  132. DataModel?.Refresh(force);
  133. Refresh();
  134. }
  135. }
  136. public void Refresh()
  137. {
  138. var forms = Property(DataModel);
  139. Device.BeginInvokeOnMainThread(() =>
  140. {
  141. _forms.LastUpdated = DataModel.LastUpdated;
  142. _forms.ItemsSource = forms
  143. .Where(FilterShell)
  144. .OrderByDescending(SortShell)
  145. .ToList();
  146. });
  147. }
  148. private DateTime SortShell(IDigitalFormInstanceShell shell)
  149. {
  150. return !shell.Completed.IsEmpty()
  151. ? shell.Completed
  152. : !shell.Started.IsEmpty()
  153. ? shell.Started
  154. : shell.Created;
  155. }
  156. private bool _showincomplete = true;
  157. private bool FilterShell(IDigitalFormInstanceShell shell)
  158. {
  159. if (_model.SeparateHistory)
  160. {
  161. bool shellincomplete = shell.Completed.IsEmpty();
  162. if (shellincomplete != _showincomplete)
  163. return false;
  164. }
  165. bool bOK =
  166. String.IsNullOrWhiteSpace(_currentfilter)
  167. || (shell.FormCode.ToUpper().Contains(_currentfilter.ToUpper())
  168. || shell.FormDescription.ToUpper().Contains(_currentfilter.ToUpper()));
  169. bOK = bOK
  170. && (Search?.Invoke(this, new ExistingFormSearchEventArgs(shell)) ?? true);
  171. return bOK;
  172. }
  173. private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
  174. {
  175. _currentfilter = args.Text;
  176. Refresh();
  177. }
  178. private void _digitalforms_OnRefresh(object sender, MobileListRefreshEventArgs args)
  179. {
  180. var newargs = new ExistingFormRefreshEventArgs() { Cancel = false };
  181. RefreshRequested?.Invoke(this, newargs);
  182. if (!newargs.Cancel)
  183. RefreshData(true,false);
  184. }
  185. private void AddForm_Tapped(object sender, EventArgs e)
  186. {
  187. var selector = new NewForms() { AppliesTo = this.AppliesTo };
  188. selector.ItemSelected += CreateNewForm;
  189. Navigation.PushAsync(selector);
  190. }
  191. private async void CreateNewForm(object sender, NewFormSelectedArgs args)
  192. {
  193. await MaterialDialog.Instance.AlertAsync("Not Implemented Yet!");
  194. return;
  195. }
  196. private async void Form_Clicked(object sender, EventArgs e)
  197. {
  198. if ((sender is MobileCard card) && (card.BindingContext is IDigitalFormInstanceShell shell))
  199. FormTapped?.Invoke(this, new ExistingFormTappedEventArgs(shell));
  200. else
  201. await MaterialDialog.Instance.AlertAsync("Tapped Item is not a Digital Form", "ERROR");
  202. }
  203. public void CreateNewForm(String appliesto, Func<IDigitalFormInstanceShell> createform, Guid parentid, Action refresh)
  204. {
  205. var selector = new NewForms() { AppliesTo = appliesto };
  206. selector.ItemSelected += (sender, args) =>
  207. {
  208. var instance = createform();
  209. instance.ParentID = parentid;
  210. instance.FormID = args.Form.ID;
  211. instance.FormCode = args.Form.Code;
  212. instance.FormDescription = args.Form.Description;
  213. instance.Save("Created on Mobile Device");
  214. Dispatcher.BeginInvokeOnMainThread(refresh);
  215. };
  216. Navigation.PushAsync(selector);
  217. }
  218. public void EditForm<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
  219. where TParent : Entity, IRemotable, IPersistent, new()
  220. where TParentLink : EntityLink<TParent>, new()
  221. where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
  222. {
  223. var form = new Client<TForm>()
  224. .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID))
  225. .Rows
  226. .Select(x => x.ToObject<TForm>())
  227. .FirstOrDefault();
  228. var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
  229. model.LoadItems(parent, form, null);
  230. var host = new DigitalFormHost(model);
  231. host.OnClosing += (o, args) =>
  232. {
  233. if (args.Changed)
  234. {
  235. //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  236. }
  237. };
  238. Navigation.PushAsync(host);
  239. }
  240. public async void EditFormAsync<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
  241. where TParent : Entity, IRemotable, IPersistent, new()
  242. where TParentLink : EntityLink<TParent>, new()
  243. where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
  244. {
  245. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  246. {
  247. var form = new Client<TForm>()
  248. .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID))
  249. .Rows
  250. .Select(x => x.ToObject<AssignmentForm>())
  251. .FirstOrDefault();
  252. var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
  253. model.LoadItems(parent, form, null);
  254. var host = new DigitalFormHost(model);
  255. host.OnClosing += (o, args) =>
  256. {
  257. if (args.Changed)
  258. {
  259. //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  260. }
  261. };
  262. Navigation.PushAsync(host);
  263. }
  264. }
  265. private void _tabStrip_OnSelectionChanged(object sender, EventArgs e)
  266. {
  267. _showincomplete = _tabStrip.SelectedItem?.Index == 0;
  268. _model.DataModel.SelectNone();
  269. _selectionmenubutton.IsVisible = false;
  270. Refresh();
  271. }
  272. private void _selectionmenu_OnAppearing(object sender, EventArgs e)
  273. {
  274. _completeform.IsVisible = _showincomplete;
  275. _reopenform.IsVisible = !_showincomplete;
  276. //bool anyselected = SelectedItems().Any();
  277. //bool anyunselected = UnselectedItems().Any();
  278. //_setstatus.IsVisible = anyselected;
  279. //_selectAll.IsVisible = anyunselected;
  280. //_selectNone.IsVisible = anyselected;
  281. //_separator.IsVisible = (_setstatus.IsVisible) && (_selectAll.IsVisible || _selectNone.IsVisible);
  282. }
  283. private IEnumerable<IShell> SelectedItems()
  284. {
  285. return _model.DataModel.SelectedItems.OfType<IShell>();
  286. }
  287. private IEnumerable<IShell> UnselectedItems()
  288. {
  289. return _model.DataModel.Items.OfType<IShell>().Except(_model.DataModel.SelectedItems.OfType<IShell>());
  290. }
  291. private void _setstatus_OnClicked(object sender, EventArgs e)
  292. {
  293. }
  294. private void _selectAll_OnClicked(object sender, EventArgs e)
  295. {
  296. _model.DataModel.SelectAll();
  297. RefreshData(false,false);
  298. }
  299. private void _selectNone_OnClicked(object sender, EventArgs e)
  300. {
  301. _model.DataModel.SelectNone();
  302. RefreshData(false,false);
  303. }
  304. private void CheckBox_Changed(object sender, EventArgs e)
  305. {
  306. if ((sender as MobileCheckBox)?.BindingContext is IShell shell)
  307. _model.DataModel.ToggleSelection(shell);
  308. _selectionmenubutton.IsVisible = SelectedItems().Any();
  309. }
  310. private void _completeform_OnClicked(object sender, EventArgs e)
  311. {
  312. var shells = _model.DataModel.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
  313. foreach (var shell in shells)
  314. {
  315. shell.Completed = DateTime.Now;
  316. shell.Save("Completed on Mobile Device");
  317. }
  318. _model.DataModel.SelectNone();
  319. _selectionmenubutton.IsVisible = false;
  320. RefreshData(true,false);
  321. }
  322. private void _reopenform_OnClicked(object sender, EventArgs e)
  323. {
  324. var shells = _model.DataModel.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
  325. foreach (var shell in shells)
  326. {
  327. shell.Completed = DateTime.MinValue;
  328. shell.Save("Re-Opened on Mobile Device");
  329. }
  330. _model.DataModel.SelectNone();
  331. _selectionmenubutton.IsVisible = false;
  332. RefreshData(true,false);
  333. }
  334. }
  335. }