using System;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace PCL.Core.App.Tasks;
///
/// 可观察的任务模型
/// NOTE: 请勿自行修改任何 observable 属性
///
public partial class TaskModel : ObservableObject
{
///
/// 任务标题
///
public required string Title { get; init; }
///
/// 任务是否支持进度
///
public required bool SupportProgress { get; init; }
///
/// 任务当前状态
///
[ObservableProperty] private TaskState _state = TaskState.Waiting;
///
/// 任务当前状态信息
///
[ObservableProperty] private string _stateMessage = string.Empty;
///
/// 任务当前进度, 为 时有效
///
[ObservableProperty] private double _progress = 0.0;
private static readonly Action _EmptyAction = (static () => {});
///
/// 取消任务时触发的事件,值为 表示不支持取消
///
public required Action? OnCancel { private get; init; }
///
/// 取消任务命令
///
public RelayCommand Cancel
{
get => field ??= new RelayCommand(OnCancel ?? _EmptyAction, () => OnCancel is not null);
} = null!;
///
/// 暂停任务时触发的事件,值为 表示不支持暂停
///
public required Action? OnPause { private get; init; }
///
/// 暂停任务命令
///
public RelayCommand Pause
{
get => field ??= new RelayCommand(OnPause ?? _EmptyAction, () => OnPause is not null);
} = null!;
///
/// 任务是否为任务组,即是否存在子任务
///
[ObservableProperty] private bool _isGroup;
///
/// 子任务模型
///
public ObservableCollection Children { get; } = [];
public TaskModel()
{
Children.CollectionChanged += (sender, _) =>
{
if (sender is ObservableCollection c) IsGroup = c.Count > 0;
};
}
}