DateSelectorButton.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Avalonia;
  2. using Avalonia.Controls.Primitives;
  3. using Avalonia.Data.Converters;
  4. using CommunityToolkit.Mvvm.Input;
  5. using InABox.Avalonia.Components.DateSelector;
  6. using InABox.Core;
  7. using System.Globalization;
  8. namespace InABox.Avalonia.Components;
  9. public class DateSelectorDateChangedEventArgs(DateTime? oldDate, DateTime? newDate)
  10. {
  11. public DateTime? OldDate { get; set; } = oldDate;
  12. public DateTime? NewDate { get; set; } = newDate;
  13. }
  14. public partial class DateSelectorButton : TemplatedControl
  15. {
  16. public static readonly StyledProperty<string> PromptProperty =
  17. AvaloniaProperty.Register<DateSelectorButton, string>(nameof(Prompt));
  18. public static readonly StyledProperty<string> PrefixProperty =
  19. AvaloniaProperty.Register<DateSelectorButton, string>(nameof(Prefix));
  20. public static readonly StyledProperty<string> FormatProperty =
  21. AvaloniaProperty.Register<DateSelectorButton, string>(nameof(Format));
  22. public static readonly StyledProperty<DateTime?> DateProperty =
  23. AvaloniaProperty.Register<DateSelectorButton, DateTime?>(nameof(Date));
  24. public string Prompt
  25. {
  26. get => GetValue(PromptProperty);
  27. set => SetValue(PromptProperty, value);
  28. }
  29. public string Prefix
  30. {
  31. get => GetValue(PrefixProperty);
  32. set => SetValue(PrefixProperty, value);
  33. }
  34. public string Format
  35. {
  36. get => GetValue(FormatProperty);
  37. set => SetValue(FormatProperty, value);
  38. }
  39. public DateTime? Date
  40. {
  41. get => GetValue(DateProperty);
  42. set => SetValue(DateProperty, value);
  43. }
  44. public event EventHandler<DateSelectorDateChangedEventArgs>? DateChanged;
  45. static DateSelectorButton()
  46. {
  47. DateProperty.Changed.AddClassHandler<DateSelectorButton>(DateProperty_Changed);
  48. }
  49. private static void DateProperty_Changed(DateSelectorButton button, AvaloniaPropertyChangedEventArgs args)
  50. {
  51. button.DateChanged?.Invoke(button, new(
  52. args.OldValue is DateTime oldDate ? oldDate : null,
  53. args.NewValue is DateTime newDate ? newDate : null));
  54. }
  55. [RelayCommand]
  56. private async Task Click()
  57. {
  58. var date = await Navigation.Popup<DateSelectorViewModel, DateTime?>(x =>
  59. {
  60. x.Date = Date;
  61. });
  62. if (date.HasValue)
  63. {
  64. Date = date.Value == DateTime.MinValue ? null : date.Value;
  65. }
  66. }
  67. }
  68. public class DateSelectorDateTimeFormatter : IMultiValueConverter
  69. {
  70. public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
  71. {
  72. var date = values[0] as DateTime?;
  73. if (values[1] is not DateSelectorButton btn) return null;
  74. if (date.HasValue && !date.Value.IsEmpty())
  75. {
  76. var sFormat = btn.Format.NotWhiteSpaceOr("dd MMMM yy");
  77. var fmt = "{0} {1:" + sFormat + "}";
  78. return string.Format(fmt, btn.Prefix, date.Value).Trim();
  79. }
  80. return btn.Prompt.NotWhiteSpaceOr("Select Date");
  81. }
  82. }