Files
ReminderOrganizer/ViewModels/TaskViewModel.cs

265 lines
8.7 KiB
C#
Raw Normal View History

#nullable enable
using System;
using System.Windows.Input;
using ReminderOrganizer.Core;
using ReminderOrganizer.Models;
using ReminderOrganizer.Services;
namespace ReminderOrganizer.ViewModels
{
public class TaskViewModel : ViewModelBase
{
private readonly ILocalizationService _localizationService;
private readonly ReminderTask _originalTask;
private readonly bool _isNewTask;
private string _name = "";
private string _description = "";
private DateTime _reminderDate = DateTime.Now.AddHours(1);
private string _errorMessage = "";
private bool _hasError = false;
private bool _isRecurring = false;
private int _selectedRecurrenceIndex = 3;
public string Name
{
get => _name;
set
{
SetProperty(ref _name, value);
ValidateInput();
}
}
public string Description
{
get => _description;
set => SetProperty(ref _description, value);
}
public DateTime ReminderDate
{
get => _reminderDate;
set
{
SetProperty(ref _reminderDate, value);
UpdateHourMinuteIndices();
OnPropertyChanged(nameof(NextOccurrenceText));
}
}
public DateTime MinDate => DateTime.Now.AddMinutes(-1);
public int SelectedHourIndex
{
get => _reminderDate.Hour;
set
{
var newDate = new DateTime(_reminderDate.Year, _reminderDate.Month, _reminderDate.Day, value, _reminderDate.Minute, 0);
SetProperty(ref _reminderDate, newDate);
OnPropertyChanged(nameof(NextOccurrenceText));
}
}
public int SelectedMinuteIndex
{
get => _reminderDate.Minute / 5;
set
{
var newDate = new DateTime(_reminderDate.Year, _reminderDate.Month, _reminderDate.Day, _reminderDate.Hour, value * 5, 0);
SetProperty(ref _reminderDate, newDate);
OnPropertyChanged(nameof(NextOccurrenceText));
}
}
public string ErrorMessage
{
get => _errorMessage;
set => SetProperty(ref _errorMessage, value);
}
public bool HasError
{
get => _hasError;
set => SetProperty(ref _hasError, value);
}
public bool IsRecurring
{
get => _isRecurring;
set
{
SetProperty(ref _isRecurring, value);
_originalTask.IsRecurring = value;
OnPropertyChanged(nameof(NextOccurrenceText));
}
}
public int SelectedRecurrenceIndex
{
get => _selectedRecurrenceIndex;
set
{
SetProperty(ref _selectedRecurrenceIndex, value);
_originalTask.RecurrenceType = (RecurrenceType)value;
OnPropertyChanged(nameof(IsDailySelected));
OnPropertyChanged(nameof(IsWeeklySelected));
OnPropertyChanged(nameof(IsMonthlySelected));
OnPropertyChanged(nameof(IsYearlySelected));
OnPropertyChanged(nameof(NextOccurrenceText));
}
}
public bool IsDailySelected
{
get => _selectedRecurrenceIndex == 0;
set { if (value) { SelectedRecurrenceIndex = 0; } }
}
public bool IsWeeklySelected
{
get => _selectedRecurrenceIndex == 1;
set { if (value) { SelectedRecurrenceIndex = 1; } }
}
public bool IsMonthlySelected
{
get => _selectedRecurrenceIndex == 2;
set { if (value) { SelectedRecurrenceIndex = 2; } }
}
public bool IsYearlySelected
{
get => _selectedRecurrenceIndex == 3;
set { if (value) { SelectedRecurrenceIndex = 3; } }
}
public string NextOccurrenceText
{
get
{
if (!IsRecurring) return "";
var tempTask = new ReminderTask
{
ReminderDate = ReminderDate,
RecurrenceType = (RecurrenceType)_selectedRecurrenceIndex
};
var next = tempTask.GetNextOccurrence();
return next.ToString("dd.MM.yyyy HH:mm");
}
}
public string WindowTitle => _isNewTask
? _localizationService.GetString("AddTask")
: _localizationService.GetString("EditTask");
public string NameLabel => _localizationService.GetString("Name");
public string DescriptionLabel => _localizationService.GetString("Description");
public string DateLabel => _localizationService.GetString("Date");
public string SaveText => _localizationService.GetString("Save");
public string CancelText => _localizationService.GetString("Cancel");
public string RecurringLabel => _localizationService.GetString("Recurring");
public string DailyText => _localizationService.GetString("Daily");
public string WeeklyText => _localizationService.GetString("Weekly");
public string MonthlyText => _localizationService.GetString("Monthly");
public string YearlyText => _localizationService.GetString("Yearly");
public ICommand SaveCommand { get; }
public ICommand CancelCommand { get; }
public ICommand SetTomorrowCommand { get; }
public ICommand SetNextWeekCommand { get; }
public ICommand SetNextMonthCommand { get; }
public ReminderTask TaskResult => _originalTask;
public event EventHandler<bool>? RequestClose;
public TaskViewModel(ILocalizationService localizationService, ReminderTask? existingTask = null)
{
_localizationService = localizationService;
_isNewTask = existingTask == null;
if (_isNewTask)
{
_originalTask = new ReminderTask();
_reminderDate = DateTime.Now.AddHours(1);
}
else
{
_originalTask = existingTask!;
_name = existingTask!.Name;
_description = existingTask.Description;
_reminderDate = existingTask.ReminderDate;
_isRecurring = existingTask.IsRecurring;
_selectedRecurrenceIndex = (int)existingTask.RecurrenceType;
}
SaveCommand = new RelayCommand(Save, CanSave);
CancelCommand = new RelayCommand(Cancel);
SetTomorrowCommand = new RelayCommand(SetTomorrow);
SetNextWeekCommand = new RelayCommand(SetNextWeek);
SetNextMonthCommand = new RelayCommand(SetNextMonth);
}
private void UpdateHourMinuteIndices()
{
OnPropertyChanged(nameof(SelectedHourIndex));
OnPropertyChanged(nameof(SelectedMinuteIndex));
}
private bool CanSave() => !string.IsNullOrWhiteSpace(Name);
private void ValidateInput()
{
if (string.IsNullOrWhiteSpace(Name))
{
ErrorMessage = _localizationService.GetString("NameRequired");
HasError = true;
}
else
{
ErrorMessage = "";
HasError = false;
}
}
private void SetTomorrow()
{
ReminderDate = DateTime.Now.Date.AddDays(1).AddHours(9);
}
private void SetNextWeek()
{
var nextWeek = DateTime.Now.Date.AddDays(7);
var dayOfWeek = nextWeek.DayOfWeek;
if (dayOfWeek == DayOfWeek.Saturday) nextWeek = nextWeek.AddDays(2);
else if (dayOfWeek == DayOfWeek.Sunday) nextWeek = nextWeek.AddDays(1);
ReminderDate = nextWeek.AddHours(9);
}
private void SetNextMonth()
{
var nextMonth = DateTime.Now.Date.AddMonths(1);
ReminderDate = new DateTime(nextMonth.Year, nextMonth.Month, 1).AddHours(9);
}
private void Save()
{
if (!CanSave()) return;
_originalTask.Name = Name;
_originalTask.Description = Description;
_originalTask.ReminderDate = ReminderDate;
_originalTask.IsNotified = false;
_originalTask.IsRecurring = IsRecurring;
_originalTask.RecurrenceType = (RecurrenceType)SelectedRecurrenceIndex;
RequestClose?.Invoke(this, true);
}
private void Cancel()
{
RequestClose?.Invoke(this, false);
}
}
}