[NUI] Change date/time picker param to formal C# DateTime class instead of int value
authorWoochanlee <wc0917.lee@samsung.com>
Wed, 16 Jun 2021 07:48:30 +0000 (16:48 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Wed, 23 Jun 2021 08:25:09 +0000 (17:25 +0900)
src/Tizen.NUI.Components/Controls/DatePicker.cs
src/Tizen.NUI.Components/Controls/TimePicker.cs
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/DatePickerSample.cs
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/TimePickerSample.cs

index d70b3c1..a3aa04e 100755 (executable)
@@ -32,38 +32,20 @@ namespace Tizen.NUI.Components
     {
         /// <summary>
         /// DateChangedEventArgs default constructor.
-        /// <param name="month">month value of DatePicker.</param>
-        /// <param name="day">day value of DatePicker.</param>
-        /// <param name="year">year value of DatePicker.</param>
+        /// <param name="date">date value of DatePicker.</param>
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]   
-        public DateChangedEventArgs(int month, int day, int year)
+        public DateChangedEventArgs(DateTime date)
         {
-            Month = month;
-            Day = day;
-            Year = year;
+            Date = date;
         }
 
         /// <summary>
         /// DateChangedEventArgs default constructor.
-        /// <returns>The current month value of DatePicker.</returns>
+        /// <returns>The current date value of DatePicker.</returns>
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]   
-        public int Month { get; }
-
-        /// <summary>
-        /// DateChangedEventArgs default constructor.
-        /// <returns>The current day value of DatePicker.</returns>
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]   
-        public int Day { get; }
-
-        /// <summary>
-        /// DateChangedEventArgs default constructor.
-        /// <returns>The current year value of DatePicker.</returns>
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]   
-        public int Year { get; }
+        public DateTime Date { get; }
     }
 
     /// <summary>
