| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 | using System;using System.Globalization;using System.Net;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 TimeButtonChangedArgs : EventArgs    {        public TimeSpan Time { get; private set; }        public TimeButtonChangedArgs(TimeSpan time)        {            Time = time;        }    }        public delegate void TimeButtonChanged(object sender, TimeButtonChangedArgs args);    public class TimeSpanFormatter : AbstractConverter<TimeSpan,String>    {        public string Format { get; set; }        public String Prompt { get; set; }        public String Prefix { get; set; }        protected override string Convert(TimeSpan value, object? parameter = null)        {            if (value != TimeSpan.Zero || String.IsNullOrWhiteSpace(Prompt))            {                try                {                    var sFormat = String.IsNullOrWhiteSpace(Format)                        ? "hh\\:mm tt"                        : Format;                    String fmt = "{0} {1:" + sFormat + "}";                    return String.Format(fmt,Prefix,DateTime.Today.Add(value)).Trim();                    //return $"{DateTime.Today.Add(value):hh\\:mm tt}";                }                catch (Exception e)                {                }            }            return Prompt;        }            }        [XamlCompilation(XamlCompilationOptions.Compile)]    public partial class MobileTimeButton    {        private SfPopupLayout popup;                public event TimeButtonChanged Changed;                public static readonly BindableProperty PromptProperty = BindableProperty.Create(            nameof(Prompt),             typeof(string),             typeof(MobileTimeButton)        );                public String Prompt        {            get => (string)GetValue(PromptProperty);            set            {                _timespanFormatter.Prompt = value;                SetValue(PromptProperty, value);            }        }                public static readonly BindableProperty PrefixProperty = BindableProperty.Create(            nameof(Prefix),             typeof(string),             typeof(MobileTimeButton)        );                public String Prefix        {            get => (string)GetValue(PrefixProperty);            set            {                _timespanFormatter.Prefix = value;                SetValue(PrefixProperty, value);            }        }                public static readonly BindableProperty FormatProperty = BindableProperty.Create(            nameof(Format),             typeof(string),             typeof(MobileTimeButton)        );                public String Format        {            get => (string)GetValue(FormatProperty);            set            {                _timespanFormatter.Format = value;                SetValue(FormatProperty, value);            }        }                public static readonly BindableProperty TimeProperty = BindableProperty.Create(            nameof(Time),             typeof(TimeSpan),             typeof(MobileTimeButton));                public TimeSpan Time        {            get => (TimeSpan)GetValue(TimeProperty);            set => SetValue(TimeProperty,value);        }                public static readonly BindableProperty TextColorProperty = BindableProperty.Create(            nameof(TextColor),            typeof(Color),            typeof(MobileTimeButton),            XF.Material.Forms.Material.Color.OnSecondary);        public Color TextColor        {            get => (Color)GetValue(TextColorProperty);            set => SetValue(TextColorProperty, value);        }                public static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(            nameof(TypeScale),             typeof(MaterialTypeScale),             typeof(MobileTimeButton),             MaterialTypeScale.Button);                public MaterialTypeScale TypeScale        {            get => (MaterialTypeScale)GetValue(TypeScaleProperty);            set => SetValue(TypeScaleProperty, value);        }                public static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(            nameof(ButtonColor),            typeof(Color),            typeof(MobileTimeButton),            XF.Material.Forms.Material.Color.Secondary);        public Color ButtonColor        {            get => (Color)GetValue(ButtonColorProperty);            set => SetValue(ButtonColorProperty, value);        }        public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(            nameof(BorderColor),            typeof(Color),            typeof(MobileTimeButton),            XF.Material.Forms.Material.Color.SecondaryVariant);        public Color BorderColor        {            get => (Color)GetValue(BorderColorProperty);            set => SetValue(BorderColorProperty, value);        }                // public static readonly BindableProperty PaddingProperty = BindableProperty.Create(        //     nameof(Padding),        //     typeof(Thickness),        //     typeof(TimeButton),        //     new Thickness(5));        //        // public new Thickness Padding        // {        //     get => (Thickness)GetValue(PaddingProperty);        //     set => SetValue(PaddingProperty, value);        // }                public MobileTimeButton()        {            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;            //popup.PopupView.PopupStyle.HasShadow = false;            popup.PopupView.PopupStyle.CornerRadius = 5;            //popup.PopupView.Background = new SolidColorBrush(Color.White);            MobileTimeSelector popupContent = new MobileTimeSelector();            popupContent.Time = Time;            popupContent.Changed += (o, args) =>            {                Time = args.Time;                DoChanged();                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();        }        protected virtual void DoChanged()        {            Changed?.Invoke(this,new TimeButtonChangedArgs(Time));        }        private void RestrictClose(object sender, CancelEventArgs e)        {            e.Cancel = true;        }    }}
 |