MobileTimeButton.xaml.cs 6.6 KB

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