Skip to content

Commit

Permalink
chore: add configurable eng/nep unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir Dahal committed Sep 14, 2024
1 parent 731c1cd commit be27d03
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 36 deletions.
32 changes: 30 additions & 2 deletions SambatWidget.Core/BaseCalendarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using SambatWidget.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace SambatWidget.Core
{
Expand Down Expand Up @@ -75,6 +75,14 @@ public string GetWeekDayName()
}
return DateTime.Now.DayOfWeek.ToString();
}
public string GetWeekDayNameNP()
{
if (!IsToday())
{
return GetNepaliWeekDayName(GetThisMonthNepaliStartDate().DayOfWeek);
}
return GetNepaliWeekDayName(_carouselNepDate.DayOfWeek);
}
public string GetShortWeekDayName()
{
if (!IsToday())
Expand All @@ -91,7 +99,15 @@ public string GetFormattedNepaliDate()
{
return $"{_carouselNepDate.MonthName} 1, {_carouselNepDate.Year}";
}
return $"{_carouselNepDate.MonthName} {_carouselNepDate.Day}, {_carouselNepDate.Year}";
return _carouselNepDate.ToLongDateString();
}
public string GetFormattedNepaliDateNP()
{
if (!IsToday())
{
return new NepaliDate(_carouselNepDate.Year, _carouselNepDate.Month, 1).ToLongDateUnicodeString();
}
return _carouselNepDate.ToLongDateUnicodeString();
}
private List<WidgetCalendarCellModel> Populate()
{
Expand Down Expand Up @@ -155,5 +171,17 @@ public string GetYearMonthKey(int date)
{
return $"{_carouselNepDate.Year}-{_carouselNepDate.Month}-{date}";
}
private string GetNepaliWeekDayName(DayOfWeek dayOfWeek) =>
dayOfWeek switch
{
DayOfWeek.Sunday => "आइतबार",
DayOfWeek.Monday => "सोमबार",
DayOfWeek.Tuesday => "मंगलबार",
DayOfWeek.Wednesday => "बुधबार",
DayOfWeek.Thursday => "बिहिबार",
DayOfWeek.Friday => "शुक्रबार",
DayOfWeek.Saturday => "शनिबार",
_ => string.Empty,
};
}
}
3 changes: 3 additions & 0 deletions SambatWidget.Core/ICalendarRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SambatWidget.Core.Models;
using System;
using System.Collections.Generic;

