Skip to content

Commit

Permalink
Merge pull request #1 from samir-dahal/feat/events
Browse files Browse the repository at this point in the history
Feat/events
  • Loading branch information
samir-dahal authored Apr 1, 2024
2 parents 5e39ec0 + 266c082 commit 6805e94
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 7 deletions.
7 changes: 7 additions & 0 deletions SambatWidget.Core/BaseCalendarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ private List<WidgetCalendarCellModel> Populate()
{
Date = i,
IsToday = i == NepaliDate.Now.Day && isTodayYearMonth,
HasEvent = EventParser.HasEvent(GetYearMonthKey(i)),
HasEventHoliday = EventParser.HasEventHoliday(GetYearMonthKey(i)),
});
}
return _calendarData;
Expand Down Expand Up @@ -139,5 +141,10 @@ public string GetMonthName()
{
return _carouselNepDate.MonthName.ToString();
}

public string GetYearMonthKey(int date)
{
return $"{_carouselNepDate.Year}-{_carouselNepDate.Month}-{date}";
}
}
}
2 changes: 1 addition & 1 deletion SambatWidget.Core/DateConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SambatWidget.Core
{
public static class DateConverter
{
public static string GetCurrentNepaliDate() => NepaliDate.Now.ToString(separator: NepDate.Core.Enums.Separators.ForwardSlash);
public static string GetCurrentNepaliDate() => NepaliDate.Now.ToString(DateFormats.YearMonthDay, Separators.ForwardSlash);
public static DateConvertResult ConvertToNepaliDate(this string engDate)
{
try
Expand Down
60 changes: 60 additions & 0 deletions SambatWidget.Core/EventParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using NepDate;
using SambatWidget.Core.Models;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;

namespace SambatWidget.Core
{
public static class EventParser
{
private static Dictionary<string, EventDataModel> _events = new Dictionary<string, EventDataModel>();
private static Dictionary<string, string[]> _cachedEvents = new Dictionary<string, string[]>();
public static void ParseEventsJson(string path)
{
try
{
if (_cachedEvents.Count <= 0)
{
_cachedEvents = JsonSerializer.Deserialize<Dictionary<string, string[]>>(File.ReadAllText(path), new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
foreach (KeyValuePair<string, string[]> kvp in _cachedEvents)
{
int day = NepaliDate.Parse(kvp.Key).Day;
_events.Add(kvp.Key, new EventDataModel(bool.Parse(kvp.Value[0]), kvp.Value[1], kvp.Value[2], day));
}
_cachedEvents = null;
}
}
catch { }
}
/// <summary>
/// Get event data by Nepali date format as param yyyy-m-d
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static EventDataModel GetEventByDate(string date)
{
EventDataModel result = null;
if (_events != null)
{
_events.TryGetValue(date, out result);
}
return result;
}
public static bool HasEvent(string date)
{
if (_events != null)
{
return _events.ContainsKey(date);
}
return false;
}
public static bool HasEventHoliday(string date)
{
return GetEventByDate(date)?.IsHoliday ?? false;
}
}
}
1 change: 1 addition & 0 deletions SambatWidget.Core/ICalendarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public interface ICalendarRenderer
string GetFormattedEnglishDate();
string GetFormattedNepaliDate();
bool IsToday();
string GetYearMonthKey(int date);
}
}
17 changes: 17 additions & 0 deletions SambatWidget.Core/Models/EventDataModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SambatWidget.Core.Models
{
public class EventDataModel
{
public EventDataModel(bool isHoliday, string nepaliName, string englishName, int date)
{
IsHoliday = isHoliday;
NepaliName = nepaliName;
EnglishName = englishName;
Date = date;
}
public int Date { get; set; }
public bool IsHoliday { get; }
public string NepaliName { get; }
public string EnglishName { get; }
}
}
2 changes: 2 additions & 0 deletions SambatWidget.Core/Models/WidgetCalendarCellModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class WidgetCalendarCellModel
public int ColIndex { get; set; }
public int Date { get; set; }
public bool IsToday { get; set; }
public bool HasEvent { get; set; }
public bool HasEventHoliday { get; set; }
public bool IsSatDay => ColIndex == 6;
}
}
7 changes: 7 additions & 0 deletions SambatWidget.UI/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{StaticResource FontRegular}" />
<Style.Triggers>
<DataTrigger Binding="{Binding HasEventHoliday}" Value="True">
<Setter Property="Foreground" Value="{DynamicResource DangerColor}" />
<Setter Property="FontFamily" Value="{StaticResource FontSemiBold}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSatDay}" Value="True">
<Setter Property="Foreground" Value="{DynamicResource DangerColor}" />
<Setter Property="FontFamily" Value="{StaticResource FontSemiBold}" />
Expand All @@ -28,6 +32,9 @@
TargetType="{x:Type Button}">
<Setter Property="BorderThickness" Value="0.5" />
<Style.Triggers>
<DataTrigger Binding="{Binding HasEvent}" Value="True">
<Setter Property="Background" Value="{DynamicResource BorderColor}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsToday}" Value="True">
<Setter Property="Background" Value="{DynamicResource SuccessColor}" />
</DataTrigger>
Expand Down
4 changes: 3 additions & 1 deletion SambatWidget.UI/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SambatWidget.UI.Helpers;
using SambatWidget.Core;
using SambatWidget.UI.Helpers;
using SambatWidget.UI.Models;
using System.Windows;

