Files
C4IT-F4SD-Client/FasdDesktopUi/Basics/UserControls/TimerView.xaml.cs
Meik 0b52999b1d feat: expand cockpit integrations and support workflows
Add Phoenix remote desktop, action connector, documentation engine, and gamification support. Refactor support-case processing and search/action UI, and update installer assets and dependencies.
2026-07-13 11:16:53 +02:00

218 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using C4IT.FASD.Base;
using static C4IT.Logging.cLogManager;
namespace FasdDesktopUi.Basics.UserControls
{
public partial class TimerView : UserControl
{
#region Fields
public event EventHandler<EventArgs> OnPauseStarted;
private static readonly List<DateTime> _startTimes = new List<DateTime>();
private static readonly List<DateTime> _endTimes = new List<DateTime>();
private static readonly List<DateTime> _pausedTimesStart = new List<DateTime>();
private static readonly List<DateTime> _pausedTimesEnd = new List<DateTime>();
public static List<cF4SDCaseTime> CaseTimes = new List<cF4SDCaseTime>();
private static Dictionary<string, object> _finalWorkingTimes = new Dictionary<string, object>();
private static DispatcherTimer _timer;
private static TimeSpan _elapsedTime;
private static TimeSpan _totalPausedTime = TimeSpan.Zero;
private static TimeSpan _pauseDuration;
#endregion
#region
public TimerView()
{
InitializeComponent();
}
public static void ResetTimer()
{
_startTimes.Clear();
_endTimes.Clear();
_pausedTimesEnd.Clear();
_pausedTimesStart.Clear();
_finalWorkingTimes.Clear();
_elapsedTime = TimeSpan.Zero;
CaseTimes.Clear();
StartTimer();
}
public static void StartTimer()
{
try
{
_startTimes?.Add(DateTime.UtcNow);
_timer?.Start();
}
catch (Exception E)
{
LogException(E);
}
}
public void StopTimer()
{
try
{
_endTimes?.Add(DateTime.UtcNow);
_timer?.Stop();
UpdateTimerControl();
}
catch (Exception E)
{
LogException(E);
}
}
private void UserControl_Initialized(object sender, EventArgs e)
{
try
{
ResetTimer();
_timer = new DispatcherTimer(TimeSpan.FromSeconds(1.0), DispatcherPriority.Loaded, new EventHandler((s, args) =>
{
try { UpdateTimerControl(); }
catch { }
}),
Dispatcher.CurrentDispatcher);
}
catch (Exception E)
{
LogException(E);
}
}
private void UpdateTimerControl()
{
try
{
TimerControl.Text = _elapsedTime.ToString(@"hh\:mm\:ss");
_elapsedTime = _elapsedTime.Add(TimeSpan.FromSeconds(1.0));
}
catch (Exception E)
{
LogException(E);
}
}
public static Dictionary<string, object> GetWorkTimes()
{
try
{
var currentDate = DateTime.UtcNow.Date.ToString("d");
var startDateTime = _startTimes.First();
var endDateTime = DateTime.UtcNow;
var bruttoWorkingTime = TimeSpan.Zero;
var nettoWorkingTime = TimeSpan.Zero;
if (_pausedTimesStart.Count == 0)
{
_totalPausedTime = TimeSpan.Zero;
}
if (_pausedTimesEnd.Count > 0)
{
_totalPausedTime = TimeSpan.Zero;
for (int i = 0; i < _pausedTimesStart.Count; i++)
{
var start = _pausedTimesStart[i];
var end = _pausedTimesEnd[i];
_pauseDuration = end - start;
_totalPausedTime += _pauseDuration;
}
}
bruttoWorkingTime = endDateTime - startDateTime;
nettoWorkingTime = bruttoWorkingTime - _totalPausedTime;
_finalWorkingTimes = new Dictionary<string, object>()
{
{"CurrentDate", currentDate},
{"StartTime", _startTimes.First()},
{"EndTime", endDateTime },
{"BruttoWorkingTime", bruttoWorkingTime },
{"NettoWorkingTime", nettoWorkingTime.TotalSeconds },
{"TotalPausedTime", _totalPausedTime }
};
}
catch (Exception E)
{
LogException(E);
}
return _finalWorkingTimes;
}
#endregion
#region Click-Events
public void EndPause()
{
try
{
if (_timer?.IsEnabled == true)
return;
_timer?.Start();
_pausedTimesEnd?.Add(DateTime.UtcNow);
CaseTimes.Add(new cF4SDCaseTime()
{
StatusId = CaseStatus.InProgress,
CaseTime = DateTime.UtcNow
});
}
catch (Exception E)
{
LogException(E);
}
}
private void PauseButton_Click()
{
try
{
_timer?.Stop();
_pausedTimesStart?.Add(DateTime.UtcNow);
CaseTimes.Add(new cF4SDCaseTime()
{
StatusId = CaseStatus.OnHold,
CaseTime = DateTime.UtcNow
});
if (OnPauseStarted != null)
OnPauseStarted?.Invoke(this, new EventArgs());
}
catch (Exception E)
{
LogException(E);
}
}
private void Border_Click(object sender, InputEventArgs e)
=> PauseButton_Click();
#endregion
}
}