namespace SambatWidget.Core
Expand All @@ -11,11 +12,13 @@ public interface ICalendarRenderer
string GetConsecutiveEnglishYearsPair();
string GetConsecutiveEnglishMonthsPair();
string GetWeekDayName();
string GetWeekDayNameNP();
string GetShortWeekDayName();
string GetMonthName();
int? GetRemainingDays();
string GetFormattedEnglishDate();
string GetFormattedNepaliDate();
string GetFormattedNepaliDateNP();
bool IsToday();
string GetYearMonthKey(int date);
}
Expand Down
1 change: 1 addition & 0 deletions SambatWidget.Core/SambatWidget.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions SambatWidget.UI/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
x:Class="SambatWidget.UI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:SambatWidget.UI.Converters"
xmlns:local="clr-namespace:SambatWidget.UI"
StartupUri="MainWindow.xaml">
<Application.Resources>
Expand Down Expand Up @@ -52,6 +53,7 @@
</Style.Triggers>
</Style>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<converters:EnglishToNepaliNumberConverter x:Key="EnglishToNepaliNumberConverter" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Fonts/Fonts.xaml" />
<ResourceDictionary Source="/Resources/WidgetContextMenus.xaml" />
Expand Down
22 changes: 8 additions & 14 deletions SambatWidget.UI/Controls/WidgetCalendarControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<Grid.Background>
<ImageBrush
ImageSource="https://png.pngtree.com/png-clipart/20220716/ourmid/pngtree-nepal-flag-waving-border-png-image_5981367.png"
Opacity="0.2"
Stretch="UniformToFill" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
Expand All @@ -35,37 +29,37 @@
Grid.Column="0"
HorizontalAlignment="Center"
Style="{StaticResource text-sm}"
Text="Sun" />
Text="{Binding WidgetCalendarViewModel.Sunday}" />
<TextBlock
Grid.Column="1"
HorizontalAlignment="Center"
Style="{StaticResource text-sm}"
Text="Mon" />
Text="{Binding WidgetCalendarViewModel.Monday}" />
<TextBlock
Grid.Column="2"
HorizontalAlignment="Center"
Style="{StaticResource text-sm}"
Text="Tue" />
Text="{Binding WidgetCalendarViewModel.Tuesday}" />
<TextBlock
Grid.Column="3"
HorizontalAlignment="Center"
Style="{StaticResource text-sm}"
Text="Wed" />
Text="{Binding WidgetCalendarViewModel.Wednesday}" />
<TextBlock
Grid.Column="4"
HorizontalAlignment="Center"
Style="{StaticResource text-sm}"
Text="Thu" />
Text="{Binding WidgetCalendarViewModel.Thursday}" />
<TextBlock
Grid.Column="5"
HorizontalAlignment="Center"
Style="{StaticResource text-sm}"
Text="Fri" />
Text="{Binding WidgetCalendarViewModel.Friday}" />
<TextBlock
Grid.Column="6"
HorizontalAlignment="Center"
helpers:Css.Class="text-sm text-semibold text-danger"
Text="Sat" />
Text="{Binding WidgetCalendarViewModel.Saturday}" />
</Grid>
<ItemsControl Grid.Row="1" ItemsSource="{Binding WidgetCalendarViewModel.CalendarCells}">
<ItemsControl.ItemsPanel>
Expand All @@ -84,7 +78,7 @@
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.HandleCellClickCommand}"
CommandParameter="{Binding .}"
Style="{StaticResource CalendarCellStyle}">
<TextBlock Style="{StaticResource CalendarCellTextStyle}" Text="{Binding Date}" />
<TextBlock Style="{StaticResource CalendarCellTextStyle}" Text="{Binding Date, Converter={StaticResource EnglishToNepaliNumberConverter}}" />
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
Expand Down
2 changes: 1 addition & 1 deletion SambatWidget.UI/Controls/WidgetCalendarEventPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<TextBlock
FontFamily="{StaticResource FontBold}"
FontSize="15"
Text="{Binding EventInfo.Date}" />
Text="{Binding EventInfo.Date, Converter={StaticResource EnglishToNepaliNumberConverter}}" />
<TextBlock
Margin="0,8"
FontFamily="{StaticResource FontSemiBold}"
Expand Down
35 changes: 35 additions & 0 deletions SambatWidget.UI/Converters/CommonConverters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Globalization;
using System.Windows.Data;

