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;
25 namespace Tizen.NUI.Components
28 /// DateChangedEventArgs is a class to notify changed DatePicker value argument which will sent to user.
30 [EditorBrowsable(EditorBrowsableState.Never)]
31 public class DateChangedEventArgs : EventArgs
34 /// DateChangedEventArgs default constructor.
35 /// <param name="date">date value of DatePicker.</param>
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 public DateChangedEventArgs(DateTime date)
44 /// DateChangedEventArgs default constructor.
45 /// <returns>The current date value of DatePicker.</returns>
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public DateTime Date { get; }
52 /// DatePicker is a class which provides a function that allows the user to select
53 /// a date through a scrolling motion by expressing the specified value as a list.
54 /// DatePicker expresses the current date using the locale information of the system.
55 /// Year range is 1970~2038 (glibc time_t struct min, max value)
57 [EditorBrowsable(EditorBrowsableState.Never)]
58 public class DatePicker : Control
60 private DateTime currentDate;
61 private Picker dayPicker;
62 private Picker monthPicker;
63 private Picker yearPicker;
64 private DatePickerStyle datePickerStyle => ViewStyle as DatePickerStyle;
67 /// Creates a new instance of DatePicker.
69 [EditorBrowsable(EditorBrowsableState.Never)]
76 /// Creates a new instance of DatePicker.
78 /// <param name="style">Creates DatePicker by special style defined in UX.</param>
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 public DatePicker(string style) : base(style)
86 /// Creates a new instance of DatePicker.
88 /// <param name="datePickerStyle">Creates DatePicker by style customized by user.</param>
89 [EditorBrowsable(EditorBrowsableState.Never)]
90 public DatePicker(DatePickerStyle datePickerStyle) : base(datePickerStyle)
97 /// Dispose DatePicker and all children on it.
99 /// <param name="type">Dispose type.</param>
100 [EditorBrowsable(EditorBrowsableState.Never)]
101 protected override void Dispose(DisposeTypes type)
108 if (type == DisposeTypes.Explicit)
111 Utility.Dispose(monthPicker);
114 Utility.Dispose(dayPicker);
117 Utility.Dispose(yearPicker);
125 /// An event emitted when DatePicker value changed, user can subscribe or unsubscribe to this event handler.
127 [EditorBrowsable(EditorBrowsableState.Never)]
128 public event EventHandler<DateChangedEventArgs> DateChanged;
131 /// The Date value of DatePicker.
133 [EditorBrowsable(EditorBrowsableState.Never)]
143 dayPicker.CurrentValue = currentDate.Day;
144 monthPicker.CurrentValue = currentDate.Month;
145 yearPicker.CurrentValue = currentDate.Year;
150 /// Initialize TimePicker object.
152 [EditorBrowsable(EditorBrowsableState.Never)]
153 public override void OnInitialize()
156 SetAccessibilityConstructor(Role.DateEditor, AccessibilityInterface.Value);
158 dayPicker = new Picker()
163 dayPicker.ValueChanged += OnDayValueChanged;
165 monthPicker = new Picker()
170 monthPicker.ValueChanged += OnMonthValueChanged;
172 yearPicker = new Picker()
177 yearPicker.ValueChanged += OnYearValueChanged;
179 currentDate = DateTime.Now;
180 dayPicker.CurrentValue = currentDate.Day;
181 monthPicker.CurrentValue = currentDate.Month;
182 yearPicker.CurrentValue = currentDate.Year;
185 [SuppressMessage("Microsoft.Reliability",
186 "CA2000:DisposeObjectsBeforeLosingScope",
187 Justification = "The CellPadding will be dispose when the date picker disposed")]
188 private void Initialize()
190 AccessibilityHighlightable = true;
191 HeightSpecification = LayoutParamPolicies.MatchParent;
193 Layout = new LinearLayout() {
194 LinearOrientation = LinearLayout.Orientation.Horizontal,
195 CellPadding = new Size(datePickerStyle.CellPadding.Width, datePickerStyle.CellPadding.Height),
200 MaxDaySet(currentDate.Month);
203 private void OnDayValueChanged(object sender, ValueChangedEventArgs e)
205 if (currentDate.Day == e.Value) return;
207 currentDate = new DateTime(currentDate.Year, currentDate.Month, e.Value);
212 private void OnMonthValueChanged(object sender, ValueChangedEventArgs e)
214 if (currentDate.Month == e.Value) return;
221 private void OnYearValueChanged(object sender, ValueChangedEventArgs e)
223 if (currentDate.Year == e.Value) return;
225 currentDate = new DateTime(e.Value, currentDate.Month, currentDate.Day);
230 private void OnDateChanged()
232 DateChangedEventArgs eventArgs = new DateChangedEventArgs(currentDate);
233 DateChanged?.Invoke(this, eventArgs);
236 private void MaxDaySet(int month)
238 int maxDaysInMonth = DateTime.DaysInMonth(currentDate.Year, month);
239 dayPicker.MaxValue = maxDaysInMonth;
240 if (currentDate.Day > maxDaysInMonth)
242 currentDate = new DateTime(currentDate.Year, month, maxDaysInMonth);
243 dayPicker.CurrentValue = maxDaysInMonth;
246 currentDate = new DateTime(currentDate.Year, month, currentDate.Day);
249 //FIXME: There is no way to know when system locale changed in NUI.
250 // Pickers order and Month text has to be follow system locale.
251 private void PickersOrderSet()
253 String locale = Environment.GetEnvironmentVariable("LC_TIME");
254 DateTimeFormatInfo DateFormat = new CultureInfo(locale, false ).DateTimeFormat;
255 String temp = DateFormat.ShortDatePattern;
256 String[] strArray = temp.Split(' ', '/');
257 foreach (String format in strArray) {
258 if (format.IndexOf("M") != -1|| format.IndexOf("m") != -1) Add(monthPicker);
259 else if (format.IndexOf("d") != -1 || format.IndexOf("D") != -1) Add(dayPicker);
260 else if (format.IndexOf("y") != -1 || format.IndexOf("Y") != -1) Add(yearPicker);
264 private void SetMonthText()
266 String locale = Environment.GetEnvironmentVariable("LC_TIME");
267 CultureInfo info = new CultureInfo(locale);
268 monthPicker.DisplayedValues = new ReadOnlyCollection<string>(info.DateTimeFormat.AbbreviatedMonthNames);