CalendarPicker:

+ Added days, monthes and years range check
+ Added language selection
* Fixed default language (it is English now)
This commit is contained in:
Danich Volk 2021-07-13 21:18:55 +03:00
parent bd908db867
commit 36fb40b6bd
2 changed files with 24 additions and 7 deletions

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Enums;
using TelegramBotBase.Form;
using TelegramBotBase.Tools;
using static TelegramBotBase.Tools.Arrays;
using static TelegramBotBase.Tools.Time;
@ -36,15 +36,17 @@ namespace TelegramBotBase.Controls.Inline
public bool EnableMonthView { get; set; } = true;
public bool EnableYearView { get; set; } = true;
public CalendarPicker()
public CalendarPicker(CultureInfo culture)
{
this.SelectedDate = DateTime.Today;
this.VisibleMonth = DateTime.Today;
this.FirstDayOfWeek = DayOfWeek.Monday;
this.Culture = new CultureInfo("de-de");
this.Culture = culture;
this.PickerMode = eMonthPickerMode.day;
}
public CalendarPicker() : this(new CultureInfo("en-en")) { }
@ -124,13 +126,13 @@ namespace TelegramBotBase.Controls.Inline
default:
int day = 0;
if (result.RawData.StartsWith("d-") && int.TryParse(result.RawData.Split('-')[1], out day))
if (result.RawData.StartsWith("d-") && TryParseDay(result.RawData.Split('-')[1], this.SelectedDate, out day))
{
this.SelectedDate = new DateTime(this.VisibleMonth.Year, this.VisibleMonth.Month, day);
}
int month = 0;
if (result.RawData.StartsWith("m-") && int.TryParse(result.RawData.Split('-')[1], out month))
if (result.RawData.StartsWith("m-") && TryParseMonth(result.RawData.Split('-')[1], out month))
{
this.SelectedDate = new DateTime(this.VisibleMonth.Year, month, 1);
this.VisibleMonth = this.SelectedDate;
@ -142,7 +144,7 @@ namespace TelegramBotBase.Controls.Inline
}
int year = 0;
if (result.RawData.StartsWith("y-") && int.TryParse(result.RawData.Split('-')[1], out year))
if (result.RawData.StartsWith("y-") && TryParseYear(result.RawData.Split('-')[1], out year))
{
this.SelectedDate = new DateTime(year, SelectedDate.Month, SelectedDate.Day);
this.VisibleMonth = this.SelectedDate;

View File

@ -8,6 +8,21 @@ namespace TelegramBotBase.Tools
{
public static class Time
{
public static bool TryParseDay(string src, DateTime currentDate, out int resultDay)
{
return int.TryParse(src, out resultDay) && resultDay >= 1 && resultDay <= DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
}
public static bool TryParseMonth(string src, out int resultMonth)
{
return int.TryParse(src, out resultMonth) && resultMonth >= 1 && resultMonth <= 12;
}
public static bool TryParseYear(string src, out int resultYear)
{
return int.TryParse(src, out resultYear) && resultYear >= 0 && resultYear <= DateTime.MaxValue.Year;
}
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;