AddEditTask.xaml.cs 48 KB

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