Initial commit: ReminderOrganizer WPF app
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
19
ViewModels/LocalizedStrings.cs
Normal file
19
ViewModels/LocalizedStrings.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
#nullable enable
|
||||
using System.Windows;
|
||||
using ReminderOrganizer.Services;
|
||||
|
||||
namespace ReminderOrganizer.ViewModels
|
||||
{
|
||||
public class LocalizedStrings
|
||||
{
|
||||
private readonly ILocalizationService _localizationService;
|
||||
|
||||
public LocalizedStrings()
|
||||
{
|
||||
_localizationService = ServiceLocator.Services.GetService(typeof(ILocalizationService)) as ILocalizationService
|
||||
?? throw new System.InvalidOperationException("LocalizationService not found");
|
||||
}
|
||||
|
||||
public string this[string key] => _localizationService.GetString(key);
|
||||
}
|
||||
}
|
||||
532
ViewModels/MainViewModel.cs
Normal file
532
ViewModels/MainViewModel.cs
Normal file
@@ -0,0 +1,532 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
using ReminderOrganizer.Core;
|
||||
using ReminderOrganizer.Models;
|
||||
using ReminderOrganizer.Services;
|
||||
|
||||
namespace ReminderOrganizer.ViewModels
|
||||
{
|
||||
public class MainViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IDataService _dataService;
|
||||
private readonly INotificationService _notificationService;
|
||||
private readonly ILocalizationService _localizationService;
|
||||
private readonly IAutostartService _autostartService;
|
||||
private readonly DispatcherTimer _reminderTimer;
|
||||
|
||||
private AppData _appData = new();
|
||||
private Folder? _selectedFolder;
|
||||
private ReminderTask? _selectedTask;
|
||||
private bool _isLoading;
|
||||
|
||||
public bool IsLoading
|
||||
{
|
||||
get => _isLoading;
|
||||
set => SetProperty(ref _isLoading, value);
|
||||
}
|
||||
|
||||
public ObservableCollection<Folder> Folders => _appData.Folders;
|
||||
|
||||
public Folder? SelectedFolder
|
||||
{
|
||||
get => _selectedFolder;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _selectedFolder, value))
|
||||
{
|
||||
OnPropertyChanged(nameof(SelectedFolderTasks));
|
||||
OnPropertyChanged(nameof(SelectedFolderName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ReminderTask? SelectedTask
|
||||
{
|
||||
get => _selectedTask;
|
||||
set => SetProperty(ref _selectedTask, value);
|
||||
}
|
||||
|
||||
public ObservableCollection<ReminderTask>? SelectedFolderTasks => SelectedFolder?.Tasks;
|
||||
|
||||
public string SelectedFolderName => SelectedFolder?.Name ?? L("Welcome");
|
||||
|
||||
public string AddFolderText => L("AddFolder");
|
||||
public string AddTaskText => L("AddTask");
|
||||
public string AutostartText => L("Autostart");
|
||||
public string ExitText => L("Exit");
|
||||
public string FileText => L("File");
|
||||
public string SettingsText => L("Settings");
|
||||
public string LanguageText => L("Language");
|
||||
public string GermanText => L("German");
|
||||
public string EnglishText => L("English");
|
||||
public string DeleteText => L("Delete");
|
||||
public string RenameText => L("Rename");
|
||||
public string ViewText => L("View");
|
||||
public string DarkModeText => L("DarkMode");
|
||||
public string LightModeText => L("LightMode");
|
||||
public string AddTaskToFolderText => L("AddTaskToFolder");
|
||||
public string MinimizeTooltip => L("Minimize");
|
||||
public string MaximizeTooltip => L("Maximize");
|
||||
public string CloseTooltip => L("Close");
|
||||
public string CreateTaskHint => L("CreateTaskHint");
|
||||
public string NoTasks => L("NoTasks");
|
||||
public string FoldersText => L("Folders");
|
||||
public string NameColumnText => L("Name");
|
||||
public string DateColumnText => L("Date");
|
||||
public string EditTaskTooltip => L("EditTaskTooltip");
|
||||
public string DeleteTaskTooltip => L("DeleteTaskTooltip");
|
||||
public string InfoText => L("Info");
|
||||
public string DiscordText => L("Discord");
|
||||
public string ImportText => L("Import");
|
||||
public string ExportText => L("Export");
|
||||
public string ImportCsvText => L("ImportCsv");
|
||||
public string ExportCsvText => L("ExportCsv");
|
||||
public string TestNotificationText => L("TestNotification");
|
||||
public string AppTitleText => L("AppTitle");
|
||||
public string RecurringTaskTooltipText => L("RecurringTaskTooltip");
|
||||
|
||||
public bool IsGerman => _localizationService.CurrentLanguage == "de";
|
||||
public bool IsEnglish => _localizationService.CurrentLanguage == "en";
|
||||
|
||||
private bool _isDarkMode = true;
|
||||
public bool IsDarkMode
|
||||
{
|
||||
get => _isDarkMode;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _isDarkMode, value))
|
||||
{
|
||||
ThemeChanged?.Invoke(this, value);
|
||||
if (_appData != null)
|
||||
{
|
||||
_appData.IsDarkMode = value;
|
||||
_ = SaveAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler<bool>? ThemeChanged;
|
||||
|
||||
public ICommand AddFolderCommand { get; }
|
||||
public ICommand AddTaskCommand { get; }
|
||||
public ICommand AddTaskToFolderCommand { get; }
|
||||
public ICommand DeleteTaskCommand { get; }
|
||||
public ICommand EditTaskCommand { get; }
|
||||
public ICommand ToggleAutostartCommand { get; }
|
||||
public ICommand ExitCommand { get; }
|
||||
public ICommand DeleteFolderCommand { get; }
|
||||
public ICommand SaveFolderCommand { get; }
|
||||
public ICommand SetLanguageDeCommand { get; }
|
||||
public ICommand SetLanguageEnCommand { get; }
|
||||
public ICommand ToggleDarkModeCommand { get; }
|
||||
public ICommand MoveTaskToFolderCommand { get; }
|
||||
public ICommand TestNotificationCommand { get; }
|
||||
public ICommand ImportCsvCommand { get; }
|
||||
public ICommand ExportCsvCommand { get; }
|
||||
|
||||
public event EventHandler<ReminderTask?>? EditTaskRequested;
|
||||
|
||||
public MainViewModel(
|
||||
IDataService dataService,
|
||||
INotificationService notificationService,
|
||||
ILocalizationService localizationService,
|
||||
IAutostartService autostartService)
|
||||
{
|
||||
_dataService = dataService;
|
||||
_notificationService = notificationService;
|
||||
_localizationService = localizationService;
|
||||
_autostartService = autostartService;
|
||||
|
||||
_localizationService.StringsChanged += (s, e) => RefreshStrings();
|
||||
|
||||
AddFolderCommand = new RelayCommand(AddFolder);
|
||||
AddTaskCommand = new RelayCommand(AddTask, () => SelectedFolder != null);
|
||||
AddTaskToFolderCommand = new RelayCommand<Folder>(AddTaskToFolder);
|
||||
DeleteTaskCommand = new RelayCommand<ReminderTask>(DeleteTask);
|
||||
EditTaskCommand = new RelayCommand<ReminderTask>(EditTask);
|
||||
DeleteFolderCommand = new RelayCommand(DeleteFolder, () => SelectedFolder != null);
|
||||
SaveFolderCommand = new RelayCommand(async () => await SaveAsync());
|
||||
ToggleAutostartCommand = new RelayCommand(ToggleAutostart);
|
||||
SetLanguageDeCommand = new RelayCommand(() => SetLanguage("de"));
|
||||
SetLanguageEnCommand = new RelayCommand(() => SetLanguage("en"));
|
||||
ToggleDarkModeCommand = new RelayCommand(() => IsDarkMode = !IsDarkMode);
|
||||
ExitCommand = new RelayCommand(() => Application.Current.Shutdown());
|
||||
MoveTaskToFolderCommand = new RelayCommand<(ReminderTask, Folder)>(MoveTaskToFolder);
|
||||
TestNotificationCommand = new RelayCommand(TestNotification);
|
||||
ImportCsvCommand = new RelayCommand(ImportCsv);
|
||||
ExportCsvCommand = new RelayCommand(ExportCsv);
|
||||
|
||||
_reminderTimer = new DispatcherTimer
|
||||
{
|
||||
Interval = TimeSpan.FromSeconds(30)
|
||||
};
|
||||
_reminderTimer.Tick += (s, e) => CheckReminders();
|
||||
}
|
||||
|
||||
private void SetLanguage(string lang)
|
||||
{
|
||||
_localizationService.SetLanguage(lang);
|
||||
}
|
||||
|
||||
private void RefreshStrings()
|
||||
{
|
||||
OnPropertyChanged(nameof(AddFolderText));
|
||||
OnPropertyChanged(nameof(AddTaskText));
|
||||
OnPropertyChanged(nameof(AutostartText));
|
||||
OnPropertyChanged(nameof(ExitText));
|
||||
OnPropertyChanged(nameof(FileText));
|
||||
OnPropertyChanged(nameof(SettingsText));
|
||||
OnPropertyChanged(nameof(LanguageText));
|
||||
OnPropertyChanged(nameof(GermanText));
|
||||
OnPropertyChanged(nameof(EnglishText));
|
||||
OnPropertyChanged(nameof(DeleteText));
|
||||
OnPropertyChanged(nameof(RenameText));
|
||||
OnPropertyChanged(nameof(ViewText));
|
||||
OnPropertyChanged(nameof(DarkModeText));
|
||||
OnPropertyChanged(nameof(LightModeText));
|
||||
OnPropertyChanged(nameof(AddTaskToFolderText));
|
||||
OnPropertyChanged(nameof(SelectedFolderName));
|
||||
OnPropertyChanged(nameof(IsGerman));
|
||||
OnPropertyChanged(nameof(IsEnglish));
|
||||
OnPropertyChanged(nameof(MinimizeTooltip));
|
||||
OnPropertyChanged(nameof(MaximizeTooltip));
|
||||
OnPropertyChanged(nameof(CloseTooltip));
|
||||
OnPropertyChanged(nameof(CreateTaskHint));
|
||||
OnPropertyChanged(nameof(NoTasks));
|
||||
OnPropertyChanged(nameof(FoldersText));
|
||||
OnPropertyChanged(nameof(NameColumnText));
|
||||
OnPropertyChanged(nameof(DateColumnText));
|
||||
OnPropertyChanged(nameof(EditTaskTooltip));
|
||||
OnPropertyChanged(nameof(DeleteTaskTooltip));
|
||||
OnPropertyChanged(nameof(InfoText));
|
||||
OnPropertyChanged(nameof(DiscordText));
|
||||
OnPropertyChanged(nameof(ImportText));
|
||||
OnPropertyChanged(nameof(ExportText));
|
||||
OnPropertyChanged(nameof(ImportCsvText));
|
||||
OnPropertyChanged(nameof(ExportCsvText));
|
||||
OnPropertyChanged(nameof(TestNotificationText));
|
||||
OnPropertyChanged(nameof(AppTitleText));
|
||||
OnPropertyChanged(nameof(RecurringTaskTooltipText));
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
IsLoading = true;
|
||||
_appData = await _dataService.LoadAsync();
|
||||
_localizationService.Initialize(_appData);
|
||||
|
||||
if (_appData.Folders.Count == 0)
|
||||
{
|
||||
_appData.Folders.Add(new Folder(L("MyTasks")));
|
||||
await SaveAsync();
|
||||
}
|
||||
|
||||
_isDarkMode = _appData.IsDarkMode;
|
||||
OnPropertyChanged(nameof(IsDarkMode));
|
||||
OnPropertyChanged(nameof(Folders));
|
||||
_reminderTimer.Start();
|
||||
IsLoading = false;
|
||||
}
|
||||
|
||||
public string L(string key) => _localizationService.GetString(key);
|
||||
|
||||
private void AddFolder()
|
||||
{
|
||||
var folder = new Folder(L("AddFolder").Replace("+ ", ""));
|
||||
_appData.Folders.Add(folder);
|
||||
_ = SaveAsync();
|
||||
}
|
||||
|
||||
private void DeleteFolder()
|
||||
{
|
||||
if (SelectedFolder == null) return;
|
||||
|
||||
_appData.Folders.Remove(SelectedFolder);
|
||||
SelectedFolder = _appData.Folders.FirstOrDefault();
|
||||
_ = SaveAsync();
|
||||
}
|
||||
|
||||
private void AddTask()
|
||||
{
|
||||
AddTaskInternal(SelectedFolder);
|
||||
}
|
||||
|
||||
private void AddTaskToFolder(Folder? folder)
|
||||
{
|
||||
AddTaskInternal(folder);
|
||||
}
|
||||
|
||||
private void AddTaskInternal(Folder? folder)
|
||||
{
|
||||
if (folder == null)
|
||||
{
|
||||
MessageBox.Show("Kein Ordner ausgewählt! Bitte wähle erst einen Ordner aus.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
|
||||
var mainWindow = Application.Current.MainWindow;
|
||||
var taskWindow = new TaskWindow();
|
||||
|
||||
if (mainWindow != null)
|
||||
{
|
||||
taskWindow.Owner = mainWindow;
|
||||
}
|
||||
|
||||
if (taskWindow.ShowDialog() == true)
|
||||
{
|
||||
folder.Tasks.Add(taskWindow.TaskResult);
|
||||
_ = SaveAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteTask(ReminderTask? task)
|
||||
{
|
||||
if (task == null || SelectedFolder == null) return;
|
||||
SelectedFolder.Tasks.Remove(task);
|
||||
_ = SaveAsync();
|
||||
}
|
||||
|
||||
private void MoveTaskToFolder((ReminderTask Task, Folder TargetFolder) args)
|
||||
{
|
||||
var (task, targetFolder) = args;
|
||||
if (task == null || targetFolder == null) return;
|
||||
|
||||
var sourceFolder = _appData.Folders.FirstOrDefault(f => f.Tasks.Contains(task));
|
||||
if (sourceFolder == null || sourceFolder == targetFolder) return;
|
||||
|
||||
sourceFolder.Tasks.Remove(task);
|
||||
targetFolder.Tasks.Add(task);
|
||||
_ = SaveAsync();
|
||||
|
||||
if (SelectedFolder == sourceFolder)
|
||||
{
|
||||
OnPropertyChanged(nameof(SelectedFolderTasks));
|
||||
}
|
||||
}
|
||||
|
||||
private void EditTask(ReminderTask? task)
|
||||
{
|
||||
if (task == null) return;
|
||||
SelectedTask = task;
|
||||
EditTaskRequested?.Invoke(this, task);
|
||||
}
|
||||
|
||||
private void ToggleAutostart()
|
||||
{
|
||||
_autostartService.ToggleAutostart();
|
||||
var message = _autostartService.IsAutostartEnabled
|
||||
? L("AutostartEnabled")
|
||||
: L("AutostartDisabled");
|
||||
MessageBox.Show(message);
|
||||
}
|
||||
|
||||
private void CheckReminders()
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"[CheckReminders] Timer fired at {DateTime.Now}");
|
||||
bool hasChanges = false;
|
||||
foreach (var folder in _appData.Folders)
|
||||
{
|
||||
foreach (var task in folder.Tasks)
|
||||
{
|
||||
if (!task.IsNotified && task.ReminderDate <= DateTime.Now)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"[CheckReminders] Task fällig: {task.Name}");
|
||||
_notificationService.ShowNotification($"🔔 {task.Name}", task.Description);
|
||||
|
||||
if (task.IsRecurring)
|
||||
{
|
||||
task.ReminderDate = task.GetNextOccurrence();
|
||||
System.Diagnostics.Debug.WriteLine($"[CheckReminders] Next occurrence: {task.ReminderDate}");
|
||||
}
|
||||
else
|
||||
{
|
||||
task.IsNotified = true;
|
||||
}
|
||||
hasChanges = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hasChanges) _ = SaveAsync();
|
||||
}
|
||||
|
||||
private void TestNotification()
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("[TestNotification] Button clicked!");
|
||||
_notificationService.ShowNotification(L("TestNotificationTitle"), L("TestNotificationMessage"));
|
||||
}
|
||||
|
||||
private void ImportCsv()
|
||||
{
|
||||
var dialog = new Microsoft.Win32.OpenFileDialog
|
||||
{
|
||||
Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*",
|
||||
Title = "Import CSV"
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() == true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var lines = System.IO.File.ReadAllLines(dialog.FileName);
|
||||
int imported = 0;
|
||||
int skipped = 0;
|
||||
var folderMap = new Dictionary<string, Folder>();
|
||||
|
||||
foreach (var folder in _appData.Folders)
|
||||
{
|
||||
folderMap[folder.Name] = folder;
|
||||
}
|
||||
|
||||
var dateFormat = "yyyy-MM-dd HH:mm";
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
var line = lines[i];
|
||||
if (string.IsNullOrWhiteSpace(line)) continue;
|
||||
|
||||
var parts = line.Split(';');
|
||||
|
||||
if (parts.Length < 4)
|
||||
{
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
var folderName = parts[0].Trim();
|
||||
var name = parts[1].Trim();
|
||||
var description = parts.Length > 2 ? parts[2].Trim() : "";
|
||||
|
||||
var dateStr = parts.Length > 3 ? parts[3].Trim() : "";
|
||||
if (!DateTime.TryParseExact(dateStr, dateFormat,
|
||||
System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.None, out var date))
|
||||
{
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(folderName))
|
||||
{
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!folderMap.TryGetValue(folderName, out var targetFolder))
|
||||
{
|
||||
targetFolder = new Folder(folderName);
|
||||
_appData.Folders.Add(targetFolder);
|
||||
folderMap[folderName] = targetFolder;
|
||||
}
|
||||
|
||||
var task = new ReminderTask(name, description, date);
|
||||
|
||||
if (parts.Length >= 6)
|
||||
{
|
||||
var recurringStr = parts[4].Trim();
|
||||
if (bool.TryParse(recurringStr, out var isRecurring))
|
||||
{
|
||||
task.IsRecurring = isRecurring;
|
||||
}
|
||||
var recTypeStr = parts[5].Trim();
|
||||
if (Enum.TryParse<RecurrenceType>(recTypeStr, out var recType))
|
||||
{
|
||||
task.RecurrenceType = recType;
|
||||
}
|
||||
}
|
||||
|
||||
targetFolder.Tasks.Add(task);
|
||||
imported++;
|
||||
}
|
||||
|
||||
_ = SaveAsync();
|
||||
OnPropertyChanged(nameof(Folders));
|
||||
OnPropertyChanged(nameof(SelectedFolderTasks));
|
||||
|
||||
var msg = skipped > 0
|
||||
? $"{L("ImportSuccess").Replace("{0}", imported.ToString())} ({skipped} skipped)"
|
||||
: L("ImportSuccess").Replace("{0}", imported.ToString());
|
||||
MessageBox.Show(msg);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(L("ImportError").Replace("{0}", ex.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ExportCsv()
|
||||
{
|
||||
var dialog = new Microsoft.Win32.SaveFileDialog
|
||||
{
|
||||
Filter = "CSV Files (*.csv)|*.csv",
|
||||
Title = "Export CSV",
|
||||
FileName = "tasks.csv"
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() == true)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var writer = new System.IO.StreamWriter(dialog.FileName);
|
||||
writer.WriteLine("Folder;Name;Description;Date;IsRecurring;RecurrenceType");
|
||||
|
||||
foreach (var folder in _appData.Folders)
|
||||
{
|
||||
foreach (var task in folder.Tasks)
|
||||
{
|
||||
var line = string.Join(";",
|
||||
CsvEscape(folder.Name),
|
||||
CsvEscape(task.Name),
|
||||
CsvEscape(task.Description),
|
||||
task.ReminderDate.ToString("yyyy-MM-dd HH:mm"),
|
||||
task.IsRecurring.ToString(),
|
||||
task.RecurrenceType.ToString());
|
||||
writer.WriteLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show(L("ExportSuccess"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(L("ExportError").Replace("{0}", ex.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string CsvEscape(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) return "";
|
||||
if (value.Contains(";") || value.Contains("\"") || value.Contains("\n"))
|
||||
{
|
||||
return "\"" + value.Replace("\"", "\"\"") + "\"";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _dataService.SaveAsync(_appData);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"{L("SaveError")}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
{
|
||||
_reminderTimer.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
264
ViewModels/TaskViewModel.cs
Normal file
264
ViewModels/TaskViewModel.cs
Normal file
@@ -0,0 +1,264 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user