From e21f3a6bcbd2d3447a77b5619435e5d19ba7afd5 Mon Sep 17 00:00:00 2001 From: Josselin TILLAY Date: Mon, 18 Mar 2024 16:03:33 +0100 Subject: [PATCH] :sparkles: Numeric Date Entry - enable custom culture --- .../NumericDateInput/NumericDateEntryTest.cs | 45 +++++++++++++++++-- .../TestableNumericDateInput.cs | 12 +++++ .../Inputs/NumericDateEntry.xaml.cs | 35 ++++++++++----- 3 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 Smartway.UiComponent.UnitTests/Inputs/NumericDateInput/TestableNumericDateInput.cs diff --git a/Smartway.UiComponent.UnitTests/Inputs/NumericDateInput/NumericDateEntryTest.cs b/Smartway.UiComponent.UnitTests/Inputs/NumericDateInput/NumericDateEntryTest.cs index 3d545c7..b89593f 100644 --- a/Smartway.UiComponent.UnitTests/Inputs/NumericDateInput/NumericDateEntryTest.cs +++ b/Smartway.UiComponent.UnitTests/Inputs/NumericDateInput/NumericDateEntryTest.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.Threading; using NFluent; +using Smartway.UiComponent.UnitTests.Inputs.NumericDateInput; using Xamarin.Forms; @@ -45,7 +46,7 @@ public void FilledUncorrectDate(string day, string month, string year) [InlineData("it-IT", "JJ", "MM", "AA")] [InlineData("ru-RU", "JJ", "MM", "AA")] [InlineData("ro-RO", "JJ", "MM", "AA")] - [InlineData("us-US", "DD", "MM", "YY")] + [InlineData("en-US", "DD", "MM", "YY")] [InlineData("en-EN", "DD", "MM", "YY")] public void DefaultPlaceholder(string culture, string day, string month, string year) { @@ -63,7 +64,7 @@ public void DefaultPlaceholder(string culture, string day, string month, string [InlineData("it-IT", "06", "10")] [InlineData("ro-RO", "06", "10")] [InlineData("ru-RU", "06", "10")] - [InlineData("us-US", "10", "06")] + [InlineData("en-US", "10", "06")] [InlineData("en-EN", "10", "06")] public void MonthDayOrderCulture(string culture, string expectedFirst, string expectedSecond) { @@ -74,15 +75,51 @@ public void MonthDayOrderCulture(string culture, string expectedFirst, string ex CheckNumericDateEntry(entry, expectedFirst, expectedSecond, "2022"); } + [Theory] + [InlineData("fr-FR", "th-TH", 2024, "67")] + [InlineData("th-TH", "fr-FR", 2024, "81")] + [InlineData("fr-FR", "fr-FR", 2024, "24")] + [InlineData("th-TH", "th-TH", 2024, "24")] + public void CustomCulturePlaceholder(string deviceCulture, string customCulture, int year, string expectedYear) + { + SetCulture(deviceCulture); + + var entry = new NumericDateEntry(); + entry.Culture = new CultureInfo(customCulture); + entry.DatePlaceholder = new DateTime(year, 10, 6, Thread.CurrentThread.CurrentCulture.Calendar); + + Check.That(entry.DayEntry.Placeholder).IsEqualTo("06"); + Check.That(entry.MonthEntry.Placeholder).IsEqualTo("10"); + Check.That(entry.YearEntry.Placeholder).IsEqualTo(expectedYear); + } + + [Theory] + [InlineData("fr-FR", "th-TH", 2024, 2024)] + [InlineData("th-TH", "fr-FR", 2024, 2024)] + [InlineData("fr-FR", "fr-FR", 2024, 2024)] + [InlineData("th-TH", "th-TH", 2024, 2024)] + public void CustomCultureFilledDate(string deviceCulture, string customCulture, int year, int expectedYear) + { + SetCulture(deviceCulture); + + var entry = CreateNumericDateEntry("06", "10", year.ToString()); + entry.Culture = new CultureInfo(customCulture); + + var filledDate = entry.GetFilledDate(); + Check.That(filledDate.Day).IsEqualTo(6); + Check.That(filledDate.Month).IsEqualTo(10); + Check.That(filledDate.Year).IsEqualTo(expectedYear); + } + private void SetCulture(string culture) { Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); } - private NumericDateEntry CreateNumericDateEntry(string day, string month, string year) + private TestableNumericDateEntry CreateNumericDateEntry(string day, string month, string year) { - var numericDateEntry = new NumericDateEntry + var numericDateEntry = new TestableNumericDateEntry { ErrorCommand = _errorCommandMock.Object }; diff --git a/Smartway.UiComponent.UnitTests/Inputs/NumericDateInput/TestableNumericDateInput.cs b/Smartway.UiComponent.UnitTests/Inputs/NumericDateInput/TestableNumericDateInput.cs new file mode 100644 index 0000000..8a06cbd --- /dev/null +++ b/Smartway.UiComponent.UnitTests/Inputs/NumericDateInput/TestableNumericDateInput.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Smartway.UiComponent.Inputs; + +namespace Smartway.UiComponent.UnitTests.Inputs.NumericDateInput +{ + class TestableNumericDateEntry: NumericDateEntry + { + public new DateTime GetFilledDate() => base.GetFilledDate(); + } +} diff --git a/Smartway.UiComponent/Inputs/NumericDateEntry.xaml.cs b/Smartway.UiComponent/Inputs/NumericDateEntry.xaml.cs index 1ed68b2..1b8f6a5 100644 --- a/Smartway.UiComponent/Inputs/NumericDateEntry.xaml.cs +++ b/Smartway.UiComponent/Inputs/NumericDateEntry.xaml.cs @@ -65,6 +65,21 @@ public ICommand ErrorCommand set => SetValue(ErrorCommandProperty, value); } + private CultureInfo _culture; + public CultureInfo Culture + { + get + { + if (_culture != null) + { + return _culture; + } + + return CultureInfo.CurrentCulture; + } + set => _culture = value; + } + private List _dateEntries; public Entry DayEntry; public Entry MonthEntry; @@ -98,7 +113,7 @@ protected virtual DateTime GetFilledDate() { var day = int.Parse(DayEntry.Text); var month = int.Parse(MonthEntry.Text); - var year = CultureInfo.CurrentCulture.Calendar.ToFourDigitYear(int.Parse(YearEntry.Text)); + var year = Culture.Calendar.ToFourDigitYear(int.Parse(YearEntry.Text)); return new DateTime(year, month, day); } @@ -158,9 +173,9 @@ private void SetEntriesPlaceHolders() private void SetDateTimePlaceholder() { - DayEntry.Placeholder = DatePlaceholder.ToString("dd"); - MonthEntry.Placeholder = DatePlaceholder.ToString("MM"); - YearEntry.Placeholder = DatePlaceholder.ToString("yy"); + DayEntry.Placeholder = DatePlaceholder.ToString("dd", Culture); + MonthEntry.Placeholder = DatePlaceholder.ToString("MM", Culture); + YearEntry.Placeholder = DatePlaceholder.ToString("yy", Culture); } protected virtual void SetFilledDate() @@ -170,9 +185,9 @@ protected virtual void SetFilledDate() var date = (DateTime)FilledDateTime; - DayEntry.Text = date.ToString("dd"); - MonthEntry.Text = date.ToString("MM"); - YearEntry.Text = date.ToString("yy"); + DayEntry.Text = date.ToString("dd", Culture); + MonthEntry.Text = date.ToString("MM", Culture); + YearEntry.Text = date.ToString("yy", Culture); } private void SetEntriesPosition() @@ -221,15 +236,13 @@ private void SetDefaultPlaceholder() private bool IsDayMonthCalendar() { - var calendarType = - CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name).DateTimeFormat.MonthDayPattern; + var calendarType = Culture.DateTimeFormat.MonthDayPattern; return calendarType.IndexOf("d") < calendarType.IndexOf("M"); } private bool IsYearsFirstCalendar() { - var calendarType = - CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name).DateTimeFormat.YearMonthPattern; + var calendarType = Culture.DateTimeFormat.YearMonthPattern; return calendarType.IndexOf("y") < calendarType.IndexOf("M"); }