Refactor settings and enhance download manager

Refactor application to use SettingService for managing settings.
Update project to target Windows desktop application.
Enhance DownloadManagerService for better task handling.
Add new dependencies and remove obsolete settings files.
Improve UI feedback and notifications for user actions.
This commit is contained in:
Misaki
2024-07-05 01:56:38 +09:00
parent 4acbd6ba49
commit 939637cc96
28 changed files with 542 additions and 261 deletions

View File

@@ -2,6 +2,7 @@
using DownloadManager.DownloaderCore;
using DownloadManager.Models;
using DownloadManager.Models.UrlGetter;
using DownloadManager.Services;
using DownloadManager.Views.Pages;
using Microsoft.Win32;
using System.IO;
@@ -44,10 +45,10 @@ namespace DownloadManager.ViewModels.Pages
{
TaskDataConfig = new()
{
SaveLocation = Properties.Settings.Default.DefaultSaveLocation,
SplitCount = Properties.Settings.Default.SplitCount,
UseParallelDownload = Properties.Settings.Default.UseParallelDownload,
RetryCount = Properties.Settings.Default.RetryCount
SaveLocation = SettingService.Current.DefaultSaveLocation,
SplitCount = SettingService.Current.SplitCount,
UseParallelDownload = SettingService.Current.UseParallelDownload,
RetryCount = SettingService.Current.RetryCount
};
}
@@ -169,6 +170,13 @@ namespace DownloadManager.ViewModels.Pages
try
{
await downloadManager.FindNextDownloadAndStart();
snackbarService.Show(
"Task added",
"Task will automatically start downloading ASAP",
ControlAppearance.Info,
new SymbolIcon(SymbolRegular.ArrowDownload24),
TimeSpan.FromSeconds(5));
}
catch (Exception ex)
{

View File

@@ -1,5 +1,8 @@
using DownloadManager.DownloaderCore;
using DownloadManager.Models;
using DownloadManager.Resources;
using DownloadManager.Services;
using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
@@ -9,7 +12,7 @@ using Wpf.Ui.Extensions;
namespace DownloadManager.ViewModels.Pages
{
public partial class CompletedPageViewModel(DownloadManagerService downloadManager, IContentDialogService contentDialog) : ObservableObject, INavigationAware
public partial class CompletedPageViewModel(DownloadManagerService downloadManager, IContentDialogService contentDialog, ISnackbarService snackbarService) : ObservableObject, INavigationAware
{
[ObservableProperty]
private ObservableCollection<CompletedItemData> completedTask = new();
@@ -67,17 +70,84 @@ namespace DownloadManager.ViewModels.Pages
}
[RelayCommand]
public async Task ClearCompletedTask()
public async Task ClearCompletedTask(string clearType)
{
await downloadManager.ClearCompletedTask();
switch (clearType)
{
case "Completed":
await downloadManager.ClearCompletedTask();
break;
case "Failed":
await downloadManager.ClearFailedTask();
break;
case "All":
await downloadManager.ClearAllTask();
break;
}
LoadTasks();
}
[RelayCommand]
private static void OpenFile(CompletedItemData completedItem)
private void ExportHistory()
{
var saveFileDialog = new SaveFileDialog
{
Filter = "JSON file (*.json)|*.json",
FileName = "DownloadHistory.json"
};
if (saveFileDialog.ShowDialog() != true)
{
return;
}
File.Copy(Constants.HistoryFilePath, saveFileDialog.FileName, true);
}
[RelayCommand]
private async Task ImportHistory()
{
var openFileDialog = new OpenFileDialog
{
Filter = "JSON file (*.json)|*.json",
FileName = "DownloadHistory.json"
};
if (openFileDialog.ShowDialog() != true)
{
return;
}
if (!File.Exists(openFileDialog.FileName))
{
return;
}
var data = await HistoryStorageService.LoadCompletedItemsAsyncFromFile(openFileDialog.FileName);
if (data == null)
{
return;
}
await HistoryStorageService.SaveCompletedItemsAsync(data);
await downloadManager.LoadDownloadHistory();
LoadTasks();
}
[RelayCommand]
private void OpenFile(CompletedItemData completedItem)
{
if (!File.Exists(completedItem.FullName))
{
snackbarService.Show(
"Can not locate the file",
"The file may be removed or moved to other place",
ControlAppearance.Caution,
new SymbolIcon(SymbolRegular.Warning24),
TimeSpan.FromSeconds(5));
return;
}
Process.Start(new ProcessStartInfo
{
@@ -87,10 +157,19 @@ namespace DownloadManager.ViewModels.Pages
}
[RelayCommand]
private static void OpenFolder(CompletedItemData completedItem)
private void OpenFolder(CompletedItemData completedItem)
{
if (!File.Exists(completedItem.FullName))
{
snackbarService.Show(
"Can not locate the folder",
"The file may be removed or moved to other place",
ControlAppearance.Caution,
new SymbolIcon(SymbolRegular.Warning24),
TimeSpan.FromSeconds(5));
return;
}
Process.Start(new ProcessStartInfo
{

View File

@@ -1,23 +1,56 @@
using DownloadManager.DownloaderCore;
using DownloadManager.Models;
using DownloadManager.Services;
using Microsoft.Toolkit.Uwp.Notifications;
using System.Collections.ObjectModel;
using Wpf.Ui.Controls;
namespace DownloadManager.ViewModels.Pages;
public partial class DownloadingPageViewModel(DownloadManagerService downloadManager) : ObservableObject, INavigationAware
public partial class DownloadingPageViewModel : ObservableObject, INavigationAware
{
private readonly DownloadManagerService _downloadManager;
[ObservableProperty]
private ObservableCollection<DownloadItemData>? downloadingTask;
public DownloadingPageViewModel(DownloadManagerService downloadManager)
{
_downloadManager = downloadManager;
_downloadManager.OnAllWorkerCompleted += DownloadService_OnAllWorkerCompleted;
_downloadManager.OnWorkerFailed += DownloadService_OnWorkerFailed;
}
private void DownloadService_OnAllWorkerCompleted()
{
if (SettingService.Current.SystemNotification)
{
new ToastContentBuilder()
.AddText("All download task completed")
.Show();
}
}
private void DownloadService_OnWorkerFailed(string taskURL, string errorMessage)
{
if (SettingService.Current.SystemNotification)
{
new ToastContentBuilder()
.AddText($"Download task failed: {taskURL}")
.AddText(errorMessage)
.Show();
}
}
public void OnNavigatedFrom()
{
downloadManager.OnActiveWorkerChanged -= DownloadService_OnDownloadServiceChanged;
_downloadManager.OnActiveWorkerChanged -= DownloadService_OnDownloadServiceChanged;
}
public void OnNavigatedTo()
{
downloadManager.OnActiveWorkerChanged += DownloadService_OnDownloadServiceChanged;
_downloadManager.OnActiveWorkerChanged += DownloadService_OnDownloadServiceChanged;
LoadTasks();
}
@@ -29,6 +62,6 @@ public partial class DownloadingPageViewModel(DownloadManagerService downloadMan
private void LoadTasks()
{
DownloadingTask = new(downloadManager.GetDownloadingTask());
DownloadingTask = new(_downloadManager.GetDownloadingTask());
}
}

View File

@@ -3,6 +3,7 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
using DownloadManager.Services;
using Microsoft.Win32;
using System.Diagnostics;
using Wpf.Ui.Appearance;
@@ -17,23 +18,28 @@ public partial class SettingsViewModel : ObservableObject, INavigationAware
private string _appVersion = string.Empty;
[ObservableProperty]
private ApplicationTheme _currentTheme = (ApplicationTheme)Properties.Settings.Default.Theme;
private ApplicationTheme _currentTheme = SettingService.Current.Theme;
[ObservableProperty]
private IEnumerable<ApplicationTheme> _themes = Enum.GetValues(typeof(ApplicationTheme)).Cast<ApplicationTheme>();
[ObservableProperty]
private WindowBackdropType _currentBackdrop = (WindowBackdropType)Properties.Settings.Default.Backdrop;
private WindowBackdropType _currentBackdrop = SettingService.Current.Backdrop;
[ObservableProperty]
private IEnumerable<WindowBackdropType> _backdrops = Enum.GetValues(typeof(WindowBackdropType)).Cast<WindowBackdropType>();
[ObservableProperty]
private string _defaultSaveLocation = Properties.Settings.Default.DefaultSaveLocation;
private string _defaultSaveLocation = SettingService.Current.DefaultSaveLocation;
[ObservableProperty]
private int _splitCount = Properties.Settings.Default.SplitCount;
private int _splitCount = SettingService.Current.SplitCount;
[ObservableProperty]
private bool _useParallelDownload = Properties.Settings.Default.UseParallelDownload;
private bool _useParallelDownload = SettingService.Current.UseParallelDownload;
[ObservableProperty]
private int _retryCount = Properties.Settings.Default.RetryCount;
private int _retryCount = SettingService.Current.RetryCount;
[ObservableProperty]
private int _maxActiveDownloadCount = SettingService.Current.MaxActiveDownloadCount;
[ObservableProperty]
private bool _systemNotification = SettingService.Current.SystemNotification;
public void OnNavigatedTo()
{
@@ -45,12 +51,16 @@ public partial class SettingsViewModel : ObservableObject, INavigationAware
private void ResetOption()
{
CurrentTheme = (ApplicationTheme)Properties.Settings.Default.Theme;
CurrentBackdrop = (WindowBackdropType)Properties.Settings.Default.Backdrop;
DefaultSaveLocation = Properties.Settings.Default.DefaultSaveLocation;
SplitCount = Properties.Settings.Default.SplitCount;
UseParallelDownload = Properties.Settings.Default.UseParallelDownload;
RetryCount = Properties.Settings.Default.RetryCount;
CurrentTheme = SettingService.Current.Theme;
CurrentBackdrop = SettingService.Current.Backdrop;
DefaultSaveLocation = SettingService.Current.DefaultSaveLocation;
SplitCount = SettingService.Current.SplitCount;
UseParallelDownload = SettingService.Current.UseParallelDownload;
RetryCount = SettingService.Current.RetryCount;
MaxActiveDownloadCount = SettingService.Current.MaxActiveDownloadCount;
SystemNotification = SettingService.Current.SystemNotification;
}
public void OnNavigatedFrom()
@@ -66,22 +76,26 @@ public partial class SettingsViewModel : ObservableObject, INavigationAware
private string GetAssemblyVersion()
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString()
?? string.Empty;
?? string.Empty;
}
[RelayCommand]
private void SaveSettings()
{
Properties.Settings.Default.Theme = (int)CurrentTheme;
Properties.Settings.Default.Backdrop = (int)CurrentBackdrop;
Properties.Settings.Default.DefaultSaveLocation = DefaultSaveLocation;
Properties.Settings.Default.SplitCount = SplitCount;
Properties.Settings.Default.UseParallelDownload = UseParallelDownload;
Properties.Settings.Default.RetryCount = RetryCount;
SettingService.Current.Theme = CurrentTheme;
SettingService.Current.Backdrop = CurrentBackdrop;
SettingService.Current.DefaultSaveLocation = DefaultSaveLocation;
SettingService.Current.SplitCount = SplitCount;
SettingService.Current.UseParallelDownload = UseParallelDownload;
SettingService.Current.RetryCount = RetryCount;
SettingService.Current.MaxActiveDownloadCount = MaxActiveDownloadCount;
SettingService.Current.SystemNotification = SystemNotification;
ApplicationThemeManager.Apply(CurrentTheme, CurrentBackdrop);
Properties.Settings.Default.Save();
SettingService.Save();
}
[RelayCommand]