ExistingForms.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 bool PullToRefresh
  119. {
  120. get => _model.PullToRefresh;
  121. set => _model.PullToRefresh = value;
  122. }
  123. public event ExistingFormRefreshEvent RefreshRequested;
  124. public event ExistingFormSearchEvent Search;
  125. public event ExistingFormTappedEvent FormTapped;
  126. // Event to handle new / existing form selection
  127. public ExistingForms()
  128. {
  129. InitializeComponent();
  130. }
  131. public void RefreshData(bool force, bool async)
  132. {
  133. if (async)
  134. DataModel?.Refresh(force, Refresh);
  135. else
  136. {
  137. DataModel?.Refresh(force);
  138. Refresh();
  139. }
  140. }
  141. public void Refresh()
  142. {
  143. var forms = Property(DataModel);
  144. Device.BeginInvokeOnMainThread(() =>
  145. {
  146. _forms.LastUpdated = DataModel?.LastUpdated ?? DateTime.MinValue;
  147. _forms.ItemsSource = forms?
  148. .Where(FilterShell)
  149. .OrderByDescending(SortShell)
  150. .ToList();
  151. });
  152. }
  153. private DateTime SortShell(IDigitalFormInstanceShell shell)
  154. {
  155. return !shell.Completed.IsEmpty()
  156. ? shell.Completed
  157. : !shell.Started.IsEmpty()
  158. ? shell.Started
  159. : shell.Created;
  160. }
  161. private bool _showincomplete = true;
  162. private bool FilterShell(IDigitalFormInstanceShell shell)
  163. {
  164. if (_model.SeparateHistory)
  165. {
  166. bool shellincomplete = shell.Completed.IsEmpty();
  167. if (shellincomplete != _showincomplete)
  168. return false;
  169. }
  170. bool bOK =
  171. String.IsNullOrWhiteSpace(_currentfilter)
  172. || (shell.FormCode.ToUpper().Contains(_currentfilter.ToUpper())
  173. || shell.FormDescription.ToUpper().Contains(_currentfilter.ToUpper()));
  174. bOK = bOK
  175. && (Search?.Invoke(this, new ExistingFormSearchEventArgs(shell)) ?? true);
  176. return bOK;
  177. }
  178. private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
  179. {
  180. _currentfilter = args.Text;
  181. Refresh();
  182. }
  183. private void _digitalforms_OnRefresh(object sender, MobileListRefreshEventArgs args)
  184. {
  185. var newargs = new ExistingFormRefreshEventArgs() { Cancel = false };
  186. RefreshRequested?.Invoke(this, newargs);
  187. if (!newargs.Cancel)
  188. RefreshData(true,false);
  189. }
  190. private void AddForm_Tapped(object sender, EventArgs e)
  191. {
  192. var selector = new NewForms() { AppliesTo = this.AppliesTo };
  193. selector.ItemSelected += CreateNewForm;
  194. Navigation.PushAsync(selector);
  195. }
  196. private async void CreateNewForm(object sender, NewFormSelectedArgs args)
  197. {
  198. await MaterialDialog.Instance.AlertAsync("Not Implemented Yet!");
  199. return;
  200. }
  201. private async void Form_Clicked(object sender, EventArgs e)
  202. {
  203. if ((sender is MobileCard card) && (card.BindingContext is IDigitalFormInstanceShell shell))
  204. FormTapped?.Invoke(this, new ExistingFormTappedEventArgs(shell));
  205. else
  206. await MaterialDialog.Instance.AlertAsync("Tapped Item is not a Digital Form", "ERROR");
  207. }
  208. public void CreateNewForm(String appliesto, Func<IDigitalFormInstanceShell> createform, Guid parentid, Action refresh)
  209. {
  210. var selector = new NewForms() { AppliesTo = appliesto };
  211. selector.ItemSelected += (sender, args) =>
  212. {
  213. var instance = createform();
  214. instance.ParentID = parentid;
  215. instance.FormID = args.Form.ID;
  216. instance.FormCode = args.Form.Code;
  217. instance.FormDescription = args.Form.Description;
  218. instance.Save("Created on Mobile Device");
  219. Dispatcher.BeginInvokeOnMainThread(refresh);
  220. };
  221. Navigation.PushAsync(selector);
  222. }
  223. public void EditForm<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
  224. where TParent : Entity, IRemotable, IPersistent, new()
  225. where TParentLink : EntityLink<TParent>, new()
  226. where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
  227. {
  228. var table = new Client<TForm>().Query(
  229. new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID),
  230. new Columns<TForm>(ColumnTypeFlags.None).Add(x=>x.ID).Add(x=>x.Form.ID)
  231. );
  232. var form = table.Rows
  233. .Select(x => x.ToObject<TForm>())
  234. .FirstOrDefault();
  235. var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
  236. model.LoadItems(
  237. parent,
  238. form.Form.ID,
  239. form.ID,
  240. null);
  241. var host = new DigitalFormHost(model);
  242. host.OnClosing += (o, args) =>
  243. {
  244. if (args.Changed)
  245. {
  246. RefreshData(true,true);
  247. //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  248. }
  249. };
  250. Navigation.PushAsync(host);
  251. }
  252. public async void EditFormAsync<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
  253. where TParent : Entity, IRemotable, IPersistent, new()
  254. where TParentLink : EntityLink<TParent>, new()
  255. where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
  256. {
  257. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  258. {
  259. var form = new Client<TForm>()
  260. .Query(
  261. new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID),
  262. new Columns<TForm>(ColumnTypeFlags.None).Add(x=>x.ID).Add(x=>x.Form.ID)
  263. )
  264. .Rows
  265. .Select(x => x.ToObject<AssignmentForm>())
  266. .FirstOrDefault();
  267. var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
  268. model.LoadItems(parent, form.Form.ID, form.ID, null);
  269. var host = new DigitalFormHost(model);
  270. host.OnClosing += (o, args) =>
  271. {
  272. if (args.Changed)
  273. {
  274. RefreshData(true,true);
  275. //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  276. }
  277. };
  278. Navigation.PushAsync(host);
  279. }
  280. }
  281. private void _tabStrip_OnSelectionChanged(object sender, EventArgs e)
  282. {
  283. _showincomplete = _tabStrip.SelectedItem?.Index == 0;
  284. _model.DataModel.SelectNone();
  285. _selectionmenubutton.IsVisible = false;
  286. Refresh();
  287. }
  288. private void _selectionmenu_OnAppearing(object sender, EventArgs e)
  289. {
  290. _completeform.IsVisible = _showincomplete;
  291. _reopenform.IsVisible = !_showincomplete;
  292. //bool anyselected = SelectedItems().Any();
  293. //bool anyunselected = UnselectedItems().Any();
  294. //_setstatus.IsVisible = anyselected;
  295. //_selectAll.IsVisible = anyunselected;
  296. //_selectNone.IsVisible = anyselected;
  297. //_separator.IsVisible = (_setstatus.IsVisible) && (_selectAll.IsVisible || _selectNone.IsVisible);
  298. }
  299. private IEnumerable<IShell> SelectedItems()
  300. {
  301. return _model.DataModel.SelectedItems.OfType<IShell>();
  302. }
  303. private IEnumerable<IShell> UnselectedItems()
  304. {
  305. return _model.DataModel.Items.OfType<IShell>().Except(_model.DataModel.SelectedItems.OfType<IShell>());
  306. }
  307. private void _setstatus_OnClicked(object sender, EventArgs e)
  308. {
  309. }
  310. private void _selectAll_OnClicked(object sender, EventArgs e)
  311. {
  312. _model.DataModel.SelectAll();
  313. RefreshData(false,false);
  314. }
  315. private void _selectNone_OnClicked(object sender, EventArgs e)
  316. {
  317. _model.DataModel.SelectNone();
  318. RefreshData(false,false);
  319. }
  320. private void CheckBox_Changed(object sender, EventArgs e)
  321. {
  322. if ((sender as MobileCheckBox)?.BindingContext is IShell shell)
  323. _model.DataModel.ToggleSelection(shell);
  324. _selectionmenubutton.IsVisible = SelectedItems().Any();
  325. }
  326. private void _completeform_OnClicked(object sender, EventArgs e)
  327. {
  328. var shells = _model.DataModel.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
  329. foreach (var shell in shells)
  330. {
  331. shell.Completed = DateTime.Now;
  332. shell.Save("Completed on Mobile Device");
  333. }
  334. _model.DataModel.SelectNone();
  335. _selectionmenubutton.IsVisible = false;
  336. RefreshData(true,false);
  337. }
  338. private void _reopenform_OnClicked(object sender, EventArgs e)
  339. {
  340. var shells = _model.DataModel.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
  341. foreach (var shell in shells)
  342. {
  343. shell.Completed = DateTime.MinValue;
  344. shell.Save("Re-Opened on Mobile Device");
  345. }
  346. _model.DataModel.SelectNone();
  347. _selectionmenubutton.IsVisible = false;
  348. RefreshData(true,false);
  349. }
  350. }
  351. }