Skip to content

Commit

Permalink
完成添加下载文件跳转功能
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Dec 20, 2023
1 parent 0ce1516 commit ec47c67
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection;

using UnoFileDownloader.Business;
using UnoFileDownloader.Utils;

namespace UnoFileDownloader
Expand Down Expand Up @@ -76,6 +77,9 @@ protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
return new DispatcherQueueProvider(MainWindow!.DispatcherQueue);
});

// 下载文件管理器
services.AddSingleton<DownloadFileListManager>();
})
.UseNavigation(ReactiveViewModelMappings.ViewModelMappings, RegisterRoutes)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,78 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

using UnoFileDownloader.Utils;

namespace UnoFileDownloader.Business
{
internal class DownloadFileListManager
public class DownloadFileListManager
{
public ObservableCollection<DownloadFileInfo> DownloadFileInfoList { get; } = new ObservableCollection<DownloadFileInfo>();

public async Task InitAsync()
{
var list = await ReadDownloadedFileListAsync();
if (list != null)
{
DownloadFileInfoList.AddRange(list);
}
}

public Task SaveAsync() => WriteDownloadedFileListToFileAsync(DownloadFileInfoList.ToList());

public async Task AddDownloadFileAsync(DownloadFileInfo downloadFileInfo)
{
DownloadFileInfoList.Add(downloadFileInfo);
await SaveAsync();
}

/// <summary>
/// 读取本地存储的下载列表
/// </summary>
/// <returns></returns>
private async Task<List<DownloadFileInfo>?> ReadDownloadedFileListAsync()
{
var file = GetStorageFilePath();

if (!File.Exists(file))
{
return null;
}

var text = await File.ReadAllTextAsync(file);

var downloadFileInfoList = JsonSerializer.Deserialize<List<DownloadFileInfo>>(text);
return downloadFileInfoList;
}

/// <summary>
/// 写入下载列表
/// </summary>
/// <param name="downloadFileInfoList"></param>
/// <returns></returns>
private async Task WriteDownloadedFileListToFileAsync(List<DownloadFileInfo> downloadFileInfoList)
{
var file = GetStorageFilePath();

var text = JsonSerializer.Serialize(downloadFileInfoList);

await File.WriteAllTextAsync(file, text);
}

private string GetStorageFilePath()
{
string folder = AppContext.BaseDirectory;
var file = Path.Join(folder, StorageFile);

return file;
}

private const string StorageFile = "DownloadedFileList.json";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ public string DownloadProcess
/// 下载项被加入的时间
/// </summary>
/// 加入时间不可变,因此不带通知
public string AddedTime { get; set; } = string.Empty;
public string AddedTime { get; init; } = string.Empty;

/// <summary>
/// 下载链接
/// </summary>
/// 下载链接不可变,因此不带通知
public string DownloadUrl { get; set; } = string.Empty;
public string DownloadUrl { get; init; } = string.Empty;

/// <summary>
/// 下载的本地文件保存路径
/// </summary>
/// 下载的本地文件保存路径不可变,因此不带通知
public string FilePath { get; set; } = string.Empty;
public string FilePath { get; init; } = string.Empty;

/// <summary>
/// 当前下载的速度
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<utu:NavigationBar Content="{Binding Title}" />

<StackPanel Grid.Row="1"
HorizontalAlignment="Center"
Expand All @@ -27,6 +26,11 @@
<Button Content="Go to Second Page"
AutomationProperties.AutomationId="SecondPageButton"
Command="{Binding GoToSecond}" />
<utu:NavigationBar Content="{Binding Title}" />

<ContentControl uen:Region.Attached="true" Height="200"/>

</StackPanel>

</Grid>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,40 @@
using System.Text;
using System.Threading.Tasks;

using UnoFileDownloader.Business;

namespace UnoFileDownloader.Presentation
{
public partial record NewTaskModel(INavigator Navigator)
public partial record NewTaskModel(INavigator Navigator, DownloadFileListManager DownloadFileListManager)
{
public string? DownloadSource { set; get; }
public string? FileName { set; get; }

public async Task StartDownloadAsync()
{
if (string.IsNullOrEmpty(DownloadSource))
{
return;
}

var filePath = FileName;

if (string.IsNullOrEmpty(filePath))
{
filePath = Path.GetFileName(DownloadSource);
}

filePath = Path.GetFullPath(filePath);

await DownloadFileListManager.AddDownloadFileAsync(new DownloadFileInfo()
{
AddedTime = DateTime.Now.ToString(format:null),
DownloadUrl = DownloadSource,
FileName = Path.GetFileName(filePath),
FilePath = filePath,
});

await Navigator.NavigateViewModelAsync<MainModel>(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" d:DataContext="{d:DesignInstance local:BindableNewTaskModel}">

<Grid>
<Grid.RowDefinitions>
Expand Down Expand Up @@ -38,7 +38,8 @@
x:Name="DownloadSourceTextBox"
x:Uid="NewTaskPage_DownloadSourceTextBox"
BorderThickness="1"
PlaceholderText="[Please input the download source here]" />
PlaceholderText="[Please input the download source here]"
Text="{Binding DownloadSource,Mode=TwoWay}"/>
<!--<Button
x:Uid="NewTaskPage_DownloadSourceBrowseButton"
Grid.Column="1"
Expand Down Expand Up @@ -107,7 +108,8 @@
x:Uid="NewTaskPage_DownloadButton"
Grid.Column="0"
HorizontalAlignment="Stretch"
Content="[Download]" />
Content="[Download]"
Command="{Binding StartDownloadAsync}"/>
<Button x:Name="CancelButton"
x:Uid="NewTaskPage_CancelButton"
Grid.Column="2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using UnoFileDownloader.Business;

namespace UnoFileDownloader.Presentation
{
public class ShellModel
{
private readonly INavigator _navigator;
private readonly DownloadFileListManager _downloadFileListManager;

public ShellModel(
INavigator navigator)
public ShellModel(INavigator navigator, DownloadFileListManager downloadFileListManager)
{
_navigator = navigator;
_downloadFileListManager = downloadFileListManager;
_ = Start();
}

public async Task Start()
{
await _navigator.NavigateViewModelAsync<AboutModel>(this);
await _downloadFileListManager.InitAsync();
await _navigator.NavigateViewModelAsync<NewTaskModel>(this);
}
}
}

0 comments on commit ec47c67

Please sign in to comment.