| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- using System;
- using System.ComponentModel;
- using System.Globalization;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Mobile;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI;
- using XF.Material.Forms.UI.Dialogs;
- using SelectionChangedEventArgs = Syncfusion.XForms.Buttons.SelectionChangedEventArgs;
- 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()
- ? $"Completed"
- : !shell.Started.IsEmpty()
- ? "In Progress"
- : $"Not Started";
- }
- 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 String.IsNullOrWhiteSpace(shell.Data)
- ? Color.Coral
- : shell.Completed.IsEmpty()
- ? Color.LightGreen
- : Color.DimGray;
- }
- 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 String.IsNullOrWhiteSpace(shell.Data)
- ? Color.Black
- : shell.Completed.IsEmpty()
- ? Color.Black
- : Color.WhiteSmoke;
- }
- 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(true);
- Refresh();
- }
- }
- public void Refresh()
- {
- var forms = Property(DataModel);
- Device.BeginInvokeOnMainThread(() =>
- {
- _forms.LastUpdated = DataModel.LastUpdated;
- _forms.ItemsSource = forms.Where(FilterShell).ToList();
- });
- }
- 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<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);
-
- }
-
- 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;
- Refresh();
- }
- }
- }
|