MobileDateButton.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 : AbstractConverter<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, object? parameter = null)
  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, object? parameter = null)
  43. {
  44. return _value;
  45. }
  46. }
  47. [XamlCompilation(XamlCompilationOptions.Compile)]
  48. public partial class MobileDateButton
  49. {
  50. public event DateButtonChanged Changed;
  51. public static readonly BindableProperty PromptProperty = BindableProperty.Create(
  52. nameof(Prompt),
  53. typeof(string),
  54. typeof(MobileDateButton)
  55. );
  56. public String Prompt
  57. {
  58. get => (string)GetValue(PromptProperty);
  59. set
  60. {
  61. _datetimeformatter.Prompt = value;
  62. SetValue(PromptProperty, value);
  63. }
  64. }
  65. public static readonly BindableProperty PrefixProperty = BindableProperty.Create(
  66. nameof(Prefix),
  67. typeof(string),
  68. typeof(MobileDateButton)
  69. );
  70. public String Prefix
  71. {
  72. get => (string)GetValue(PrefixProperty);
  73. set
  74. {
  75. _datetimeformatter.Prefix = value;
  76. SetValue(PrefixProperty, value);
  77. }
  78. }
  79. public static readonly BindableProperty FormatProperty = BindableProperty.Create(
  80. nameof(Format),
  81. typeof(string),
  82. typeof(MobileDateButton)
  83. );
  84. public String Format
  85. {
  86. get => (string)GetValue(FormatProperty);
  87. set
  88. {
  89. _datetimeformatter.Format = value;
  90. SetValue(FormatProperty, value);
  91. }
  92. }
  93. public static readonly BindableProperty DateProperty = BindableProperty.Create(
  94. nameof(Date),
  95. typeof(DateTime),
  96. typeof(MobileDateButton)
  97. );
  98. public DateTime Date
  99. {
  100. get => (DateTime)GetValue(DateProperty);
  101. set => SetValue(DateProperty,value);
  102. }
  103. private static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  104. nameof(TextColor),
  105. typeof(Color),
  106. typeof(MobileDateButton),
  107. XF.Material.Forms.Material.Color.OnSecondary);
  108. public Color TextColor
  109. {
  110. get => (Color)GetValue(TextColorProperty);
  111. set => SetValue(TextColorProperty, value);
  112. }
  113. private static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  114. nameof(TypeScale),
  115. typeof(MaterialTypeScale),
  116. typeof(MobileDateButton),
  117. MaterialTypeScale.Button);
  118. public MaterialTypeScale TypeScale
  119. {
  120. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  121. set => SetValue(TypeScaleProperty, value);
  122. }
  123. private static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(
  124. nameof(ButtonColor),
  125. typeof(Color),
  126. typeof(MobileDateButton),
  127. XF.Material.Forms.Material.Color.Secondary);
  128. public Color ButtonColor
  129. {
  130. get => (Color)GetValue(ButtonColorProperty);
  131. set => SetValue(ButtonColorProperty, value);
  132. }
  133. private static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  134. nameof(BorderColor),
  135. typeof(Color),
  136. typeof(MobileDateButton),
  137. XF.Material.Forms.Material.Color.SecondaryVariant);
  138. public Color BorderColor
  139. {
  140. get => (Color)GetValue(BorderColorProperty);
  141. set => SetValue(BorderColorProperty, value);
  142. }
  143. public MobileDateButton()
  144. {
  145. InitializeComponent();
  146. }
  147. private void _frame_OnClicked(object sender, EventArgs e)
  148. {
  149. MobileDateSelector popupContent = new MobileDateSelector();
  150. popupContent.Date = Date;
  151. popupContent.Changed += (o, args) =>
  152. {
  153. Date = args.Date;
  154. DoChanged();
  155. PopupManager.DismissPopup();
  156. };
  157. popupContent.Cancelled += (o, args) =>
  158. {
  159. PopupManager.DismissPopup();
  160. };
  161. popupContent.HorizontalOptions = LayoutOptions.Fill;
  162. popupContent.VerticalOptions = LayoutOptions.Fill;
  163. PopupManager.ShowPopup(
  164. this,
  165. () => popupContent,
  166. new PopupManagerConfiguration()
  167. {
  168. Modal = true,
  169. RequestedHeight = 500,
  170. RequestedWidth = 350
  171. }
  172. );
  173. }
  174. protected virtual void DoChanged()
  175. {
  176. Changed?.Invoke(this,new DateButtonChangedArgs(Date));
  177. }
  178. private void DateTimeFormatter_OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  179. {
  180. OnPropertyChanged(nameof(Date));
  181. }
  182. }
  183. }