namespace SambatWidget.UI.Converters
{
public class EnglishToNepaliNumberConverter : IValueConverter
{
private static char[] _digits = new char[] { '०', '१', '२', '३', '४', '५', '६', '७', '८', '९' };
private string ConvertToNepali(int number)
{
var result = new List<char>();
int numDigits = (int)Math.Floor(Math.Log10(number) + 1);
for (int i = numDigits - 1; i >= 0; i--)
{
int divisor = (int)Math.Pow(10, i);
int digit = (number / divisor) % 10;
result.Add(_digits[digit]);
}
return string.Join("", result);
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (App.Setting.IsNepali)
{
return ConvertToNepali((int)value);
}
return value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
1 change: 1 addition & 0 deletions SambatWidget.UI/Models/SettingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SettingModel
public bool AllowGlobalPosition { get; set; } = true;
public string Theme { get; set; } = "LightTheme";
public Point Position { get; set; }
public bool IsNepali { get; set; }
public virtual SettingModel Save(Action<SettingModel> action = null)
{
action?.Invoke(this);
Expand Down
5 changes: 5 additions & 0 deletions SambatWidget.UI/Resources/WidgetContextMenus.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
CommandParameter="{Binding WidgetContextMenuViewModel.LongUnicodeFormat}"
Header="{Binding WidgetContextMenuViewModel.LongUnicodeFormat}" />
</MenuItem>
<MenuItem
Command="{Binding ToggleNepaliLangCommand}"
Header="Nepali Lang"
IsCheckable="True"
IsChecked="{Binding IsNepali}" />
<MenuItem
Command="{Binding WidgetContextMenuViewModel.LockPositionCommand}"
Header="Lock Position"
Expand Down
7 changes: 0 additions & 7 deletions SambatWidget.UI/SambatWidget.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Costura.Fody" Version="5.7.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand All @@ -41,10 +38,6 @@
<Resource Include="Resources\Images\usa.png" />
</ItemGroup>

<ItemGroup>
<Folder Include="Converters\" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\events_minified.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
56 changes: 54 additions & 2 deletions SambatWidget.UI/ViewModels/WidgetCalendarViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using SambatWidget.Core;
using CommunityToolkit.Mvvm.ComponentModel;
using SambatWidget.Core;
using SambatWidget.Core.Models;
using SambatWidget.UI.Helpers;

namespace SambatWidget.UI.ViewModels
{
public partial class WidgetCalendarViewModel
public partial class WidgetCalendarViewModel : ObservableObject
{
private readonly ICalendarRenderer _calendarRenderer;
public WpfObservableRangeCollection<WidgetCalendarCellModel> CalendarCells { get; }
Expand All @@ -14,10 +15,56 @@ public WidgetCalendarViewModel(ICalendarRenderer renderer)
CalendarCells = new WpfObservableRangeCollection<WidgetCalendarCellModel>();
Init();
}
[ObservableProperty]
string sunday = "Sun";

[ObservableProperty]
string monday = "Mon";

[ObservableProperty]
string tuesday = "Tue";

[ObservableProperty]
string wednesday = "Wed";

[ObservableProperty]
string thursday = "Thu";

[ObservableProperty]
string friday = "Fri";

[ObservableProperty]
string saturday = "Sat";
public void Init()
{
SetDynamicWeekdayNamesForHeader();
Render(() => CalendarCells.AddRange(_calendarRenderer.RenderToday()));
}

private void SetDynamicWeekdayNamesForHeader()
{
if (App.Setting.IsNepali)
{
Sunday = "आइ";
Monday = "सोम";
Tuesday = "मंगल";
Wednesday = "बुध";
Thursday = "बिहि";
Friday = "शुक्र";
Saturday = "शनि";
}
else
{
Sunday = "Sun";
Monday = "Mon";
Tuesday = "Tue";
Wednesday = "Wed";
Thursday = "Thu";
Friday = "Fri";
Saturday = "Sat";
}
}

public void RenderNext()
{
Render(() => CalendarCells.AddRange(_calendarRenderer.RenderNext()));
Expand All @@ -26,6 +73,11 @@ public void RenderPrevious()
{
Render(() => CalendarCells.AddRange(_calendarRenderer.RenderPrevious()));
}
public void ReRender()
{
CalendarCells.Clear();
Init();
}
private void Render(Action renderAction)
{
CalendarCells.Clear();
Expand Down
37 changes: 27 additions & 10 deletions SambatWidget.UI/ViewModels/WidgetHeaderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,35 @@ public WidgetHeaderViewModel(ICalendarRenderer renderer)
string timeZone;
public void InitHeader()
{
//use the abbreviated day of week name to reduce the space when timezone is enabled
Header = App.Setting.ShowTimeZone
? _calendarRenderer.GetShortWeekDayName()
: _calendarRenderer.GetWeekDayName();
string engHeader = _calendarRenderer.GetConsecutiveEnglishMonthsPair();
if (!App.Setting.IsExpanded)
SetHeader();
InitTimeZone();
SetSubHeader();
}
private void SetHeader()
{
if (App.Setting.IsNepali)
{
engHeader = _calendarRenderer.GetFormattedEnglishDate();
Header = _calendarRenderer.GetWeekDayNameNP();
}
SubHeader = $"{_calendarRenderer.GetFormattedNepaliDate()} | {engHeader}";
InitTimeZone();
else
{
// Use the abbreviated day of the week name when the timezone is enabled
Header = App.Setting.ShowTimeZone
? _calendarRenderer.GetShortWeekDayName()
: _calendarRenderer.GetWeekDayName();
}
}
private void SetSubHeader()
{
// Get the English header, conditionally formatting based on expanded setting
string engHeader = App.Setting.IsExpanded
? _calendarRenderer.GetConsecutiveEnglishMonthsPair()
: _calendarRenderer.GetFormattedEnglishDate();

// Set subheader with Nepali and English dates
SubHeader = App.Setting.IsNepali
? $"{_calendarRenderer.GetFormattedNepaliDateNP()} | {engHeader}"
: $"{_calendarRenderer.GetFormattedNepaliDate()} | {engHeader}";
}
public void InitTimeZone()
{
Expand Down
Loading

0 comments on commit be27d03

Please sign in to comment.