123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Globalization;
- using System.Linq;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Mobile;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace PRS.Mobile
- {
-
- public class ExistingFormStatusConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is IDigitalFormInstanceShell shell)
- {
- return !shell.Completed.IsEmpty()
- ? $"{shell.Completed:dd MMMM yy}"
- : !shell.Started.IsEmpty()
- ? $"{shell.Started:dd MMMM yy}"
- : $"{shell.Created:dd MMMM yy}";
- }
- return "";
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
-
- public class ExistingFormBackgroundColorConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is IDigitalFormInstanceShell shell)
- {
- return !shell.Completed.IsEmpty()
- ? Color.DimGray
- : !shell.Started.IsEmpty()
- ? Color.LightGreen
- : Color.Coral;
- }
- return Color.Transparent;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
-
- public class ExistingFormForegroundColorConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is IDigitalFormInstanceShell shell)
- {
- return !shell.Completed.IsEmpty()
- ? Color.WhiteSmoke
- : !shell.Started.IsEmpty()
- ? Color.Black
- : Color.Black;
- }
- return Color.Transparent;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
-
- public class ExistingFormRefreshEventArgs : CancelEventArgs
- {
- }
-
- public delegate void ExistingFormRefreshEvent(object sender, ExistingFormRefreshEventArgs args);
-
- public class ExistingFormSearchEventArgs : EventArgs
- {
- public IDigitalFormInstanceShell Shell { get; private set; }
- public ExistingFormSearchEventArgs(IDigitalFormInstanceShell shell)
- {
- Shell = shell;
- }
- }
- public delegate bool ExistingFormSearchEvent(object sender, ExistingFormSearchEventArgs args);
-
-
- public class ExistingFormTappedEventArgs : EventArgs
- {
- public IDigitalFormInstanceShell Shell { get; private set; }
- public ExistingFormTappedEventArgs(IDigitalFormInstanceShell shell)
- {
- Shell = shell;
- }
- }
- public delegate void ExistingFormTappedEvent(object sender, ExistingFormTappedEventArgs args);
-
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class ExistingForms
- {
-
- private String _currentfilter = "";
- public ICoreRepository DataModel
- {
- get => _model.DataModel;
- set => _model.DataModel = value;
- }
- public String AppliesTo
- {
- get => _model.AppliesTo;
- set => _model.AppliesTo = value;
- }
- public Func<ICoreRepository, IDigitalFormInstanceShell[]> Property
- {
- get => _model.Property;
- set => _model.Property = value;
- }
- public bool SeparateHistory
- {
- get => _model.SeparateHistory;
- set => _model.SeparateHistory = value;
- }
- public event ExistingFormRefreshEvent RefreshRequested;
- public event ExistingFormSearchEvent Search;
- public event ExistingFormTappedEvent FormTapped;
-
- // Event to handle new / existing form selection
-
- public ExistingForms()
- {
- InitializeComponent();
- }
-
- public void RefreshData(bool force, bool async)
- {
- if (async)
- DataModel?.Refresh(force, Refresh);
- else
- {
- DataModel?.Refresh(force);
- Refresh();
- }
- }
- public void Refresh()
- {
- var forms = Property(DataModel);
- Device.BeginInvokeOnMainThread(() =>
- {
- _forms.LastUpdated = DataModel.LastUpdated;
- _forms.ItemsSource = forms
- .Where(FilterShell)
- .OrderByDescending(SortShell)
- .ToList();
- });
- }
- private DateTime SortShell(IDigitalFormInstanceShell shell)
- {
- return !shell.Completed.IsEmpty()
- ? shell.Completed
- : !shell.Started.IsEmpty()
- ? shell.Started
- : shell.Created;
- }
-
- private bool _showincomplete = true;
-
- private bool FilterShell(IDigitalFormInstanceShell shell)
- {
- if (_model.SeparateHistory)
- {
- bool shellincomplete = shell.Completed.IsEmpty();
- if (shellincomplete != _showincomplete)
- return false;
- }
- bool bOK =
- String.IsNullOrWhiteSpace(_currentfilter)
- || (shell.FormCode.ToUpper().Contains(_currentfilter.ToUpper())
- || shell.FormDescription.ToUpper().Contains(_currentfilter.ToUpper()));
-
- bOK = bOK
- && (Search?.Invoke(this, new ExistingFormSearchEventArgs(shell)) ?? true);
-
- return bOK;
- }
- private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
- {
- _currentfilter = args.Text;
- Refresh();
- }
- private void _digitalforms_OnRefresh(object sender, MobileListRefreshEventArgs args)
- {
- var newargs = new ExistingFormRefreshEventArgs() { Cancel = false };
- RefreshRequested?.Invoke(this, newargs);
- if (!newargs.Cancel)
- RefreshData(true,false);
- }
-
- private void AddForm_Tapped(object sender, EventArgs e)
- {
- var selector = new NewForms() { AppliesTo = this.AppliesTo };
- selector.ItemSelected += CreateNewForm;
- Navigation.PushAsync(selector);
- }
- private async void CreateNewForm(object sender, NewFormSelectedArgs args)
- {
- await MaterialDialog.Instance.AlertAsync("Not Implemented Yet!");
- return;
- }
- private async void Form_Clicked(object sender, EventArgs e)
- {
- if ((sender is MobileCard card) && (card.BindingContext is IDigitalFormInstanceShell shell))
- FormTapped?.Invoke(this, new ExistingFormTappedEventArgs(shell));
- else
- await MaterialDialog.Instance.AlertAsync("Tapped Item is not a Digital Form", "ERROR");
- }
-
- public void CreateNewForm(String appliesto, Func<IDigitalFormInstanceShell> createform, Guid parentid, Action refresh)
- {
- var selector = new NewForms() { AppliesTo = appliesto };
- selector.ItemSelected += (sender, args) =>
- {
- var instance = createform();
- instance.ParentID = parentid;
- instance.FormID = args.Form.ID;
- instance.FormCode = args.Form.Code;
- instance.FormDescription = args.Form.Description;
- instance.Save("Created on Mobile Device");
- Dispatcher.BeginInvokeOnMainThread(refresh);
- };
- Navigation.PushAsync(selector);
- }
-
- public void EditForm<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
- where TParent : Entity, IRemotable, IPersistent, new()
- where TParentLink : EntityLink<TParent>, new()
- where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
- {
-
- var form = new Client<TForm>()
- .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID))
- .Rows
- .Select(x => x.ToObject<TForm>())
- .FirstOrDefault();
-
- var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
- model.LoadItems(parent, form, null);
- var host = new DigitalFormHost(model);
- host.OnClosing += (o, args) =>
- {
- if (args.Changed)
- {
- //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
- }
- };
- Navigation.PushAsync(host);
-
- }
-
- public async void EditFormAsync<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
- where TParent : Entity, IRemotable, IPersistent, new()
- where TParentLink : EntityLink<TParent>, new()
- where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
- {
-
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
- {
- var form = new Client<TForm>()
- .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID))
- .Rows
- .Select(x => x.ToObject<AssignmentForm>())
- .FirstOrDefault();
-
- var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();
- model.LoadItems(parent, form, null);
- var host = new DigitalFormHost(model);
- host.OnClosing += (o, args) =>
- {
- if (args.Changed)
- {
- //instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
- }
- };
- Navigation.PushAsync(host);
- }
-
- }
- private void _tabStrip_OnSelectionChanged(object sender, EventArgs e)
- {
- _showincomplete = _tabStrip.SelectedItem?.Index == 0;
- _model.DataModel.SelectNone();
- _selectionmenubutton.IsVisible = false;
- Refresh();
- }
- private void _selectionmenu_OnAppearing(object sender, EventArgs e)
- {
- _completeform.IsVisible = _showincomplete;
- _reopenform.IsVisible = !_showincomplete;
- //bool anyselected = SelectedItems().Any();
- //bool anyunselected = UnselectedItems().Any();
- //_setstatus.IsVisible = anyselected;
- //_selectAll.IsVisible = anyunselected;
- //_selectNone.IsVisible = anyselected;
- //_separator.IsVisible = (_setstatus.IsVisible) && (_selectAll.IsVisible || _selectNone.IsVisible);
- }
- private IEnumerable<IShell> SelectedItems()
- {
- return _model.DataModel.SelectedItems.OfType<IShell>();
- }
-
- private IEnumerable<IShell> UnselectedItems()
- {
- return _model.DataModel.Items.OfType<IShell>().Except(_model.DataModel.SelectedItems.OfType<IShell>());
- }
- private void _setstatus_OnClicked(object sender, EventArgs e)
- {
-
- }
- private void _selectAll_OnClicked(object sender, EventArgs e)
- {
- _model.DataModel.SelectAll();
- RefreshData(false,false);
- }
- private void _selectNone_OnClicked(object sender, EventArgs e)
- {
- _model.DataModel.SelectNone();
- RefreshData(false,false);
- }
- private void CheckBox_Changed(object sender, EventArgs e)
- {
- if ((sender as MobileCheckBox)?.BindingContext is IShell shell)
- _model.DataModel.ToggleSelection(shell);
- _selectionmenubutton.IsVisible = SelectedItems().Any();
- }
- private void _completeform_OnClicked(object sender, EventArgs e)
- {
- var shells = _model.DataModel.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
- foreach (var shell in shells)
- {
- shell.Completed = DateTime.Now;
- shell.Save("Completed on Mobile Device");
- }
- _model.DataModel.SelectNone();
- _selectionmenubutton.IsVisible = false;
- RefreshData(true,false);
- }
- private void _reopenform_OnClicked(object sender, EventArgs e)
- {
- var shells = _model.DataModel.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
- foreach (var shell in shells)
- {
- shell.Completed = DateTime.MinValue;
- shell.Save("Re-Opened on Mobile Device");
- }
- _model.DataModel.SelectNone();
- _selectionmenubutton.IsVisible = false;
- RefreshData(true,false);
- }
- }
- }
|