Files
29WxOrder/29WxOrderWpf/Ctrls/OrderCtrls/OrderTaskDialogView.xaml.cs
T

186 lines
4.4 KiB
C#

using _29WxOrderWpf.Commands;
using _29WxOrderWpf.Services;
using AllBase.Model.MvvmBaseClasses;
using AllBase.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Work.KJDServer.API.Models;
using WPFCommon.Models;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
namespace _29WxOrderWpf.Ctrls.OrderCtrls;
/// <summary>
/// OrderTaskDialogView.xaml 的交互逻辑
/// </summary>
public partial class OrderTaskDialogView : UserControl
{
private readonly OrderTaskDialogViewModel _viewModel;
public OrderTaskDialogView()
{
InitializeComponent();
this.Loaded += OrderTaskDialogView_Loaded;
_viewModel = new OrderTaskDialogViewModel();
DataContext = _viewModel;
}
private async void OrderTaskDialogView_Loaded(object sender, RoutedEventArgs e)
{
await _viewModel.LoadTasks();
}
}
/// <summary>
/// 任务列表 - ViewModel
/// </summary>
public class OrderTaskDialogViewModel : BindableBaseEx, IDisposable
{
private DispatcherTimer _timer;
private const int RefreshIntervals = 10;
private int _timeCount = 0;
private readonly PullWXOrderService _service;
private readonly NotifyCollectionChangedEventHandler _collectionChangedHandler;
public OrderTaskDialogViewModel()
: this(PullWXOrderService.Instance)
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += _timer_Tick;
_timer.Start();
}
private async void _timer_Tick(object sender, EventArgs e)
{
_timeCount++;
if (_timeCount % RefreshIntervals == 0)
{
await LoadTasks();
_timeCount = 0;
}
RefreshTime = RefreshIntervals - _timeCount;
}
public OrderTaskDialogViewModel(PullWXOrderService service)
{
_service = service ?? PullWXOrderService.Instance;
Tasks = _service.Tasks;
_collectionChangedHandler = OnTasksCollectionChanged;
Tasks.CollectionChanged += _collectionChangedHandler;
ClearFinishedCommand = new DelegateCommand(ClearFinished);
ClearAllCommand = new DelegateCommand(ClearAll);
UpdateSummary();
TasksView = CollectionViewSource.GetDefaultView(Tasks);
TasksView.Filter = FilterTaskStatus;
}
public async Task LoadTasks()
{
await _service.GetTaskStatus();
}
private bool FilterTaskStatus(object obj)
{
if (obj is not OrderTask task)
return false;
if (IsShowAll == true)
return true;
return task.Status !=2;
}
/// <summary>任务列表</summary>
public ObservableCollection<OrderTask> Tasks { get; }
public ICollectionView TasksView { get; }
/// <summary>清空已完成/失败任务</summary>
public ICommand ClearFinishedCommand { get; }
/// <summary>清空全部任务</summary>
public ICommand ClearAllCommand { get; }
/// <summary>汇总文字(总数/执行中/成功/失败)</summary>
public string SummaryText
{
get => GetValue<string>();
set => SetValue(value);
}
public int RefreshTime
{
get => GetValue<int>();
set => SetValue(value);
}
public bool IsShowAll
{
get => GetValue<bool>();
set
{
SetValue(value);
TasksView.Refresh();
}
}
private void ClearFinished()
{
_service.ClearFinished();
}
private void ClearAll()
{
if (System.Windows.MessageBox.Show("确定清空全部任务吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK)
return;
_service.ClearAll();
}
private void OnTasksCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateSummary();
}
private void OnTaskPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(OrderTask.Status))
UpdateSummary();
}
private void UpdateSummary()
{
var items = Tasks;
//int running = items.Count(t => t.Status == OrderTask.Running);
//int success = items.Count(t => t.Status == OrderTask.Success);
//int failed = items.Count(t => t.Status == OrderTask.Failed);
//string.Format("任务总数:{0} 执行中:{1} 成功:{2} 失败:{3}",
//items.Length, running, success, failed);
}
public void Dispose()
{
_timer.Stop();
_timer = null;
}
}