TasksList.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using comal.timesheets.Tasks;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using Xamarin.Forms;
  13. using Xamarin.Forms.Xaml;
  14. using XF.Material.Forms.UI;
  15. using XF.Material.Forms.UI.Dialogs;
  16. using System.Threading;
  17. namespace comal.timesheets
  18. {
  19. [XamlCompilation(XamlCompilationOptions.Compile)]
  20. public partial class TasksList
  21. {
  22. Color color = Color.FromHex("#15C7C1");
  23. int currentCategory = 0;
  24. bool searching = false;
  25. bool firstLoad = true;
  26. bool tapping = false;
  27. bool doubleTap = false;
  28. public TasksList()
  29. {
  30. InitializeComponent();
  31. LoadPreferences();
  32. searchEnt.IsEnabled = false;
  33. App.Data.Kanbans.Refresh(
  34. false,
  35. () => Device.BeginInvokeOnMainThread(DisplayList)
  36. );
  37. }
  38. private void LoadPreferences()
  39. {
  40. try
  41. {
  42. if (!App.Current.Properties.ContainsKey("ObserverPreference"))
  43. {
  44. App.Current.Properties.Add("ObserverPreference", "True");
  45. observerSwitch.IsToggled = true;
  46. }
  47. else
  48. {
  49. observerSwitch.IsToggled = Convert.ToBoolean(App.Current.Properties["ObserverPreference"]);
  50. }
  51. if (!App.Current.Properties.ContainsKey("PromptedOnUsage"))
  52. {
  53. App.Current.Properties.Add("PromptedOnUsage", "True");
  54. DisplayAlert("One-time Prompt:", "Single Tap to Open Task," + System.Environment.NewLine + "Double Tap to Complete Task", "OK");
  55. }
  56. }
  57. catch { }
  58. }
  59. private void DisplayList()
  60. {
  61. App.Data.Kanbans.Search((shell) =>
  62. {
  63. bool bResult = true;
  64. bResult = bResult && (App.Current.Properties["ObserverPreference"].Equals("True") || !shell.IsObserver);
  65. bResult = bResult &&
  66. currentCategory switch
  67. {
  68. 0 => shell.Category == "To Do",
  69. 1 => shell.Category == "Current",
  70. 2 => shell.Category == "Waiting",
  71. _ => false
  72. };
  73. bResult = bResult && (
  74. String.IsNullOrWhiteSpace(searchEnt.Text)
  75. || String.Equals(shell.Number.ToString(), searchEnt.Text)
  76. || shell.ManagerName.ToUpper().Contains(searchEnt.Text.ToUpper())
  77. || shell.Title.ToUpper().Contains(searchEnt.Text.ToUpper())
  78. || shell.Summary.ToUpper().Contains(searchEnt.Text.ToUpper())
  79. || shell.DueDate.ToString().Contains(searchEnt.Text)
  80. );
  81. return bResult;
  82. });
  83. searchEnt.IsEnabled = true;
  84. observerSwitch.IsEnabled = true;
  85. buttonToDo.Text = "To Do (" + App.Data.Kanbans.Items.Count(x => x.Category == "To Do") + ")";
  86. buttonCurrent.Text = "Current (" + App.Data.Kanbans.Items.Count(x => x.Category == "Current") + ")";
  87. buttonWaiting.Text = "Waiting (" + App.Data.Kanbans.Items.Count(x => x.Category == "Waiting") + ")";
  88. }
  89. private string ChooseCategory(string _category)
  90. {
  91. switch (_category)
  92. {
  93. case "Open":
  94. _category = "To Do";
  95. break;
  96. case "In Progress":
  97. _category = "Current";
  98. break;
  99. default:
  100. break;
  101. }
  102. return _category;
  103. }
  104. private void AddTask_Clicked(object sender, EventArgs e)
  105. {
  106. try
  107. {
  108. AddEditTask addEditTask = new AddEditTask();
  109. Navigation.PushAsync(addEditTask);
  110. }
  111. catch { }
  112. }
  113. async void KanbanList_Tapped(object sender, EventArgs e)
  114. {
  115. try
  116. {
  117. if (tapping)
  118. {
  119. doubleTap = true;
  120. var selectedTask = taskListView.SelectedItem as KanbanShell;
  121. if (selectedTask.Locked)
  122. {
  123. doubleTap = false;
  124. DisplayAlert("Alert", "Unable to complete locked task", "Cancel");
  125. return;
  126. }
  127. string chosenOption = await DisplayActionSheet("Complete Task?", "Cancel", null, "Yes", "No");
  128. switch (chosenOption)
  129. {
  130. case "Cancel":
  131. break;
  132. case "No":
  133. break;
  134. case "Yes":
  135. CompleteTask(selectedTask);
  136. break;
  137. default:
  138. break;
  139. }
  140. doubleTap = false;
  141. }
  142. else
  143. {
  144. tapping = true;
  145. Timer t = new Timer(TimerCallBack, null, 750, Timeout.Infinite);
  146. }
  147. }
  148. catch { }
  149. }
  150. private async void CompleteTask(KanbanShell shell)
  151. {
  152. // try
  153. // {
  154. // kanbanSubscriberShells.Remove(shell);
  155. // RefreshButtons(kanbanSubscriberShells);
  156. // DisplayList(kanbanSubscriberShells);
  157. // await Task.Run(() =>
  158. // {
  159. // CoreTable table = new Client<Kanban>().Query
  160. // (
  161. // new Filter<Kanban>(x => x.ID).IsEqualTo(shell.ID),
  162. // new Columns<Kanban>(x => x.ID, x => x.Category)
  163. // );
  164. // Kanban kanban = table.Rows.First().ToObject<Kanban>();
  165. // kanban.Category = "Complete";
  166. // new Client<Kanban>().Save(kanban, "Completed from mobile device");
  167. // });
  168. // }
  169. // catch { }
  170. }
  171. private async void TimerCallBack(object o) //for single tap
  172. {
  173. try
  174. {
  175. tapping = false;
  176. if (!doubleTap)
  177. {
  178. var selectedTask = taskListView.SelectedItem as KanbanShell;
  179. string loadingMessage = "Loading Task " + selectedTask.Number;
  180. if (selectedTask.Attachments != 0)
  181. {
  182. loadingMessage = loadingMessage + " with " + selectedTask.Attachments + " attached document(s). Please wait for photos to appear.";
  183. }
  184. Device.BeginInvokeOnMainThread(async () =>
  185. {
  186. using (await MaterialDialog.Instance.LoadingDialogAsync(message: loadingMessage))
  187. {
  188. Guid ID = selectedTask.ID;
  189. var form = new AddEditTask(ID);
  190. await Navigation.PushAsync(form);
  191. }
  192. });
  193. }
  194. }
  195. catch { }
  196. }
  197. private async void SearchEnt_Changed(object sender, EventArgs e)
  198. {
  199. DisplayList();
  200. }
  201. private void ButtonToDo_Clicked(object sender, EventArgs e)
  202. {
  203. currentCategory = 0;
  204. DisplayList();
  205. ChangeButtonColour();
  206. }
  207. private void ButtonCurrent_Clicked(object sender, EventArgs e)
  208. {
  209. currentCategory = 1;
  210. DisplayList();
  211. ChangeButtonColour();
  212. }
  213. private void ButtonWaiting_Clicked(object sender, EventArgs e)
  214. {
  215. currentCategory = 2;
  216. DisplayList();
  217. ChangeButtonColour();
  218. }
  219. private void ChangeButtonColour()
  220. {
  221. buttonToDo.BackgroundColor = currentCategory == 0 ? color : Color.Default;
  222. buttonCurrent.BackgroundColor = currentCategory == 1 ? color : Color.Default;
  223. buttonWaiting.BackgroundColor = currentCategory == 2 ? color : Color.Default;
  224. }
  225. private void ObserverSwitch_Toggled(object sender, EventArgs e)
  226. {
  227. if (firstLoad)
  228. return;
  229. if (observerSwitch.IsToggled)
  230. {
  231. if (App.Current.Properties.ContainsKey("ObserverPreference"))
  232. {
  233. App.Current.Properties["ObserverPreference"] = "True";
  234. }
  235. else
  236. {
  237. App.Current.Properties.Add("ObserverPreference", "True");
  238. }
  239. }
  240. else
  241. {
  242. if (App.Current.Properties.ContainsKey("ObserverPreference"))
  243. {
  244. App.Current.Properties["ObserverPreference"] = "False";
  245. }
  246. else
  247. {
  248. App.Current.Properties.Add("ObserverPreference", "False");
  249. }
  250. }
  251. DisplayList();
  252. }
  253. }
  254. }