Skip to content

Commit

Permalink
开始修改WPF,新增CommunityToolkit.Mvvm
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 5, 2024
1 parent 38d295c commit 7441437
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 96 deletions.
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.Core/DependencyInjectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SimpleFFmpegGUI
{
public static class DependencyInjectionExtension
{
public static void AddFFmpegServices(this ServiceCollection services)
public static void AddFFmpegServices(this IServiceCollection services)
{
services.AddDbContext<FFmpegDbContext>()
.AddTransient<Logger>()
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.Core/Manager/PresetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<int> AddOrUpdatePresetAsync(string name, TaskType type, Output
return preset.Id;
}

public async void UpdatePresetAsync(CodePreset preset)
public async Task UpdatePresetAsync(CodePreset preset)
{
db.Presets.Update(preset);
await db.SaveChangesAsync();
Expand Down
6 changes: 3 additions & 3 deletions SimpleFFmpegGUI.Core/Manager/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<TaskInfo> AddTaskAsync(TaskType type, List<InputArguments> pat
return task;
}

public async Task<List<TaskInfo>> GetCurrentTasks(DateTime startTime)
public async Task<List<TaskInfo>> GetCurrentTasksAsync(DateTime startTime)
{
var tasks = db.Tasks.Where(p => p.IsDeleted == false);
var runningTasks = await tasks.Where(p => p.Status == TaskStatus.Processing).ToListAsync();
Expand Down Expand Up @@ -104,7 +104,7 @@ public Task<bool> HasQueueTasksAsync()
return db.Tasks.AnyAsync(p => p.IsDeleted == false && p.Status == TaskStatus.Queue);
}

public async Task ResetTaskAsync(int id, QueueManager queue)
public async Task ResetTaskAsync(int id)
{
TaskInfo task = await db.Tasks.FindAsync(id) ?? throw new ArgumentException($"找不到ID为{id}的任务");
if (queue.Tasks.Any(p => p.Id == id))
Expand All @@ -116,7 +116,7 @@ public async Task ResetTaskAsync(int id, QueueManager queue)
await db.SaveChangesAsync();
}

public async Task<int> TryResetTasksAsync(IEnumerable<int> ids, QueueManager queue)
public async Task<int> TryResetTasksAsync(IEnumerable<int> ids)
{
int count = 0;
foreach (var id in ids)
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.Core/SimpleFFmpegGUI.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="FFMpegCore" Version="5.1.0" />
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.Host/SimpleFFmpegGUI.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="FubarDev.FtpServer" Version="3.1.2" />
<PackageReference Include="FubarDev.FtpServer.FileSystem.DotNet" Version="3.1.2" />
<PackageReference Include="JKang.IpcServiceFramework.Hosting.NamedPipe" Version="3.1.0" />
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="log4net" Version="2.0.17" />
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
4 changes: 3 additions & 1 deletion SimpleFFmpegGUI.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using static SimpleFFmpegGUI.DependencyInjectionExtension;
using System.Windows.Interop;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Expand Down Expand Up @@ -83,7 +84,6 @@ private void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<Config>();

services.AddSingleton<QueueManager>();
services.AddSingleton<TasksAndStatuses>();
services.AddSingleton<AllTasks>();

Expand Down Expand Up @@ -119,6 +119,8 @@ private void ConfigureServices(IServiceCollection services)
services.AddTransient<FileIOPanelViewModel>();
services.AddTransient<PresetsPanelViewModel>();
services.AddTransient<StatusPanelViewModel>();

services.AddFFmpegServices();
}

private void InitializeLogs()
Expand Down
5 changes: 3 additions & 2 deletions SimpleFFmpegGUI.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,10 @@ private async void SettingButton_Click(object sender, RoutedEventArgs e)
await ShowTopTabAsync<SettingPage>();
}

private void StartButton_Click(object sender, RoutedEventArgs e)
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
if (!TaskManager.HasQueueTasks())
var tm = App.ServiceProvider.GetRequiredService<TaskManager>();
if (!await tm.HasQueueTasksAsync())
{
this.CreateMessage().QueueError("没有排队中的任务");
return;
Expand Down
15 changes: 9 additions & 6 deletions SimpleFFmpegGUI.WPF/Model/AllTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace SimpleFFmpegGUI.WPF.Model
{
public class AllTasks : TaskCollectionBase
{
public AllTasks()
public AllTasks(TaskManager tm)
{
Refresh();
taskManager = tm;
RefreshAsync();
}

private int page = 0;
Expand All @@ -22,7 +24,7 @@ public int Page
set
{
this.SetValueAndNotify(ref page, value, nameof(Page));
Refresh();
RefreshAsync();
}
}

Expand All @@ -34,7 +36,7 @@ public int CountPerPage
set
{
this.SetValueAndNotify(ref countPerPage, value, nameof(CountPerPage));
Refresh();
RefreshAsync();
}
}

Expand All @@ -47,16 +49,17 @@ public int PageCount
}

private int count;
private readonly TaskManager taskManager;

public int Count
{
get => count;
set => this.SetValueAndNotify(ref count, value, nameof(Count));
}

public override void Refresh()
public override async Task RefreshAsync()
{
var tasks = TaskManager.GetTasks(null, Page * CountPerPage, CountPerPage);
var tasks = await taskManager.GetTasksAsync(null, Page * CountPerPage, CountPerPage);
Count = tasks.TotalCount;
PageCount = (int)Math.Ceiling(1.0 * Count / CountPerPage);
Tasks = new ObservableCollection<UITaskInfo>(tasks.List.Adapt<List<UITaskInfo>>());
Expand Down
3 changes: 2 additions & 1 deletion SimpleFFmpegGUI.WPF/Model/TaskCollectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace SimpleFFmpegGUI.WPF.Model
{
public abstract class TaskCollectionBase : INotifyPropertyChanged
{
public abstract void Refresh();
public abstract Task RefreshAsync();

private ObservableCollection<UITaskInfo> tasks;

Expand Down
12 changes: 8 additions & 4 deletions SimpleFFmpegGUI.WPF/Model/TasksAndStatuses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
using System.Linq;
using System;
using System.Windows.Shell;
using System.Threading.Tasks;
using TaskStatus = SimpleFFmpegGUI.Model.TaskStatus;

namespace SimpleFFmpegGUI.WPF.Model
{
public class TasksAndStatuses : TaskCollectionBase
{
private List<UITaskInfo> processingTasks;
private readonly TaskManager taskManager;

public TasksAndStatuses(QueueManager queue)
public TasksAndStatuses(QueueManager queue, TaskManager tm)
{
Refresh();
Queue = queue;
taskManager = tm;
queue.TaskManagersChanged += Queue_TaskManagersChanged;
RefreshAsync();
}

public List<UITaskInfo> ProcessingTasks
Expand All @@ -43,9 +47,9 @@ public void NotifyTaskReseted(UITaskInfo task)
}
}

public override void Refresh()
public override async Task RefreshAsync()
{
var tasks = TaskManager.GetCurrentTasks(App.AppStartTime);
var tasks = await taskManager.GetCurrentTasksAsync(App.AppStartTime);
Tasks = new ObservableCollection<UITaskInfo>(tasks.Adapt<List<UITaskInfo>>());
}

Expand Down
12 changes: 6 additions & 6 deletions SimpleFFmpegGUI.WPF/Model/UITaskInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class UITaskInfo : ModelBase, INotifyPropertyChanged

private FFmpegManager processManager;

private int processPriority = ConfigManager.DefaultProcessPriority;
private int processPriority = App.ServiceProvider.GetRequiredService<ConfigManager>().DefaultProcessPriority;

private StatusDto processStatus;

Expand Down Expand Up @@ -303,7 +303,7 @@ public TaskStatus Status
_ => DescriptionConverter.GetDescription(Status)
};

public string Title => Type == TaskType.Custom ? AttributeHelper.GetAttributeValue<NameDescriptionAttribute, string>(Type, p => p.Name)
public string Title => Type == TaskType.Custom ? AttributeHelper.GetAttributeValue<NameDescriptionAttribute, string>(Type, p => p.Name)
: AttributeHelper.GetAttributeValue<NameDescriptionAttribute, string>(Type, p => p.Name) + ":" + InputText;

public TaskType Type
Expand All @@ -317,19 +317,19 @@ public static UITaskInfo FromTask(TaskInfo task)
return task.Adapt<UITaskInfo>();
}

public TaskInfo GetTask()
public Task<TaskInfo> GetTaskAsync()
{
return TaskManager.GetTask(Id);
return App.ServiceProvider.GetRequiredService<TaskManager>().GetTaskAsync(Id);
}

public TaskInfo ToTask()
{
return this.Adapt<TaskInfo>();
}

public void UpdateSelf()
public async Task UpdateSelfAsync()
{
TaskManager.GetTask(Id).Adapt(this);
(await GetTaskAsync()).Adapt(this);
}

private void Manager_ProcessChanged(object sender, ProcessChangedEventArgs e)
Expand Down
14 changes: 8 additions & 6 deletions SimpleFFmpegGUI.WPF/Pages/AddTaskPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,31 @@ private async void AddToQueueButton_Click(object sender, RoutedEventArgs e)
{
List<InputArguments> inputs = fileIOPanel.GetInputs();
OutputArguments args = argumentsPanel.GetOutputArguments();
var tm = App.ServiceProvider.GetRequiredService<TaskManager>();

switch (ViewModel.Type)
{
case TaskType.Code://需要将输入文件单独加入任务
foreach (var input in inputs)
{
TaskInfo task = null;
await Task.Run(() => task = TaskManager.AddTask(TaskType.Code, new List<InputArguments>() { input }, fileIOPanel.GetOutput(input), args));
await tm.AddTaskAsync(TaskType.Code, new List<InputArguments>() { input }, fileIOPanel.GetOutput(input), args);
Dispatcher.Invoke(() => App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task)));
}
this.CreateMessage().QueueSuccess($"已加入{inputs.Count}个任务队列");
break;
case TaskType.Custom or TaskType.Compare://不存在文件输出
{
TaskInfo task = null;
await Task.Run(() => task = TaskManager.AddTask(ViewModel.Type, inputs, null, args));
await tm.AddTaskAsync(ViewModel.Type, inputs, null, args);
App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task));
this.CreateMessage().QueueSuccess("已加入队列");
}
break;
default:
{
TaskInfo task = null;
await Task.Run(() => task = TaskManager.AddTask(ViewModel.Type, inputs, fileIOPanel.GetOutput(inputs[0]), args));
await tm.AddTaskAsync(ViewModel.Type, inputs, fileIOPanel.GetOutput(inputs[0]), args);
App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task));
this.CreateMessage().QueueSuccess("已加入队列");
}
Expand All @@ -157,10 +159,10 @@ private async void AddToQueueButton_Click(object sender, RoutedEventArgs e)
{
fileIOPanel.Reset();
}
if("queue".Equals((sender as Button).Tag as string))
if ("queue".Equals((sender as Button).Tag as string))
{
await Task.Run(() => App.ServiceProvider.GetService<QueueManager>().StartQueue());
this.CreateMessage().QueueSuccess("已开始队列");
await Task.Run(() => App.ServiceProvider.GetService<QueueManager>().StartQueue());
this.CreateMessage().QueueSuccess("已开始队列");
}
SaveAsLastOutputArguments(args);
}
Expand Down
27 changes: 17 additions & 10 deletions SimpleFFmpegGUI.WPF/Pages/LogsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ namespace SimpleFFmpegGUI.WPF.Pages
{
public class LogsPageViewModel : INotifyPropertyChanged
{
public LogsPageViewModel()
public LogsPageViewModel(TaskManager taskManager, LogManager logManager)
{
Tasks = TaskManager.GetTasks().List.Adapt<List<UITaskInfo>>();
taskManager.GetTasksAsync().ContinueWith(data =>
{
Tasks = data.Result.List.Adapt<List<UITaskInfo>>();
});
this.taskManager = taskManager;
this.logManager = logManager;
}

public event PropertyChangedEventHandler PropertyChanged;

private List<Log> logs;
private IList<Log> logs;

public List<Log> Logs
public IList<Log> Logs
{
get => logs;
set => this.SetValueAndNotify(ref logs, value, nameof(Logs));
Expand Down Expand Up @@ -75,12 +80,14 @@ public UITaskInfo SelectedTask
set => this.SetValueAndNotify(ref selectedTask, value, nameof(SelectedTask));
}

public void FillLogs()
public async Task FillLogsAsync()
{
Logs = LogManager.GetLogs(type: Type, taskId: SelectedTask?.Id ?? 0, from: From, to: To).List;
Logs = (await logManager.GetLogsAsync(type: Type, taskId: SelectedTask?.Id ?? 0, from: From, to: To)).List;
}

private int typeIndex;
private readonly TaskManager taskManager;
private readonly LogManager logManager;

public int TypeIndex
{
Expand Down Expand Up @@ -112,18 +119,18 @@ public LogsPage(LogsPageViewModel viewModel)
InitializeComponent();
}

private void FilterButton_Click(object sender, RoutedEventArgs e)
private async void FilterButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.FillLogs();
await ViewModel.FillLogsAsync();
}

public void FillLogs(int taskID)
public async void FillLogs(int taskID)
{
var task = ViewModel.Tasks.FirstOrDefault(p => p.Id == taskID);
Debug.Assert(task != null);
ViewModel.SelectedTask = task;
ViewModel.From = DateTime.MinValue;
ViewModel.FillLogs();
await ViewModel.FillLogsAsync();
}
}
}
Loading

0 comments on commit 7441437

Please sign in to comment.