@@ -75,9 +57,7 @@ namespace Tizen.NUI.Components
     [EditorBrowsable(EditorBrowsableState.Never)]
     public class DatePicker : Control
     {
-        private int day;
-        private int month;
-        private int year;
+        private DateTime currentDate;
         private Picker dayPicker;
         private Picker monthPicker;
         private Picker yearPicker;
@@ -148,63 +128,25 @@ namespace Tizen.NUI.Components
         public event EventHandler<DateChangedEventArgs> DateChanged;
         
         /// <summary>
-        /// The month value of DatePicker.
+        /// The Date value of DatePicker.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public int Month
+        public DateTime Date
         {
             get
             {
-                return month;
+                return currentDate;
             }
             set
             {
-                if (value < 1 || value > 12 || value == month) return;
-
-                month = value;
-                monthPicker.CurrentValue = month;
+                currentDate = value;
+                dayPicker.CurrentValue = currentDate.Day;
+                monthPicker.CurrentValue = currentDate.Month;
+                yearPicker.CurrentValue = currentDate.Year;
             }
         }
 
         /// <summary>
-        /// The day value of DatePicker.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public int Day
-        {
-            get
-            {
-                return day;
-            }
-            set
-            {
-                if (value < 1 || value > 31 || value == day) return;
-
-                day = value;
-                dayPicker.CurrentValue = day;
-            }
-        }
-
-        /// <summary>
-        /// The year value of DatePicker.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public int Year
-        {
-            get
-            {
-                return year;
-            }
-            set
-            {
-                if (value < 1970 || value > 2038 || value == year) return;
-
-                year = value;
-                yearPicker.CurrentValue = year;
-            }
-        }
-
-                /// <summary>
         /// Initialize TimePicker object.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
@@ -230,14 +172,14 @@ namespace Tizen.NUI.Components
             yearPicker = new Picker()
             {
                 MinValue = 1970,
-                MaxValue = 2038,
+                MaxValue = 2100,
             };
             yearPicker.ValueChanged += OnYearValueChanged;
 
-            //FIXME: Changes it to get current system date.
-            day = 1;
-            month = 1;
-            year = 1970;
+            currentDate = DateTime.Now;
+            dayPicker.CurrentValue = currentDate.Day;
+            monthPicker.CurrentValue = currentDate.Month;
+            yearPicker.CurrentValue = currentDate.Year;
         }
     
         [SuppressMessage("Microsoft.Reliability",
@@ -255,52 +197,53 @@ namespace Tizen.NUI.Components
 
             PickersOrderSet();
             SetMonthText();
-            MaxDaySet();
+            MaxDaySet(currentDate.Month);
         }
 
         private void OnDayValueChanged(object sender, ValueChangedEventArgs e)
         {
-            if (day == e.Value) return;
+            if (currentDate.Day == e.Value) return;
 
-            day = e.Value;
+            currentDate = new DateTime(currentDate.Year, currentDate.Month, e.Value);
             
             OnDateChanged();
         }
 
         private void OnMonthValueChanged(object sender, ValueChangedEventArgs e)
         { 
-            if (Month == e.Value) return;
+            if (currentDate.Month == e.Value) return;
 
-            Month = e.Value;
-            MaxDaySet();
+            MaxDaySet(e.Value);
 
             OnDateChanged();
         }
 
         private void OnYearValueChanged(object sender, ValueChangedEventArgs e)
         { 
-            if (Year == e.Value) return;
+            if (currentDate.Year == e.Value) return;
 
-            Year = e.Value;
+            currentDate = new DateTime(e.Value, currentDate.Month, currentDate.Day);
 
             OnDateChanged();
         }
 
         private void OnDateChanged()
         { 
-            DateChangedEventArgs eventArgs = new DateChangedEventArgs(day, month, year);
+            DateChangedEventArgs eventArgs = new DateChangedEventArgs(currentDate);
             DateChanged?.Invoke(this, eventArgs);
         }
 
-        private void MaxDaySet()
+        private void MaxDaySet(int month)
         {
-            int maxDaysInMonth = DateTime.DaysInMonth(year, month);
+            int maxDaysInMonth = DateTime.DaysInMonth(currentDate.Year, month);
             dayPicker.MaxValue = maxDaysInMonth;
-            if (day > maxDaysInMonth) 
+            if (currentDate.Day > maxDaysInMonth)
             {
-                day = maxDaysInMonth;
-                dayPicker.CurrentValue = day;
+                currentDate = new DateTime(currentDate.Year, month, maxDaysInMonth);
+                dayPicker.CurrentValue = maxDaysInMonth;
+                return;
             }
+            currentDate = new DateTime(currentDate.Year, month, currentDate.Day);
         }
 
         //FIXME: There is no way to know when system locale changed in NUI.
index 2e05b13..8731aa8 100755 (executable)
@@ -32,29 +32,20 @@ namespace Tizen.NUI.Components
     {
         /// <summary>
         /// TimeChangedEventArgs default constructor.
-        /// <param name="hour">hour value of TimePicker.</param>
-        /// <param name="minute">minute value of TimePicker.</param>
+        /// <param name="time">time value of TimePicker.</param>
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]   
-        public TimeChangedEventArgs(int hour, int minute)
+        public TimeChangedEventArgs(DateTime time)
         {
-            Hour = hour;
-            Minute = minute;
+            Time = time;
         }
 
         /// <summary>
         /// TimeChangedEventArgs default constructor.
-        /// <returns>The current hour value of TimePicker.</returns>
+        /// <returns>The current time value of TimePicker.</returns>
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]   
-        public int Hour { get; }
-
-        /// <summary>
-        /// TimeChangedEventArgs default constructor.
-        /// <returns>The current minute value of TimePicker.</returns>
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]   
-        public int Minute { get; }
+        public DateTime Time { get; }
     }
 
     /// <summary>
