lilcodelab / Xamarin.Plugin.Calendar

Calendar plugin for Xamarin.Forms
MIT License
260 stars 61 forks source link

[Feature Request] Suggest a new property, StartDay. #73

Open kuririn2001 opened 3 years ago

kuririn2001 commented 3 years ago

Hello. I would like to propose a new StartDay property. Currently, we are using the FirstDayOfWeek property of CultureInfo. In the case of Japan (ja-JP), CultureInfo's FirstDayOfWeek is set to start on Sunday, but some people prefer a calendar that starts on Monday, and I would like to switch between Sunday and Monday. Therefore, I would like this property to be newly added.

  Add the following code to CalendarPlugin/Shared/Controls/Calendar.xaml.

            <controls:MonthDaysView
                x:Name="monthDaysView"
                AnimateCalendar="{Binding AnimateCalendar, Source={x:Reference calendar}}"
                Culture="{Binding Culture, Source={x:Reference calendar}}"
                StartDay="{Binding StartDay, Source={x:Reference calendar}}" <- add

The description here is an error, so fix it. DaysTitleMaximumLength="{Binding DaysTitleMaximumLength}, Source={x:Reference calendar}}" ↓ DaysTitleMaximumLength="{Binding DaysTitleMaximumLength, Source={x:Reference calendar}}"

Add the following code to CalendarPlugin/Shared/Controls/Calendar.xaml.cs.

    public static readonly BindableProperty StartDayProperty =
        BindableProperty.Create(nameof(StartDay), typeof(DayOfWeek), typeof(Calendar), DayOfWeek.Sunday,
            propertyChanged: (bindable, oldValue, newValue) => {
                (bindable as Calendar).ChangeStartDay((DayOfWeek)newValue, (DayOfWeek)oldValue);
            },
            defaultBindingMode: BindingMode.TwoWay);

    protected void ChangeStartDay(DayOfWeek newValue, DayOfWeek oldValue)
    {
        if (newValue == oldValue) return;
        StartDay = newValue;
    }

    public DayOfWeek StartDay {
        get => (DayOfWeek)GetValue(StartDayProperty);
        set => SetValue(StartDayProperty, value);
    }

Added the following code to CalendarPlugin/Shared/Controls/MonthDaysView.xaml.cs.

    /// <summary> Bindable property for StartDay </summary>
    public static readonly BindableProperty StartDayProperty =
        BindableProperty.Create(nameof(StartDay), typeof(DayOfWeek), typeof(MonthDaysView), DayOfWeek.Sunday,
                        propertyChanged: (bindable, oldValue, newValue) => {
                            (bindable as MonthDaysView).ChangeStartDay((DayOfWeek)newValue, (DayOfWeek)oldValue);
                        },
                        defaultBindingMode: BindingMode.TwoWay);

    protected void ChangeStartDay(DayOfWeek newValue, DayOfWeek oldValue)
    {
        if (newValue == oldValue) return;
        StartDay = newValue;
    }

    public DayOfWeek StartDay {
        get => (DayOfWeek)GetValue(StartDayProperty);
        set => SetValue(StartDayProperty, value);
    }

----------------------------------------------------- 

protected override void OnPropertyChanged([CallerMemberName] string propertyName = null) : : : case nameof(StartDay): <- add case nameof(Culture): UpdateDayTitles(); UpdateDays(AnimateCalendar); break;

----------------------------------------------------- 

    private void UpdateDayTitles()
    {

// var dayNumber = (int)Culture.DateTimeFormat.FirstDayOfWeek; <- delete var dayNumber = (int)StartDay; <- add

----------------------------------------------------- 

    private void LoadDays()
    {
        var monthStart = new DateTime(DisplayedMonthYear.Year, DisplayedMonthYear.Month, 1);

// var addDays = ((int)Culture.DateTimeFormat.FirstDayOfWeek) - (int)monthStart.DayOfWeek; <- delete var addDays = ((int)StartDay) - (int)monthStart.DayOfWeek; <- add

-----------------------------------------------------  Thank you for your cooperation.