Files
LeanderShiftPlannerV2/LeanderShiftPlannerV2/View/LeanderShiftPlannerView/LeanderShiftPlannerMainWindow.axaml.cs
2025-10-10 10:49:20 +02:00

362 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using LeanderShiftPlannerV2.FileIO;
using LeanderShiftPlannerV2.Model;
using LeanderShiftPlannerV2.Service;
using LeanderShiftPlannerV2.View.ErrorView;
using LeanderShiftPlannerV2.View.LeanderControls;
namespace LeanderShiftPlannerV2.View.LeanderShiftPlannerView;
public partial class LeanderShiftPlannerMainWindow : Window
{
private readonly AppService _appService = ((App)Application.Current!).AppService;
private CancellationTokenSource _cts = new CancellationTokenSource();
private CancellationToken _cancelCalculation;
private bool _calcRunning;
private readonly Dispatcher _dispatcher = Dispatcher.UIThread;
private readonly Dictionary<Filter, FilterElement> _filters = new Dictionary<Filter, FilterElement>();
public LeanderShiftPlannerMainWindow()
{
InitializeComponent();
LoadData();
DeactivateView();
ViewModePersons();
ListViewPersons.ItemsSource = _appService.PersonService.GetPersonList();
ListViewShifts.ItemsSource = _appService.ShiftService.GetShiftList();
_appService.PersonService.GetPersonList().CollectionChanged += PersonListChanged;
_appService.ShiftService.GetShiftList().CollectionChanged += ShiftListChanged;
_appService.ShiftPlanService.GetShiftPlans().CollectionChanged += ShiftPlansChanged;
_appService.FilterService.Filters.CollectionChanged += FilterListChanged;
}
// ------------------------------------------------------UI-Interactions------------------------------------------------------
private void ButtonAddPerson_Click(object? sender, RoutedEventArgs e)
{
ViewService.ShowPersonCreator(this, _appService);
}
private void ButtonEditPerson_Click(object? sender, RoutedEventArgs e)
{
if (ListViewPersons.SelectedItem is Person selectedPerson)
{
ViewService.ShowPersonEditor(this, _appService, selectedPerson);
}
}
private void ButtonDeletePerson_Click(object? sender, RoutedEventArgs e)
{
AreYouSure confirm = ViewService.ShowAreYouSure(this);
confirm.Closed += (_, _) =>
{
if (!confirm.DialogResult) return;
if (ListViewPersons.SelectedItem is not Person selectedPerson) return;
_appService.PersonService.DeletePerson(selectedPerson);
PersonListChanged(sender, e);
};
}
private void ButtonAddShiftToPerson_Click(object? sender, RoutedEventArgs e)
{
if (ListViewPersons.SelectedItem is Person selectedPerson)
{
ViewService.ShowPersonShiftSelector(this, _appService, selectedPerson,
_appService.ShiftService.GetShiftList());
}
}
private void ButtonAddShift_Click(object? sender, RoutedEventArgs e)
{
ViewService.ShowShiftCreator(this, _appService);
}
private void ButtonEditShift_Click(object? sender, RoutedEventArgs e)
{
if (ListViewShifts.SelectedItem is Shift selectedShift)
{
ViewService.ShowShiftEditor(this, _appService, selectedShift);
}
}
private void ButtonDeleteShift_Click(object? sender, RoutedEventArgs e)
{
AreYouSure confirm = ViewService.ShowAreYouSure(this);
confirm.Closed += (_, _) =>
{
if (!confirm.DialogResult) return;
if (ListViewShifts.SelectedItem is not Shift selectedShift) return;
_appService.ShiftService.RemoveShift(selectedShift);
ShiftListChanged(sender, e);
};
}
private void ButtonAddPersonToShift_Click(object? sender, RoutedEventArgs e)
{
if (ListViewShifts.SelectedItem is Shift selectedShift)
{
ViewService.ShowShiftPersonSelector(this, _appService, selectedShift,
_appService.PersonService.GetPersonList());
}
}
private void ButtonOpenShiftPlan_Click(object? sender, RoutedEventArgs e)
{
if (ListViewShiftPlans.SelectedItem is ShiftPlan selectedShiftPlan)
{
ViewService.ShowShiftPlanViewer(this, _appService, selectedShiftPlan);
}
}
private async void ButtonGenerateTimesheets_OnClick(object? sender, RoutedEventArgs e)
{
try
{
ProgressbarCalculaion.IsIndeterminate = true;
await _appService.TimesheetService.GenerateGoogleTimeSheets(_appService.PersonService.GetGoogleTimeSheetList());
ProgressbarCalculaion.IsIndeterminate = false;
int error = TimesheetIO.ExportTimesheets(_appService.PersonService.GetPersonList().ToList());
if (error == 1) ViewService.ShowGeneralError(this).ErrorText = "There was an error exporting the timesheets.";
if (error == 2) ViewService.ShowGeneralError(this).ErrorText = "There was an error creating the timesheets.";
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private void ButtonFilterShiftPlans_Click(object? sender, RoutedEventArgs e)
{
ViewService.ShowFilterSelector(this, _appService);
}
private void ButtonResetAllFilters_Click(object? sender, RoutedEventArgs e)
{
_appService.ShiftPlanService.ResetShiftPlanList();
_appService.FilterService.ResetAllFilters();
StackPanelFilterList.Children.Clear();
_filters.Clear();
}
private void ButtonSaveShiftPlans_Click(object? sender, RoutedEventArgs e)
{
foreach (ShiftPlan shiftPlan in _appService.ShiftPlanService.GetShiftPlans())
ShiftPlanIO.ExportShiftPlan(ShiftPlanIO.GenerateShiftPlanPng(shiftPlan),
_appService.ShiftPlanService.GetNextID());
}
private void ButtonStartCalculation_Click(object? sender, RoutedEventArgs e)
{
if (_calcRunning)
{
_calcRunning = false;
ViewModeCalcRunning();
ButtonStartCalculation.Content = "Start calculation";
ProgressbarCalculaion.IsIndeterminate = false;
_cts.Cancel();
}
else
{
StackPanelFilterList.Children.Clear();
_filters.Clear();
_appService.ShiftPlanService.RestartShiftPlanList();
_cts = new CancellationTokenSource();
_calcRunning = true;
ViewModeCalcRunning();
_cancelCalculation = _cts.Token;
ButtonStartCalculation.Content = "Stop calculation";
ProgressbarCalculaion.IsIndeterminate = true;
Task calculation = new Task(() => { _appService.ShiftPlanService.StartCalculation(_cancelCalculation); });
calculation.Start();
calculation.GetAwaiter().OnCompleted(() =>
{
_calcRunning = false;
ViewModeCalcRunning();
ProgressbarCalculaion.IsIndeterminate = false;
ButtonStartCalculation.Content = "Start calculation";
_cts.Dispose();
});
}
}
private void Window_Closing(object? sender, WindowClosingEventArgs e)
{
SaveDataToFile.SavePersons(_appService.PersonService.GetPersonList().ToList());
SaveDataToFile.SaveShifts(_appService.ShiftService.GetShiftList().ToList());
SaveDataToFile.SavePersonShiftConnections(_appService.PersonService.GetPersonList().ToList());
}
private void TabControlRoot_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 0) return;
if (TabItemPersons == e.AddedItems[0] && !_calcRunning)
{
ViewModePersons();
}
else if (TabItemShifts == e.AddedItems[0] && !_calcRunning)
{
ViewModeShifts();
}
else if (TabItemShiftPlans == e.AddedItems[0])
{
ViewModeShiftPlans();
}
}
// ------------------------------------------------------Helper------------------------------------------------------
private void PersonListChanged(object? sender, EventArgs e)
{
}
private void ShiftListChanged(object? sender, EventArgs e)
{
}
private void ShiftPlansChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e)
{
case { Action: NotifyCollectionChangedAction.Add, NewItems: not null }:
_dispatcher.InvokeAsync(() =>
{
foreach (object item in e.NewItems)
{
ListViewShiftPlans.Items.Add(item);
}
}, DispatcherPriority.Background);
break;
case { Action: NotifyCollectionChangedAction.Remove, OldItems: not null }:
_dispatcher.InvokeAsync(() =>
{
foreach (object item in e.OldItems)
{
ListViewShiftPlans.Items.Remove(item);
}
});
break;
}
}
private void FilterListChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e)
{
case { Action: NotifyCollectionChangedAction.Add, NewItems: not null }:
{
foreach (Filter filter in e.NewItems)
{
_filters[filter] = new FilterElement(_appService, filter);
StackPanelFilterList.Children.Add(_filters[filter]);
}
break;
}
case { Action: NotifyCollectionChangedAction.Remove, OldItems: not null }:
{
foreach (Filter filter in e.OldItems)
{
StackPanelFilterList.Children.Remove(_filters[filter]);
}
break;
}
}
}
private void LoadData()
{
List<Person> persons = _appService.PersonService.GetPersonList().ToList();
List<Shift> shifts = _appService.ShiftService.GetShiftList().ToList();
LoadDataFromFile.LoadPersonShiftConnections(persons, shifts, _appService);
}
private void DeactivateView()
{
ButtonAddPerson.IsEnabled = false;
ButtonEditPerson.IsEnabled = false;
ButtonDeletePerson.IsEnabled = false;
ButtonAddShiftToPerson.IsEnabled = false;
ButtonOpenShiftPlan.IsEnabled = false;
ButtonSaveShiftPlans.IsEnabled = false;
ButtonFilterShiftPlans.IsEnabled = false;
ButtonResetAllFilters.IsEnabled = false;
ButtonAddShift.IsEnabled = false;
ButtonEditShift.IsEnabled = false;
ButtonDeleteShift.IsEnabled = false;
ButtonAddPersonToShift.IsEnabled = false;
}
private void ViewModePersons()
{
DeactivateView();
ButtonAddPerson.IsEnabled = true;
ButtonEditPerson.IsEnabled = true;
ButtonDeletePerson.IsEnabled = true;
ButtonAddShiftToPerson.IsEnabled = true;
}
private void ViewModeShifts()
{
DeactivateView();
ButtonAddShift.IsEnabled = true;
ButtonEditShift.IsEnabled = true;
ButtonDeleteShift.IsEnabled = true;
ButtonAddPersonToShift.IsEnabled = true;
}
private void ViewModeShiftPlans()
{
if (_calcRunning) return;
DeactivateView();
ButtonOpenShiftPlan.IsEnabled = true;
ButtonSaveShiftPlans.IsEnabled = true;
ButtonFilterShiftPlans.IsEnabled = true;
ButtonResetAllFilters.IsEnabled = true;
}
private void ViewModeCalcRunning()
{
if (_calcRunning)
{
ButtonOpenShiftPlan.IsEnabled = false;
ButtonSaveShiftPlans.IsEnabled = false;
ButtonFilterShiftPlans.IsEnabled = false;
ButtonResetAllFilters.IsEnabled = false;
}
else
{
ButtonOpenShiftPlan.IsEnabled = true;
ButtonSaveShiftPlans.IsEnabled = true;
ButtonFilterShiftPlans.IsEnabled = true;
ButtonResetAllFilters.IsEnabled = true;
}
}
}