MobileDateButton.xaml.cs 5.1 KB

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