MobileTimeButton.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Globalization;
  3. using Syncfusion.XForms.PopupLayout;
  4. using Xamarin.Forms;
  5. using Xamarin.Forms.Xaml;
  6. using XF.Material.Forms.Resources.Typography;
  7. namespace InABox.Mobile
  8. {
  9. public class TimeButtonChangedArgs : EventArgs
  10. {
  11. public TimeSpan Time { get; private set; }
  12. public TimeButtonChangedArgs(TimeSpan time)
  13. {
  14. Time = time;
  15. }
  16. }
  17. public delegate void TimeButtonChanged(object sender, TimeButtonChangedArgs args);
  18. public class TimeSpanFormatter : IValueConverter
  19. {
  20. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  21. {
  22. if ((value is TimeSpan time) && (time != TimeSpan.Zero))
  23. return $"{DateTime.Today.Add(time):hh\\:mm tt}";
  24. return "--";
  25. }
  26. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. }
  31. [XamlCompilation(XamlCompilationOptions.Compile)]
  32. public partial class MobileTimeButton
  33. {
  34. private SfPopupLayout popup;
  35. public event TimeButtonChanged Changed;
  36. public static readonly BindableProperty TimeProperty = BindableProperty.Create(
  37. nameof(Time),
  38. typeof(TimeSpan),
  39. typeof(MobileTimeButton));
  40. public TimeSpan Time
  41. {
  42. get => (TimeSpan)GetValue(TimeProperty);
  43. set => SetValue(TimeProperty,value);
  44. }
  45. public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  46. nameof(TextColor),
  47. typeof(Color),
  48. typeof(MobileTimeButton),
  49. XF.Material.Forms.Material.Color.OnSecondary);
  50. public Color TextColor
  51. {
  52. get => (Color)GetValue(TextColorProperty);
  53. set => SetValue(TextColorProperty, value);
  54. }
  55. public static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  56. nameof(TypeScale),
  57. typeof(MaterialTypeScale),
  58. typeof(MobileTimeButton),
  59. MaterialTypeScale.Button);
  60. public MaterialTypeScale TypeScale
  61. {
  62. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  63. set => SetValue(TypeScaleProperty, value);
  64. }
  65. public static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(
  66. nameof(ButtonColor),
  67. typeof(Color),
  68. typeof(MobileTimeButton),
  69. XF.Material.Forms.Material.Color.Secondary);
  70. public Color ButtonColor
  71. {
  72. get => (Color)GetValue(ButtonColorProperty);
  73. set => SetValue(ButtonColorProperty, value);
  74. }
  75. public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  76. nameof(BorderColor),
  77. typeof(Color),
  78. typeof(MobileTimeButton),
  79. XF.Material.Forms.Material.Color.SecondaryVariant);
  80. public Color BorderColor
  81. {
  82. get => (Color)GetValue(BorderColorProperty);
  83. set => SetValue(BorderColorProperty, value);
  84. }
  85. // public static readonly BindableProperty PaddingProperty = BindableProperty.Create(
  86. // nameof(Padding),
  87. // typeof(Thickness),
  88. // typeof(TimeButton),
  89. // new Thickness(5));
  90. //
  91. // public new Thickness Padding
  92. // {
  93. // get => (Thickness)GetValue(PaddingProperty);
  94. // set => SetValue(PaddingProperty, value);
  95. // }
  96. public MobileTimeButton()
  97. {
  98. InitializeComponent();
  99. popup = new SfPopupLayout();
  100. }
  101. private void _frame_OnClicked(object sender, EventArgs e)
  102. {
  103. popup.PopupView.WidthRequest = 300;
  104. popup.PopupView.HeightRequest = 500;
  105. popup.PopupView.ShowHeader = false;
  106. popup.PopupView.ShowFooter = false;
  107. MobileTimeSelector popupContent = new MobileTimeSelector();
  108. popupContent.Time = Time;
  109. popupContent.TimeChanged += (o, args) =>
  110. {
  111. Time = args.Time;
  112. Changed?.Invoke(this,new TimeButtonChangedArgs(Time));
  113. };
  114. popupContent.Margin = new Thickness(10);
  115. popupContent.HorizontalOptions = LayoutOptions.Fill;
  116. popupContent.VerticalOptions = LayoutOptions.Fill;
  117. popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
  118. popup.Show();
  119. }
  120. }
  121. }