123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215 |
- 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 static Android.Provider.MediaStore;
- 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();
- }
- }
- #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 async void ExistingKanbanTrack()
- {
- await Task.Run(() =>
- {
- try
- {
- kanban = new Client<Kanban>().Load(
- new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanID)).FirstOrDefault();
- }
- catch (ArgumentException e) when (kanban == null)
- {
- Device.BeginInvokeOnMainThread(() => { DisplayAlert("Error", "Failed to load from database", "Ok"); });
- }
- UpdateImages();
- UpdateScreen();
- });
- }
- 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;
- 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 async void CheckForDigitalForms()
- {
- if (newKanban) return;
- formsBtn.Text = "Checking";
- await Task.Run(() =>
- {
- kanbanFormList.Clear();
- try
- {
- CoreTable table = 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)
- );
- 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
- { }
- });
- }
- #endregion
- #region Fields Changed
- 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(async () =>
- {
- if (kanban.Attachments != 0)
- {
- int count = kanban.Attachments;
- photosLbl.TextColor = Color.Orange;
- photosLbl.Text = "Loading " + kanban.Attachments + " Photos";
- await Task.Run(() =>
- {
- var table = new Client<KanbanDocument>().Query(
- new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID),
- new Columns<KanbanDocument>(x => x.DocumentLink.ID),
- null
- );
- if (table.Rows.Count != 0)
- {
- foreach (var row in table.Rows)
- {
- List<object> list = row.Values;
- if (list[0] == null) { list[0] = Guid.Empty; }
- Guid kanbanDocLinkID = Guid.Parse(list[0].ToString());
- new Client<Document>().Query(
- new Filter<Document>(x => x.ID).IsEqualTo(kanbanDocLinkID),
- null,
- null,
- (t, e) =>
- {
- CoreRow docrow = t.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 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 = 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)
- );
- 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);
- }
- }
- #endregion
- #region Submit btn + photos save
- async void SubmitBtn_Clicked()
- {
- try
- {
- if (searching)
- return;
- else
- {
- searching = true;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
- {
- new Client<Kanban>().Save(kanban, "Updated From Mobile Device");
- if (imagesourcedocs.Values.Count != 0)
- {
- new Client<Document>().Save(imagesourcedocs.Values, "Photo Taken on Device");
- }
- var task = 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 async void SavePhotos()
- {
- try
- {
- if (imagesourcedocs.Values.Count != 0)
- {
- ObservableList<KanbanDocument> newKanbanDocuments = new ObservableList<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(() =>
- {
- new Client<KanbanDocument>().Save(newKanbanDocuments, "Photo Taken on Device");
- });
- }
- }
- catch { }
- }
- #endregion
- #region Subscribers Buttons / Functionality
- private async void DisplayObserverList()
- {
- try
- {
- await Task.Run(() =>
- {
- if (!newKanban)
- {
- CoreTable table = new Client<KanbanSubscriber>().Query(
- new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(kanban.ID).And(x => x.Observer).IsEqualTo(true)
- );
- foreach (CoreRow row in table.Rows)
- {
- KanbanSubscriber subscriber = row.ToObject<KanbanSubscriber>();
- if (!subscriber.Manager && !subscriber.Assignee)
- {
- observerList.Add(subscriber);
- AddSubscriberLabel(subscriber);
- }
- }
- }
- });
- }
- catch { }
- }
- 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);
- }
- new Client<KanbanSubscriber>().Save(subscribers, "");
- }
- catch { }
- }
- 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 = new Client<KanbanSubscriber>().Query(
- new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(kanban.ID)
- );
- 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);
- }
- }
- new Client<KanbanSubscriber>().Save(subscribersToSave, "Updated from mobile device");
- new Client<KanbanSubscriber>().Delete(subscribersToDelete, "Updated from mobile device");
- }
- catch { }
- }
- #endregion
- }
- }
|