MobileTimeButton.xaml.cs 5.4 KB

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