| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using comal.timesheets.Tasks;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI;
- using XF.Material.Forms.UI.Dialogs;
- using Plugin.Media;
- using InABox.Mobile;
- using System.IO;
- using comal.timesheets.QAForms;
- using comal.timesheets.CustomControls;
- using PRSSecurity = InABox.Core.Security;
- using Xamarin.Essentials;
- namespace comal.timesheets.Tasks
- {
- public delegate void TaskSavedEvent(int TaskNumber);
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class AddEditTask : ContentPage
- {
- public Kanban kanban = new Kanban();
- bool newKanban = false;
- bool searching = false;
- bool displaying = false;
- List<KanbanForm> kanbanFormList = new List<KanbanForm>();
- List<KanbanSubscriber> observerList = new List<KanbanSubscriber>();
- Guid kanbanID = Guid.Empty;
- int estimatedTime;
- List<Image> imageList = new List<Image>();
- Dictionary<ImageSource, Document> imagesourcedocs = new Dictionary<ImageSource, Document>();
- public TaskSavedEvent OnTaskSaved;
- string kanbanTitle = "";
- public AddEditTask(Guid selectedID = default(Guid), string title = "")
- {
- InitializeComponent();
- kanbanID = selectedID;
- Title = "Loading";
- AddToolBars();
- kanbanTitle = title;
- if (selectedID == Guid.Empty)
- {
- NewKanbanTrack();
- UpdateScreen();
- }
- else
- {
- ExistingKanbanTrack();
- if (PRSSecurity.IsAllowed<CanShareTaskDetails>())
- shareBtn.IsVisible = true;
- }
- }
- #region OnAppearing and Screen Population
- protected override void OnAppearing()
- {
- base.OnAppearing();
- searching = false;
- CheckForDigitalForms();
- }
- private void NewKanbanTrack()
- {
- newKanban = true;
- kanban.DueDate = DateTime.Today;
- kanban.Category = "Open";
- kanban.StartDate = DateTime.Today;
- kanban.EmployeeLink.ID = GlobalVariables.EmpID;
- kanban.ManagerLink.ID = GlobalVariables.EmpID;
- kanban.EmployeeLink.Name = GlobalVariables.EmpName;
- kanban.ManagerLink.Name = GlobalVariables.EmpName;
- kanban.Title = kanbanTitle;
- }
- private void ExistingKanbanTrack()
- {
- Task.Run(() =>
- {
- CoreTable table = QueryKanban();
- while (table == null)
- table = QueryKanban();
- kanban = table.Rows.FirstOrDefault().ToObject<Kanban>();
- UpdateImages();
- UpdateScreen();
- });
- }
- private CoreTable QueryKanban()
- {
- try
- {
- return new Client<Kanban>().Query(
- new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanID),
- new Columns<Kanban>(
- x => x.ID,
- x => x.Title,
- x => x.Category,
- x => x.StartDate,
- x => x.Number,
- x => x.Notes,
- x => x.DueDate,
- x => x.JobLink.ID,
- x => x.JobLink.Name,
- x => x.JobLink.JobNumber,
- x => x.Private,
- x => x.Description,
- x => x.Summary,
- x => x.Type.Description,
- x => x.EmployeeLink.ID,
- x => x.EmployeeLink.Name,
- x => x.EmployeeLink.Code,
- x => x.ManagerLink.ID,
- x => x.ManagerLink.Name,
- x => x.ManagerLink.Code,
- x => x.EstimatedTime,
- x => x.Completed,
- x => x.ActualTime,
- x => x.Locked,
- x => x.Closed,
- x => x.Attachments,
- x => x.Delivery.ID
- )
- );
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, "QueryKanban()", ex.Message + ex.StackTrace, this.GetType().Name);
- return null;
- }
- }
- private void AddToolBars()
- {
- NavigationPage.SetHasBackButton(this, false);
- ToolbarItems.Add(new ToolbarItem("Cancel", "", () =>
- {
- Navigation.PopAsync();
- }));
- ToolbarItems.Add(new ToolbarItem(" ", "", () =>
- {
- //button added to create space on toolbar
- }));
- ToolbarItems.Add(new ToolbarItem("Save", "", () =>
- {
- SubmitBtn_Clicked();
- }));
- }
- public void UpdateScreen(bool lockTaskType = false)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- if (newKanban)
- {
- Title = "New Task";
- }
- else
- {
- Title = "Task " + kanban.Number;
- }
- titleEdt.Text = kanban.Title;
- jobNoLbl.Text = (kanban.JobLink.JobNumber + " " + kanban.JobLink.Name);
- descriptionEdt.Text = kanban.Summary;
- descriptionEdt.IsEnabled = kanban.ID == Guid.Empty ? true : false;
- existingNotesLbl.Text = BuildNotes(kanban.Notes);
- taskTypeLbl.Text = kanban.Type.Description;
- if (lockTaskType)
- taskTypeBtn.IsEnabled = false;
- assignedToLbl.Text = kanban.EmployeeLink.Name;
- allocatedByLbl.Text = kanban.ManagerLink.Name;
- categoryPck.SelectedIndex = chooseIndex();
- dueDatePck.Date = kanban.DueDate;
- startDatePck.Date = kanban.StartDate;
- DisplayEstimatedTime();
- DisplayObserverList();
- if (kanban.Private)
- {
- privateCheckBox.IsChecked = true;
- }
- if (kanban.Locked)
- {
- categoryPck.IsEnabled = false;
- }
- });
- }
- private string BuildNotes(string[] notes)
- {
- string result = "";
- foreach (string note in notes)
- result = result + note + System.Environment.NewLine;
- return result;
- }
- private void AddNotes_Clicked(object sender, EventArgs e)
- {
- if (kanban.Notes.Count() == 0)
- {
- kanban.Notes = new string[] { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + GlobalVariables.EmpName + ": " + notesEdt.Text };
- notesEdt.Text = "";
- }
- else
- {
- var list = kanban.Notes.ToList();
- list.Add("===================================");
- list.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + GlobalVariables.EmpName + ": " + notesEdt.Text);
- kanban.Notes = list.ToArray();
- notesEdt.Text = "";
- }
- UpdateScreen();
- }
- private void NotesEdt_TextChanged(object sender, EventArgs e)
- {
- addNotesBtn.IsEnabled = string.IsNullOrWhiteSpace(notesEdt.Text) ? false : true;
- }
- private async void CheckForDigitalForms()
- {
- if (newKanban) return;
- formsBtn.Text = "Checking";
- await Task.Run(() =>
- {
- kanbanFormList.Clear();
- try
- {
- CoreTable table = QueryKanbanForms();
- while (table == null)
- table = QueryKanbanForms();
- if (table.Rows.Any())
- {
- foreach (CoreRow row in table.Rows)
- {
- KanbanForm kanbanForm = row.ToObject<KanbanForm>();
- kanbanFormList.Add(kanbanForm);
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- formsBtn.Text = "Forms";
- formsBtn.IsEnabled = true;
- });
- }
- else
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- formsBtn.Text = "Forms";
- formsBtn.IsEnabled = true;
- });
- }
- }
- catch
- { }
- });
- }
- private CoreTable QueryKanbanForms()
- {
- try
- {
- return new Client<KanbanForm>().Query(
- new Filter<KanbanForm>(x => x.Parent.ID).IsEqualTo(kanbanID),
- new Columns<KanbanForm>(
- x => x.ID,
- x => x.Parent.ID,
- x => x.Form.ID,
- x => x.Form.Description,
- x => x.Form.AppliesTo,
- x => x.Created,
- x => x.FormData,
- x => x.BlobData,
- x => x.FormCompleted,
- x => x.FormCompletedBy.ID,
- x => x.FormCompletedBy.UserID,
- x => x.FormOpen,
- x => x.FormStarted
- ),
- new SortOrder<KanbanForm>(x => x.Created)
- );
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, "QueryKanbanForms()", ex.Message + ex.StackTrace, this.GetType().Name);
- return null;
- }
- }
- #endregion
- #region Fields Changed
- private async void ShareBtn_Clicked(object sender, EventArgs e)
- {
- try
- {
- CoreTable table = QueryKanban();
- while (table == null)
- table = QueryKanban();
- if (table.Rows.Any())
- {
- var detail = KanbanGrid.GenerateDetail(table.Rows.First());
- var message = new EmailMessage
- {
- Subject = "Task Details shared from: " + GlobalVariables.EmpName,
- Body = detail,
- };
- await Xamarin.Essentials.Email.ComposeAsync(message);
- }
- }
- catch { }
- }
- private void TitleEdt_Changed(object sender, EventArgs e)
- {
- kanban.Title = titleEdt.Text;
- }
- private void DescriptionEdt_Changed(object sender, EventArgs e)
- {
- kanban.Description = descriptionEdt.Text;
- }
- private void DueDatePck_Selected(object sender, EventArgs e)
- {
- kanban.DueDate = dueDatePck.Date;
- }
- private void StartDatePck_Selected(object sender, EventArgs e)
- {
- kanban.StartDate = startDatePck.Date;
- }
- private void JobNoBtn_Clicked(object sender, EventArgs e)
- {
- if (searching)
- return;
- else
- {
- searching = true;
- JobSelectionPage jobSelectionPage = new JobSelectionPage();
- jobSelectionPage.OnItemSelected += (() =>
- {
- kanban.JobLink.ID = jobSelectionPage.Job.ID;
- kanban.JobLink.Name = jobSelectionPage.Job.Name;
- kanban.JobLink.JobNumber = jobSelectionPage.Job.JobNumber;
- UpdateScreen();
- });
- Navigation.PushAsync(jobSelectionPage);
- }
- }
- private void TaskType_Clicked(object sender, EventArgs e)
- {
- if (searching)
- return;
- else
- {
- searching = true;
- GenericSelectionPage page = new GenericSelectionPage
- (
- "Select Type",
- new SelectionViewModel<KanbanType>
- (
- new Filter<KanbanType>(x => x.Hidden).IsEqualTo(false),
- new Expression<Func<KanbanType, object>>[] { x => x.Description },
- new Expression<Func<KanbanType, object>>[] { x => x.Hidden },
- new SortOrder<KanbanType>(x => x.Description)
- ));
- page.OnItemSelected += (row) =>
- {
- var kanbanType = row.ToObject<KanbanType>();
- kanban.Type.ID = kanbanType.ID;
- kanban.Type.Synchronise(kanbanType);
- UpdateScreen();
- };
- Navigation.PushAsync(page);
- }
- }
- private void AllocatedByBtn_Clicked(object sender, EventArgs e)
- {
- EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage();
- employeeSelectionPage.OnItemSelected += (() =>
- {
- kanban.ManagerLink.ID = employeeSelectionPage.employee.ID;
- kanban.ManagerLink.Name = employeeSelectionPage.employee.Name;
- UpdateScreen();
- });
- Navigation.PushAsync(employeeSelectionPage);
- }
- private void AssignedToBtn_Clicked(object sender, EventArgs e)
- {
- EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage();
- employeeSelectionPage.OnItemSelected += (() =>
- {
- kanban.EmployeeLink.ID = employeeSelectionPage.employee.ID;
- kanban.EmployeeLink.Name = employeeSelectionPage.employee.Name;
- UpdateScreen();
- });
- Navigation.PushAsync(employeeSelectionPage);
- }
- private void CheckPrivateChanged(object sender, CheckedChangedEventArgs e)
- {
- if (privateCheckBox.IsChecked)
- {
- Employee employee = new Employee();
- var table = new Client<Employee>().Query(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid));
- foreach (CoreRow row in table.Rows)
- {
- employee = row.ToObject<Employee>();
- }
- kanban.ManagerLink.ID = employee.ID;
- kanban.ManagerLink.Synchronise(employee);
- kanban.EmployeeLink.ID = employee.ID;
- kanban.EmployeeLink.Synchronise(employee);
- kanban.Private = true;
- assignedToBtn.IsEnabled = false;
- allocatedByBtn.IsEnabled = false;
- UpdateScreen();
- }
- if (!privateCheckBox.IsChecked)
- {
- kanban.Private = false;
- assignedToBtn.IsEnabled = true;
- allocatedByBtn.IsEnabled = true;
- }
- }
- private void category_Changed(object sender, EventArgs e)
- {
- if (categoryPck.SelectedIndex == 0)
- {
- kanban.Category = "Open";
- }
- if (categoryPck.SelectedIndex == 1)
- {
- kanban.Category = "In Progress";
- }
- if (categoryPck.SelectedIndex == 2)
- {
- kanban.Category = "Waiting";
- }
- if (categoryPck.SelectedIndex == 3)
- {
- kanban.Category = "Complete";
- }
- }
- private int chooseIndex()
- {
- int indexNo = -1;
- if (kanban.Category != null)
- {
- if (kanban.Category.Equals("Open"))
- {
- indexNo = 0;
- }
- if (kanban.Category.Equals("In Progress"))
- {
- indexNo = 1;
- }
- if (kanban.Category.Equals("Waiting"))
- {
- indexNo = 2;
- }
- if (kanban.Category.Equals("Complete"))
- {
- indexNo = 3;
- }
- }
- return indexNo;
- }
- #endregion
- #region Estimated Time
- private void DecreaseBtn_Clicked(object sender, EventArgs e)
- {
- if (estimatedTime == 0 || estimatedTime < 0)
- return;
- else
- {
- estimatedTime = estimatedTime - 15;
- kanban.EstimatedTime = new TimeSpan(0, estimatedTime, 0);
- DisplayEstimatedTime();
- }
- }
- private void IncreaseBtn_Clicked(object sender, EventArgs e)
- {
- estimatedTime = estimatedTime + 15;
- kanban.EstimatedTime = new TimeSpan(0, estimatedTime, 0);
- DisplayEstimatedTime();
- }
- private void EstimatedHoursEdt_Changed(object sender, EventArgs e)
- {
- if (displaying)
- return;
- else
- CalculateEstimatedTime();
- }
- private void EstimatedMinsEdt_Changed(object sender, EventArgs e)
- {
- if (displaying)
- return;
- else
- CalculateEstimatedTime();
- }
- private async void CalculateEstimatedTime() //to timespan
- {
- try
- {
- int minutes = 0;
- int hours = 0;
- if (!string.IsNullOrWhiteSpace(estimatedHoursEdt.Text))
- {
- hours = Convert.ToInt32(estimatedHoursEdt.Text);
- }
- if (!string.IsNullOrWhiteSpace(estimatedMinsEdt.Text))
- {
- minutes = Convert.ToInt32(estimatedMinsEdt.Text);
- }
- kanban.EstimatedTime = new TimeSpan(hours, minutes, 0);
- estimatedTime = Convert.ToInt32(kanban.EstimatedTime.TotalMinutes);
- }
- catch
- {
- await DisplayAlert("Error", "Only whole numbers for estimated time fields", "OK");
- int isNumber;
- if (!int.TryParse(estimatedHoursEdt.Text, out isNumber))
- {
- estimatedHoursEdt.Text = "0";
- };
- if (!int.TryParse(estimatedMinsEdt.Text, out isNumber))
- {
- estimatedMinsEdt.Text = "0";
- };
- }
- }
- private async void DisplayEstimatedTime() //from timespan
- {
- await Task.Run(() =>
- {
- displaying = true;
- estimatedTime = Convert.ToInt32(kanban.EstimatedTime.TotalMinutes);
- int hours = estimatedTime / 60;
- int minutes = estimatedTime % 60;
- Device.BeginInvokeOnMainThread(() =>
- {
- estimatedHoursEdt.Text = hours.ToString();
- estimatedMinsEdt.Text = minutes.ToString();
- displaying = false;
- });
- });
- }
- #endregion
- #region Display or add images
- private async void UpdateImages()
- {
- try
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- if (kanban.Attachments != 0)
- {
- int count = kanban.Attachments;
- photosLbl.TextColor = Color.Orange;
- photosLbl.Text = "Loading " + kanban.Attachments + " Photos";
- Task.Run(() =>
- {
- var table = QueryKanbanDocuments();
- while (table == null)
- table = QueryKanbanDocuments();
- if (table.Rows.Count != 0)
- {
- foreach (var row in table.Rows)
- {
- CoreTable docstable = QueryDocument(row.Get<KanbanDocument, Guid>(x => x.DocumentLink.ID));
- while (docstable == null)
- docstable = QueryDocument(row.Get<KanbanDocument, Guid>(x => x.DocumentLink.ID));
- CoreRow docrow = docstable.Rows.FirstOrDefault();
- if (docrow != null)
- {
- byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
- ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
- Image img = new Image();
- img.HeightRequest = 150;
- img.WidthRequest = 150;
- img.Aspect = Aspect.AspectFit;
- img.Source = src;
- img.GestureRecognizers.Add(new TapGestureRecognizer
- {
- Command = new Command(OnTap),
- CommandParameter = src,
- NumberOfTapsRequired = 1
- });
- imageList.Add(img);
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- count = count - 1;
- photosLbl.Text = "Loading " + count + " Photo(s)";
- if (count == 0)
- {
- photosLbl.Text = "Photos";
- photosLbl.TextColor = Color.Default;
- }
- });
- }
- }
- }
- });
- }
- });
- }
- catch { }
- }
- private CoreTable QueryKanbanDocuments()
- {
- try
- {
- return new Client<KanbanDocument>().Query(
- new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID),
- new Columns<KanbanDocument>(x => x.DocumentLink.ID),
- null
- );
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, "QueryKanbanDocuments()", ex.Message + ex.StackTrace, this.GetType().Name);
- return null;
- }
- }
- private CoreTable QueryDocument(Guid id)
- {
- try
- {
- return new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(id),
- new Columns<Document>(x => x.Data));
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, "QueryDocument()", ex.Message + ex.StackTrace, this.GetType().Name);
- return null;
- }
- }
- private void OnTap(object obj)
- {
- ImageViewerEditor imageViewEditor = new ImageViewerEditor(obj as ImageSource);
- imageViewEditor.OnSaveSelected += (byte[] array) =>
- {
- try
- {
- Image img = imageList.Find(x => x.Source.Equals(obj as ImageSource));
- imageList.Remove(img);
- imagesourcedocs.Remove(obj as ImageSource);
- }
- catch { }
- DataToImage(array);
- };
- Navigation.PushAsync(imageViewEditor);
- }
- public void DataToImage(byte[] data)
- {
- try
- {
- ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
- Image img = new Image();
- img.HeightRequest = 150;
- img.WidthRequest = 150;
- img.Aspect = Aspect.AspectFit;
- img.VerticalOptions = LayoutOptions.FillAndExpand;
- img.HorizontalOptions = LayoutOptions.FillAndExpand;
- img.Source = src;
- img.GestureRecognizers.Add(new TapGestureRecognizer
- {
- Command = new Command(OnTap),
- CommandParameter = src,
- NumberOfTapsRequired = 1
- });
- if (img != null)
- {
- imageList.Add(img);
- RefreshView();
- }
- String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
- Document doc = new Document()
- {
- FileName = filename,
- Data = data,
- CRC = CoreUtils.CalculateCRC(data),
- TimeStamp = DateTime.Now
- };
- imagesourcedocs.Add(src, doc);
- }
- catch
- { }
- }
- private void RefreshView()
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- images.Children.Clear();
- foreach (Image img in imageList)
- {
- images.Children.Add(img);
- }
- });
- }
- async void TakePhoto_Clicked(object sender, EventArgs e)
- {
- try
- {
- await CrossMedia.Current.Initialize();
- if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
- {
- await DisplayAlert("No Camera", ":( No camera available.", "OK");
- return;
- }
- String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
- var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
- {
- Name = filename,
- CompressionQuality = 10,
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
- });
- if (file == null)
- return;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- Image img = null;
- var memoryStream = new MemoryStream();
- file.GetStream().CopyTo(memoryStream);
- var data = memoryStream.ToArray();
- Document doc = new Document()
- {
- FileName = filename,
- Data = data,
- CRC = CoreUtils.CalculateCRC(data),
- TimeStamp = DateTime.Now
- };
- ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
- imagesourcedocs.Add(src, doc);
- img = new Image();
- img.HeightRequest = 150;
- img.WidthRequest = 150;
- img.Aspect = Aspect.AspectFit;
- img.Source = src;
- img.GestureRecognizers.Add(new TapGestureRecognizer
- {
- Command = new Command(OnTap),
- CommandParameter = src,
- NumberOfTapsRequired = 1
- });
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- });
- }
- await pageScroller.ScrollToAsync(photoFrame, ScrollToPosition.Center, false);
- }
- }
- catch { }
- }
- async void ChooseImage_Clicked(object sender, EventArgs e)
- {
- try
- {
- await CrossMedia.Current.Initialize();
- if (!CrossMedia.Current.IsPickPhotoSupported)
- {
- await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
- return;
- }
- var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
- {
- CompressionQuality = 10,
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
- });
- if (file == null)
- return;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- Image img = null;
- var memoryStream = new MemoryStream();
- file.GetStream().CopyTo(memoryStream);
- var data = memoryStream.ToArray();
- Document doc = new Document()
- {
- FileName = Path.GetFileName(file.Path),
- Data = data,
- CRC = CoreUtils.CalculateCRC(data),
- TimeStamp = DateTime.Now
- };
- ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
- imagesourcedocs.Add(src, doc);
- img = new Image();
- img.HeightRequest = 150;
- img.WidthRequest = 150;
- img.Aspect = Aspect.AspectFit;
- img.Source = src;
- img.GestureRecognizers.Add(new TapGestureRecognizer
- {
- Command = new Command(OnTap),
- CommandParameter = src,
- NumberOfTapsRequired = 1
- });
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- });
- }
- await pageScroller.ScrollToAsync(photoFrame, ScrollToPosition.Center, false);
- }
- }
- catch { }
- }
- #endregion
- #region Digital Forms
- private async void Forms_Clicked(object sender, EventArgs e)
- {
- try
- {
- string chosenOptionOne = "";
- if (kanbanFormList.Count == 0)
- {
- chosenOptionOne = await DisplayActionSheet("Choose An Option", "Cancel", null, "Add Form to Task");
- }
- else if (kanbanFormList.Count > 0)
- {
- chosenOptionOne = await DisplayActionSheet("Choose An Option", "Cancel", null, "Add Form to Task", "View Form(s)");
- }
- switch (chosenOptionOne)
- {
- case "Cancel":
- return;
- case "Add Form to Task":
- DigitalFormsPicker digitalFormPicker = new DigitalFormsPicker(kanban);
- Navigation.PushAsync(digitalFormPicker);
- break;
- case "View Form(s)":
- ChooseForm();
- break;
- default: break;
- }
- }
- catch { }
- }
- private async void ChooseForm()
- {
- ListSelectionPage page = new ListSelectionPage(CreatePairs(), "Forms");
- page.OnDictionaryItemTapped += (id, value) => { LaunchForm(id); };
- Navigation.PushAsync(page);
- }
- private Dictionary<Guid, string> CreatePairs()
- {
- Dictionary<Guid, string> pairs = new Dictionary<Guid, string>();
- foreach (KanbanForm kanbanForm in kanbanFormList)
- {
- string formDescription = CreateDescription(kanbanForm);
- pairs.Add(kanbanForm.ID, formDescription);
- }
- return pairs;
- }
- private string CreateDescription(KanbanForm kanbanForm)
- {
- string formDescription = kanbanForm.Form.Description;
- if (kanbanForm.FormCompleted != DateTime.MinValue)
- formDescription = formDescription
- + " (Completed: "
- + kanbanForm.FormCompleted.ToString("hh:mm - dd MMM yy")
- + " by "
- + kanbanForm.FormCompletedBy.UserID
- + ")";
- else
- formDescription = formDescription
- + " (Created: "
- + kanbanForm.Created.ToString("hh:mm - dd MMM yy")
- + ")";
- return formDescription;
- }
- private async void LaunchForm(Guid id)
- {
- KanbanForm form = kanbanFormList.FirstOrDefault(x => x.ID == id);
- CoreTable table = QueryDigitalFormLayout(form);
- while (table == null)
- table = QueryDigitalFormLayout(form);
- CoreRow row = table.Rows.FirstOrDefault();
- DigitalFormLayout layout = row.ToObject<DigitalFormLayout>();
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
- {
- DigitalFormHost host = new DigitalFormHost(
- DigitalFormsHelper.LoadModel(
- layout, typeof(KanbanForm), kanban, Guid.Empty,
- new ExistingFormShell
- {
- ID = form.ID,
- ParentID = kanban.ID,
- Type = typeof(KanbanForm),
- FormID = form.ID,
- }
- ));
- Navigation.PushAsync(host);
- }
- }
- private CoreTable QueryDigitalFormLayout(KanbanForm form)
- {
- try
- {
- return new Client<DigitalFormLayout>().Query(
- new Filter<DigitalFormLayout>(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Description).IsEqualTo(form.Form.Description),
- new Columns<DigitalFormLayout>(x => x.Description, x => x.ID, x => x.Code, x => x.Form.AppliesTo, x => x.Form.ID, x => x.Layout),
- new SortOrder<DigitalFormLayout>(x => x.Description)
- );
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, "QueryDigitalFormLayout()", ex.Message + ex.StackTrace, this.GetType().Name);
- return null;
- }
- }
- #endregion
- #region Submit btn + photos save
- private async void SubmitBtn_Clicked()
- {
- try
- {
- if (searching)
- return;
- else
- {
- searching = true;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
- {
- SaveKanban();
- SaveDocuments();
- Task.Run(() => { SaveSubscribers(); });
- SavePhotos();
- }
- string successMessage = "Task number : " + kanban.Number + System.Environment.NewLine
- + "New Photo(s): " + imagesourcedocs.Values.Count;
- await DisplayAlert("Success", successMessage, "OK");
- OnTaskSaved?.Invoke(kanban.Number);
- await Navigation.PopAsync();
- }
- }
- catch (Exception ex)
- {
- DisplayAlert("Error saving", ex.Message, "OK");
- }
- }
- private void SaveKanban()
- {
- try
- {
- new Client<Kanban>().Save(kanban, "Updated From Mobile Device");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, "SaveKanban()", ex.Message + ex.StackTrace, this.GetType().Name);
- SaveKanban();
- }
- }
- private void SaveDocuments()
- {
- try
- {
- if (imagesourcedocs.Values.Count != 0)
- new Client<Document>().Save(imagesourcedocs.Values, "Photo Taken on Device");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, "SaveDocuments()", ex.Message + ex.StackTrace, this.GetType().Name);
- SaveDocuments();
- }
- }
- private void SavePhotos()
- {
- try
- {
- if (imagesourcedocs.Values.Count != 0)
- {
- List<KanbanDocument> newKanbanDocuments = new List<KanbanDocument>();
- foreach (Document doc in imagesourcedocs.Values)
- {
- var kanbanDocument = new KanbanDocument();
- kanbanDocument.EntityLink.ID = kanban.ID;
- kanbanDocument.DocumentLink.ID = doc.ID;
- kanbanDocument.DocumentLink.FileName = doc.FileName;
- newKanbanDocuments.Add(kanbanDocument);
- }
- Task.Run(() =>
- {
- SaveKanbanDocuments(newKanbanDocuments);
- });
- }
- }
- catch { }
- }
- private void SaveKanbanDocuments(List<KanbanDocument> newKanbanDocuments)
- {
- try
- {
- new Client<KanbanDocument>().Save(newKanbanDocuments, "Photo Taken on Device");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, typeof(KanbanDocument).ToString(), ex.Message, this.GetType().Name);
- SaveKanbanDocuments(newKanbanDocuments);
- }
- }
- #endregion
- #region Subscribers Buttons / Functionality
- private async void DisplayObserverList()
- {
- try
- {
- await Task.Run(() =>
- {
- if (!newKanban)
- {
- CoreTable table = QueryObservers();
- while (table == null)
- table = QueryObservers();
- foreach (CoreRow row in table.Rows)
- {
- KanbanSubscriber subscriber = row.ToObject<KanbanSubscriber>();
- if (!subscriber.Manager && !subscriber.Assignee)
- {
- observerList.Add(subscriber);
- AddSubscriberLabel(subscriber);
- }
- }
- }
- });
- }
- catch { }
- }
- private CoreTable QueryObservers()
- {
- try
- {
- return new Client<KanbanSubscriber>().Query(
- new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(kanban.ID).And(x => x.Observer).IsEqualTo(true)
- );
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, "QueryObservers()", ex.Message + ex.StackTrace, this.GetType().Name);
- return null;
- }
- }
- private void AddSubscriberLabel(KanbanSubscriber subscriber)
- {
- Label label = new Label();
- label.Text = subscriber.Employee.Name;
- label.FontSize = Device.GetNamedSize(NamedSize.Medium, label);
- label.Margin = 5;
- label.HorizontalTextAlignment = TextAlignment.Start;
- label.VerticalTextAlignment = TextAlignment.Center;
- Device.BeginInvokeOnMainThread(() =>
- {
- observerStackLayout.Children.Add(label);
- });
- }
- private async void AddSubscriberBtn_Clicked(object sender, EventArgs e)
- {
- string chosenOption = await DisplayActionSheet("Add", "Cancel", null, "Person", "Team");
- switch (chosenOption)
- {
- case "Cancel":
- break;
- case "Person":
- SelectEmployeeForSubscriber();
- break;
- case "Team":
- SelectTeamForSubscriber();
- break;
- default:
- break;
- }
- }
- private void SelectEmployeeForSubscriber()
- {
- EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage();
- employeeSelectionPage.OnItemSelected += (() =>
- {
- KanbanSubscriber subscriber = new KanbanSubscriber();
- subscriber.Kanban.ID = kanban.ID;
- subscriber.Observer = true;
- subscriber.Employee.ID = employeeSelectionPage.employee.ID;
- subscriber.Employee.Name = employeeSelectionPage.employee.Name;
- CheckListAndAddSubscriber(subscriber);
- });
- Navigation.PushAsync(employeeSelectionPage);
- }
- private async void SelectTeamForSubscriber()
- {
- string[] array = GlobalVariables.TeamNames.ToArray<string>();
- string chosenTeam = await DisplayActionSheet("Choose Team", "Cancel", null, array);
- switch (chosenTeam)
- {
- case "Cancel":
- return;
- break;
- }
- if (!string.IsNullOrWhiteSpace(chosenTeam))
- {
- List<EmployeeShell> employees = GlobalVariables.TeamEmployeeShells.Where(x => x.TeamName == chosenTeam).ToList();
- foreach (EmployeeShell employee in employees)
- {
- KanbanSubscriber subscriber = new KanbanSubscriber();
- subscriber.Kanban.ID = kanban.ID;
- subscriber.Employee.ID = employee.ID;
- subscriber.Employee.Name = employee.Name;
- subscriber.Observer = true;
- CheckListAndAddSubscriber(subscriber);
- }
- }
- }
- private void CheckListAndAddSubscriber(KanbanSubscriber subscriber)
- {
- List<Guid> guids = new List<Guid>();
- foreach (KanbanSubscriber sub in observerList)
- {
- guids.Add(sub.Employee.ID);
- }
- if (!guids.Contains(subscriber.Employee.ID))
- {
- if (subscriber.Employee.ID != kanban.EmployeeLink.ID)
- {
- if (subscriber.Employee.ID != kanban.ManagerLink.ID)
- {
- observerList.Add(subscriber);
- AddSubscriberLabel(subscriber);
- }
- }
- }
- }
- private async void RemoveSubscriberBtn_Clicked(object sender, EventArgs e)
- {
- try
- {
- Dictionary<string, KanbanSubscriber> nameSubscriberPairs = new Dictionary<string, KanbanSubscriber>();
- foreach (KanbanSubscriber subscriber in observerList)
- {
- nameSubscriberPairs.Add(subscriber.Employee.Name, subscriber);
- }
- string[] array = nameSubscriberPairs.Keys.ToArray();
- string chosenOption = await DisplayActionSheet("Remove", "Cancel", null, array);
- if (chosenOption == "Cancel" || string.IsNullOrWhiteSpace(chosenOption))
- {
- return;
- }
- else
- {
- KanbanSubscriber subscriber = nameSubscriberPairs[chosenOption];
- observerList.Remove(subscriber);
- observerStackLayout.Children.Clear();
- foreach (KanbanSubscriber sub in observerList)
- {
- AddSubscriberLabel(sub);
- }
- }
- }
- catch { }
- }
- #endregion
- #region Subscribers Saving
- private void SaveSubscribers()
- {
- Task.Run(() =>
- {
- if (newKanban)
- {
- SaveNewSubs();
- }
- else
- {
- SaveExistingSubs();
- }
- });
- }
- private void SaveNewSubs()
- {
- try
- {
- List<KanbanSubscriber> subscribers = new List<KanbanSubscriber>();
- KanbanSubscriber sub = null;
- if (kanban.EmployeeLink.ID != Guid.Empty)
- {
- sub = new KanbanSubscriber();
- sub.Kanban.ID = kanban.ID;
- sub.Employee.ID = kanban.EmployeeLink.ID;
- sub.Assignee = true;
- if (kanban.EmployeeLink.ID == kanban.ManagerLink.ID)
- {
- sub.Manager = true;
- }
- subscribers.Add(sub);
- }
- if (kanban.ManagerLink.ID != Guid.Empty)
- {
- if (kanban.ManagerLink.ID != kanban.EmployeeLink.ID)
- {
- sub = new KanbanSubscriber();
- sub.Kanban.ID = kanban.ID;
- sub.Employee.ID = kanban.ManagerLink.ID;
- sub.Manager = true;
- subscribers.Add(sub);
- }
- }
- foreach (KanbanSubscriber subscriber in observerList)
- {
- subscriber.Kanban.ID = kanban.ID;
- subscribers.Add(subscriber);
- }
- DoSaveSubscribers(subscribers);
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, typeof(KanbanSubscriber).ToString(), ex.Message, this.GetType().Name);
- }
- }
- private void DoSaveSubscribers(List<KanbanSubscriber> subscribers)
- {
- try
- {
- new Client<KanbanSubscriber>().Save(subscribers, "Updated from mobile device");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, typeof(KanbanSubscriber).ToString(), ex.Message, this.GetType().Name);
- DoSaveSubscribers(subscribers);
- }
- }
- private void SaveExistingSubs()
- {
- try
- {
- KanbanSubscriber oldAssignee = new KanbanSubscriber();
- KanbanSubscriber oldManager = new KanbanSubscriber();
- KanbanSubscriber oldBoth = new KanbanSubscriber();
- List<KanbanSubscriber> oldObservers = new List<KanbanSubscriber>();
- List<KanbanSubscriber> subscribersToDelete = new List<KanbanSubscriber>();
- List<KanbanSubscriber> subscribersToSave = new List<KanbanSubscriber>();
- List<KanbanSubscriber> subscribers = new List<KanbanSubscriber>();
- CoreTable table = QuerySubscribers();
- while (table == null)
- table = QuerySubscribers();
- foreach (CoreRow row in table.Rows)
- {
- KanbanSubscriber subscriber = row.ToObject<KanbanSubscriber>();
- if (subscriber.Assignee && subscriber.Manager)
- {
- oldBoth = subscriber;
- }
- else
- {
- if (subscriber.Assignee)
- {
- oldAssignee = subscriber;
- }
- else if (subscriber.Manager)
- {
- oldManager = subscriber;
- }
- else if (subscriber.Observer)
- {
- oldObservers.Add(subscriber);
- }
- }
- }
- if (kanban.ManagerLink.ID == kanban.EmployeeLink.ID)
- {
- if (kanban.ManagerLink.ID != oldBoth.Employee.ID && oldBoth.Employee.ID != Guid.Empty)
- {
- subscribersToDelete.Add(oldBoth);
- KanbanSubscriber subscriber = new KanbanSubscriber();
- subscriber.Assignee = true;
- subscriber.Manager = true;
- subscriber.Kanban.ID = kanban.ID;
- subscriber.Employee.ID = kanban.EmployeeLink.ID;
- subscribersToSave.Add(subscriber);
- }
- }
- if (oldAssignee.Employee.ID != kanban.EmployeeLink.ID && oldAssignee.Employee.ID != Guid.Empty)
- {
- subscribersToDelete.Add(oldAssignee);
- KanbanSubscriber subscriber = new KanbanSubscriber();
- subscriber.Assignee = true;
- subscriber.Manager = false;
- subscriber.Kanban.ID = kanban.ID;
- subscriber.Employee.ID = kanban.EmployeeLink.ID;
- subscribersToSave.Add(subscriber);
- }
- if (oldManager.Employee.ID != kanban.ManagerLink.ID && oldManager.Employee.ID != Guid.Empty)
- {
- subscribersToDelete.Add(oldManager);
- KanbanSubscriber subscriber = new KanbanSubscriber();
- subscriber.Assignee = false;
- subscriber.Manager = true;
- subscriber.Kanban.ID = kanban.ID;
- subscriber.Employee.ID = kanban.ManagerLink.ID;
- subscribersToSave.Add(subscriber);
- }
- List<Guid> oldGuids = new List<Guid>();
- List<Guid> newGuids = new List<Guid>();
- foreach (KanbanSubscriber sub in observerList)
- {
- newGuids.Add(sub.Employee.ID);
- }
- foreach (KanbanSubscriber sub in oldObservers)
- {
- oldGuids.Add(sub.Employee.ID);
- if (!newGuids.Contains(sub.Employee.ID))
- {
- subscribersToDelete.Add(sub);
- }
- }
- foreach (KanbanSubscriber sub in observerList)
- {
- if (!oldGuids.Contains(sub.Employee.ID))
- {
- subscribersToSave.Add(sub);
- }
- }
- DoSaveSubscribers(subscribersToSave);
- DeleteSubscribers(subscribersToDelete);
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, typeof(KanbanSubscriber).ToString(), ex.Message, this.GetType().Name);
- }
- }
- private CoreTable QuerySubscribers()
- {
- try
- {
- return new Client<KanbanSubscriber>().Query(
- new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(kanban.ID)
- );
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, typeof(KanbanSubscriber).ToString(), ex.Message, this.GetType().Name);
- return null;
- }
- }
- private void DeleteSubscribers(List<KanbanSubscriber> subscribersToDelete)
- {
- try
- {
- new Client<KanbanSubscriber>().Delete(subscribersToDelete, "Updated from mobile device");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Delete, typeof(KanbanSubscriber).ToString(), ex.Message, this.GetType().Name);
- DeleteSubscribers(subscribersToDelete);
- }
- }
- #endregion
- }
- }
|