80 lines
2.4 KiB
C#
80 lines
2.4 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Threading;
|
|
using Eto.Drawing;
|
|
using Eto.Forms;
|
|
using UpToolLib.DataStructures;
|
|
|
|
namespace UpToolEto.Forms
|
|
{
|
|
public class DownloadDialog : Dialog
|
|
{
|
|
private readonly Uri _url;
|
|
private readonly Application _application;
|
|
private readonly IExternalFunctionality _platform;
|
|
public State CurrentState = State.NotStarted;
|
|
public byte[] Download = new byte[0];
|
|
private readonly ProgressBar _bar;
|
|
public DownloadDialog(Uri url, Application application, IExternalFunctionality platform)
|
|
{
|
|
_url = url;
|
|
_application = application;
|
|
_platform = platform;
|
|
Title = "Downloader";
|
|
Resizable = false;
|
|
Maximizable = false;
|
|
Minimizable = false;
|
|
WindowStyle = WindowStyle.Utility;
|
|
_bar = new();
|
|
_bar.MaxValue = 100;
|
|
Content = new StackLayout
|
|
{
|
|
Padding = 10,
|
|
Items =
|
|
{
|
|
"Downloading " + url,
|
|
new StackLayoutItem(_bar, HorizontalAlignment.Stretch, true)
|
|
}
|
|
};
|
|
Size = new Size(700, 400);
|
|
}
|
|
|
|
public void StartDownload()
|
|
{
|
|
CurrentState = State.Downloading;
|
|
new Thread(() =>
|
|
{
|
|
try
|
|
{
|
|
WebClient client = new();
|
|
client.DownloadProgressChanged += (_, args) => _application.Invoke(() =>
|
|
{
|
|
_bar.Value = args.ProgressPercentage;
|
|
_application.RunIteration();
|
|
});
|
|
client.DownloadDataCompleted += (_, args) =>
|
|
{
|
|
CurrentState = State.Success;
|
|
Download = args.Result;
|
|
_application.Invoke(Close);
|
|
};
|
|
client.DownloadDataAsync(_url, client);
|
|
}
|
|
catch
|
|
{
|
|
CurrentState = State.Failed;
|
|
_application.Invoke(Close);
|
|
}
|
|
}).Start();
|
|
}
|
|
|
|
public enum State
|
|
{
|
|
NotStarted,
|
|
Downloading,
|
|
Failed,
|
|
Success
|
|
}
|
|
}
|
|
}
|