MobileTimeButton.xaml.cs 5.1 KB

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