AddEditTask.xaml.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Threading.Tasks;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using XF.Material.Forms.UI.Dialogs;
  12. using Plugin.Media;
  13. using System.IO;
  14. using comal.timesheets.QAForms;
  15. using InABox.Mobile;
  16. using PRSSecurity = InABox.Core.Security;
  17. using Xamarin.Essentials;
  18. using LogType = InABox.Core.LogType;
  19. namespace comal.timesheets.Tasks
  20. {
  21. public delegate void TaskSavedEvent(int TaskNumber);
  22. [XamlCompilation(XamlCompilationOptions.Compile)]
  23. public partial class AddEditTask
  24. {
  25. public Kanban kanban = new Kanban();
  26. bool newKanban = false;
  27. bool searching = false;
  28. bool displaying = false;
  29. List<KanbanForm> kanbanFormList = new List<KanbanForm>();
  30. List<KanbanSubscriber> observerList = new List<KanbanSubscriber>();
  31. Guid kanbanID = Guid.Empty;
  32. int estimatedTime;
  33. List<Image> imageList = new List<Image>();
  34. Dictionary<ImageSource, Document> imagesourcedocs = new Dictionary<ImageSource, Document>();
  35. public TaskSavedEvent OnTaskSaved;
  36. string kanbanTitle = "";
  37. public AddEditTask(Guid selectedID = default(Guid), string title = "")
  38. {
  39. InitializeComponent();
  40. kanbanID = selectedID;
  41. Title = "Loading";
  42. AddToolBars();
  43. kanbanTitle = title;
  44. if (selectedID == Guid.Empty)
  45. {
  46. NewKanbanTrack();
  47. UpdateScreen();
  48. }
  49. else
  50. {
  51. ExistingKanbanTrack();
  52. if (PRSSecurity.IsAllowed<CanShareTaskDetails>())
  53. shareBtn.IsVisible = true;
  54. }
  55. }
  56. #region OnAppearing and Screen Population
  57. protected override void OnAppearing()
  58. {
  59. base.OnAppearing();
  60. searching = false;
  61. CheckForDigitalForms();
  62. }
  63. private void NewKanbanTrack()
  64. {
  65. newKanban = true;
  66. kanban.DueDate = DateTime.Today;
  67. kanban.Category = "Open";
  68. kanban.StartDate = DateTime.Today;
  69. kanban.EmployeeLink.ID = App.Data.Me.ID;
  70. kanban.ManagerLink.ID = App.Data.Me.ID;
  71. kanban.EmployeeLink.Name = App.Data.Me.Name;
  72. kanban.ManagerLink.Name = App.Data.Me.Name;
  73. kanban.Title = kanbanTitle;
  74. }
  75. private void ExistingKanbanTrack()
  76. {
  77. Task.Run(() =>
  78. {
  79. CoreTable table = QueryKanban();
  80. while (table == null)
  81. table = QueryKanban();
  82. kanban = table.Rows.FirstOrDefault().ToObject<Kanban>();
  83. UpdateImages();
  84. UpdateScreen();
  85. });
  86. }
  87. private CoreTable QueryKanban()
  88. {
  89. try
  90. {
  91. return new Client<Kanban>().Query(
  92. new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanID),
  93. new Columns<Kanban>(
  94. x => x.ID,
  95. x => x.Title,
  96. x => x.Category,
  97. x => x.StartDate,
  98. x => x.Number,
  99. x => x.Notes,
  100. x => x.DueDate,
  101. x => x.JobLink.ID,
  102. x => x.JobLink.Name,
  103. x => x.JobLink.JobNumber,
  104. x => x.Private,
  105. x => x.Description,
  106. x => x.Summary,
  107. x => x.Type.Description,
  108. x => x.EmployeeLink.ID,
  109. x => x.EmployeeLink.Name,
  110. x => x.EmployeeLink.Code,
  111. x => x.ManagerLink.ID,
  112. x => x.ManagerLink.Name,
  113. x => x.ManagerLink.Code,
  114. x => x.EstimatedTime,
  115. x => x.Completed,
  116. x => x.ActualTime,
  117. x => x.Locked,
  118. x => x.Closed,
  119. x => x.Attachments,
  120. x => x.Delivery.ID
  121. )
  122. );
  123. }
  124. catch (Exception ex)
  125. {
  126. InABox.Mobile.MobileLogging.Log(ex);
  127. return null;
  128. }
  129. }
  130. private void AddToolBars()
  131. {
  132. NavigationPage.SetHasBackButton(this, false);
  133. ToolbarItems.Add(new ToolbarItem("Cancel", "", () =>
  134. {
  135. Navigation.PopAsync();
  136. }));
  137. ToolbarItems.Add(new ToolbarItem(" ", "", () =>
  138. {
  139. //button added to create space on toolbar
  140. }));
  141. ToolbarItems.Add(new ToolbarItem("Save", "", () =>
  142. {
  143. SubmitBtn_Clicked();
  144. }));
  145. }
  146. public void UpdateScreen(bool lockTaskType = false)
  147. {
  148. Device.BeginInvokeOnMainThread(() =>
  149. {
  150. if (newKanban)
  151. {
  152. Title = "New Task";
  153. }
  154. else
  155. {
  156. Title = "Task " + kanban.Number;
  157. }
  158. titleEdt.Text = kanban.Title;
  159. jobNoLbl.Text = (kanban.JobLink.JobNumber + " " + kanban.JobLink.Name);
  160. descriptionEdt.Text = kanban.Summary;
  161. descriptionEdt.IsEnabled = kanban.ID == Guid.Empty ? true : false;
  162. existingNotesLbl.Text = BuildNotes(kanban.Notes);
  163. taskTypeLbl.Text = kanban.Type.Description;
  164. if (lockTaskType)
  165. taskTypeBtn.IsEnabled = false;
  166. assignedToLbl.Text = kanban.EmployeeLink.Name;
  167. allocatedByLbl.Text = kanban.ManagerLink.Name;
  168. categoryPck.SelectedIndex = chooseIndex();
  169. dueDatePck.Date = kanban.DueDate;
  170. startDatePck.Date = kanban.StartDate;
  171. DisplayEstimatedTime();
  172. DisplayObserverList();
  173. if (kanban.Private)
  174. {
  175. privateCheckBox.IsChecked = true;
  176. }
  177. if (kanban.Locked)
  178. {
  179. categoryPck.IsEnabled = false;
  180. }
  181. });
  182. }
  183. private string BuildNotes(string[] notes)
  184. {
  185. string result = "";
  186. foreach (string note in notes)
  187. result = result + note + System.Environment.NewLine;
  188. return result;
  189. }
  190. private void AddNotes_Clicked(object sender, EventArgs e)
  191. {
  192. if (kanban.Notes.Count() == 0)
  193. {
  194. kanban.Notes = new string[] { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + App.Data.Me.Name + ": " + notesEdt.Text };
  195. notesEdt.Text = "";
  196. }
  197. else
  198. {
  199. var list = kanban.Notes.ToList();
  200. list.Add("===================================");
  201. list.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + App.Data.Me.Name + ": " + notesEdt.Text);
  202. kanban.Notes = list.ToArray();
  203. notesEdt.Text = "";
  204. }
  205. UpdateScreen();
  206. }
  207. private void NotesEdt_TextChanged(object sender, EventArgs e)
  208. {
  209. addNotesBtn.IsEnabled = string.IsNullOrWhiteSpace(notesEdt.Text) ? false : true;
  210. }
  211. private async void CheckForDigitalForms()
  212. {
  213. if (newKanban) return;
  214. formsBtn.Text = "Checking";
  215. await Task.Run(() =>
  216. {
  217. kanbanFormList.Clear();
  218. try
  219. {
  220. CoreTable table = QueryKanbanForms();
  221. while (table == null)
  222. table = QueryKanbanForms();
  223. if (table.Rows.Any())
  224. {
  225. foreach (CoreRow row in table.Rows)
  226. {
  227. KanbanForm kanbanForm = row.ToObject<KanbanForm>();
  228. kanbanFormList.Add(kanbanForm);
  229. }
  230. Device.BeginInvokeOnMainThread(() =>
  231. {
  232. formsBtn.Text = "Forms";
  233. formsBtn.IsEnabled = true;
  234. });
  235. }
  236. else
  237. {
  238. Device.BeginInvokeOnMainThread(() =>
  239. {
  240. formsBtn.Text = "Forms";
  241. formsBtn.IsEnabled = true;
  242. });
  243. }
  244. }
  245. catch
  246. { }
  247. });
  248. }
  249. private CoreTable QueryKanbanForms()
  250. {
  251. try
  252. {
  253. return new Client<KanbanForm>().Query(
  254. new Filter<KanbanForm>(x => x.Parent.ID).IsEqualTo(kanbanID),
  255. new Columns<KanbanForm>(
  256. x => x.ID,
  257. x => x.Parent.ID,
  258. x => x.Form.ID,
  259. x => x.Form.Description,
  260. x => x.Form.AppliesTo,
  261. x => x.Created,
  262. x => x.FormData,
  263. x => x.BlobData,
  264. x => x.FormCompleted,
  265. x => x.FormCompletedBy.ID,
  266. x => x.FormCompletedBy.UserID,
  267. x => x.FormOpen,
  268. x => x.FormStarted
  269. ),
  270. new SortOrder<KanbanForm>(x => x.Created)
  271. );
  272. }
  273. catch (Exception ex)
  274. {
  275. InABox.Mobile.MobileLogging.Log(ex);
  276. return null;
  277. }
  278. }
  279. #endregion
  280. #region Fields Changed
  281. private async void ShareBtn_Clicked(object sender, EventArgs e)
  282. {
  283. try
  284. {
  285. CoreTable table = QueryKanban();
  286. while (table == null)
  287. table = QueryKanban();
  288. if (table.Rows.Any())
  289. {
  290. var detail = GenerateDetail(table.Rows.First());
  291. var message = new EmailMessage
  292. {
  293. Subject = "Task Details shared from: " + App.Data.Me.Name,
  294. Body = detail,
  295. };
  296. await Xamarin.Essentials.Email.ComposeAsync(message);
  297. }
  298. }
  299. catch { }
  300. }
  301. private string GenerateDetail(CoreRow row)
  302. {
  303. string detail = "";
  304. if (!string.IsNullOrWhiteSpace(row.Get<Kanban, string>(x => x.Title)))
  305. detail = "TITLE: " + row.Get<Kanban, string>(x => x.Title) + System.Environment.NewLine + System.Environment.NewLine;
  306. if (!string.IsNullOrWhiteSpace(row.Get<Kanban, string>(x => x.Summary)))
  307. detail = detail + "SUMMARY: " + row.Get<Kanban, string>(x => x.Summary) + System.Environment.NewLine + System.Environment.NewLine;
  308. if (row.Get<Kanban, string[]>(x => x.Notes).Any())
  309. {
  310. detail = detail + "NOTES: ";
  311. foreach (var note in row.Get<Kanban, string[]>(x => x.Notes))
  312. detail = detail + note + System.Environment.NewLine;
  313. }
  314. if (!string.IsNullOrWhiteSpace(row.Get<Kanban, string>(x => x.ManagerLink.Name)))
  315. detail = detail + "MANAGER: " + row.Get<Kanban, string>(x => x.ManagerLink.Name) + System.Environment.NewLine + System.Environment.NewLine;
  316. if (!string.IsNullOrWhiteSpace(row.Get<Kanban, string>(x => x.Category)))
  317. detail = detail + "CATEGORY: " + row.Get<Kanban, string>(x => x.Category) + System.Environment.NewLine + System.Environment.NewLine;
  318. if (!string.IsNullOrWhiteSpace(row.Get<string>("IssueNumber")))
  319. detail = detail + "ISSUE NUMBER: " + row.Get<string>("IssueNumber") + System.Environment.NewLine + System.Environment.NewLine;
  320. if (row.Get<Kanban, DateTime>(x => x.Created) != DateTime.MinValue)
  321. detail = detail + "CREATED: " + row.Get<Kanban, DateTime>(x => x.Created).ToString("dd MMM yy") + System.Environment.NewLine + System.Environment.NewLine;
  322. if (row.Get<Kanban, DateTime>(x => x.StartDate) != DateTime.MinValue)
  323. detail = detail + "STARTED: " + row.Get<Kanban, DateTime>(x => x.StartDate).ToString("dd MMM yy") + System.Environment.NewLine + System.Environment.NewLine;
  324. if (row.Get<Kanban, DateTime>(x => x.DueDate) != DateTime.MinValue)
  325. detail = detail + "DUE: " + row.Get<Kanban, DateTime>(x => x.DueDate).ToString("dd MMM yy") + System.Environment.NewLine + System.Environment.NewLine;
  326. return detail;
  327. }
  328. private void TitleEdt_Changed(object sender, EventArgs e)
  329. {
  330. kanban.Title = titleEdt.Text;
  331. }
  332. private void DescriptionEdt_Changed(object sender, EventArgs e)
  333. {
  334. kanban.Description = descriptionEdt.Text;
  335. }
  336. private void DueDatePck_Selected(object sender, EventArgs e)
  337. {
  338. kanban.DueDate = dueDatePck.Date;
  339. }
  340. private void StartDatePck_Selected(object sender, EventArgs e)
  341. {
  342. kanban.StartDate = startDatePck.Date;
  343. }
  344. private void JobNoBtn_Clicked(object sender, EventArgs e)
  345. {
  346. if (searching)
  347. return;
  348. else
  349. {
  350. searching = true;
  351. JobSelectionPage jobSelectionPage = new JobSelectionPage(
  352. (job) =>
  353. {
  354. kanban.JobLink.ID = job.ID;
  355. kanban.JobLink.Name = job.Name;
  356. kanban.JobLink.JobNumber = job.JobNumber;
  357. UpdateScreen();
  358. }
  359. );
  360. Navigation.PushAsync(jobSelectionPage);
  361. }
  362. }
  363. private void TaskType_Clicked(object sender, EventArgs e)
  364. {
  365. if (searching)
  366. return;
  367. else
  368. {
  369. searching = true;
  370. GenericSelectionPage page = new GenericSelectionPage
  371. (
  372. "Select Type",
  373. new SelectionViewModel<KanbanType>
  374. (
  375. new Filter<KanbanType>(x => x.Hidden).IsEqualTo(false),
  376. new Expression<Func<KanbanType, object>>[] { x => x.Description },
  377. new Expression<Func<KanbanType, object>>[] { x => x.Hidden },
  378. new SortOrder<KanbanType>(x => x.Description)
  379. ));
  380. page.OnItemSelected += (row) =>
  381. {
  382. var kanbanType = row.ToObject<KanbanType>();
  383. kanban.Type.ID = kanbanType.ID;
  384. kanban.Type.Synchronise(kanbanType);
  385. UpdateScreen();
  386. };
  387. Navigation.PushAsync(page);
  388. }
  389. }
  390. private void AllocatedByBtn_Clicked(object sender, EventArgs e)
  391. {
  392. EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage(
  393. (employee) =>
  394. {
  395. kanban.ManagerLink.ID = employee.ID;
  396. kanban.ManagerLink.Name = employee.Name;
  397. UpdateScreen();
  398. }
  399. );
  400. Navigation.PushAsync(employeeSelectionPage);
  401. }
  402. private void AssignedToBtn_Clicked(object sender, EventArgs e)
  403. {
  404. EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage(
  405. (employee) =>
  406. {
  407. kanban.EmployeeLink.ID = employee.ID;
  408. kanban.EmployeeLink.Name = employee.Name;
  409. UpdateScreen();
  410. }
  411. );
  412. Navigation.PushAsync(employeeSelectionPage);
  413. }
  414. private void CheckPrivateChanged(object sender, CheckedChangedEventArgs e)
  415. {
  416. if (privateCheckBox.IsChecked)
  417. {
  418. Employee employee = new Employee();
  419. var table = new Client<Employee>().Query(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid));
  420. foreach (CoreRow row in table.Rows)
  421. {
  422. employee = row.ToObject<Employee>();
  423. }
  424. kanban.ManagerLink.ID = employee.ID;
  425. kanban.ManagerLink.Synchronise(employee);
  426. kanban.EmployeeLink.ID = employee.ID;
  427. kanban.EmployeeLink.Synchronise(employee);
  428. kanban.Private = true;
  429. assignedToBtn.IsEnabled = false;
  430. allocatedByBtn.IsEnabled = false;
  431. UpdateScreen();
  432. }
  433. if (!privateCheckBox.IsChecked)
  434. {
  435. kanban.Private = false;
  436. assignedToBtn.IsEnabled = true;
  437. allocatedByBtn.IsEnabled = true;
  438. }
  439. }
  440. private void category_Changed(object sender, EventArgs e)
  441. {
  442. if (categoryPck.SelectedIndex == 0)
  443. {
  444. kanban.Category = "Open";
  445. }
  446. if (categoryPck.SelectedIndex == 1)
  447. {
  448. kanban.Category = "In Progress";
  449. }
  450. if (categoryPck.SelectedIndex == 2)
  451. {
  452. kanban.Category = "Waiting";
  453. }
  454. if (categoryPck.SelectedIndex == 3)
  455. {
  456. kanban.Category = "Complete";
  457. }
  458. }
  459. private int chooseIndex()
  460. {
  461. int indexNo = -1;
  462. if (kanban.Category != null)
  463. {
  464. if (kanban.Category.Equals("Open"))
  465. {
  466. indexNo = 0;
  467. }
  468. if (kanban.Category.Equals("In Progress"))
  469. {
  470. indexNo = 1;
  471. }
  472. if (kanban.Category.Equals("Waiting"))
  473. {
  474. indexNo = 2;
  475. }
  476. if (kanban.Category.Equals("Complete"))
  477. {
  478. indexNo = 3;
  479. }
  480. }
  481. return indexNo;
  482. }
  483. #endregion
  484. #region Estimated Time
  485. private void DecreaseBtn_Clicked(object sender, EventArgs e)
  486. {
  487. if (estimatedTime == 0 || estimatedTime < 0)
  488. return;
  489. else
  490. {
  491. estimatedTime = estimatedTime - 15;
  492. kanban.EstimatedTime = new TimeSpan(0, estimatedTime, 0);
  493. DisplayEstimatedTime();
  494. }
  495. }
  496. private void IncreaseBtn_Clicked(object sender, EventArgs e)
  497. {
  498. estimatedTime = estimatedTime + 15;
  499. kanban.EstimatedTime = new TimeSpan(0, estimatedTime, 0);
  500. DisplayEstimatedTime();
  501. }
  502. private void EstimatedHoursEdt_Changed(object sender, EventArgs e)
  503. {
  504. if (displaying)
  505. return;
  506. else
  507. CalculateEstimatedTime();
  508. }
  509. private void EstimatedMinsEdt_Changed(object sender, EventArgs e)
  510. {
  511. if (displaying)
  512. return;
  513. else
  514. CalculateEstimatedTime();
  515. }
  516. private async void CalculateEstimatedTime() //to timespan
  517. {
  518. try
  519. {
  520. int minutes = 0;
  521. int hours = 0;
  522. if (!string.IsNullOrWhiteSpace(estimatedHoursEdt.Text))
  523. {
  524. hours = Convert.ToInt32(estimatedHoursEdt.Text);
  525. }
  526. if (!string.IsNullOrWhiteSpace(estimatedMinsEdt.Text))
  527. {
  528. minutes = Convert.ToInt32(estimatedMinsEdt.Text);
  529. }
  530. kanban.EstimatedTime = new TimeSpan(hours, minutes, 0);
  531. estimatedTime = Convert.ToInt32(kanban.EstimatedTime.TotalMinutes);
  532. }
  533. catch
  534. {
  535. await DisplayAlert("Error", "Only whole numbers for estimated time fields", "OK");
  536. int isNumber;
  537. if (!int.TryParse(estimatedHoursEdt.Text, out isNumber))
  538. {
  539. estimatedHoursEdt.Text = "0";
  540. };
  541. if (!int.TryParse(estimatedMinsEdt.Text, out isNumber))
  542. {
  543. estimatedMinsEdt.Text = "0";
  544. };
  545. }
  546. }
  547. private async void DisplayEstimatedTime() //from timespan
  548. {
  549. await Task.Run(() =>
  550. {
  551. displaying = true;
  552. estimatedTime = Convert.ToInt32(kanban.EstimatedTime.TotalMinutes);
  553. int hours = estimatedTime / 60;
  554. int minutes = estimatedTime % 60;
  555. Device.BeginInvokeOnMainThread(() =>
  556. {
  557. estimatedHoursEdt.Text = hours.ToString();
  558. estimatedMinsEdt.Text = minutes.ToString();
  559. displaying = false;
  560. });
  561. });
  562. }
  563. #endregion
  564. #region Display or add images
  565. private async void UpdateImages()
  566. {
  567. try
  568. {
  569. Device.BeginInvokeOnMainThread(() =>
  570. {
  571. if (kanban.Attachments != 0)
  572. {
  573. int count = kanban.Attachments;
  574. photosLbl.TextColor = Color.Orange;
  575. photosLbl.Text = "Loading " + kanban.Attachments + " Photos";
  576. Task.Run(() =>
  577. {
  578. var table = QueryKanbanDocuments();
  579. while (table == null)
  580. table = QueryKanbanDocuments();
  581. if (table.Rows.Count != 0)
  582. {
  583. foreach (var row in table.Rows)
  584. {
  585. CoreTable docstable = QueryDocument(row.Get<KanbanDocument, Guid>(x => x.DocumentLink.ID));
  586. while (docstable == null)
  587. docstable = QueryDocument(row.Get<KanbanDocument, Guid>(x => x.DocumentLink.ID));
  588. CoreRow docrow = docstable.Rows.FirstOrDefault();
  589. if (docrow != null)
  590. {
  591. byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
  592. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  593. Image img = new Image();
  594. img.HeightRequest = 150;
  595. img.WidthRequest = 150;
  596. img.Aspect = Aspect.AspectFit;
  597. img.Source = src;
  598. img.GestureRecognizers.Add(new TapGestureRecognizer
  599. {
  600. Command = new Command(OnTap),
  601. CommandParameter = src,
  602. NumberOfTapsRequired = 1
  603. });
  604. imageList.Add(img);
  605. Device.BeginInvokeOnMainThread(() =>
  606. {
  607. ImageScroller.IsVisible = true;
  608. images.Children.Add(img);
  609. count = count - 1;
  610. photosLbl.Text = "Loading " + count + " Photo(s)";
  611. if (count == 0)
  612. {
  613. photosLbl.Text = "Photos";
  614. photosLbl.TextColor = Color.Default;
  615. }
  616. });
  617. }
  618. }
  619. }
  620. });
  621. }
  622. });
  623. }
  624. catch { }
  625. }
  626. private CoreTable QueryKanbanDocuments()
  627. {
  628. try
  629. {
  630. return new Client<KanbanDocument>().Query(
  631. new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID),
  632. new Columns<KanbanDocument>(x => x.DocumentLink.ID),
  633. null
  634. );
  635. }
  636. catch (Exception ex)
  637. {
  638. InABox.Mobile.MobileLogging.Log(ex);
  639. return null;
  640. }
  641. }
  642. private CoreTable QueryDocument(Guid id)
  643. {
  644. try
  645. {
  646. return new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(id),
  647. new Columns<Document>(x => x.Data));
  648. }
  649. catch (Exception ex)
  650. {
  651. InABox.Mobile.MobileLogging.Log(ex);
  652. return null;
  653. }
  654. }
  655. private void OnTap(object obj)
  656. {
  657. ImageViewerEditor imageViewEditor = new ImageViewerEditor(obj as ImageSource);
  658. imageViewEditor.OnSaveSelected += (byte[] array) =>
  659. {
  660. try
  661. {
  662. Image img = imageList.Find(x => x.Source.Equals(obj as ImageSource));
  663. imageList.Remove(img);
  664. imagesourcedocs.Remove(obj as ImageSource);
  665. }
  666. catch { }
  667. DataToImage(array);
  668. };
  669. Navigation.PushAsync(imageViewEditor);
  670. }
  671. public void DataToImage(byte[] data)
  672. {
  673. try
  674. {
  675. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  676. Image img = new Image();
  677. img.HeightRequest = 150;
  678. img.WidthRequest = 150;
  679. img.Aspect = Aspect.AspectFit;
  680. img.VerticalOptions = LayoutOptions.FillAndExpand;
  681. img.HorizontalOptions = LayoutOptions.FillAndExpand;
  682. img.Source = src;
  683. img.GestureRecognizers.Add(new TapGestureRecognizer
  684. {
  685. Command = new Command(OnTap),
  686. CommandParameter = src,
  687. NumberOfTapsRequired = 1
  688. });
  689. if (img != null)
  690. {
  691. imageList.Add(img);
  692. RefreshView();
  693. }
  694. String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  695. Document doc = new Document()
  696. {
  697. FileName = filename,
  698. Data = data,
  699. CRC = CoreUtils.CalculateCRC(data),
  700. TimeStamp = DateTime.Now
  701. };
  702. imagesourcedocs.Add(src, doc);
  703. }
  704. catch
  705. { }
  706. }
  707. private void RefreshView()
  708. {
  709. Device.BeginInvokeOnMainThread(() =>
  710. {
  711. images.Children.Clear();
  712. foreach (Image img in imageList)
  713. {
  714. images.Children.Add(img);
  715. }
  716. });
  717. }
  718. async void TakePhoto_Clicked(object sender, EventArgs e)
  719. {
  720. try
  721. {
  722. await CrossMedia.Current.Initialize();
  723. if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  724. {
  725. await DisplayAlert("No Camera", ":( No camera available.", "OK");
  726. return;
  727. }
  728. String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  729. var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  730. {
  731. Name = filename,
  732. CompressionQuality = 10,
  733. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  734. });
  735. if (file == null)
  736. return;
  737. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  738. {
  739. Image img = null;
  740. var memoryStream = new MemoryStream();
  741. file.GetStream().CopyTo(memoryStream);
  742. var data = memoryStream.ToArray();
  743. Document doc = new Document()
  744. {
  745. FileName = filename,
  746. Data = data,
  747. CRC = CoreUtils.CalculateCRC(data),
  748. TimeStamp = DateTime.Now
  749. };
  750. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  751. imagesourcedocs.Add(src, doc);
  752. img = new Image();
  753. img.HeightRequest = 150;
  754. img.WidthRequest = 150;
  755. img.Aspect = Aspect.AspectFit;
  756. img.Source = src;
  757. img.GestureRecognizers.Add(new TapGestureRecognizer
  758. {
  759. Command = new Command(OnTap),
  760. CommandParameter = src,
  761. NumberOfTapsRequired = 1
  762. });
  763. file.Dispose();
  764. if (img != null)
  765. {
  766. Device.BeginInvokeOnMainThread(() =>
  767. {
  768. ImageScroller.IsVisible = true;
  769. images.Children.Add(img);
  770. });
  771. }
  772. await pageScroller.ScrollToAsync(photoFrame, ScrollToPosition.Center, false);
  773. }
  774. }
  775. catch { }
  776. }
  777. async void ChooseImage_Clicked(object sender, EventArgs e)
  778. {
  779. try
  780. {
  781. await CrossMedia.Current.Initialize();
  782. if (!CrossMedia.Current.IsPickPhotoSupported)
  783. {
  784. await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
  785. return;
  786. }
  787. var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  788. {
  789. CompressionQuality = 10,
  790. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  791. });
  792. if (file == null)
  793. return;
  794. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  795. {
  796. Image img = null;
  797. var memoryStream = new MemoryStream();
  798. file.GetStream().CopyTo(memoryStream);
  799. var data = memoryStream.ToArray();
  800. Document doc = new Document()
  801. {
  802. FileName = Path.GetFileName(file.Path),
  803. Data = data,
  804. CRC = CoreUtils.CalculateCRC(data),
  805. TimeStamp = DateTime.Now
  806. };
  807. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  808. imagesourcedocs.Add(src, doc);
  809. img = new Image();
  810. img.HeightRequest = 150;
  811. img.WidthRequest = 150;
  812. img.Aspect = Aspect.AspectFit;
  813. img.Source = src;
  814. img.GestureRecognizers.Add(new TapGestureRecognizer
  815. {
  816. Command = new Command(OnTap),
  817. CommandParameter = src,
  818. NumberOfTapsRequired = 1
  819. });
  820. file.Dispose();
  821. if (img != null)
  822. {
  823. Device.BeginInvokeOnMainThread(() =>
  824. {
  825. ImageScroller.IsVisible = true;
  826. images.Children.Add(img);
  827. });
  828. }
  829. await pageScroller.ScrollToAsync(photoFrame, ScrollToPosition.Center, false);
  830. }
  831. }
  832. catch { }
  833. }
  834. #endregion
  835. #region Digital Forms
  836. private async void Forms_Clicked(object sender, EventArgs e)
  837. {
  838. try
  839. {
  840. string chosenOptionOne = "";
  841. if (kanbanFormList.Count == 0)
  842. {
  843. chosenOptionOne = await DisplayActionSheet("Choose An Option", "Cancel", null, "Add Form to Task");
  844. }
  845. else if (kanbanFormList.Count > 0)
  846. {
  847. chosenOptionOne = await DisplayActionSheet("Choose An Option", "Cancel", null, "Add Form to Task", "View Form(s)");
  848. }
  849. switch (chosenOptionOne)
  850. {
  851. case "Cancel":
  852. return;
  853. case "Add Form to Task":
  854. DigitalFormsPicker digitalFormPicker = new DigitalFormsPicker("Kanban", kanban.ID);
  855. Navigation.PushAsync(digitalFormPicker);
  856. break;
  857. case "View Form(s)":
  858. ChooseForm();
  859. break;
  860. default: break;
  861. }
  862. }
  863. catch { }
  864. }
  865. private async void ChooseForm()
  866. {
  867. ListSelectionPage page = new ListSelectionPage(CreatePairs(), "Forms");
  868. page.OnDictionaryItemTapped += (id, value) => { LaunchForm(id); };
  869. Navigation.PushAsync(page);
  870. }
  871. private Dictionary<Guid, string> CreatePairs()
  872. {
  873. Dictionary<Guid, string> pairs = new Dictionary<Guid, string>();
  874. foreach (KanbanForm kanbanForm in kanbanFormList)
  875. {
  876. string formDescription = CreateDescription(kanbanForm);
  877. pairs.Add(kanbanForm.ID, formDescription);
  878. }
  879. return pairs;
  880. }
  881. private string CreateDescription(KanbanForm kanbanForm)
  882. {
  883. string formDescription = kanbanForm.Form.Description;
  884. if (kanbanForm.FormCompleted != DateTime.MinValue)
  885. formDescription = formDescription
  886. + " (Completed: "
  887. + kanbanForm.FormCompleted.ToString("hh:mm - dd MMM yy")
  888. + " by "
  889. + kanbanForm.FormCompletedBy.UserID
  890. + ")";
  891. else
  892. formDescription = formDescription
  893. + " (Created: "
  894. + kanbanForm.Created.ToString("hh:mm - dd MMM yy")
  895. + ")";
  896. return formDescription;
  897. }
  898. private async void LaunchForm(Guid id)
  899. {
  900. KanbanForm form = kanbanFormList.FirstOrDefault(x => x.ID == id);
  901. CoreTable table = QueryDigitalFormLayout(form);
  902. while (table == null)
  903. table = QueryDigitalFormLayout(form);
  904. CoreRow row = table.Rows.FirstOrDefault();
  905. DigitalFormLayout layout = row.ToObject<DigitalFormLayout>();
  906. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  907. {
  908. DigitalFormHost host = new DigitalFormHost(
  909. DigitalFormsHelper.LoadModel(
  910. layout, typeof(KanbanForm), kanban, Guid.Empty,
  911. new ExistingFormShell
  912. {
  913. ID = form.ID,
  914. ParentID = kanban.ID,
  915. Type = typeof(KanbanForm),
  916. FormID = form.ID,
  917. }
  918. ));
  919. Navigation.PushAsync(host);
  920. }
  921. }
  922. private CoreTable QueryDigitalFormLayout(KanbanForm form)
  923. {
  924. try
  925. {
  926. return new Client<DigitalFormLayout>().Query(
  927. new Filter<DigitalFormLayout>(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Description).IsEqualTo(form.Form.Description),
  928. new Columns<DigitalFormLayout>(x => x.Description, x => x.ID, x => x.Code, x => x.Form.AppliesTo, x => x.Form.ID, x => x.Layout),
  929. new SortOrder<DigitalFormLayout>(x => x.Description)
  930. );
  931. }
  932. catch (Exception ex)
  933. {
  934. InABox.Mobile.MobileLogging.Log(ex);
  935. return null;
  936. }
  937. }
  938. #endregion
  939. #region Submit btn + photos save
  940. private async void SubmitBtn_Clicked()
  941. {
  942. try
  943. {
  944. if (searching)
  945. return;
  946. else
  947. {
  948. searching = true;
  949. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  950. {
  951. SaveKanban();
  952. SaveDocuments();
  953. Task.Run(() => { SaveSubscribers(); });
  954. SavePhotos();
  955. }
  956. string successMessage = "Task number : " + kanban.Number + System.Environment.NewLine
  957. + "New Photo(s): " + imagesourcedocs.Values.Count;
  958. await DisplayAlert("Success", successMessage, "OK");
  959. OnTaskSaved?.Invoke(kanban.Number);
  960. await Navigation.PopAsync();
  961. }
  962. }
  963. catch (Exception ex)
  964. {
  965. DisplayAlert("Error saving", ex.Message, "OK");
  966. }
  967. }
  968. private void SaveKanban()
  969. {
  970. try
  971. {
  972. new Client<Kanban>().Save(kanban, "Updated From Mobile Device");
  973. }
  974. catch (Exception ex)
  975. {
  976. InABox.Mobile.MobileLogging.Log(ex);
  977. SaveKanban();
  978. }
  979. }
  980. private void SaveDocuments()
  981. {
  982. try
  983. {
  984. if (imagesourcedocs.Values.Count != 0)
  985. new Client<Document>().Save(imagesourcedocs.Values, "Photo Taken on Device");
  986. }
  987. catch (Exception ex)
  988. {
  989. InABox.Mobile.MobileLogging.Log(ex);
  990. SaveDocuments();
  991. }
  992. }
  993. private void SavePhotos()
  994. {
  995. try
  996. {
  997. if (imagesourcedocs.Values.Count != 0)
  998. {
  999. List<KanbanDocument> newKanbanDocuments = new List<KanbanDocument>();
  1000. foreach (Document doc in imagesourcedocs.Values)
  1001. {
  1002. var kanbanDocument = new KanbanDocument();
  1003. kanbanDocument.EntityLink.ID = kanban.ID;
  1004. kanbanDocument.DocumentLink.ID = doc.ID;
  1005. kanbanDocument.DocumentLink.FileName = doc.FileName;
  1006. newKanbanDocuments.Add(kanbanDocument);
  1007. }
  1008. Task.Run(() =>
  1009. {
  1010. SaveKanbanDocuments(newKanbanDocuments);
  1011. });
  1012. }
  1013. }
  1014. catch { }
  1015. }
  1016. private void SaveKanbanDocuments(List<KanbanDocument> newKanbanDocuments)
  1017. {
  1018. try
  1019. {
  1020. new Client<KanbanDocument>().Save(newKanbanDocuments, "Photo Taken on Device");
  1021. }
  1022. catch (Exception ex)
  1023. {
  1024. InABox.Mobile.MobileLogging.Log(ex);
  1025. SaveKanbanDocuments(newKanbanDocuments);
  1026. }
  1027. }
  1028. #endregion
  1029. #region Subscribers Buttons / Functionality
  1030. private async void DisplayObserverList()
  1031. {
  1032. try
  1033. {
  1034. await Task.Run(() =>
  1035. {
  1036. if (!newKanban)
  1037. {
  1038. CoreTable table = QueryObservers();
  1039. while (table == null)
  1040. table = QueryObservers();
  1041. foreach (CoreRow row in table.Rows)
  1042. {
  1043. KanbanSubscriber subscriber = row.ToObject<KanbanSubscriber>();
  1044. if (!subscriber.Manager && !subscriber.Assignee)
  1045. {
  1046. observerList.Add(subscriber);
  1047. AddSubscriberLabel(subscriber);
  1048. }
  1049. }
  1050. }
  1051. });
  1052. }
  1053. catch { }
  1054. }
  1055. private CoreTable QueryObservers()
  1056. {
  1057. try
  1058. {
  1059. return new Client<KanbanSubscriber>().Query(
  1060. new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(kanban.ID).And(x => x.Observer).IsEqualTo(true)
  1061. );
  1062. }
  1063. catch (Exception ex)
  1064. {
  1065. InABox.Mobile.MobileLogging.Log(ex);
  1066. return null;
  1067. }
  1068. }
  1069. private void AddSubscriberLabel(KanbanSubscriber subscriber)
  1070. {
  1071. Label label = new Label();
  1072. label.Text = subscriber.Employee.Name;
  1073. label.FontSize = Device.GetNamedSize(NamedSize.Medium, label);
  1074. label.Margin = 5;
  1075. label.HorizontalTextAlignment = TextAlignment.Start;
  1076. label.VerticalTextAlignment = TextAlignment.Center;
  1077. Device.BeginInvokeOnMainThread(() =>
  1078. {
  1079. observerStackLayout.Children.Add(label);
  1080. });
  1081. }
  1082. private async void AddSubscriberBtn_Clicked(object sender, EventArgs e)
  1083. {
  1084. string chosenOption = await DisplayActionSheet("Add", "Cancel", null, "Person", "Team");
  1085. switch (chosenOption)
  1086. {
  1087. case "Cancel":
  1088. break;
  1089. case "Person":
  1090. SelectEmployeeForSubscriber();
  1091. break;
  1092. case "Team":
  1093. SelectTeamForSubscriber();
  1094. break;
  1095. default:
  1096. break;
  1097. }
  1098. }
  1099. private void SelectEmployeeForSubscriber()
  1100. {
  1101. EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage(
  1102. (employee) =>
  1103. {
  1104. KanbanSubscriber subscriber = new KanbanSubscriber();
  1105. subscriber.Kanban.ID = kanban.ID;
  1106. subscriber.Observer = true;
  1107. subscriber.Employee.ID = employee.ID;
  1108. subscriber.Employee.Name = employee.Name;
  1109. CheckListAndAddSubscriber(subscriber);
  1110. }
  1111. );
  1112. Navigation.PushAsync(employeeSelectionPage);
  1113. }
  1114. private async void SelectTeamForSubscriber()
  1115. {
  1116. string[] array = App.Data.EmployeeTeams.Select(x=>x.TeamName).Distinct().ToArray();
  1117. string chosenTeam = await DisplayActionSheet("Choose Team", "Cancel", null, array);
  1118. switch (chosenTeam)
  1119. {
  1120. case "Cancel":
  1121. return;
  1122. break;
  1123. }
  1124. if (!string.IsNullOrWhiteSpace(chosenTeam))
  1125. {
  1126. List<EmployeeTeamShell> employees = App.Data.EmployeeTeams.Where(x => x.TeamName == chosenTeam).ToList();
  1127. foreach (var employee in employees)
  1128. {
  1129. KanbanSubscriber subscriber = new KanbanSubscriber();
  1130. subscriber.Kanban.ID = kanban.ID;
  1131. subscriber.Employee.ID = employee.ID;
  1132. subscriber.Employee.Name = employee.Name;
  1133. subscriber.Observer = true;
  1134. CheckListAndAddSubscriber(subscriber);
  1135. }
  1136. }
  1137. }
  1138. private void CheckListAndAddSubscriber(KanbanSubscriber subscriber)
  1139. {
  1140. List<Guid> guids = new List<Guid>();
  1141. foreach (KanbanSubscriber sub in observerList)
  1142. {
  1143. guids.Add(sub.Employee.ID);
  1144. }
  1145. if (!guids.Contains(subscriber.Employee.ID))
  1146. {
  1147. if (subscriber.Employee.ID != kanban.EmployeeLink.ID)
  1148. {
  1149. if (subscriber.Employee.ID != kanban.ManagerLink.ID)
  1150. {
  1151. observerList.Add(subscriber);
  1152. AddSubscriberLabel(subscriber);
  1153. }
  1154. }
  1155. }
  1156. }
  1157. private async void RemoveSubscriberBtn_Clicked(object sender, EventArgs e)
  1158. {
  1159. try
  1160. {
  1161. Dictionary<string, KanbanSubscriber> nameSubscriberPairs = new Dictionary<string, KanbanSubscriber>();
  1162. foreach (KanbanSubscriber subscriber in observerList)
  1163. {
  1164. nameSubscriberPairs.Add(subscriber.Employee.Name, subscriber);
  1165. }
  1166. string[] array = nameSubscriberPairs.Keys.ToArray();
  1167. string chosenOption = await DisplayActionSheet("Remove", "Cancel", null, array);
  1168. if (chosenOption == "Cancel" || string.IsNullOrWhiteSpace(chosenOption))
  1169. {
  1170. return;
  1171. }
  1172. else
  1173. {
  1174. KanbanSubscriber subscriber = nameSubscriberPairs[chosenOption];
  1175. observerList.Remove(subscriber);
  1176. observerStackLayout.Children.Clear();
  1177. foreach (KanbanSubscriber sub in observerList)
  1178. {
  1179. AddSubscriberLabel(sub);
  1180. }
  1181. }
  1182. }
  1183. catch { }
  1184. }
  1185. #endregion
  1186. #region Subscribers Saving
  1187. private void SaveSubscribers()
  1188. {
  1189. Task.Run(() =>
  1190. {
  1191. if (newKanban)
  1192. {
  1193. SaveNewSubs();
  1194. }
  1195. else
  1196. {
  1197. SaveExistingSubs();
  1198. }
  1199. });
  1200. }
  1201. private void SaveNewSubs()
  1202. {
  1203. try
  1204. {
  1205. List<KanbanSubscriber> subscribers = new List<KanbanSubscriber>();
  1206. KanbanSubscriber sub = null;
  1207. if (kanban.EmployeeLink.ID != Guid.Empty)
  1208. {
  1209. sub = new KanbanSubscriber();
  1210. sub.Kanban.ID = kanban.ID;
  1211. sub.Employee.ID = kanban.EmployeeLink.ID;
  1212. sub.Assignee = true;
  1213. if (kanban.EmployeeLink.ID == kanban.ManagerLink.ID)
  1214. {
  1215. sub.Manager = true;
  1216. }
  1217. subscribers.Add(sub);
  1218. }
  1219. if (kanban.ManagerLink.ID != Guid.Empty)
  1220. {
  1221. if (kanban.ManagerLink.ID != kanban.EmployeeLink.ID)
  1222. {
  1223. sub = new KanbanSubscriber();
  1224. sub.Kanban.ID = kanban.ID;
  1225. sub.Employee.ID = kanban.ManagerLink.ID;
  1226. sub.Manager = true;
  1227. subscribers.Add(sub);
  1228. }
  1229. }
  1230. foreach (KanbanSubscriber subscriber in observerList)
  1231. {
  1232. subscriber.Kanban.ID = kanban.ID;
  1233. subscribers.Add(subscriber);
  1234. }
  1235. DoSaveSubscribers(subscribers);
  1236. }
  1237. catch (Exception ex)
  1238. {
  1239. InABox.Mobile.MobileLogging.Log(ex);
  1240. }
  1241. }
  1242. private void DoSaveSubscribers(List<KanbanSubscriber> subscribers)
  1243. {
  1244. try
  1245. {
  1246. new Client<KanbanSubscriber>().Save(subscribers, "Updated from mobile device");
  1247. }
  1248. catch (Exception ex)
  1249. {
  1250. InABox.Mobile.MobileLogging.Log(ex);
  1251. DoSaveSubscribers(subscribers);
  1252. }
  1253. }
  1254. private void SaveExistingSubs()
  1255. {
  1256. try
  1257. {
  1258. KanbanSubscriber oldAssignee = new KanbanSubscriber();
  1259. KanbanSubscriber oldManager = new KanbanSubscriber();
  1260. KanbanSubscriber oldBoth = new KanbanSubscriber();
  1261. List<KanbanSubscriber> oldObservers = new List<KanbanSubscriber>();
  1262. List<KanbanSubscriber> subscribersToDelete = new List<KanbanSubscriber>();
  1263. List<KanbanSubscriber> subscribersToSave = new List<KanbanSubscriber>();
  1264. List<KanbanSubscriber> subscribers = new List<KanbanSubscriber>();
  1265. CoreTable table = QuerySubscribers();
  1266. while (table == null)
  1267. table = QuerySubscribers();
  1268. foreach (CoreRow row in table.Rows)
  1269. {
  1270. KanbanSubscriber subscriber = row.ToObject<KanbanSubscriber>();
  1271. if (subscriber.Assignee && subscriber.Manager)
  1272. {
  1273. oldBoth = subscriber;
  1274. }
  1275. else
  1276. {
  1277. if (subscriber.Assignee)
  1278. {
  1279. oldAssignee = subscriber;
  1280. }
  1281. else if (subscriber.Manager)
  1282. {
  1283. oldManager = subscriber;
  1284. }
  1285. else if (subscriber.Observer)
  1286. {
  1287. oldObservers.Add(subscriber);
  1288. }
  1289. }
  1290. }
  1291. if (kanban.ManagerLink.ID == kanban.EmployeeLink.ID)
  1292. {
  1293. if (kanban.ManagerLink.ID != oldBoth.Employee.ID && oldBoth.Employee.ID != Guid.Empty)
  1294. {
  1295. subscribersToDelete.Add(oldBoth);
  1296. KanbanSubscriber subscriber = new KanbanSubscriber();
  1297. subscriber.Assignee = true;
  1298. subscriber.Manager = true;
  1299. subscriber.Kanban.ID = kanban.ID;
  1300. subscriber.Employee.ID = kanban.EmployeeLink.ID;
  1301. subscribersToSave.Add(subscriber);
  1302. }
  1303. }
  1304. if (oldAssignee.Employee.ID != kanban.EmployeeLink.ID && oldAssignee.Employee.ID != Guid.Empty)
  1305. {
  1306. subscribersToDelete.Add(oldAssignee);
  1307. KanbanSubscriber subscriber = new KanbanSubscriber();
  1308. subscriber.Assignee = true;
  1309. subscriber.Manager = false;
  1310. subscriber.Kanban.ID = kanban.ID;
  1311. subscriber.Employee.ID = kanban.EmployeeLink.ID;
  1312. subscribersToSave.Add(subscriber);
  1313. }
  1314. if (oldManager.Employee.ID != kanban.ManagerLink.ID && oldManager.Employee.ID != Guid.Empty)
  1315. {
  1316. subscribersToDelete.Add(oldManager);
  1317. KanbanSubscriber subscriber = new KanbanSubscriber();
  1318. subscriber.Assignee = false;
  1319. subscriber.Manager = true;
  1320. subscriber.Kanban.ID = kanban.ID;
  1321. subscriber.Employee.ID = kanban.ManagerLink.ID;
  1322. subscribersToSave.Add(subscriber);
  1323. }
  1324. List<Guid> oldGuids = new List<Guid>();
  1325. List<Guid> newGuids = new List<Guid>();
  1326. foreach (KanbanSubscriber sub in observerList)
  1327. {
  1328. newGuids.Add(sub.Employee.ID);
  1329. }
  1330. foreach (KanbanSubscriber sub in oldObservers)
  1331. {
  1332. oldGuids.Add(sub.Employee.ID);
  1333. if (!newGuids.Contains(sub.Employee.ID))
  1334. {
  1335. subscribersToDelete.Add(sub);
  1336. }
  1337. }
  1338. foreach (KanbanSubscriber sub in observerList)
  1339. {
  1340. if (!oldGuids.Contains(sub.Employee.ID))
  1341. {
  1342. subscribersToSave.Add(sub);
  1343. }
  1344. }
  1345. DoSaveSubscribers(subscribersToSave);
  1346. DeleteSubscribers(subscribersToDelete);
  1347. }
  1348. catch (Exception ex)
  1349. {
  1350. InABox.Mobile.MobileLogging.Log(ex);
  1351. }
  1352. }
  1353. private CoreTable QuerySubscribers()
  1354. {
  1355. try
  1356. {
  1357. return new Client<KanbanSubscriber>().Query(
  1358. new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(kanban.ID)
  1359. );
  1360. }
  1361. catch (Exception ex)
  1362. {
  1363. InABox.Mobile.MobileLogging.Log(ex);
  1364. return null;
  1365. }
  1366. }
  1367. private void DeleteSubscribers(List<KanbanSubscriber> subscribersToDelete)
  1368. {
  1369. try
  1370. {
  1371. new Client<KanbanSubscriber>().Delete(subscribersToDelete, "Updated from mobile device");
  1372. }
  1373. catch (Exception ex)
  1374. {
  1375. InABox.Mobile.MobileLogging.Log(ex);
  1376. DeleteSubscribers(subscribersToDelete);
  1377. }
  1378. }
  1379. #endregion
  1380. }
  1381. }