| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 | using Comal.Classes;using comal.timesheets.QAForms;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Comal.Classes;using InABox.Clients;using InABox.Configuration;using InABox.Core;using System.Linq;using System.Threading;using System.Threading.Tasks;using comal.timesheets.Data_Classes;using Xamarin.Forms;using Xamarin.Forms.Xaml;using XF.Material.Forms.UI.Dialogs;namespace comal.timesheets{	[XamlCompilation(XamlCompilationOptions.Compile)]	public partial class DigitalFormsPicker : ContentPage	{        #region Fields        bool _searching = false;        Kanban addToTaskKanban = new Kanban();        List<DigitalFormLayoutShell> layouts = new List<DigitalFormLayoutShell>();        List<String> types = new List<String>();        private bool firstLoad = true;        private bool incompleteVisible = true;        private bool addingToTask = false;        Guid JobID = Guid.Empty;        List<ExistingFormShell> incompleteForms = new List<ExistingFormShell>();        List<ExistingFormShell> completeForms = new List<ExistingFormShell>();        #endregion        public DigitalFormsPicker(string appliesTo = "Kanban", Guid _jobid = new Guid()) //normal Forms Library - default is kanban type        {			InitializeComponent ();            JobID = _jobid;            NavigationPage.SetHasBackButton(this, false);            LoadScreen(appliesTo);        }        public DigitalFormsPicker(Kanban _kanban) //used for adding forms to a task        {            InitializeComponent();            addingToTask = true;            addToTaskKanban = _kanban;            NavigationPage.SetHasBackButton(this, false);            LoadScreen("Kanban");        }        private void ExitBtn_Clicked(object sender, EventArgs e)        {            Navigation.PopAsync();        }        #region OnAppearing and Loading Screen        protected override void OnAppearing()        {            try            {                _searching = false;                if (RetainedResults.IsFormRetained)                {                    DigitalFormHost host = new DigitalFormHost(LoadModel(RetainedResults.LastDigitalFormLayout, CheckType()), JobID);                    Navigation.PushAsync(host);                }                LoadExistingForms();            }            catch { }            base.OnAppearing();        }        private async void LoadScreen(string appliesTo)        {            try            {                await Task.Run(() =>                {                    types.Add("All");                    var forms = new Client<DigitalFormLayout>();                    Filter<DigitalFormLayout> filter = new Filter<DigitalFormLayout>(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Secure).IsEqualTo(false)                        .And(x => x.Form.Active).IsEqualTo(true);                    filter = filter.And(x => x.Form.AppliesTo).IsEqualTo(appliesTo);                    Columns<DigitalFormLayout> columns = new Columns<DigitalFormLayout>(                        x => x.Description, //0                        x => x.ID, //1                        x => x.Code, //2                        x => x.Form.AppliesTo, //3                        x => x.Form.ID, //4                        x => x.Layout, //5                        x => x.Form.Group.Description //6                        );                    var table = forms.Query(                        filter,                        columns                        ,                        new SortOrder<DigitalFormLayout>(x => x.Description)                        );                    foreach (CoreRow row in table.Rows)                    {                        List<object> list = row.Values;                        if (list[0] == null) list[0] = ""; //0                        if (list[1] == null) list[1] = Guid.Empty; //1                        if (list[2] == null) list[2] = ""; //2                        if (list[3] == null) list[3] = ""; //3                        if (list[4] == null) list[4] = Guid.Empty; //4                        if (list[5] == null) list[5] = ""; //5                        if (list[6] == null) list[6] = ""; //6                        DigitalFormLayoutShell layout = new DigitalFormLayoutShell();                        layout.Description = list[0].ToString();                        layout.ID = Guid.Parse(list[1].ToString());                        layout.Code = list[2].ToString();                        layout.AppliesTo = list[3].ToString();                        layout.FormID = Guid.Parse(list[4].ToString());                        layout.Layout = list[5].ToString();                        layout.FormGroupDescription = list[6].ToString();                        layouts.Add(layout);                        if (!types.Contains(layout.FormGroupDescription) && !string.IsNullOrWhiteSpace(layout.FormGroupDescription))                        {                            types.Add(layout.FormGroupDescription);                        }                    }                    GetAverages(appliesTo);                    Device.BeginInvokeOnMainThread(() =>                    {                        if (appliesTo == "Kanban")                        {                            layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals("Factory"));                            filterOptionsControl.Options = types;                            filterOptionsControl.CreateRadioButtonsAndSetDefault("Factory");                        }                        else                        {                            layoutsList.ItemsSource = layouts;                            filterOptionsControl.Options = types;                            filterOptionsControl.CreateRadioButtonsAndSetDefault(types.First());                        }                    });                    filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;                });                firstLoad = false;            }            catch (Exception e)            {                string error = e.Message;            }        }        private void GetAverages(string appliesTo)        {            try            {                Task.Run(() =>                {                    foreach (var layout in layouts)                    {                        TimeSpan span = new TimeSpan();                        CoreTable table = new Client<KanbanForm>().Query                            (                            new Filter<KanbanForm>(x => x.Form.ID).IsEqualTo(layout.FormID).And(x => x.FormOpen).IsNotEqualTo(null),                            new Columns<KanbanForm>(x => x.FormOpen)                            );                        if (table.Rows.Any())                        {                            foreach (CoreRow row in table.Rows)                            {                                List<object> list = row.Values;                                TimeSpan timespan = TimeSpan.Parse(list[0].ToString());                                span = span + timespan;                            }                            TimeSpan average = span / table.Rows.Count();                            layout.AverageTime = "Average time to complete: " + average.Minutes.ToString() + "m " + average.Seconds.ToString() + "s";                            layout.AverageTimeRow = 30;                            layout.ImageRowSpan = 2;                        }                    }                    Device.BeginInvokeOnMainThread(() =>                    {                        layoutsList.ItemsSource = null;                        if (filterOptionsControl.CurrentOption == "All")                        {                            layoutsList.ItemsSource = layouts;                        }                        else                        {                            layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals(filterOptionsControl.CurrentOption));                        }                    });                });            }            catch { }        }        private void LoadExistingForms()        {            //Task.Run(() =>            //{            try            {                List<IFormPickerQueryLoader> loaderList = new List<IFormPickerQueryLoader>()                {                new FormPickerQueryLoader<Kanban, KanbanLink, KanbanForm>(),                new FormPickerQueryLoader<Job, JobLink, JobForm>()                 };                incompleteForms.Clear();                completeForms.Clear();                foreach (var loader in loaderList)                {                    List<ExistingFormShell> incomplete = loader.QueryIncomplete();                    foreach (var v in incomplete)                    {                        incompleteForms.Add(v);                    }                    List<ExistingFormShell> complete = loader.QueryComplete();                    foreach (var v in complete)                    {                        completeForms.Add(v);                    }                }                Device.BeginInvokeOnMainThread(() =>                {                    ShowOrHideIncompleteFormsNotifications();                    RefreshMyForms();                });            }            catch { }            //});        }        private void ShowOrHideIncompleteFormsNotifications()        {            if (incompleteForms.Count > 0)            {                notificationFrame.IsVisible = true;                notificationColumn.Width = 40;                numberOfIncompleteFormsLbl.Text = incompleteForms.Count.ToString();            }            else            {                notificationFrame.IsVisible = false;                notificationColumn.Width = 0;            }        }        private void RefreshMyForms()        {            incompleteFormsList.ItemsSource = null;            completeFormsList.ItemsSource = null;            incompleteFormsList.ItemsSource = incompleteForms;            completeFormsList.ItemsSource = completeForms;            incompleteBtn.Text = "Incomplete (" + incompleteForms.Count + ")";            completeBtn.Text = "Complete (" + completeForms.Count + ")";        }        #endregion        #region User Interaction        #region New Forms Section        private void NewButton_Clicked(object sender, EventArgs e)        {            templatesColumn.Width = GridLength.Star;            formsColumn.Width = 0;            existingFormsGrid.IsVisible = false;            templatesGrid.IsVisible = true;            newButton.BackgroundColor = Color.FromHex("#15C7C1");            myFormsButton.BackgroundColor = Color.Default;        }        private void MyFormsButton_Clicked(object sender, EventArgs e)        {            templatesColumn.Width = 0;            formsColumn.Width = GridLength.Star;            existingFormsGrid.IsVisible = true;            templatesGrid.IsVisible = false;            newButton.BackgroundColor = Color.Default;            myFormsButton.BackgroundColor = Color.FromHex("#15C7C1");        }        private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)        {            if (filterOption == filterOptionsControl.CurrentOption)                return;            filterOptionsControl.CurrentOption = filterOption;            if (filterOption == "All")            {                layoutsList.ItemsSource = layouts;            }            else            {                layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals(filterOption));            }        }        #endregion        #region My Forms Section        private async void IncompleteFormsList_Tapped(object sender, EventArgs e)        {            using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))            {                var form = incompleteFormsList.SelectedItem as ExistingFormShell;                DigitalFormLayout layout = new Client<DigitalFormLayout>().Query(                    new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(form.FormID)                    ).Rows.FirstOrDefault().ToObject<DigitalFormLayout>();                if (form.Type == typeof(JobForm))                    JobID = form.ParentID;                else                    JobID = Guid.Empty;                DigitalFormHost host = new DigitalFormHost(LoadModel(layout, form.Type, form), JobID);                Navigation.PushAsync(host);            }        }        private async void CompleteFormsList_Tapped(object sender, EventArgs e)        {            using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))            {                var form = completeFormsList.SelectedItem as ExistingFormShell;                DigitalFormLayout layout = new Client<DigitalFormLayout>().Query(                    new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(form.FormID)                    ).Rows.FirstOrDefault().ToObject<DigitalFormLayout>();                if (form.Type == typeof(JobForm))                    JobID = form.ParentID;                else                    JobID = Guid.Empty;                DigitalFormHost host = new DigitalFormHost(LoadModel(layout, form.Type, form), JobID);                Navigation.PushAsync(host);            }        }        private void Incomplete_Tapped(object sender, EventArgs e)        {            incompleteFormsColumn.Width = GridLength.Star;            completeFormsColumn.Width = 0;            incompleteFormsList.IsVisible = true;            completeFormsList.IsVisible = false;            incompleteBtn.BackgroundColor = Color.FromHex("#15C7C1");            completeBtn.BackgroundColor = Color.Default;            incompleteVisible = true;            searchEnt.Text = "";        }        private void Complete_Tapped(object sender, EventArgs e)        {            completeFormsColumn.Width = GridLength.Star;            incompleteFormsColumn.Width = 0;            completeFormsList.IsVisible = true;            incompleteFormsList.IsVisible = false;            incompleteBtn.BackgroundColor = Color.Default;            completeBtn.BackgroundColor = Color.FromHex("#15C7C1");            incompleteVisible = false;            searchEnt.Text = "";        }        #region Loading From History Section        private void LayoutsList_Tapped(object sender, EventArgs e)        {            if (_searching)                return;            else                LoadHost();        }        private async void LoadHost()        {            try            {                var digitalFormLayoutShell = layoutsList.SelectedItem as DigitalFormLayoutShell;                DigitalFormLayout digitalFormLayout = new DigitalFormLayout();                using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))                {                    _searching = true;                    digitalFormLayout.ID = digitalFormLayoutShell.ID;                    digitalFormLayout.Description = digitalFormLayoutShell.Description;                    digitalFormLayout.Code = digitalFormLayoutShell.Code;                    digitalFormLayout.Form.AppliesTo = digitalFormLayoutShell.AppliesTo;                    digitalFormLayout.Form.ID = digitalFormLayoutShell.FormID;                    digitalFormLayout.Layout = digitalFormLayoutShell.Layout;                    digitalFormLayout.Form.Group.Description = digitalFormLayoutShell.FormGroupDescription;                    RetainedResults.LastDigitalFormLayout = digitalFormLayout;                }                DigitalFormHost host = new DigitalFormHost(LoadModel(digitalFormLayout, CheckType()), JobID);                Navigation.PushAsync(host);            }            catch { }        }        private Type CheckType()        {            if (JobID != Guid.Empty)                return typeof(JobForm);            else                return typeof(KanbanForm);        }        private IDigitalFormHostModel LoadModel(DigitalFormLayout layout, Type type, ExistingFormShell form = null)        {            if (type == typeof(JobForm))            {                var model = new DigitalFormHostModel<Job, JobLink, JobForm>();                var job = new Job();                var jobForm = new JobForm();                jobForm.Form.ID = layout.Form.ID;                if (form == null)                {                    job.ID = JobID;                }                else                {                    jobForm.ID = form.ID;                    job.ID = form.ParentID;                }                model.LoadItems(job, jobForm, layout);                return model;            }            else            {                var model = new DigitalFormHostModel<Kanban, KanbanLink, KanbanForm>();                var kanban = new Kanban();                var kanbanForm = new KanbanForm();                kanbanForm.Form.ID = layout.Form.ID;                if (form != null)                {                    kanbanForm.ID = form.ID;                    kanban.ID = form.ParentID;                }                if (addingToTask)                {                    kanbanForm.Parent.ID = addToTaskKanban.ID;                    kanban.ID = addToTaskKanban.ID;                }                model.LoadItems(kanban, kanbanForm, layout);                return model;            }        }        #endregion        #region Searching        private void SearchEnt_Changed(object sender, EventArgs e)        {            if (CheckEmptySearch())                return;            else            {                RunSearch();            }        }        private bool CheckEmptySearch()        {            if (string.IsNullOrWhiteSpace(searchEnt.Text))            {                incompleteFormsList.ItemsSource = incompleteForms;                completeFormsList.ItemsSource = completeForms;                return true;            }            else                return false;        }        private void RunSearch()        {            try            {                if (incompleteVisible)                    RunSearchOnIncomplete();                else                    RunSearchOnHistory();            }            catch (Exception ex)            {                string message = ex.Message;            }        }        private void RunSearchOnIncomplete()        {            incompleteFormsList.ItemsSource = incompleteForms.Where(x =>            x.Description.Contains(searchEnt.Text) || x.Description.Contains(searchEnt.Text.ToUpper())            || x.Description.Contains(searchEnt.Text.ToLower()) || x.Description.Contains(SearchUtils.UpperCaseFirst(searchEnt.Text))            );        }        private void RunSearchOnHistory()        {            completeFormsList.ItemsSource = completeForms.Where(x =>            x.Description.Contains(searchEnt.Text) || x.Description.Contains(searchEnt.Text.ToUpper())            || x.Description.Contains(searchEnt.Text.ToLower()) || x.Description.Contains(SearchUtils.UpperCaseFirst(searchEnt.Text))            );        }        #endregion        #endregion        #endregion    }}
 |