Skip to content

Commit

Permalink
Add a way to change the culture of the calendar in playground page of…
Browse files Browse the repository at this point in the history
… sample app (#182)
  • Loading branch information
ME-MarvinE authored Feb 11, 2024
2 parents 2094107 + 1388a88 commit 08d6e79
Showing 6 changed files with 125 additions and 2 deletions.
3 changes: 2 additions & 1 deletion XCalendarFormsSample/XCalendarFormsSample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xamarin.Forms;
using System.Globalization;
using Xamarin.Forms;
using XCalendarFormsSample.Views;

namespace XCalendarFormsSample
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Input;
using Xamarin.Forms;
@@ -46,6 +47,7 @@ public class PlaygroundViewModel : BaseViewModel
public bool DayAutoSetStyleBasedOnDayState { get; set; } = true;
public int ForwardsNavigationAmount { get; set; } = 1;
public int BackwardsNavigationAmount { get; set; } = -1;
public string TargetCultureCode { get; set; } = CultureInfo.CurrentCulture?.Name ?? CultureInfo.DefaultThreadCurrentCulture?.Name ?? CultureInfo.CurrentUICulture?.Name ?? CultureInfo.DefaultThreadCurrentUICulture?.Name ?? "en";
public Color CalendarBackgroundColor { get; set; } = (Color)Application.Current.Resources["CalendarBackgroundColor"];
public Color NavigationBackgroundColor { get; set; } = (Color)Application.Current.Resources["CalendarPrimaryColor"];
public Color NavigationTextColor { get; set; } = (Color)Application.Current.Resources["CalendarPrimaryTextColor"];
@@ -89,6 +91,7 @@ public class PlaygroundViewModel : BaseViewModel
public ICommand NavigateCalendarCommand { get; set; }
public ICommand ChangeDateSelectionCommand { get; set; }
public ICommand ChangeCalendarVisibilityCommand { get; set; }
public ICommand UpdateCurrentCultureCommand { get; set; }
#endregion

#region Constructors
@@ -119,6 +122,8 @@ public PlaygroundViewModel()
NavigateCalendarCommand = new Command<int>(NavigateCalendar);
ChangeDateSelectionCommand = new Command<DateTime>(ChangeDateSelection);
ChangeCalendarVisibilityCommand = new Command<bool>(ChangeCalendarVisibility);
UpdateCurrentCultureCommand = new Command(UpdateCurrentCulture);
UpdateCurrentCulture();
}
#endregion