@@ -67,8 +58,7 @@ namespace Tizen.NUI.Components
     {
         private bool isAm;
         private bool is24HourView;
-        private int hour;
-        private int minute;
+        private DateTime currentTime;
         private String[] ampmText;
         private Picker hourPicker;
         private Picker minutePicker;
@@ -142,53 +132,34 @@ namespace Tizen.NUI.Components
         /// The hour value of TimePicker.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public int Hour
+        public DateTime Time
         {
             get
             {
-                return hour;
+                return currentTime;
             }
             set
             {
-                if (value < 1 || value > 24 || value == hour) return;
-
-                hour = value;
+                currentTime = value;
                 if (!is24HourView)
                 {
-                    if (hour >= 12 && hour <= 23) 
+                    if (currentTime.Hour >= 12 && currentTime.Hour <= 23)
                     {
                         isAm = false;
-                        if (hour == 12) hourPicker.CurrentValue = hour;
-                        else hourPicker.CurrentValue = hour -= 12;
+                        if (currentTime.Hour == 12) hourPicker.CurrentValue = currentTime.Hour;
+                        else hourPicker.CurrentValue = currentTime.Hour - 12;
                         ampmPicker.CurrentValue = 2;
                     }
                     else 
                     {
                         isAm = true;
-                        hourPicker.CurrentValue = hour;
+                        hourPicker.CurrentValue = currentTime.Hour;
                         ampmPicker.CurrentValue = 1;
                     }
                 }
-                else hourPicker.CurrentValue = hour;
-            }
-        }
+                else hourPicker.CurrentValue = currentTime.Hour;
 
-        /// <summary>
-        /// The Minute value of TimePicker.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public int Minute
-        {
-            get
-            {
-                return minute;
-            }
-            set
-            {
-                if (value < 1 || value > 60 || value == minute) return;
-
-                minute = value;
-                minutePicker.CurrentValue = minute;
+                minutePicker.CurrentValue = currentTime.Minute;
             }
         }
 
@@ -206,17 +177,26 @@ namespace Tizen.NUI.Components
             {
                 if (is24HourView == value) return;
 
+                Console.WriteLine(" Is 24 Hour View");
                 is24HourView = value;
                 if (value == true)
                 {
                     Remove(ampmPicker);
-                    hourPicker.MaxValue = 24;
+                    hourPicker.MinValue = 0;
+                    hourPicker.MaxValue = 23;
+                    hourPicker.CurrentValue = currentTime.Hour;
                 }
                 else 
                 {
+                    hourPicker.MinValue = 1;
                     hourPicker.MaxValue = 12;
                     PickersOrderSet(true);
                     SetAmpmText();
+                    if (currentTime.Hour > 12)
+                    {
+                        ampmPicker.CurrentValue = 2;
+                        hourPicker.CurrentValue = currentTime.Hour - 12;
+                    }
                 }
             }
         }
@@ -233,7 +213,7 @@ namespace Tizen.NUI.Components
             hourPicker = new Picker()
             {
                 MinValue = 1,
-                MaxValue = 24,
+                MaxValue = 12,
             };
             hourPicker.ValueChanged += OnHourValueChanged;
 
@@ -250,6 +230,18 @@ namespace Tizen.NUI.Components
                 MaxValue = 2,
             };
             ampmPicker.ValueChanged += OnAmpmValueChanged;
+
+            currentTime = DateTime.Now;
+            Console.WriteLine(" Time " + currentTime.Hour + " " + currentTime.Minute);
+            if (currentTime.Hour > 12)
+            {
+                ampmPicker.CurrentValue = 2;
+                hourPicker.CurrentValue = currentTime.Hour - 12;
+            }
+            else
+                hourPicker.CurrentValue = currentTime.Hour;
+
+            minutePicker.CurrentValue = currentTime.Minute;
         }
 
         /// <summary>
@@ -286,46 +278,50 @@ namespace Tizen.NUI.Components
                 LinearOrientation = LinearLayout.Orientation.Horizontal,
                 CellPadding = new Size(timePickerStyle.CellPadding.Width, timePickerStyle.CellPadding.Height),
             };
+            Console.WriteLine("initialize");
 
-            is24HourView = true;
+            is24HourView = false;
 
             PickersOrderSet(false);
+            SetAmpmText();
+        }
 
