171 lines
4.8 KiB
C#
171 lines
4.8 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Windows;
|
|
using BeWo.Core.Commands;
|
|
using BS.Shared.Extensions;
|
|
using DevExpress.Mvvm;
|
|
|
|
namespace BeWo.SchulbegleitenderDienst.ViewModel
|
|
{
|
|
public class AbsenceTimeEditViewModel : ViewModelBase, IDataErrorInfo
|
|
{
|
|
private bool _IsAllDay;
|
|
private DateTime _StartDate;
|
|
private DateTime? _EndDate;
|
|
|
|
public ButtonClickCommand ValidateButtonCommand { get; }
|
|
|
|
public Action<TagesansichtVM> SaveChanges { get; set; }
|
|
|
|
private readonly TagesansichtVM _TagesansichtVM;
|
|
|
|
public bool IsAllDay
|
|
{
|
|
get => _IsAllDay;
|
|
|
|
set
|
|
{
|
|
_IsAllDay = value;
|
|
|
|
if (_IsAllDay)
|
|
{
|
|
_StartDate = _StartDate.MergeDatesByDate(StartDate.Date);
|
|
|
|
_EndDate = _EndDate?.MergeDatesByDate(_EndDate.Value.Date);
|
|
}
|
|
else if(_TagesansichtVM?.StartDate != null)
|
|
{
|
|
_StartDate = _StartDate.MergeDatesByDate(_TagesansichtVM.StartDate.Value);
|
|
|
|
_EndDate = _TagesansichtVM?.EndDate != null ? _EndDate?.MergeDatesByDate(_TagesansichtVM.EndDate.Value) : null;
|
|
}
|
|
|
|
RaisePropertiesChanged();
|
|
}
|
|
}
|
|
|
|
public string StartTime
|
|
{
|
|
get => $"{_StartDate:HH:mm}";
|
|
|
|
set
|
|
{
|
|
if (DateTime.TryParse($"{DateTime.Today:yyyy-MM-dd} {value ?? "00:00"}", out var parsedPrototypeStartDate))
|
|
{
|
|
_StartDate = _StartDate.MergeDatesByDate(parsedPrototypeStartDate);
|
|
RaisePropertiesChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public string EndTime
|
|
{
|
|
get => $"{(_EndDate.HasValue ? _EndDate.Value.ToString("HH:mm") : string.Empty)}";
|
|
|
|
set
|
|
{
|
|
if (DateTime.TryParse($"{DateTime.Today:yyyy-MM-dd} {value ?? "00:00"}", out var parsedPrototypeStartDate))
|
|
{
|
|
_EndDate = _EndDate?.MergeDatesByDate(parsedPrototypeStartDate);
|
|
|
|
RaisePropertiesChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public DateTime StartDate
|
|
{
|
|
get => _StartDate;
|
|
|
|
set
|
|
{
|
|
if (!Equals(_StartDate, value))
|
|
{
|
|
_StartDate = value;
|
|
SetIsAllDay();
|
|
RaisePropertiesChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public DateTime? EndDate
|
|
{
|
|
get => _EndDate;
|
|
|
|
set
|
|
{
|
|
if (!Equals(_EndDate, value))
|
|
{
|
|
_EndDate = value;
|
|
SetIsAllDay();
|
|
RaisePropertiesChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetIsAllDay()
|
|
{
|
|
IsAllDay = _StartDate.Hour == 0 && _StartDate.Minute == 0 && (_EndDate == null || _EndDate.Value.Hour == 0 && _EndDate.Value.Minute == 0);
|
|
}
|
|
|
|
public AbsenceTimeEditViewModel() { }
|
|
|
|
public AbsenceTimeEditViewModel(DateTime startDate, DateTime? endDate, TagesansichtVM tagesansichtVM, Action<TagesansichtVM> saveChanges)
|
|
{
|
|
ValidateButtonCommand = new ButtonClickCommand(ValidateInput);
|
|
|
|
_TagesansichtVM = tagesansichtVM;
|
|
|
|
SaveChanges = saveChanges;
|
|
|
|
StartDate = startDate;
|
|
EndDate = endDate;
|
|
|
|
IsAllDay = startDate.Hour == 0 && startDate.Minute == 0 && (endDate == null || endDate.Value.Hour == 0 && endDate.Value.Minute == 0);
|
|
}
|
|
|
|
string IDataErrorInfo.Error
|
|
{
|
|
get
|
|
{
|
|
var me = (IDataErrorInfo) this;
|
|
var error = me[GetPropertyName(() => StartDate)] +
|
|
me[GetPropertyName(() => EndDate)];
|
|
|
|
return error;
|
|
}
|
|
}
|
|
|
|
string IDataErrorInfo.this[string columnName]
|
|
{
|
|
get
|
|
{
|
|
if (EndDate != null && EndDate.Value < StartDate)
|
|
{
|
|
return "Das Startdatum muss vor dem Enddatum liegen.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void ValidateInput()
|
|
{
|
|
var error = ((IDataErrorInfo) this).Error;
|
|
|
|
if (!string.IsNullOrEmpty(error))
|
|
{
|
|
MessageBox.Show("Das Startdatum muss vor dem Enddatum liegen.", "Ungültige Datumsangaben", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
RaisePropertiesChanged();
|
|
}
|
|
else
|
|
{
|
|
_TagesansichtVM.Absencetime.Start = StartDate;
|
|
_TagesansichtVM.Absencetime.End = EndDate;
|
|
|
|
SaveChanges?.Invoke(_TagesansichtVM);
|
|
}
|
|
}
|
|
}
|
|
}
|