using System; using System.Globalization; using InABox.Core; using Syncfusion.XForms.Core; using Syncfusion.XForms.PopupLayout; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.Resources.Typography; namespace InABox.Mobile { public class DateButtonChangedArgs : EventArgs { public DateTime Date { get; private set; } public DateButtonChangedArgs(DateTime date) { Date = date; } } public delegate void DateButtonChanged(object sender, DateButtonChangedArgs args); public class DateTimeFormatter : IValueConverter { public String Prefix { get; set; } public String Format { get; set; } public String Prompt { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if ((value is DateTime date) && (!date.IsEmpty())) { var sFormat = String.IsNullOrWhiteSpace(Format) ? "dd MMMM yy" : Format; String fmt = "{0} {1:" + sFormat + "}"; return String.Format(fmt,Prefix,date).Trim(); } return String.IsNullOrWhiteSpace(Prompt) ? "Select Date" : Prompt; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } public DateTimeFormatter() { } } [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MobileDateButton { private SfPopupLayout popup; public event DateButtonChanged Changed; public String Prompt { get => _datetimeformatter.Prompt; set => _datetimeformatter.Prompt = value; } public String Prefix { get => _datetimeformatter.Prefix; set => _datetimeformatter.Prefix = value; } public String Format { get => _datetimeformatter.Format; set => _datetimeformatter.Format = value; } public static readonly BindableProperty DateProperty = BindableProperty.Create( nameof(Date), typeof(DateTime), typeof(MobileDateButton)); public DateTime Date { get => (DateTime)GetValue(DateProperty); set => SetValue(DateProperty,value); } private static readonly BindableProperty TextColorProperty = BindableProperty.Create( nameof(TextColor), typeof(Color), typeof(MobileDateButton), XF.Material.Forms.Material.Color.OnSecondary); public Color TextColor { get => (Color)GetValue(TextColorProperty); set => SetValue(TextColorProperty, value); } private static readonly BindableProperty TypeScaleProperty = BindableProperty.Create( nameof(TypeScale), typeof(MaterialTypeScale), typeof(MobileDateButton), MaterialTypeScale.Button); public MaterialTypeScale TypeScale { get => (MaterialTypeScale)GetValue(TypeScaleProperty); set => SetValue(TypeScaleProperty, value); } private static readonly BindableProperty ButtonColorProperty = BindableProperty.Create( nameof(ButtonColor), typeof(Color), typeof(MobileDateButton), XF.Material.Forms.Material.Color.Secondary); public Color ButtonColor { get => (Color)GetValue(ButtonColorProperty); set => SetValue(ButtonColorProperty, value); } private static readonly BindableProperty BorderColorProperty = BindableProperty.Create( nameof(BorderColor), typeof(Color), typeof(MobileDateButton), XF.Material.Forms.Material.Color.SecondaryVariant); public Color BorderColor { get => (Color)GetValue(BorderColorProperty); set => SetValue(BorderColorProperty, value); } public MobileDateButton() { InitializeComponent(); popup = new SfPopupLayout(); } private void _frame_OnClicked(object sender, EventArgs e) { popup.PopupView.WidthRequest = 300; popup.PopupView.HeightRequest = 500; popup.PopupView.ShowHeader = false; popup.PopupView.ShowFooter = false; MobileDateSelector popupContent = new MobileDateSelector(); popupContent.Date = Date; popupContent.Changed += (o, args) => { Date = args.Date; Changed?.Invoke(this,new DateButtonChangedArgs(Date)); popup.Closing -= RestrictClose; popup.Dismiss(); }; popupContent.Cancelled += (o, args) => { popup.Closing -= RestrictClose; popup.Dismiss(); }; popup.Closing += RestrictClose; popupContent.Margin = new Thickness(10); popupContent.HorizontalOptions = LayoutOptions.Fill; popupContent.VerticalOptions = LayoutOptions.Fill; popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent); popup.Show(); } private void RestrictClose(object sender, CancelEventArgs e) { e.Cancel = true; } } }