MobileTimeButton.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 : UtilityConverter<TimeSpan,String>
  21. {
  22. public string Format { get; set; }
  23. public String Prompt { get; set; }
  24. public String Prefix { get; set; }
  25. protected override string Convert(TimeSpan value)
  26. {
  27. if (value != TimeSpan.Zero || String.IsNullOrWhiteSpace(Prompt))
  28. {
  29. try
  30. {
  31. var sFormat = String.IsNullOrWhiteSpace(Format)
  32. ? "hh\\:mm tt"
  33. : Format;
  34. String fmt = "{0} {1:" + sFormat + "}";
  35. return String.Format(fmt,Prefix,DateTime.Today.Add(value)).Trim();
  36. //return $"{DateTime.Today.Add(value):hh\\:mm tt}";
  37. }
  38. catch (Exception e)
  39. {
  40. }
  41. }
  42. return Prompt;
  43. }
  44. }
  45. [XamlCompilation(XamlCompilationOptions.Compile)]
  46. public partial class MobileTimeButton
  47. {
  48. private SfPopupLayout popup;
  49. public event TimeButtonChanged Changed;
  50. public static readonly BindableProperty PromptProperty = BindableProperty.Create(
  51. nameof(Prompt),
  52. typeof(string),
  53. typeof(MobileTimeButton)
  54. );
  55. public String Prompt
  56. {
  57. get => (string)GetValue(PromptProperty);
  58. set
  59. {
  60. _timespanFormatter.Prompt = value;
  61. SetValue(PromptProperty, value);
  62. }
  63. }
  64. public static readonly BindableProperty PrefixProperty = BindableProperty.Create(
  65. nameof(Prefix),
  66. typeof(string),
  67. typeof(MobileTimeButton)
  68. );
  69. public String Prefix
  70. {
  71. get => (string)GetValue(PrefixProperty);
  72. set
  73. {
  74. _timespanFormatter.Prefix = value;
  75. SetValue(PrefixProperty, value);
  76. }
  77. }
  78. public static readonly BindableProperty FormatProperty = BindableProperty.Create(
  79. nameof(Format),
  80. typeof(string),
  81. typeof(MobileTimeButton)
  82. );
  83. public String Format
  84. {
  85. get => (string)GetValue(FormatProperty);
  86. set
  87. {
  88. _timespanFormatter.Format = value;
  89. SetValue(FormatProperty, value);
  90. }
  91. }
  92. public static readonly BindableProperty TimeProperty = BindableProperty.Create(
  93. nameof(Time),
  94. typeof(TimeSpan),
  95. typeof(MobileTimeButton));
  96. public TimeSpan Time
  97. {
  98. get => (TimeSpan)GetValue(TimeProperty);
  99. set => SetValue(TimeProperty,value);
  100. }
  101. public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  102. nameof(TextColor),
  103. typeof(Color),
  104. typeof(MobileTimeButton),
  105. XF.Material.Forms.Material.Color.OnSecondary);
  106. public Color TextColor
  107. {
  108. get => (Color)GetValue(TextColorProperty);
  109. set => SetValue(TextColorProperty, value);
  110. }
  111. public static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  112. nameof(TypeScale),
  113. typeof(MaterialTypeScale),
  114. typeof(MobileTimeButton),
  115. MaterialTypeScale.Button);
  116. public MaterialTypeScale TypeScale
  117. {
  118. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  119. set => SetValue(TypeScaleProperty, value);
  120. }
  121. public static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(
  122. nameof(ButtonColor),
  123. typeof(Color),
  124. typeof(MobileTimeButton),
  125. XF.Material.Forms.Material.Color.Secondary);
  126. public Color ButtonColor
  127. {
  128. get => (Color)GetValue(ButtonColorProperty);
  129. set => SetValue(ButtonColorProperty, value);
  130. }
  131. public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  132. nameof(BorderColor),
  133. typeof(Color),
  134. typeof(MobileTimeButton),
  135. XF.Material.Forms.Material.Color.SecondaryVariant);
  136. public Color BorderColor
  137. {
  138. get => (Color)GetValue(BorderColorProperty);
  139. set => SetValue(BorderColorProperty, value);
  140. }
  141. // public static readonly BindableProperty PaddingProperty = BindableProperty.Create(
  142. // nameof(Padding),
  143. // typeof(Thickness),
  144. // typeof(TimeButton),
  145. // new Thickness(5));
  146. //
  147. // public new Thickness Padding
  148. // {
  149. // get => (Thickness)GetValue(PaddingProperty);
  150. // set => SetValue(PaddingProperty, value);
  151. // }
  152. public MobileTimeButton()
  153. {
  154. InitializeComponent();
  155. popup = new SfPopupLayout();
  156. }
  157. private void _frame_OnClicked(object sender, EventArgs e)
  158. {
  159. popup.PopupView.WidthRequest = 300;
  160. popup.PopupView.HeightRequest = 500;
  161. popup.PopupView.ShowHeader = false;
  162. popup.PopupView.ShowFooter = false;
  163. //popup.PopupView.PopupStyle.HasShadow = false;
  164. popup.PopupView.PopupStyle.CornerRadius = 5;
  165. //popup.PopupView.Background = new SolidColorBrush(Color.White);
  166. MobileTimeSelector popupContent = new MobileTimeSelector();
  167. popupContent.Time = Time;
  168. popupContent.Changed += (o, args) =>
  169. {
  170. Time = args.Time;
  171. DoChanged();
  172. popup.Closing -= RestrictClose;
  173. popup.Dismiss();
  174. };
  175. popupContent.Cancelled += (o, args) =>
  176. {
  177. popup.Closing -= RestrictClose;
  178. popup.Dismiss();
  179. };
  180. popup.Closing += RestrictClose;
  181. popupContent.Margin = new Thickness(10);
  182. popupContent.HorizontalOptions = LayoutOptions.Fill;
  183. popupContent.VerticalOptions = LayoutOptions.Fill;
  184. popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
  185. popup.Show();
  186. }
  187. protected virtual void DoChanged()
  188. {
  189. Changed?.Invoke(this,new TimeButtonChangedArgs(Time));
  190. }
  191. private void RestrictClose(object sender, CancelEventArgs e)
  192. {
  193. e.Cancel = true;
  194. }
  195. }
  196. }