123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Data.Converters;
- using Avalonia.Markup.Xaml;
- using Avalonia.Media;
- using Avalonia.Threading;
- using CommunityToolkit.Mvvm.ComponentModel;
- using CommunityToolkit.Mvvm.Input;
- using DynamicData.Binding;
- using InABox.Avalonia;
- using InABox.Avalonia.Components;
- using InABox.Avalonia.Converters;
- using InABox.Clients;
- using InABox.Core;
- using JetBrains.Annotations;
- using PRS.Avalonia.DigitalForms;
- using System;
- using System.ComponentModel;
- using System.Globalization;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace PRS.Avalonia.Components;
- public class FormsListBackgroundColorConverter : AbstractConverter<IDigitalFormInstanceShell, IBrush>
- {
- protected override IBrush? Convert(IDigitalFormInstanceShell? value, object? parameter = null)
- {
- if(value is null) return new SolidColorBrush(Colors.Transparent);
- return new SolidColorBrush(!value.Completed.IsEmpty()
- ? Colors.DimGray
- : !value.Started.IsEmpty()
- ? Colors.LightGreen
- : Colors.Coral);
- }
- }
- public class FormsListForegroundColorConverter : AbstractConverter<IDigitalFormInstanceShell, IBrush>
- {
- protected override IBrush? Convert(IDigitalFormInstanceShell? value, object? parameter = null)
- {
- if(value is null) return new SolidColorBrush(Colors.Transparent);
- return new SolidColorBrush(!value.Completed.IsEmpty()
- ? Colors.WhiteSmoke
- : !value.Started.IsEmpty()
- ? Colors.Black
- : Colors.Black);
- }
- }
- public class ExistingFormStatusConverter : AbstractConverter<IDigitalFormInstanceShell, string>
- {
- public static readonly ExistingFormStatusConverter Instance = new();
- protected override string? Convert(IDigitalFormInstanceShell? value, object? parameter = null)
- {
- if (value is null) return "";
- return !value.Completed.IsEmpty()
- ? $"{value.Completed:dd MMMM yy}"
- : !value.Started.IsEmpty()
- ? $"{value.Started:dd MMMM yy}"
- : $"{value.Created:dd MMMM yy}";
- }
- }
- public class FormsListSearchEventArgs(IDigitalFormInstanceShell shell)
- {
- public IDigitalFormInstanceShell Shell { get; set; } = shell;
- }
- public delegate bool FormsListSearchEvent(object sender, FormsListSearchEventArgs args);
- public partial class FormsList : UserControl
- {
- // public static readonly StyledProperty<bool> SeparateHistoryProperty =
- // AvaloniaProperty.Register<FormsList, bool>("SeparateHistory", true);
- public static readonly StyledProperty<string> AppliesToProperty =
- AvaloniaProperty.Register<FormsList, string>(nameof(AppliesTo), "");
- public static readonly StyledProperty<ICoreRepository?> ModelProperty =
- AvaloniaProperty.Register<FormsList, ICoreRepository?>(nameof(Model));
- public static readonly StyledProperty<ICommand?> FormClickedProperty =
- AvaloniaProperty.Register<FormsList, ICommand?>(nameof(FormClicked));
- public bool SeparateHistory { get; set; } = true;
- public string AppliesTo
- {
- get => GetValue(AppliesToProperty);
- set => SetValue(AppliesToProperty, value);
- }
- private bool SelectionMenuVisible
- {
- set => SelectionMenuButton.IsVisible = value;
- }
- private bool ShowIncomplete { get; set; } = true;
- public event FormsListSearchEvent? Search;
- public ICoreRepository? Model
- {
- get => GetValue(ModelProperty);
- set => SetValue(ModelProperty, value);
- }
-
- public ICommand? FormClicked
- {
- get => GetValue(FormClickedProperty);
- set => SetValue(FormClickedProperty, value);
- }
- public FormsList()
- {
- InitializeComponent();
- }
- // static FormsList()
- // {
- // SeparateHistoryProperty.Changed.AddClassHandler<FormsList>(SeparateHistory_Changed);
- // }
- // private static void SeparateHistory_Changed(FormsList list, AvaloniaPropertyChangedEventArgs args)
- // {
- // list._separateHistory = list.GetValue(SeparateHistoryProperty);
- // }
- private bool FilterShell(IShell shell)
- {
- if (shell is not IDigitalFormInstanceShell formShell) return false;
- return (!SeparateHistory || ShowIncomplete == (formShell.Completed == DateTime.MinValue))
- && (Search is null || Search.Invoke(this, new(formShell)));
- }
- private void TabStrip_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (_tabList is null) return;
- ShowIncomplete = _tabList.SelectedIndex == 0;
- Model?.SelectNone();
- SelectionMenuVisible = false;
- }
- [RelayCommand]
- private void FormChecked(IDigitalFormInstanceShell form)
- {
- SelectionMenuVisible = Model?.SelectedItems.OfType<IShell>().Any() == true;
- }
- [RelayCommand]
- private void SelectionMenu(IDigitalFormInstanceShell form)
- {
- var menu = new ContextMenu();
- AvaloniaMenuItem.LoadMenuItems([
- new CoreMenuItem<IImage>("Complete Forms", null, CompleteForms, () => ShowIncomplete),
- new CoreMenuItem<IImage>("Re-open Forms", null, ReopenForms, () => !ShowIncomplete),
- new CoreMenuSeparator(),
- new CoreMenuItem<IImage>("Select All", null, SelectAll),
- new CoreMenuItem<IImage>("Select None", null, SelectNone),
- ], menu.Items);
- menu.Open(SelectionMenuButton);
- }
- private Task<bool> SelectNone()
- {
- Model?.SelectNone();
- return Task.FromResult(true);
- }
- private Task<bool> SelectAll()
- {
- Model?.SelectAll();
- return Task.FromResult(true);
- }
- private async Task<bool> ReopenForms()
- {
- if (Model is null) return true;
- var shells = Model.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
- foreach(var shell in shells)
- {
- shell.Completed = DateTime.MinValue;
- }
- await Model.SaveAsync("Re-opened on Mobile Device");
- Model.SelectNone();
- SelectionMenuVisible = false;
- await Model.RefreshAsync(true);
- return true;
- }
- private async Task<bool> CompleteForms()
- {
- if (Model is null) return true;
- var shells = Model.SelectedItems.OfType<IDigitalFormInstanceShell>().ToArray();
- foreach(var shell in shells)
- {
- shell.Completed = DateTime.Now;
- }
- await Model.SaveAsync("Completed on Mobile Device");
- Model.SelectNone();
- SelectionMenuVisible = false;
- await Model.RefreshAsync(true);
- return true;
- }
- public static async Task<DigitalFormShell?> SelectForm<TForm, TParent, TLink>(DigitalFormModel formModel)
- where TForm : EntityForm<TParent, TLink, TForm>
- where TParent : Entity, new()
- where TLink : EntityLink<TParent>, new()
- {
- return (await SelectionViewModel.ExecutePopup<DigitalFormShell>(model =>
- {
- model.AddFilters(formModel.AvailableFilters.Select(x => x.Name).NotNull());
- model.Columns.BeginUpdate()
- .Add(new AvaloniaDataGridTextColumn<DigitalFormShell>
- {
- Column = x => x.Code,
- Width = GridLength.Auto
- })
- .Add(new AvaloniaDataGridTextColumn<DigitalFormShell>
- {
- Column = x => x.Description,
- Width = GridLength.Star
- })
- .EndUpdate();
- }, args =>
- {
- formModel.SelectFilter(args.Filter);
- formModel.Search(x => x.AppliesTo == typeof(TParent).Name);
- return formModel.Refresh(args.Force);
- }))?.FirstOrDefault();
- }
- }
|