MobileDateButton.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Globalization;
  3. using InABox.Core;
  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 DateButtonChangedArgs : EventArgs
  12. {
  13. public DateTime Date { get; private set; }
  14. public DateButtonChangedArgs(DateTime date)
  15. {
  16. Date = date;
  17. }
  18. }
  19. public delegate void DateButtonChanged(object sender, DateButtonChangedArgs args);
  20. public class DateTimeFormatter : IValueConverter
  21. {
  22. public String Prefix { get; set; }
  23. public String Format { get; set; }
  24. public String Prompt { get; set; }
  25. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  26. {
  27. if ((value is DateTime date) && (!date.IsEmpty()))
  28. {
  29. var sFormat = String.IsNullOrWhiteSpace(Format)
  30. ? "dd MMMM yy"
  31. : Format;
  32. String fmt = "{0} {1:" + sFormat + "}";
  33. return String.Format(fmt,Prefix,date).Trim();
  34. }
  35. return String.IsNullOrWhiteSpace(Prompt)
  36. ? "Select Date"
  37. : Prompt;
  38. }
  39. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  40. {
  41. throw new NotImplementedException();
  42. }
  43. public DateTimeFormatter()
  44. {
  45. }
  46. }
  47. [XamlCompilation(XamlCompilationOptions.Compile)]
  48. public partial class MobileDateButton
  49. {
  50. private SfPopupLayout popup;
  51. public event DateButtonChanged Changed;
  52. public String Prompt
  53. {
  54. get => _datetimeformatter.Prompt;
  55. set => _datetimeformatter.Prompt = value;
  56. }
  57. public String Prefix
  58. {
  59. get => _datetimeformatter.Prefix;
  60. set => _datetimeformatter.Prefix = value;
  61. }
  62. public String Format
  63. {
  64. get => _datetimeformatter.Format;
  65. set => _datetimeformatter.Format = value;
  66. }
  67. public static readonly BindableProperty DateProperty = BindableProperty.Create(
  68. nameof(Date),
  69. typeof(DateTime),
  70. typeof(MobileDateButton));
  71. public DateTime Date
  72. {
  73. get => (DateTime)GetValue(DateProperty);
  74. set => SetValue(DateProperty,value);
  75. }
  76. private static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  77. nameof(TextColor),
  78. typeof(Color),
  79. typeof(MobileDateButton),
  80. XF.Material.Forms.Material.Color.OnSecondary);
  81. public Color TextColor
  82. {
  83. get => (Color)GetValue(TextColorProperty);
  84. set => SetValue(TextColorProperty, value);
  85. }
  86. private static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  87. nameof(TypeScale),
  88. typeof(MaterialTypeScale),
  89. typeof(MobileDateButton),
  90. MaterialTypeScale.Button);
  91. public MaterialTypeScale TypeScale
  92. {
  93. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  94. set => SetValue(TypeScaleProperty, value);
  95. }
  96. private static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(
  97. nameof(ButtonColor),
  98. typeof(Color),
  99. typeof(MobileDateButton),
  100. XF.Material.Forms.Material.Color.Secondary);
  101. public Color ButtonColor
  102. {
  103. get => (Color)GetValue(ButtonColorProperty);
  104. set => SetValue(ButtonColorProperty, value);
  105. }
  106. private static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  107. nameof(BorderColor),
  108. typeof(Color),
  109. typeof(MobileDateButton),
  110. XF.Material.Forms.Material.Color.SecondaryVariant);
  111. public Color BorderColor
  112. {
  113. get => (Color)GetValue(BorderColorProperty);
  114. set => SetValue(BorderColorProperty, value);
  115. }
  116. public MobileDateButton()
  117. {
  118. InitializeComponent();
  119. popup = new SfPopupLayout();
  120. }
  121. private void _frame_OnClicked(object sender, EventArgs e)
  122. {
  123. popup.PopupView.WidthRequest = 300;
  124. popup.PopupView.HeightRequest = 500;
  125. popup.PopupView.ShowHeader = false;
  126. popup.PopupView.ShowFooter = false;
  127. MobileDateSelector popupContent = new MobileDateSelector();
  128. popupContent.Date = Date;
  129. popupContent.Changed += (o, args) =>
  130. {
  131. Date = args.Date;
  132. Changed?.Invoke(this,new DateButtonChangedArgs(Date));
  133. popup.Closing -= RestrictClose;
  134. popup.Dismiss();
  135. };
  136. popupContent.Cancelled += (o, args) =>
  137. {
  138. popup.Closing -= RestrictClose;
  139. popup.Dismiss();
  140. };
  141. popup.Closing += RestrictClose;
  142. popupContent.Margin = new Thickness(10);
  143. popupContent.HorizontalOptions = LayoutOptions.Fill;
  144. popupContent.VerticalOptions = LayoutOptions.Fill;
  145. popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
  146. popup.Show();
  147. }
  148. private void RestrictClose(object sender, CancelEventArgs e)
  149. {
  150. e.Cancel = true;
  151. }
  152. }
  153. }