| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- using System.IO;
- using PRSSecurity = InABox.Core.Security;
- using Xamarin.Essentials;
- namespace PRS.Mobile
- {
- public delegate void TaskSavedEvent(int TaskNumber);
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class AddEditTask
- {
- 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.Status = KanbanStatus.Open;
- kanban.StartDate = DateTime.Today;
- kanban.EmployeeLink.ID = App.Data.Me.ID;
- kanban.ManagerLink.ID = App.Data.Me.ID;
- kanban.EmployeeLink.Name = App.Data.Me.Name;
- kanban.ManagerLink.Name = App.Data.Me.Name;
- 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>(ColumnTypeFlags.None).Add(
- x => x.ID,
- x => x.Title,
- x => x.Status,
- 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)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- 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") + " " + App.Data.Me.Name + ": " + notesEdt.Text };
- notesEdt.Text = "";
- }
- else
- {
- var list = kanban.Notes.ToList();
- list.Add("===================================");
- list.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + App.Data.Me.Name + ": " + 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>(ColumnTypeFlags.None).Add(
- 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)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- 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 = GenerateDetail(table.Rows.First());
- var message = new EmailMessage
- {
- Subject = "Task Details shared from: " + App.Data.Me.Name,
- Body = detail,
- };
- await Xamarin.Essentials.Email.ComposeAsync(message);
- }
- }
- catch { }
- }
-
- private string GenerateDetail(CoreRow row)
- {
- string detail = "";
-
- if (!string.IsNullOrWhiteSpace(row.Get<Kanban, string>(x => x.Title)))
- detail = "TITLE: " + row.Get<Kanban, string>(x => x.Title) + System.Environment.NewLine + System.Environment.NewLine;
-
- if (!string.IsNullOrWhiteSpace(row.Get<Kanban, string>(x => x.Summary)))
- detail = detail + "SUMMARY: " + row.Get<Kanban, string>(x => x.Summary) + System.Environment.NewLine + System.Environment.NewLine;
-
- if (row.Get<Kanban, string[]>(x => x.Notes).Any())
- {
- detail = detail + "NOTES: ";
- foreach (var note in row.Get<Kanban, string[]>(x => x.Notes))
- detail = detail + note + System.Environment.NewLine;
- }
-
- if (!string.IsNullOrWhiteSpace(row.Get<Kanban, string>(x => x.ManagerLink.Name)))
- detail = detail + "MANAGER: " + row.Get<Kanban, string>(x => x.ManagerLink.Name) + System.Environment.NewLine + System.Environment.NewLine;
-
- detail = detail + "STATUS: " + row.Get<Kanban, KanbanStatus>(x => x.Status).ToString() + System.Environment.NewLine + System.Environment.NewLine;
-
- if (!string.IsNullOrWhiteSpace(row.Get<string>("IssueNumber")))
- detail = detail + "ISSUE NUMBER: " + row.Get<string>("IssueNumber") + System.Environment.NewLine + System.Environment.NewLine;
-
- if (row.Get<Kanban, DateTime>(x => x.Created) != DateTime.MinValue)
- detail = detail + "CREATED: " + row.Get<Kanban, DateTime>(x => x.Created).ToString("dd MMM yy") + System.Environment.NewLine + System.Environment.NewLine;
-
- if (row.Get<Kanban, DateTime>(x => x.StartDate) != DateTime.MinValue)
- detail = detail + "STARTED: " + row.Get<Kanban, DateTime>(x => x.StartDate).ToString("dd MMM yy") + System.Environment.NewLine + System.Environment.NewLine;
-
- if (row.Get<Kanban, DateTime>(x => x.DueDate) != DateTime.MinValue)
- detail = detail + "DUE: " + row.Get<Kanban, DateTime>(x => x.DueDate).ToString("dd MMM yy") + System.Environment.NewLine + System.Environment.NewLine;
-
- return detail;
- }
-
-
- 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(
- (job) =>
- {
- kanban.JobLink.ID = job.ID;
- kanban.JobLink.Name = job.Name;
- kanban.JobLink.JobNumber = 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 += (o,e) =>
- {
- var kanbanType = e.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(
- (employee) =>
- {
- kanban.ManagerLink.ID = employee.ID;
- kanban.ManagerLink.Name = employee.Name;
- UpdateScreen();
- }
- );
- Navigation.PushAsync(employeeSelectionPage);
- }
- private void AssignedToBtn_Clicked(object sender, EventArgs e)
- {
- EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage(
- (employee) =>
- {
- kanban.EmployeeLink.ID = employee.ID;
- kanban.EmployeeLink.Name = 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)
- {
- kanban.Status = (KanbanStatus)categoryPck.SelectedIndex;
- }
- private int chooseIndex()
- {
- return (int)kanban.Status;
- }
- #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>(ColumnTypeFlags.None).Add(x => x.DocumentLink.ID),
- null
- );
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- return null;
- }
- }
- private CoreTable QueryDocument(Guid id)
- {
- try
- {
- return new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(id),
- new Columns<Document>(ColumnTypeFlags.None).Add(x => x.Data));
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- return null;
- }
- }
- private void OnTap(object obj)
- {
- ImageViewerEditor imageViewEditor = new ImageViewerEditor(
- obj as ImageSource,
- (data) =>
- {
- try
- {
- Image img = imageList.Find(x => x.Source.Equals(obj as ImageSource));
- imageList.Remove(img);
- imagesourcedocs.Remove(obj as ImageSource);
- }
- catch { }
- DataToImage(data);
- },
- null
- );
- 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
- {
- var file = await MediaPicker.CapturePhotoAsync();
- if (file == null)
- return;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- Image img = null;
- var memoryStream = new MemoryStream();
- using (var stream = await file.OpenReadAsync())
- await stream.CopyToAsync(memoryStream);
- var data = memoryStream.ToArray();
- Document doc = new Document()
- {
- FileName = Path.GetFileName(file.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
- });
- 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
- {
- var file = await MediaPicker.PickPhotoAsync();
- if (file == null)
- return;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- Image img = null;
- var memoryStream = new MemoryStream();
- using (var stream = await file.OpenReadAsync())
- await stream.CopyToAsync(memoryStream);
- var data = memoryStream.ToArray();
- Document doc = new Document()
- {
- FileName = Path.GetFileName(file.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
- });
- 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", kanban.ID);
- 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>(ColumnTypeFlags.None).Add(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)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- 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)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- SaveKanban();
- }
- }
- private void SaveDocuments()
- {
- try
- {
- if (imagesourcedocs.Values.Count != 0)
- new Client<Document>().Save(imagesourcedocs.Values, "Photo Taken on Device");
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- 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)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- 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)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- 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(
- (employee) =>
- {
- KanbanSubscriber subscriber = new KanbanSubscriber();
- subscriber.Kanban.ID = kanban.ID;
- subscriber.Observer = true;
- subscriber.Employee.ID = employee.ID;
- subscriber.Employee.Name = employee.Name;
- CheckListAndAddSubscriber(subscriber);
- }
- );
- Navigation.PushAsync(employeeSelectionPage);
- }
- private async void SelectTeamForSubscriber()
- {
- string[] array = App.Data.EmployeeTeams.Select(x=>x.TeamName).Distinct().ToArray();
- string chosenTeam = await DisplayActionSheet("Choose Team", "Cancel", null, array);
- switch (chosenTeam)
- {
- case "Cancel":
- return;
- break;
- }
- if (!string.IsNullOrWhiteSpace(chosenTeam))
- {
- List<EmployeeTeamShell> employees = App.Data.EmployeeTeams.Where(x => x.TeamName == chosenTeam).ToList();
- foreach (var 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)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- }
- }
- private void DoSaveSubscribers(List<KanbanSubscriber> subscribers)
- {
- try
- {
- new Client<KanbanSubscriber>().Save(subscribers, "Updated from mobile device");
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- 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)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- }
- }
- private CoreTable QuerySubscribers()
- {
- try
- {
- return new Client<KanbanSubscriber>().Query(
- new Filter<KanbanSubscriber>(x => x.Kanban.ID).IsEqualTo(kanban.ID)
- );
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- return null;
- }
- }
- private void DeleteSubscribers(List<KanbanSubscriber> subscribersToDelete)
- {
- try
- {
- new Client<KanbanSubscriber>().Delete(subscribersToDelete, "Updated from mobile device");
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- DeleteSubscribers(subscribersToDelete);
- }
- }
- #endregion
- }
- }
|