MobileDateButton.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using InABox.Core;
  5. using Syncfusion.XForms.PopupLayout;
  6. using Xamarin.Forms;
  7. using Xamarin.Forms.Xaml;
  8. using XF.Material.Forms.Resources.Typography;
  9. using CancelEventArgs = Syncfusion.XForms.Core.CancelEventArgs;
  10. namespace InABox.Mobile
  11. {
  12. public class DateButtonChangedArgs : EventArgs
  13. {
  14. public DateTime Date { get; private set; }
  15. public DateButtonChangedArgs(DateTime date)
  16. {
  17. Date = date;
  18. }
  19. }
  20. public delegate void DateButtonChanged(object sender, DateButtonChangedArgs args);
  21. public class DateTimeFormatter : UtilityConverter<DateTime,String>
  22. {
  23. public String Prefix { get; set; }
  24. public String Format { get; set; }
  25. public String Prompt { get; set; }
  26. private DateTime _value;
  27. protected override string Convert(DateTime value)
  28. {
  29. _value = value;
  30. if (!value.IsEmpty())
  31. {
  32. var sFormat = String.IsNullOrWhiteSpace(Format)
  33. ? "dd MMMM yy"
  34. : Format;
  35. String fmt = "{0} {1:" + sFormat + "}";
  36. return String.Format(fmt,Prefix,value).Trim();
  37. }
  38. return String.IsNullOrWhiteSpace(Prompt)
  39. ? "Select Date"
  40. : Prompt;
  41. }
  42. protected override DateTime Deconvert(string value)
  43. {
  44. return _value;
  45. }
  46. }
  47. [XamlCompilation(XamlCompilationOptions.Compile)]
  48. public partial class MobileDateButton
  49. {
  50. private SfPopupLayout popup;
  51. public event DateButtonChanged Changed;
  52. public static readonly BindableProperty PromptProperty = BindableProperty.Create(
  53. nameof(Prompt),
  54. typeof(string),
  55. typeof(MobileDateButton)
  56. );
  57. public String Prompt
  58. {
  59. get => (string)GetValue(PromptProperty);
  60. set
  61. {
  62. _datetimeformatter.Prompt = value;
  63. SetValue(PromptProperty, value);
  64. }
  65. }
  66. public static readonly BindableProperty PrefixProperty = BindableProperty.Create(
  67. nameof(Prefix),
  68. typeof(string),
  69. typeof(MobileDateButton)
  70. );
  71. public String Prefix
  72. {
  73. get => (string)GetValue(PrefixProperty);
  74. set
  75. {
  76. _datetimeformatter.Prefix = value;
  77. SetValue(PrefixProperty, value);
  78. }
  79. }
  80. public static readonly BindableProperty FormatProperty = BindableProperty.Create(
  81. nameof(Format),
  82. typeof(string),
  83. typeof(MobileDateButton)
  84. );
  85. public String Format
  86. {
  87. get => (string)GetValue(FormatProperty);
  88. set
  89. {
  90. _datetimeformatter.Format = value;
  91. SetValue(FormatProperty, value);
  92. }
  93. }
  94. public static readonly BindableProperty DateProperty = BindableProperty.Create(
  95. nameof(Date),
  96. typeof(DateTime),
  97. typeof(MobileDateButton)
  98. );
  99. public DateTime Date
  100. {
  101. get => (DateTime)GetValue(DateProperty);
  102. set => SetValue(DateProperty,value);
  103. }
  104. private static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  105. nameof(TextColor),
  106. typeof(Color),
  107. typeof(MobileDateButton),
  108. XF.Material.Forms.Material.Color.OnSecondary);
  109. public Color TextColor
  110. {
  111. get => (Color)GetValue(TextColorProperty);
  112. set => SetValue(TextColorProperty, value);
  113. }
  114. private static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  115. nameof(TypeScale),
  116. typeof(MaterialTypeScale),
  117. typeof(MobileDateButton),
  118. MaterialTypeScale.Button);
  119. public MaterialTypeScale TypeScale
  120. {
  121. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  122. set => SetValue(TypeScaleProperty, value);
  123. }
  124. private static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(
  125. nameof(ButtonColor),
  126. typeof(Color),
  127. typeof(MobileDateButton),
  128. XF.Material.Forms.Material.Color.Secondary);
  129. public Color ButtonColor
  130. {
  131. get => (Color)GetValue(ButtonColorProperty);
  132. set => SetValue(ButtonColorProperty, value);
  133. }
  134. private static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  135. nameof(BorderColor),
  136. typeof(Color),
  137. typeof(MobileDateButton),
  138. XF.Material.Forms.Material.Color.SecondaryVariant);
  139. public Color BorderColor
  140. {
  141. get => (Color)GetValue(BorderColorProperty);
  142. set => SetValue(BorderColorProperty, value);
  143. }
  144. public MobileDateButton()
  145. {
  146. InitializeComponent();
  147. popup = new SfPopupLayout();
  148. }
  149. private void _frame_OnClicked(object sender, EventArgs e)
  150. {
  151. popup.PopupView.WidthRequest = 300;
  152. popup.PopupView.HeightRequest = 500;
  153. popup.PopupView.ShowHeader = false;
  154. popup.PopupView.ShowFooter = false;
  155. //popup.PopupView.PopupStyle.HasShadow = false;
  156. popup.PopupView.PopupStyle.CornerRadius = 5;
  157. //popup.PopupView.Background = new SolidColorBrush(Color.White);
  158. MobileDateSelector popupContent = new MobileDateSelector();
  159. popupContent.Date = Date;
  160. popupContent.Changed += (o, args) =>
  161. {
  162. Date = args.Date;
  163. DoChanged();
  164. popup.Closing -= RestrictClose;
  165. popup.Dismiss();
  166. };
  167. popupContent.Cancelled += (o, args) =>
  168. {
  169. popup.Closing -= RestrictClose;
  170. popup.Dismiss();
  171. };
  172. popup.Closing += RestrictClose;
  173. popupContent.Margin = new Thickness(10);
  174. popupContent.HorizontalOptions = LayoutOptions.Fill;
  175. popupContent.VerticalOptions = LayoutOptions.Fill;
  176. popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
  177. popup.Show();
  178. }
  179. protected virtual void DoChanged()
  180. {
  181. Changed?.Invoke(this,new DateButtonChangedArgs(Date));
  182. }
  183. private void RestrictClose(object sender, CancelEventArgs e)
  184. {
  185. e.Cancel = true;
  186. }
  187. private void DateTimeFormatter_OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  188. {
  189. OnPropertyChanged(nameof(Date));
  190. }
  191. }
  192. }