1 /* Copyright (c) 2021 Samsung Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 using Tizen.NUI.BaseComponents;
19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
21 using System.ComponentModel;
22 using System.Globalization;
23 using System.Diagnostics.CodeAnalysis;
24 using Tizen.NUI.Binding;
26 namespace Tizen.NUI.Components
29 /// DateChangedEventArgs is a class to notify changed DatePicker value argument which will sent to user.
31 /// <since_tizen> 9 </since_tizen>
32 public class DateChangedEventArgs : EventArgs
35 /// DateChangedEventArgs default constructor.
36 /// <param name="date">date value of DatePicker.</param>
38 [EditorBrowsable(EditorBrowsableState.Never)]
39 public DateChangedEventArgs(DateTime date)
45 /// DateChangedEventArgs default constructor.
46 /// <returns>The current date value of DatePicker.</returns>
48 /// <since_tizen> 9 </since_tizen>
49 public DateTime Date { get; }
53 /// DatePicker is a class which provides a function that allows the user to select
54 /// a date through a scrolling motion by expressing the specified value as a list.
55 /// DatePicker expresses the current date using the locale information of the system.
56 /// Year range is 1970~2038 (glibc time_t struct min, max value)
58 /// <since_tizen> 9 </since_tizen>
59 public class DatePicker : Control
64 [EditorBrowsable(EditorBrowsableState.Never)]
65 public static readonly BindableProperty DateProperty = BindableProperty.Create(nameof(Date), typeof(DateTime), typeof(DatePicker), default(DateTime), propertyChanged: (bindable, oldValue, newValue) =>
67 var instance = (DatePicker)bindable;
70 instance.InternalDate = (DateTime)newValue;
73 defaultValueCreator: (bindable) =>
75 var instance = (DatePicker)bindable;
76 return instance.InternalDate;
79 private DateTime currentDate;
80 private Picker dayPicker;
81 private Picker monthPicker;
82 private Picker yearPicker;
85 /// Creates a new instance of DatePicker.
87 /// <since_tizen> 9 </since_tizen>
93 /// Creates a new instance of DatePicker.
95 /// <param name="style">Creates DatePicker by special style defined in UX.</param>
96 /// <since_tizen> 9 </since_tizen>
97 public DatePicker(string style) : base(style)
102 /// Creates a new instance of DatePicker.
104 /// <param name="datePickerStyle">Creates DatePicker by style customized by user.</param>
105 /// <since_tizen> 9 </since_tizen>
106 public DatePicker(DatePickerStyle datePickerStyle) : base(datePickerStyle)
112 /// Dispose DatePicker and all children on it.
114 /// <param name="type">Dispose type.</param>
115 [EditorBrowsable(EditorBrowsableState.Never)]
116 protected override void Dispose(DisposeTypes type)
123 if (type == DisposeTypes.Explicit)
126 Utility.Dispose(monthPicker);
129 Utility.Dispose(dayPicker);
132 Utility.Dispose(yearPicker);
140 /// An event emitted when DatePicker value changed, user can subscribe or unsubscribe to this event handler.
142 /// <since_tizen> 9 </since_tizen>
143 public event EventHandler<DateChangedEventArgs> DateChanged;
146 /// The Date value of DatePicker.
148 /// <since_tizen> 9 </since_tizen>
153 return (DateTime)GetValue(DateProperty);
157 SetValue(DateProperty, value);
158 NotifyPropertyChanged();
161 private DateTime InternalDate
170 dayPicker.CurrentValue = currentDate.Day;
171 monthPicker.CurrentValue = currentDate.Month;
172 yearPicker.CurrentValue = currentDate.Year;
177 /// Initialize TimePicker object.
179 [EditorBrowsable(EditorBrowsableState.Never)]
180 public override void OnInitialize()
183 SetAccessibilityConstructor(Role.DateEditor);
185 dayPicker = new Picker()
190 dayPicker.ValueChanged += OnDayValueChanged;
192 monthPicker = new Picker()
197 monthPicker.ValueChanged += OnMonthValueChanged;
199 yearPicker = new Picker()
204 yearPicker.ValueChanged += OnYearValueChanged;
206 currentDate = DateTime.Now;
207 dayPicker.CurrentValue = currentDate.Day;
208 monthPicker.CurrentValue = currentDate.Month;
209 yearPicker.CurrentValue = currentDate.Year;
215 [EditorBrowsable(EditorBrowsableState.Never)]
216 [SuppressMessage("Microsoft.Reliability",
217 "CA2000:DisposeObjectsBeforeLosingScope",
218 Justification = "The CellPadding will be dispose when the date picker disposed")]
219 public override void ApplyStyle(ViewStyle viewStyle)
221 base.ApplyStyle(viewStyle);
223 if (viewStyle is DatePickerStyle datePickerStyle && Layout is LinearLayout linearLayout)
225 linearLayout.CellPadding = new Size(datePickerStyle.CellPadding.Width, datePickerStyle.CellPadding.Height);
227 yearPicker.ApplyStyle(datePickerStyle.Pickers);
228 monthPicker.ApplyStyle(datePickerStyle.Pickers);
229 dayPicker.ApplyStyle(datePickerStyle.Pickers);
233 private void Initialize()
235 HeightSpecification = LayoutParamPolicies.MatchParent;
237 Layout = new LinearLayout() {
238 LinearOrientation = LinearLayout.Orientation.Horizontal,
246 private void OnDayValueChanged(object sender, ValueChangedEventArgs e)
248 if (currentDate.Day == e.Value) return;
250 currentDate = new DateTime(currentDate.Year, currentDate.Month, e.Value);
255 private void OnMonthValueChanged(object sender, ValueChangedEventArgs e)
257 if (currentDate.Month == e.Value) return;
259 currentDate = new DateTime(currentDate.Year, e.Value, currentDate.Day);
265 private void OnYearValueChanged(object sender, ValueChangedEventArgs e)
267 if (currentDate.Year == e.Value) return;
269 currentDate = new DateTime(e.Value, currentDate.Month, currentDate.Day);
275 private void OnDateChanged()
277 DateChangedEventArgs eventArgs = new DateChangedEventArgs(currentDate);
278 DateChanged?.Invoke(this, eventArgs);
281 private void MaxDaySet()
283 int maxDaysInMonth = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
284 dayPicker.MaxValue = maxDaysInMonth;
285 if (currentDate.Day > maxDaysInMonth)
287 currentDate = new DateTime(currentDate.Year, currentDate.Month, maxDaysInMonth);
288 dayPicker.CurrentValue = maxDaysInMonth;
291 currentDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day);
294 //FIXME: There is no way to know when system locale changed in NUI.
295 // Pickers order and Month text has to be follow system locale.
296 private void PickersOrderSet()
298 DateTimeFormatInfo DateFormat = CultureInfo.CurrentCulture.DateTimeFormat;
299 String temp = DateFormat.ShortDatePattern;
300 String[] strArray = temp.Split(' ', '/');
301 foreach (String format in strArray) {
302 if (format.IndexOf("M") != -1|| format.IndexOf("m") != -1) Add(monthPicker);
303 else if (format.IndexOf("d") != -1 || format.IndexOf("D") != -1) Add(dayPicker);
304 else if (format.IndexOf("y") != -1 || format.IndexOf("Y") != -1) Add(yearPicker);
308 private void SetMonthText()
310 CultureInfo info = CultureInfo.CurrentCulture;
311 monthPicker.DisplayedValues = new ReadOnlyCollection<string>(info.DateTimeFormat.AbbreviatedMonthNames);