From: Seunghyun Choi Date: Wed, 22 Feb 2017 05:10:16 +0000 (+0900) Subject: Add CalendarView X-Git-Tag: accepted/tizen/ivi/20170324.024757~15^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f10deb37d3749db85da45bec01a67f2662aee405;p=platform%2Fcore%2Fcsapi%2Fxamarin-forms-extension.git Add CalendarView - RFC : http://suprem.sec.samsung.net/confluence/display/SPTDTLC/%5BFormsTizen%5D+RFC+11+-+CalendarView Change-Id: I35e33cc6f32dd4d68642b5eaecd1293ea02ede4d Signed-off-by: Seunghyun Choi --- diff --git a/Tizen.Xamarin.Forms.Extension.Renderer/CalendarViewRenderer.cs b/Tizen.Xamarin.Forms.Extension.Renderer/CalendarViewRenderer.cs new file mode 100644 index 0000000..614f4b1 --- /dev/null +++ b/Tizen.Xamarin.Forms.Extension.Renderer/CalendarViewRenderer.cs @@ -0,0 +1,74 @@ +using Xamarin.Forms; +using Xamarin.Forms.Platform.Tizen; +using TForms = Xamarin.Forms.Platform.Tizen.Forms; +using ECalendar = ElmSharp.Calendar; +using Tizen.Xamarin.Forms.Extension; +using Tizen.Xamarin.Forms.Extension.Renderer; + +[assembly: ExportRenderer(typeof(CalendarView), typeof(CalendarViewRenderer))] + +namespace Tizen.Xamarin.Forms.Extension.Renderer +{ + public class CalendarViewRenderer : ViewRenderer + { + public CalendarViewRenderer() + { + RegisterPropertyHandler(CalendarView.MinimumDateProperty, UpdateMinimumDate); + RegisterPropertyHandler(CalendarView.MaximumDateProperty, UpdateMaximumDate); + RegisterPropertyHandler(CalendarView.FirstDayOfWeekProperty, UpdateFirstDayOfWeek); + RegisterPropertyHandler(CalendarView.WeekDayNamesProperty, UpdateWeekDayNames); + RegisterPropertyHandler(CalendarView.SelectedDateProperty, UpdateSelectedDate); + } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + if (Control == null) + { + var calrendarView = new ECalendar(TForms.Context.MainWindow); + SetNativeControl(calrendarView); + } + + if (e.OldElement != null) + { + Control.DateChanged -= DateChangedHandler; + } + + if (e.NewElement != null) + { + Control.DateChanged += DateChangedHandler; + } + + base.OnElementChanged(e); + } + + void DateChangedHandler(object sender, ElmSharp.DateChangedEventArgs e) + { + ((IElementController)Element).SetValueFromRenderer(CalendarView.SelectedDateProperty, e.NewDate); + } + + void UpdateMinimumDate() + { + Control.MinimumYear = Element.MinimumDate.Year; + } + + void UpdateMaximumDate() + { + Control.MaximumYear = Element.MaximumDate.Year; + } + + void UpdateFirstDayOfWeek() + { + Control.FirstDayOfWeek = Element.FirstDayOfWeek; + } + + void UpdateWeekDayNames() + { + Control.WeekDayNames = Element.WeekDayNames; + } + + void UpdateSelectedDate() + { + Control.SelectedDate = Element.SelectedDate; + } + } +} \ No newline at end of file diff --git a/Tizen.Xamarin.Forms.Extension.Renderer/Tizen.Xamarin.Forms.Extension.Renderer.csproj b/Tizen.Xamarin.Forms.Extension.Renderer/Tizen.Xamarin.Forms.Extension.Renderer.csproj index 49f809b..af02ca8 100644 --- a/Tizen.Xamarin.Forms.Extension.Renderer/Tizen.Xamarin.Forms.Extension.Renderer.csproj +++ b/Tizen.Xamarin.Forms.Extension.Renderer/Tizen.Xamarin.Forms.Extension.Renderer.csproj @@ -39,6 +39,7 @@ + @@ -79,4 +80,4 @@ <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory) true - + diff --git a/Tizen.Xamarin.Forms.Extension/CalendarView.cs b/Tizen.Xamarin.Forms.Extension/CalendarView.cs new file mode 100644 index 0000000..3935162 --- /dev/null +++ b/Tizen.Xamarin.Forms.Extension/CalendarView.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using Xamarin.Forms; + +namespace Tizen.Xamarin.Forms.Extension +{ + /// + /// This is a calendar widget. It helps applications to flexibly display a calendar with day of the week, date, year and month. + /// + /// + /// + /// new CalendarView + /// { + /// FirstDayOfWeek = DayOfWeek.Monday, + /// MinimumDate = DateTime.MinValue, + /// MaximumDate = DateTime.MaxValue + /// } + /// + /// + public class CalendarView : View + { + public static readonly BindableProperty MinimumDateProperty = BindableProperty.Create(nameof(MinimumDate), typeof(DateTime), typeof(CalendarView), DateTime.MinValue); + + public static readonly BindableProperty MaximumDateProperty = BindableProperty.Create(nameof(MaximumDate), typeof(DateTime), typeof(CalendarView), DateTime.MaxValue); + + public static readonly BindableProperty FirstDayOfWeekProperty = BindableProperty.Create(nameof(FirstDayOfWeek), typeof(DayOfWeek), typeof(CalendarView), DayOfWeek.Sunday); + + public static readonly BindableProperty WeekDayNamesProperty = BindableProperty.Create(nameof(WeekDayNames), typeof(IReadOnlyList), typeof(CalendarView), new List { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }, validateValue: (bindable, value) => + { + List weekDayNames = (List)value; + int count = weekDayNames.Count; + + return (count == 7); + }); + + public static readonly BindableProperty SelectedDateProperty = BindableProperty.Create(nameof(SelectedDate), typeof(DateTime), typeof(CalendarView), DateTime.Today, defaultBindingMode: BindingMode.TwoWay, propertyChanged: (bindable, oldValue, newValue) => + { + ((CalendarView)bindable).SelectedDateChanged?.Invoke(bindable, new DateChangedEventArgs((DateTime)oldValue, (DateTime)newValue)); + }); + + public event EventHandler SelectedDateChanged; + + /// + /// Gets or sets the MinimumDate value. + /// Can use only Year. + /// + public DateTime MinimumDate + { + get { return (DateTime)GetValue(MinimumDateProperty); } + set { SetValue(MinimumDateProperty, value); } + } + + /// + /// Gets or sets the MaximumDate value. + /// Can use only Year. + /// + public DateTime MaximumDate + { + get { return (DateTime)GetValue(MaximumDateProperty); } + set { SetValue(MaximumDateProperty, value); } + } + + /// + /// Gets or sets the first day of week. + /// + public DayOfWeek FirstDayOfWeek + { + get { return (DayOfWeek)GetValue(FirstDayOfWeekProperty); } + set { SetValue(FirstDayOfWeekProperty, value); } + } + + /// + /// Gets or sets the weekday names displayed by the calendar. + /// The number of items in WeekdayNames must be 7. + /// + public IReadOnlyList WeekDayNames + { + get { return (IReadOnlyList)GetValue(WeekDayNamesProperty); } + set { SetValue(WeekDayNamesProperty, value); } + } + + /// + /// Gets or sets the selected date. + /// + public DateTime SelectedDate + { + get { return (DateTime)GetValue(SelectedDateProperty); } + set { SetValue(SelectedDateProperty, value); } + } + } +} \ No newline at end of file diff --git a/Tizen.Xamarin.Forms.Extension/Tizen.Xamarin.Forms.Extension.csproj b/Tizen.Xamarin.Forms.Extension/Tizen.Xamarin.Forms.Extension.csproj index 6f0b282..973f90b 100644 --- a/Tizen.Xamarin.Forms.Extension/Tizen.Xamarin.Forms.Extension.csproj +++ b/Tizen.Xamarin.Forms.Extension/Tizen.Xamarin.Forms.Extension.csproj @@ -59,6 +59,7 @@ +