ExistingForms.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 table = new Client<TForm>()
  224. .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID)
  225. );
  226. var form = table.Rows
  227. .Select(x => x.ToObject<TForm>())
  228. .FirstOrDefault();
  229. var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
  230. model.LoadItems(parent, form, null);
  231. var host = new DigitalFormHost(model);
  232. host.OnClosing += (o, args) =>
  233. {
  234. if (args.Changed)
  235. {
  236. //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  237. }
  238. };
  239. Navigation.PushAsync(host);
  240. }
  241. public async void EditFormAsync<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
  242. where TParent : Entity, IRemotable, IPersistent, new()
  243. where TParentLink : EntityLink<TParent>, new()
  244. where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
  245. {
  246. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  247. {
  248. var form = new Client<TForm>()
  249. .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID))
  250. .Rows
  251. .Select(x => x.ToObject<AssignmentForm>())
  252. .FirstOrDefault();
  253. var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
  254. model.LoadItems(parent, form, null);
  255. var host = new DigitalFormHost(model);
  256. host.OnClosing += (o, args) =>
  257. {
  258. if (args.Changed)
  259. {
  260. //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  261. }
  262. };
  263. Navigation.PushAsync(host);
  264. }
  265. }
  266. private void _tabStrip_OnSelectionChanged(object sender, EventArgs e)
  267. {
  268. _showincomplete = _tabStrip.SelectedItem?.Index == 0;
  269. _model.DataModel.SelectNone();
  270. _selectionmenubutton.IsVisible = false;
  271. Refresh();
  272. }
  273. private void _selectionmenu_OnAppearing(object sender, EventArgs e)
  274. {
  275. _completeform.IsVisible = _showincomplete;
  276. _reopenform.IsVisible = !_showincomplete;
  277. //bool anyselected = SelectedItems().Any();
  278. //bool anyunselected = UnselectedItems().Any();
  279. //_setstatus.IsVisible = anyselected;
  280. //_selectAll.IsVisible = anyunselected;
  281. //_selectNone.IsVisible = anyselected;
  282. //_separator.IsVisible = (_setstatus.IsVisible) && (_selectAll.IsVisible || _selectNone.IsVisible);
  283. }
  284. private IEnumerable<IShell> SelectedItems()
  285. {
  286. return _model.DataModel.SelectedItems.OfType<IShell>();
  287. }
  288. private IEnumerable<IShell> UnselectedItems()
  289. {
  290. return _model.DataModel.Items.OfType<IShell>().Except(_model.DataModel.SelectedItems.OfType<IShell>());
  291. }
  292. private void _setstatus_OnClicked(object sender, EventArgs e)
  293. {
  294. }
  295. private void _selectAll_OnClicked(object sender, EventArgs e)
  296. {
  297. _model.DataModel.SelectAll();
  298. RefreshData(false,false);
  299. }
  300. private void _selectNone_OnClicked(object sender, EventArgs e)
  301. {
  302. _model.DataModel.SelectNone();
  303. RefreshData(false,false);
  304. }
  305. private void CheckBox_Changed(object sender, EventArgs e)
  306. {
  307. if ((sender as MobileCheckBox)?.BindingContext is IShell shell)
  308. _model.DataModel.ToggleSelection(shell);
  309. _selectionmenubutton.IsVisible = SelectedItems().Any();
  310. }
  311. private void _completeform_OnClicked(object sender, EventArgs e)
  312. {
  313. var shells = _model.DataModel.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
  314. foreach (var shell in shells)
  315. {
  316. shell.Completed = DateTime.Now;
  317. shell.Save("Completed on Mobile Device");
  318. }
  319. _model.DataModel.SelectNone();
  320. _selectionmenubutton.IsVisible = false;
  321. RefreshData(true,false);
  322. }
  323. private void _reopenform_OnClicked(object sender, EventArgs e)
  324. {
  325. var shells = _model.DataModel.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
  326. foreach (var shell in shells)
  327. {
  328. shell.Completed = DateTime.MinValue;
  329. shell.Save("Re-Opened on Mobile Device");
  330. }
  331. _model.DataModel.SelectNone();
  332. _selectionmenubutton.IsVisible = false;
  333. RefreshData(true,false);
  334. }
  335. }
  336. }