Fix crash if person hours doesn't fit into standard pattern

This commit is contained in:
2025-07-09 14:59:37 +02:00
parent a5a8c3410d
commit 1e5c7d259f
5 changed files with 62 additions and 4 deletions

View File

@@ -28,9 +28,10 @@ public class TimesheetService
foreach (Person person in persons)
{
List<int> pattern = GetPattern(person);
if (pattern.Count == 0) continue;
List<DateTime> holidays = await GetHolidays();
List<DateTime> weekends = GetWeekends();
int monthHours = person.Hours * 4;
foreach (string month in Constants.Months)
{
@@ -40,6 +41,7 @@ public class TimesheetService
int currentPersonHours = 0;
int currentPatternIndex = 0;
int dayOfMonth = 1;
int lastDayOfMonth = new DateTime(DateTime.Now.Year, Constants.Months.IndexOf(month), dayOfMonth).AddMonths(1).AddDays(-1).Day;
while (currentPersonHours != monthHours)
{
DateTime currentDay = new DateTime(DateTime.Now.Year, Constants.Months.IndexOf(month), dayOfMonth);
@@ -75,9 +77,18 @@ public class TimesheetService
currentPersonHours += 2;
break;
}
case 1:
{
DateTime dayStart = new DateTime(currentDay.Year, currentDay.Month, currentDay.Day, 9, 0, 0);
DateTime dayEnd = new DateTime(currentDay.Year, currentDay.Month, currentDay.Day, 10, 0, 0);
timesheetValue.Add((dayStart, dayEnd, false));
currentPersonHours += 1;
break;
}
}
dayOfMonth++;
if (dayOfMonth > lastDayOfMonth) throw new Exception("Too many hours!");
currentPatternIndex = (currentPatternIndex + 1) % pattern.Count;
}
@@ -158,10 +169,14 @@ public class TimesheetService
pattern.Add(2);
hours += 2;
}
else if (hours + 1 <= person.Hours)
{
pattern.Add(1);
hours += 1;
}
else if (hours + 2 > person.Hours)
{
// Error! Could not find pattern!
return null;
return new List<int>();
}
}

View File

@@ -82,5 +82,12 @@ namespace LeanderShiftPlannerV2.Service
areYouSure.Show(sender);
return areYouSure;
}
public static GeneralError ShowGeneralError(Window sender)
{
GeneralError generalError = new GeneralError();
generalError.Show(sender);
return generalError;
}
}
}

View File

@@ -0,0 +1,12 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="LeanderShiftPlannerV2.View.ErrorView.GeneralError"
Title="GeneralError" Height="100" Width="300" CanResize="False" WindowStartupLocation="CenterOwner">
<Grid>
<TextBlock x:Name="TextBlockErrorText" Text="Here should be your exception code" Height="32" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0, 0, 0, 15"/>
<Button x:Name="ButtonAccept" Content="Accept" Height="32" Width="100" HorizontalAlignment="Right" VerticalAlignment="Bottom" HorizontalContentAlignment="Center" Margin="0, 0, 0, 10" Click="ButtonAccept_OnClick"/>
</Grid>
</Window>

View File

@@ -0,0 +1,24 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
namespace LeanderShiftPlannerV2.View.ErrorView;
public partial class GeneralError : Window
{
public GeneralError()
{
InitializeComponent();
}
public string ErrorText
{
set => TextBlockErrorText.Text = value;
}
private void ButtonAccept_OnClick(object? sender, RoutedEventArgs e)
{
this.Close();
}
}

View File

@@ -136,7 +136,7 @@ public partial class LeanderShiftPlannerMainWindow : Window
}
catch (Exception exception)
{
throw; // TODO handle exception
ViewService.ShowGeneralError(this).ErrorText = exception.Message;
}
}