Expand All @@ -15,6 +16,7 @@ protected override void OnStartup(StartupEventArgs e)
{
Setting = AppHelpers.LoadAppSettings();
ThemeManager.ApplyDefaultTheme();
EventParser.ParseEventsJson("./Resources/events_minified.json");
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
Expand Down
6 changes: 5 additions & 1 deletion SambatWidget.UI/Controls/WidgetCalendarControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:helpers="clr-namespace:SambatWidget.UI.Helpers"
xmlns:local="clr-namespace:SambatWidget.UI.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="this"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
Expand Down Expand Up @@ -73,7 +74,10 @@
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{StaticResource CalendarCellStyle}">
<Button
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.HandleCellClickCommand}"
CommandParameter="{Binding Date}"
Style="{StaticResource CalendarCellStyle}">
<TextBlock Style="{StaticResource CalendarCellTextStyle}" Text="{Binding Date}" />
</Button>
</DataTemplate>
Expand Down
41 changes: 41 additions & 0 deletions SambatWidget.UI/Controls/WidgetCalendarEventPopup.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<UserControl
x:Class="SambatWidget.UI.Controls.WidgetCalendarEventPopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SambatWidget.UI.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Popup
IsOpen="{Binding EventPopupVisible, Mode=OneWay}"
Placement="MousePoint"
PopupAnimation="Fade"
StaysOpen="False">
<Border Padding="5" Background="#fff3cd">
<StackPanel Width="150">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
<Style.Triggers>
<DataTrigger Binding="{Binding EventInfo.IsHoliday}" Value="True">
<Setter Property="Foreground" Value="{DynamicResource DangerColor}" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock
FontFamily="{StaticResource FontBold}"
FontSize="15"
Text="{Binding EventInfo.Date}" />
<TextBlock
Margin="0,8"
FontFamily="{StaticResource FontSemiBold}"
FontSize="10"
Text="{Binding EventInfo.EnglishName}" />
<TextBlock FontSize="10" Text="{Binding EventInfo.NepaliName}" />
</StackPanel>
</Border>
</Popup>
</UserControl>
28 changes: 28 additions & 0 deletions SambatWidget.UI/Controls/WidgetCalendarEventPopup.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SambatWidget.UI.Controls
{
/// <summary>
/// Interaction logic for WidgetCalendarEventPopup.xaml
/// </summary>
public partial class WidgetCalendarEventPopup : UserControl
{
public WidgetCalendarEventPopup()
{
InitializeComponent();
}
}
}
2 changes: 1 addition & 1 deletion SambatWidget.UI/Helpers/AppHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static void CoreAutoStartAtStartup(bool enable)
RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_PATH, true);
if (enable)
{
string startPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Programs), APP_NAME, $"{APP_NAME}.appref-ms");
string startPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
key.SetValue(APP_NAME, startPath);
}
else
Expand Down
1 change: 1 addition & 0 deletions SambatWidget.UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<controls:WidgetCalendarEventPopup Grid.Row="1" />
<controls:WidgetHeaderControl
x:Name="WidgetHeader"
Padding="5"
Expand Down
11 changes: 11 additions & 0 deletions SambatWidget.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
IsMouseDown = false;
_settings.SetMouseActionRender(true);
if (_vm.EventPopupVisible)
{
_vm.HideEventPopupCommand.Execute(null);
return;
}
if (_settings.Position.X == this.Left && _settings.Position.Y == this.Top)
{
_vm.ToggleExpandCommand.Execute(null);
Expand All @@ -102,12 +107,14 @@ protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
{
base.OnPreviewMouseWheel(e);
_vm.HideEventPopupCommand.Execute(null);
_widgetObserver.RestartScrollTimer();
_widgetObserver.SetScrollDelta(e.Delta);
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
_vm.HideEventPopupCommand.Execute(null);
switch (e.Key)
{
case Key.Down:
Expand All @@ -127,6 +134,10 @@ protected override void OnDeactivated(EventArgs e)
{
_settings.SetWindowHeight(this.ActualHeight);
_settings.SetMouseActionRender(true);
if (_vm.EventPopupVisible)
{
_vm.HideEventPopupCommand.Execute(null);
}
if (App.Setting.MinimizeOnLostFocus && !App.IsShuttingDown)
{
_vm.MinimizeWidgetCommand.Execute(null);
Expand Down
1 change: 1 addition & 0 deletions SambatWidget.UI/Resources/events_minified.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions SambatWidget.UI/SambatWidget.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@
<Folder Include="Converters\" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\events_minified.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion SambatWidget.UI/SettingWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:local="clr-namespace:SambatWidget.UI"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:SambatWidget.UI.ViewModels"
Title="Settings"
Title="Settings v1.1.0"
Width="400"
Height="450"
Background="{DynamicResource BackgroundColor}"
Expand Down
2 changes: 1 addition & 1 deletion SambatWidget.UI/ViewModels/WidgetContextMenuViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void InitCopyTodayContextMenu()
LongDateFormat = NepaliDate.Now.ToLongDateString();
UnicodeFormat = NepaliDate.Now.ToUnicodeString();
LongUnicodeFormat = NepaliDate.Now.ToLongDateUnicodeString();
DashedFormat = NepaliDate.Now.ToString(separator: NepDate.Core.Enums.Separators.Dash);
DashedFormat = NepaliDate.Now.ToString(DateFormats.YearMonthDay, Separators.Dash);
}
}
}
22 changes: 22 additions & 0 deletions SambatWidget.UI/ViewModels/WidgetViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using SambatWidget.Core;
using SambatWidget.Core.Models;

namespace SambatWidget.UI.ViewModels
{
Expand Down Expand Up @@ -30,6 +31,12 @@ public WidgetViewModel()
[ObservableProperty]
bool isTransparent = App.Setting.AllowTransparency && !App.Setting.IsExpanded;

[ObservableProperty]
bool eventPopupVisible;

[ObservableProperty]
EventDataModel eventInfo;

[RelayCommand]
private void RenderNext()
{
Expand Down Expand Up @@ -84,6 +91,21 @@ private void ToggleTransparency()
App.Setting.AllowTransparency = AllowTransparency;
DecideWhenToTransparent();
}
[RelayCommand]
private void HandleCellClick(int date)
{
EventPopupVisible = false;
EventInfo = EventParser.GetEventByDate(_calendarRenderer.GetYearMonthKey(date));
EventPopupVisible = EventInfo != null;
}
[RelayCommand]
private void HideEventPopup()
{
if (EventPopupVisible)
{
EventPopupVisible = false;
}
}
private void DecideWhenToTransparent()
{
if (App.Setting.AllowTransparency)
Expand Down
2 changes: 1 addition & 1 deletion SambatWidget.UI/WidgetObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public WidgetObserver(MainWindow observedWindow, WidgetViewModel widgetVm)
_timer.Interval = TimeSpan.FromSeconds(1);

_scrollTimer = new DispatcherTimer();
_scrollTimer.Interval = TimeSpan.FromMilliseconds(200);
_scrollTimer.Interval = TimeSpan.FromMilliseconds(100);

_relocationTimer = new DispatcherTimer { IsEnabled = App.Setting.AllowGlobalPosition, };
_relocationTimer.Interval = TimeSpan.FromSeconds(1);
Expand Down

0 comments on commit 6805e94

Please sign in to comment.