Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Numeric Date Entry - enable custom culture #145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.Threading;
using NFluent;
using Smartway.UiComponent.UnitTests.Inputs.NumericDateInput;
using Xamarin.Forms;


Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Comment on lines +6 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est quoi l'interet de cette classe si la seule méthode que tu créees c'est la même méthode que la classe mère? ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je la rend public

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha le fourbe, je connaissais pas cette méthode, j'approuve!

35 changes: 24 additions & 11 deletions Smartway.UiComponent/Inputs/NumericDateEntry.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public ICommand ErrorCommand
set => SetValue(ErrorCommandProperty, value);
}

private CultureInfo _culture;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pour résumer, l'objectif c'est que dans l'appli on utilise ce composant systématiquement avec une culture à "calendrier grégorien" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pas tout a fait. Ce composant est le date input de SDET. Je rajoute à ce composant la possibilité de spécifier une culture différente de la culture par défaut du device. Une fois que ce sera fait, je pourrais retourner dans l'app xamarin et mettre à jour partout où ce composant est utilisé.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, je ne comprends pas la nuance. On est bien d'accord que l'objectif c'est que dans l'app, on n'utilise plus ce composant que dans sa version "avec spécification d'une culture" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, oui, j'avais mal lu la question. J'avais compris "on utilise systématiquement ce composant quand on veut afficher une date" ><

Oui, on utilisera systématiquement avec une culture spécifiée

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaah ok, merci ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Par contre je me rends compte que ce n'est pas exactement ce que j'avais compris ce matin. Je pensais que dans le ToString on pourrait préciser un type de calendrier, pas une culture. Du coup par exemple on passera quoi comme "culture" dans l'app pour in fine avoir un calendrier grégorien ?

Copy link
Contributor Author

@JosselinTILLAY JosselinTILLAY Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Par contre je me rends compte que ce n'est pas exactement ce que j'avais compris ce matin. Je pensais que dans le ToString on pourrait préciser un type de calendrier, pas une culture

Je pensais aussi ça ce matin ^^ Sauf que pour formatter un datetime, il n'attend pas juste un calendrier mais un IFormatProvider (que CultureInfo implémente) car il y a d'autres paramètres (comme l'ordre JOUR / MOIS / ANNEE de certains affichages).

Ce que je suis en train de faire côté app xamarin, c'est que quand c'est la culture thai, je fournit une autre culture au toString (pour l'instant je suis partit sur la français, mais c'est le temps de mes tests. Il faudra qu eje trouve la plus adaptée) et pour toutes les autres cultures, je fournis la culture du device. Comme ça on impacte uniquement le thai

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ! Bon ça me rassure j'étais pas à côté de la plaque x)
C'est dommage ça nous fait faire vraiment un gros hack mais tant pis... Si déjà on n'envoie une culture que dans les cas ou c'est du thaï c'est un moindre mal

public CultureInfo Culture
{
get
{
if (_culture != null)
{
return _culture;
}

return CultureInfo.CurrentCulture;
}
set => _culture = value;
}

private List<Entry> _dateEntries;
public Entry DayEntry;
public Entry MonthEntry;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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");
}

Expand Down
Loading