-            if (!is24HourView) 
-            {
-                SetAmpmText();
-                hourPicker.MaxValue = 12;
-            }
+        private void ChangeTime(int hour, int minute, bool hourUpdate)
+        {
+            if (hourUpdate)
+                currentTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, hour, currentTime.Minute, 0);
+            else
+                currentTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, currentTime.Hour, minute, 0);
         }
 
         private void OnHourValueChanged(object sender, ValueChangedEventArgs e)
         {
-            if (hour == e.Value) return;
+            if (currentTime.Hour == e.Value) return;
 
             if (!is24HourView)
             {
                 if (isAm) 
                 {
-                    if (e.Value == 12) hour = 24;
-                    else hour = e.Value;
+                    if (e.Value == 12) ChangeTime(12, 0, true);
+                    else ChangeTime(e.Value, 0, true);
                 }
                 else 
                 {
-                    if (e.Value == 12) hour = 12;
-                    else hour = e.Value + 12;
+                    if (e.Value == 12) ChangeTime(0, 0, true);
+                    else ChangeTime(e.Value + 12, 0, true);
                 }
             }
             else
-                hour = e.Value;
+                ChangeTime(e.Value, 0, true);
             
             OnTimeChanged();
         }
 
         private void OnMinuteValueChanged(object sender, ValueChangedEventArgs e)
         { 
-            if (minute == e.Value) return;
+            if (currentTime.Minute == e.Value) return;
 
-            minute = e.Value;
+            ChangeTime(0, e.Value, false);
 
             OnTimeChanged();
         }
@@ -336,19 +332,19 @@ namespace Tizen.NUI.Components
 
             if (e.Value == 1)
             { //AM
-                if (hour >= 12 || hour < 24)
+                if (currentTime.Hour > 12 || currentTime.Hour == 0)
                 { 
-                    if (hour == 12) hour += 12;
-                    else hour -= 12;
+                    if (currentTime.Hour == 0) ChangeTime(12, 0, true);
+                    else ChangeTime(currentTime.Hour - 12, 0, true);
                 }
                 isAm = true;
             }
             else 
             { //PM
-                if (hour == 24 || hour < 12) 
+                if (currentTime.Hour > 0 && currentTime.Hour <= 12)
                 {
-                     if (hour == 24) hour -= 12;
-                     else hour += 12; 
+                     if (currentTime.Hour == 12) ChangeTime(0, 0, true);
+                     else ChangeTime(currentTime.Hour + 12, 0, true);
                 }
                 isAm = false;
             }
@@ -358,7 +354,7 @@ namespace Tizen.NUI.Components
 
         private void OnTimeChanged()
         { 
-            TimeChangedEventArgs eventArgs = new TimeChangedEventArgs(hour, minute);
+            TimeChangedEventArgs eventArgs = new TimeChangedEventArgs(currentTime);
             TimeChanged?.Invoke(this, eventArgs);
         }
 
index 05b20c5..0ca6fbc 100755 (executable)
@@ -16,7 +16,7 @@ namespace Tizen.NUI.Samples
 
         private void OnValueChanged(object sender, DateChangedEventArgs e)
         {
-            Console.WriteLine(" Date is " + e.Day + " " + e.Month + " " + e.Year);
+            Console.WriteLine(" Date is " + e.Date.Day + " " + e.Date.Month + " " + e.Date.Year);
         }
 
         public void Activate()
@@ -28,10 +28,6 @@ namespace Tizen.NUI.Samples
             {
                 Size = new Size(pickerWidth, pickerHeight),
                 Position = new Position(Window.Instance.Size.Width / 2 - pickerWidth / 2, Window.Instance.Size.Height/ 2 - pickerHeight / 2),
-
-                Day = 1,
-                Month = 10,
-                Year = 2021,
             };
             datePicker.DateChanged += OnValueChanged;
             window.Add(datePicker);
index 4ae8761..f877676 100755 (executable)
@@ -16,8 +16,8 @@ namespace Tizen.NUI.Samples
 
         private void TimeChanged(object sender, TimeChangedEventArgs e)
         {
-            Console.WriteLine(" Time " + e.Hour + " " + e.Minute);
-        }
+            Console.WriteLine(" Time " + e.Time.Hour + " " + e.Time.Minute);
+        }                                                                      
 
         public void Activate()
         {
@@ -28,9 +28,6 @@ namespace Tizen.NUI.Samples
             {
                 Size = new Size(pickerWidth, pickerHeight),
                 Position = new Position(Window.Instance.Size.Width / 2 - pickerWidth / 2, Window.Instance.Size.Height/ 2 - pickerHeight / 2),
-                Hour = 12,
-                Minute = 30,
-                Is24HourView = false,
             };
             timePicker.TimeChanged += TimeChanged;
             window.Add(timePicker);