Custom Date Picker Renderer

shreyashreya INMember ✭✭

I have to make a date time custom date picker. For it , I have made a renderer. The problem that I am facing is the date picker shows the correct minimum date when the current time is less than 6 pm. After 6 pm, it shows one hour behind the current time. Please help. The code is as follows:
[assembly: ExportRendererAttribute(typeof(CustomDatePicker), typeof(WingMan.iOS.CustomRenderers.DatePickerCustomRenderer))]

namespace WingMan.iOS.CustomRenderers
{
internal class NoCaretField : UITextField
{
public NoCaretField() : base(new RectangleF())
{
}

    public override RectangleF GetCaretRectForPosition(UITextPosition position)
    {
        return new RectangleF();
    }
}

public class DatePickerCustomRenderer : ViewRenderer<DatePicker, UITextField>
{
    UIDatePicker _picker = new UIDatePicker();
    UIColor _defaultTextColor;
    bool _disposed;
    CustomDatePicker view;
    NoCaretField entry = new NoCaretField { BorderStyle = UITextBorderStyle.RoundedRect };
    IElementController ElementController => Element as IElementController;


    protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement == null)
            return;
        if (e.NewElement != null)
            view = e.NewElement as CustomDatePicker;

        if (Control == null)
        {
            entry.EditingDidBegin += OnStarted;
            entry.EditingDidEnd += OnEnded;
            DateTime now = DateTime.Now;
            if (DateTime.Now.IsDaylightSavingTime())
            {
                now = now.AddHours(1);
            }
            var currentDate = Extensions.NSExtensions.DateTimeToNSDate(now);

            _picker = new UIDatePicker();
            _picker.Mode = UIDatePickerMode.DateAndTime;

            var utcDateTime = DateTime.Now;
            var formatter = new NSDateFormatter();
            formatter.TimeZone = NSTimeZone.LocalTimeZone;
            formatter.DateFormat = "MM/dd/yyyy hh:mm:ss a";
            Console.WriteLine(formatter.Parse(utcDateTime.ToString()));
            var convertedDate = NSDate.Now;
            var pickerDate = _picker.Date;
            formatter.TimeZone = NSTimeZone.LocalTimeZone;
            formatter.DateFormat = "dd-MM-yyyy hh:mm:ss a";
            var currentDateString = formatter.ToString(convertedDate);
            formatter.TimeZone = NSTimeZone.SystemTimeZone;
            currentDate = formatter.Parse(currentDateString);

            UIView.Animate(1.0f, () =>
            { }, () =>
            {
                _picker.MinimumDate = currentDate;
            });

            NSDateFormatter dateFormat = new NSDateFormatter();
            dateFormat.Locale = NSLocale.CurrentLocale;
            entry.Text = dateFormat.ToString(_picker.Date);
            var width = UIScreen.MainScreen.Bounds.Width;
            var toolbar = new UIToolbar(new RectangleF(0, 0, (float)width, 44)) { BarStyle = UIBarStyle.Default, Translucent = true };
            var spacer = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
            var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, HandleEventHandler);

            toolbar.SetItems(new[] { spacer, doneButton }, false);

            entry.InputView = _picker;
            entry.InputAccessoryView = toolbar;

            _defaultTextColor = entry.TextColor;

            SetNativeControl(entry);
        }

        UpdateDateFromModel(false);
        UpdateTextColor();
    }


    void HandleValueChanged(object sender, EventArgs e)
    {
        ElementController?.SetValueFromRenderer(DatePicker.DateProperty, _picker.Date.ToDateTime().Date);
    }

    void OnEnded(object sender, EventArgs eventArgs)
    {
        var date = DateTime.SpecifyKind((System.DateTime)_picker.Date, DateTimeKind.Utc).ToLocalTime();
        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
        view.SelectedDate = _picker.Date.ToDateTime().ToString();
        view.SelectedDateTime = DateTime.SpecifyKind((System.DateTime)_picker.Date, DateTimeKind.Utc).ToLocalTime();
    }

    void OnStarted(object sender, EventArgs eventArgs)
    {
        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
    }

    void UpdateDateFromModel(bool animate)
    {
        Control.Text = Element.Date.ToString(Element.Format);

    }

    void UpdateTextColor()
    {
        var textColor = Element.TextColor;

        if (!Element.IsEnabled)
            Control.TextColor = _defaultTextColor;
        else
            Control.TextColor = textColor.ToUIColor();
    }

    protected override void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        _disposed = true;

        if (disposing)
        {
            _defaultTextColor = null;

            if (_picker != null)
            {
                _picker.RemoveFromSuperview();
                _picker.ValueChanged -= HandleValueChanged;
                _picker.Dispose();
                _picker = null;
            }

            if (Control != null)
            {
                Control.EditingDidBegin -= OnStarted;
                Control.EditingDidEnd -= OnEnded;
            }
        }

        base.Dispose(disposing);
    }

    void HandleEventHandler(object sender, EventArgs e)
    {
        entry.ResignFirstResponder();
        view.DateSelectedEvent = view.SelectedDateTime;
        view.HandleDateSelected(view, new EventArgs());
    }

}

}

Answers

Sign In or Register to comment.