@@ -187,6 +192,29 @@ public void ChangeCalendarVisibility(bool isVisible)
{
CalendarIsVisible = isVisible;
}
public async void UpdateCurrentCulture()
{
try
{
//Set DefaultThreadCurrentCulture because CurrentCulture gets automatically reset when changed.
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(TargetCultureCode);
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(TargetCultureCode);

//This causes the binding converters (which use the current culture) to update.
//Day Names
var oldDayNamesOlder = Calendar.DayNamesOrder.ToList();
Calendar.DayNamesOrder.ReplaceRange(new List<DayOfWeek>() { DayOfWeek.Monday });
Calendar.DayNamesOrder.ReplaceRange(oldDayNamesOlder);

//NavigationView Title
NavigateCalendar(1);
NavigateCalendar(-1);
}
catch
{
await Shell.Current.DisplayAlert("Invalid Culture Code", "The specified culture code was invalid.", "OK");
}
}
public async void ShowCustomDayNamesOrderDialog()
{
IEnumerable<DayOfWeek> newCustomDayNamesOrder = await PopupHelper.ShowConstructListDialogAsync(Calendar.CustomDayNamesOrder ?? new ObservableRangeCollection<DayOfWeek>(), Calendar.StartOfWeek.GetWeekAsFirst());
@@ -291,6 +319,7 @@ public async void ShowDayInvalidTextColorDialog()
{
DayInvalidTextColor = await PopupHelper.ShowColorDialogAsync(DayInvalidTextColor);
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -471,6 +471,37 @@
</Frame>
</Grid>

<Grid ColumnDefinitions="*, *, *">
<Label
Grid.Column="0"
FontSize="{StaticResource SmallFontSize}"
HorizontalTextAlignment="Center"
Text="Culture*"
VerticalTextAlignment="Center"/>

<Grid
Grid.Column="2"
ColumnDefinitions="auto, auto"
ColumnSpacing="5"
HorizontalOptions="Center"
VerticalOptions="Center">
<Editor
Grid.Column="0"
Keyboard="Text"
Text="{Binding TargetCultureCode}"
VerticalOptions="Center"
WidthRequest="75"/>

<Button
Grid.Column="1"
BackgroundColor="{StaticResource PrimaryColor}"
Command="{Binding UpdateCurrentCultureCommand}"
Text="Update"
TextColor="{StaticResource PrimaryTextColor}"
VerticalOptions="Center"/>
</Grid>
</Grid>

<Label
BackgroundColor="{StaticResource PrimaryColor}"
HorizontalTextAlignment="Center"
30 changes: 29 additions & 1 deletion XCalendarMauiSample/ViewModels/PlaygroundViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Input;
using System.Globalization;
using System.Windows.Input;
using XCalendar.Core.Collections;
using XCalendar.Core.Enums;
using XCalendar.Core.Extensions;
@@ -42,6 +43,7 @@ public class PlaygroundViewModel : BaseViewModel
public bool DayAutoSetStyleBasedOnDayState { get; set; } = true;
public int ForwardsNavigationAmount { get; set; } = 1;
public int BackwardsNavigationAmount { get; set; } = -1;
public string TargetCultureCode { get; set; } = CultureInfo.CurrentCulture?.Name ?? CultureInfo.DefaultThreadCurrentCulture?.Name ?? CultureInfo.CurrentUICulture?.Name ?? CultureInfo.DefaultThreadCurrentUICulture?.Name ?? "en";
public Color CalendarBackgroundColor { get; set; } = (Color)Application.Current.Resources["CalendarBackgroundColor"];
public Color NavigationBackgroundColor { get; set; } = (Color)Application.Current.Resources["CalendarPrimaryColor"];
public Color NavigationTextColor { get; set; } = (Color)Application.Current.Resources["CalendarPrimaryTextColor"];
@@ -85,6 +87,7 @@ public class PlaygroundViewModel : BaseViewModel
public ICommand NavigateCalendarCommand { get; set; }
public ICommand ChangeDateSelectionCommand { get; set; }
public ICommand ChangeCalendarVisibilityCommand { get; set; }
public ICommand UpdateCurrentCultureCommand { get; set; }
#endregion

#region Constructors
@@ -115,6 +118,8 @@ public PlaygroundViewModel()
NavigateCalendarCommand = new Command<int>(NavigateCalendar);
ChangeDateSelectionCommand = new Command<DateTime>(ChangeDateSelection);
ChangeCalendarVisibilityCommand = new Command<bool>(ChangeCalendarVisibility);
UpdateCurrentCultureCommand = new Command(UpdateCurrentCulture);
UpdateCurrentCulture();
}
#endregion

@@ -183,6 +188,29 @@ public void ChangeCalendarVisibility(bool isVisible)
{
CalendarIsVisible = isVisible;
}
public async void UpdateCurrentCulture()
{
try
{
//Set DefaultThreadCurrentCulture because CurrentCulture gets automatically reset when changed.
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(TargetCultureCode);
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(TargetCultureCode);

//This causes the binding converters (which use the current culture) to update.
//Day Names
var oldDayNamesOlder = Calendar.DayNamesOrder.ToList();
Calendar.DayNamesOrder.ReplaceRange(new List<DayOfWeek>() { DayOfWeek.Monday });
Calendar.DayNamesOrder.ReplaceRange(oldDayNamesOlder);

//NavigationView Title
NavigateCalendar(1);
NavigateCalendar(-1);
}
catch
{
await Shell.Current.DisplayAlert("Invalid Culture Code", "The specified culture code was invalid.", "OK");
}
}
public async void ShowCustomDayNamesOrderDialog()
{
IEnumerable<DayOfWeek> newCustomDayNamesOrder = await PopupHelper.ShowConstructListDialogPopupAsync(Calendar.CustomDayNamesOrder ?? new ObservableRangeCollection<DayOfWeek>(), Calendar.StartOfWeek.GetWeekAsFirst());
30 changes: 30 additions & 0 deletions XCalendarMauiSample/Views/PlaygroundPage.xaml
Original file line number Diff line number Diff line change
@@ -471,6 +471,36 @@
</Border>
</Grid>

<Grid ColumnDefinitions="*, *">
<Label
Grid.Column="0"
FontSize="{StaticResource SmallFontSize}"
HorizontalTextAlignment="Center"
Text="Culture*"
VerticalTextAlignment="Center"/>

<Grid
Grid.Column="2"
ColumnDefinitions="auto, auto"
ColumnSpacing="5"
HorizontalOptions="Center">
<Editor
Grid.Column="0"
Keyboard="Text"
Text="{Binding TargetCultureCode}"
VerticalOptions="Center"
WidthRequest="75"/>

<Button
Grid.Column="1"
BackgroundColor="{StaticResource PrimaryColor}"
Command="{Binding UpdateCurrentCultureCommand}"
Text="Update"
TextColor="{StaticResource PrimaryTextColor}"
VerticalOptions="Center"/>
</Grid>
</Grid>

<Label
BackgroundColor="{StaticResource PrimaryColor}"
HorizontalTextAlignment="Center"
4 changes: 4 additions & 0 deletions XCalendarMauiSample/XCalendarMauiSample.csproj
Original file line number Diff line number Diff line change
@@ -34,6 +34,10 @@
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'">
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" Color="#512BD4" />

0 comments on commit 08d6e79

Please sign in to comment.