TimeSelectorButton.cs 3.1 KB

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