Fix download dialog, fix threaded invokes of Eto UTLibFunctions, use UTLibFunctions instead of console logging and direct msgbox calls

This commit is contained in:
JFronny 2021-02-13 13:05:38 +01:00
parent 6af8eba6c6
commit 5814336814
8 changed files with 154 additions and 80 deletions

View File

@ -8,10 +8,10 @@ namespace UpToolEto.Gtk
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
{ {
UpToolEto.Main.Entry(new Application(Eto.Platforms.Gtk), () => new Main(new Application(Eto.Platforms.Gtk), () =>
{ {
}); }, args).Entry();
} }
} }
} }

View File

@ -10,12 +10,12 @@ namespace UpToolEto.Wpf
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
{ {
UpToolEto.Main.Entry(new Application(Eto.Platforms.Wpf), () => new Main(new Application(Eto.Platforms.Wpf), () =>
{ {
Process[] processes = Process.GetProcessesByName("UpTool2"); Process[] processes = Process.GetProcessesByName("UpTool2");
if (processes.Length > 0) if (processes.Length > 0)
BringProcessToFront(processes[0]); BringProcessToFront(processes[0]);
}); }, args).Entry();
} }
public static void BringProcessToFront(Process process) public static void BringProcessToFront(Process process)

View File

@ -1,17 +1,18 @@
using Eto.Forms; using Eto.Forms;
using UpToolEto.Forms; using UpToolEto.Forms;
using UpToolLib.DataStructures;
using UpToolLib.v2; using UpToolLib.v2;
namespace UpToolEto.Controls namespace UpToolEto.Controls
{ {
public class UTMenuBar : MenuBar public class UTMenuBar : MenuBar
{ {
public UTMenuBar(MainForm mainForm, RepoManagement repoManagement, AppList appList) public UTMenuBar(MainForm mainForm, RepoManagement repoManagement, AppList appList, IExternalFunctionality platform)
{ {
Command reloadRepos = new() {MenuText = "Reload", ToolBarText = "Reload"}; Command reloadRepos = new() {MenuText = "Reload", ToolBarText = "Reload"};
reloadRepos.Executed += (_, _) => reloadRepos.Executed += (_, _) =>
{ {
if (Main.Platform.YesNoDialog("This may take some time. Are you sure?", true)) if (platform.YesNoDialog("This may take some time. Are you sure?", true))
{ {
repoManagement.FetchRepos(); repoManagement.FetchRepos();
repoManagement.GetReposFromDisk(); repoManagement.GetReposFromDisk();

View File

@ -1,52 +1,80 @@
using System; using System;
using System.Net; using System.Net;
using System.Threading;
using Eto.Drawing; using Eto.Drawing;
using Eto.Forms; using Eto.Forms;
using UpToolLib.DataStructures;
namespace UpToolEto.Forms namespace UpToolEto.Forms
{ {
public class DownloadDialog : Form public class DownloadDialog : Form
{ {
public bool Success = false; private readonly Uri _url;
private readonly Application _application;
private readonly IExternalFunctionality _platform;
public State CurrentState = State.NotStarted;
public byte[] Download = new byte[0]; public byte[] Download = new byte[0];
public DownloadDialog(Uri url) private readonly ProgressBar _bar;
public DownloadDialog(Uri url, Application application, IExternalFunctionality platform)
{ {
_url = url;
_application = application;
_platform = platform;
Title = "Downloader"; Title = "Downloader";
Resizable = false; Resizable = false;
Maximizable = false; Maximizable = false;
Minimizable = false; Minimizable = false;
WindowStyle = WindowStyle.Utility; WindowStyle = WindowStyle.Utility;
ProgressBar bar = new(); _bar = new();
bar.MaxValue = 100; _bar.MaxValue = 100;
Content = new StackLayout Content = new StackLayout
{ {
Padding = 10, Padding = 10,
Items = Items =
{ {
"Downloading " + url, "Downloading " + url,
new StackLayoutItem(bar, HorizontalAlignment.Stretch, true) new StackLayoutItem(_bar, HorizontalAlignment.Stretch, true)
} }
}; };
Size = new Size(700, 400); Size = new Size(700, 400);
try }
public void StartDownload()
{
CurrentState = State.Downloading;
new Thread(() =>
{ {
WebClient client = new(); try
client.DownloadProgressChanged += (_, args) =>
{ {
bar.Value = args.ProgressPercentage; WebClient client = new();
}; client.DownloadProgressChanged += (_, args) =>
client.DownloadDataCompleted += (_, args) => {
_platform.Log($"{args.BytesReceived}/{args.TotalBytesToReceive}");
_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
{ {
Success = true; CurrentState = State.Failed;
Download = args.Result; _application.Invoke(Close);
Close(); }
}; }).Start();
client.DownloadDataAsync(url, client); }
}
catch public enum State
{ {
Close(); NotStarted,
} Downloading,
Failed,
Success
} }
} }
} }

View File

@ -1,16 +1,19 @@
using System; using System;
using Eto.Drawing; using Eto.Drawing;
using Eto.Forms; using Eto.Forms;
using UpToolLib.DataStructures;
namespace UpToolEto.Forms namespace UpToolEto.Forms
{ {
public class InitScreen : Form public class InitScreen : Form
{ {
private readonly Application _application; private readonly Application _application;
private readonly IExternalFunctionality _platform;
private readonly Label _lab; private readonly Label _lab;
public InitScreen(Application application) public InitScreen(Application application, IExternalFunctionality platform)
{ {
_application = application; _application = application;
_platform = platform;
Title = "UpTool2 Init"; Title = "UpTool2 Init";
Resizable = false; Resizable = false;
Maximizable = false; Maximizable = false;
@ -35,9 +38,14 @@ namespace UpToolEto.Forms
public void SetText(string text) public void SetText(string text)
{ {
Console.WriteLine(text); _application.Invoke(() =>
_lab.Text = text; {
_application.RunIteration(); _platform.Log(text);
_lab.Text = text;
_application.RunIteration();
});
} }
public override void Close() => _application.Invoke(() => base.Close());
} }
} }

View File

@ -2,6 +2,7 @@ using Eto.Drawing;
using Eto.Forms; using Eto.Forms;
using UpToolEto.Controls; using UpToolEto.Controls;
using UpToolLib; using UpToolLib;
using UpToolLib.DataStructures;
//TODO implement tasks queue UI //TODO implement tasks queue UI
//TODO keep track of app state //TODO keep track of app state
@ -12,7 +13,7 @@ namespace UpToolEto.Forms
public partial class MainForm : Form public partial class MainForm : Form
{ {
private readonly AppPanel _appPanel; private readonly AppPanel _appPanel;
public MainForm(InitScreen init, UpToolLibMain lib) public MainForm(InitScreen init, UpToolLibMain lib, IExternalFunctionality platform, bool online)
{ {
Title = "UpTool2"; Title = "UpTool2";
MinimumSize = new Size(600, 100); MinimumSize = new Size(600, 100);
@ -29,7 +30,7 @@ namespace UpToolEto.Forms
}, },
Orientation = Orientation.Horizontal Orientation = Orientation.Horizontal
}; };
Menu = new UTMenuBar(this, lib.V2.RepoManagement, appList); Menu = new UTMenuBar(this, lib.V2.RepoManagement, appList, platform);
Shown += (_, _) => init.Close(); Shown += (_, _) => init.Close();
} }
} }

View File

@ -2,6 +2,7 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading; using System.Threading;
@ -18,59 +19,86 @@ namespace UpToolEto
{ {
public class Main public class Main
{ {
public static bool Online; private readonly IExternalFunctionality _platform;
public static IExternalFunctionality Platform; private readonly UpToolLibMain _lib;
public static void Entry(Application application, Action activityExistsException) private readonly Application _application;
private readonly Action _activityExistsException;
private readonly InitScreen _init;
private readonly bool _skipFetch;
public Main(Application application, Action activityExistsException, string[] args)
{
_skipFetch = args.Contains("--skip-fetch");
_platform = new UTLibFunctions(application);
_lib = new UpToolLibMain(_platform);
_application = application;
_activityExistsException = activityExistsException;
_init = new(application, _platform);
}
public void Entry()
{
new Thread(InitThread).Start();
_application.Run(_init);
}
private void InitThread()
{ {
InitScreen init = new(application);
init.Show();
try try
{ {
Platform = new UTLibFunctions(); _init.SetText("Initializing paths");
UpToolLibMain lib = new(Platform); if (!Directory.Exists(_lib.V1.PathTool.Dir))
init.SetText("Initializing paths"); Directory.CreateDirectory(_lib.V1.PathTool.Dir);
if (!Directory.Exists(lib.V1.PathTool.Dir)) FixXml(_lib.V1.XmlTool, _lib.V1.PathTool);
Directory.CreateDirectory(lib.V1.PathTool.Dir); _init.SetText("Performing checks");
FixXml(lib.V1.XmlTool, lib.V1.PathTool); bool online = false;
init.SetText("Performing checks");
UpdateCheck updateCheck = null; UpdateCheck updateCheck = null;
try try
{ {
updateCheck = lib.V2.UpdateChecker.Check(); updateCheck = _lib.V2.UpdateChecker.Check();
Online = true; online = true;
} }
catch catch
{ {
Online = false; _platform.Log("Could not perform update check, starting offline");
} }
if (Online) if (online && UpdateCheck(updateCheck, _lib.V1.PathTool, _init))
{ {
if (UpdateCheck(updateCheck, lib.V1.PathTool, init)) _platform.Log("Quitting");
{ _application.Quit();
application.Quit(); return;
return;
}
} }
if (!Directory.Exists(lib.V1.PathTool.GetRelative("Apps"))) if (!Directory.Exists(_lib.V1.PathTool.GetRelative("Apps")))
Directory.CreateDirectory(lib.V1.PathTool.GetRelative("Apps")); Directory.CreateDirectory(_lib.V1.PathTool.GetRelative("Apps"));
/*if (Online) if (!_skipFetch && online)
{ {
init.SetText("Fetching repos"); _init.SetText("Fetching repos");
lib.V2.RepoManagement.FetchRepos(); _lib.V2.RepoManagement.FetchRepos();
}*/ }
lib.V2.RepoManagement.GetReposFromDisk(); _lib.V2.RepoManagement.GetReposFromDisk();
init.SetText("Opening"); _init.SetText("Opening");
application.Run(new MainForm(init, lib)); _application.Invoke(() => _application.Run(new MainForm(_init, _lib, _platform, online)));
} }
catch (MutexLockLockedException) catch (MutexLockLockedException)
{ {
init.Close(); _application.Invoke(() =>
MessageBox.Show("Mutex property of other process, quitting"); {
activityExistsException(); _init.Close();
_platform.OkDialog("Mutex property of other process, quitting");
_activityExistsException();
});
}
catch (Exception e)
{
_platform.Log(e.ToString());
}
finally
{
_lib?.Dispose();
} }
} }
public static void FixXml(XmlTool xmlTool, PathTool pathTool, bool throwOnError = false) public void FixXml(XmlTool xmlTool, PathTool pathTool, bool throwOnError = false)
{ {
try try
{ {
@ -79,20 +107,20 @@ namespace UpToolEto
catch (XmlException) catch (XmlException)
{ {
if (throwOnError) throw; if (throwOnError) throw;
MessageBox.Show("Something went wrong while trying to parse XML. Retrying..."); _platform.OkDialog("Something went wrong while trying to parse XML. Retrying...");
File.Delete(pathTool.InfoXml); File.Delete(pathTool.InfoXml);
FixXml(xmlTool, pathTool); FixXml(xmlTool, pathTool);
} }
} }
private static bool UpdateCheck(UpdateCheck updateCheck, PathTool pathTool, InitScreen init) private bool UpdateCheck(UpdateCheck updateCheck, PathTool pathTool, InitScreen init)
{ {
init.SetText("Comparing online version"); init.SetText("Comparing online version");
if (Assembly.GetExecutingAssembly().GetName().Version >= updateCheck.OnlineVersion) return false; if (Assembly.GetExecutingAssembly().GetName().Version >= updateCheck.OnlineVersion) return false;
if (PlatformCheck.IsWindows) if (PlatformCheck.IsWindows)
{ {
init.SetText("Downloading latest"); init.SetText("Downloading latest");
(bool success, byte[] dl) = Platform.Download(updateCheck.Installer); (bool success, byte[] dl) = _platform.Download(updateCheck.Installer);
if (!success) if (!success)
throw new Exception("Failed to update"); throw new Exception("Failed to update");
init.SetText("Verifying"); init.SetText("Verifying");
@ -125,9 +153,9 @@ namespace UpToolEto
} }
else else
{ {
MessageBox.Show("A new version is available. Please install it"); _platform.OkDialog("A new version is available. Please install it");
return false; return false;
} }
} }
} }
} }

View File

@ -12,13 +12,17 @@ namespace UpToolEto
{ {
public class UTLibFunctions : IExternalFunctionality public class UTLibFunctions : IExternalFunctionality
{ {
private readonly Application _application;
private const int ImgSize = 32; private const int ImgSize = 32;
public Tuple<bool, byte[]> Download(Uri link) public Tuple<bool, byte[]> Download(Uri link)
{ {
DownloadDialog dlg = new(link); DownloadDialog dlg = new(link, _application, this);
dlg.Show(); _application.AsyncInvoke(() => dlg.Show());
while (dlg.Visible) Thread.Sleep(20); dlg.StartDownload();
return new Tuple<bool, byte[]>(dlg.Success, dlg.Download); Log("Downloading " + link);
while (dlg.CurrentState == DownloadDialog.State.Downloading) Thread.Sleep(20);
Log("Download complete");
return new Tuple<bool, byte[]>(dlg.CurrentState == DownloadDialog.State.Success, dlg.Download);
} }
public string FetchImageB64(Uri link) public string FetchImageB64(Uri link)
@ -34,10 +38,11 @@ namespace UpToolEto
} }
public bool YesNoDialog(string text, bool defaultVal) => public bool YesNoDialog(string text, bool defaultVal) =>
MessageBox.Show(text, MessageBoxButtons.YesNo, _application.Invoke(() => MessageBox.Show(text, MessageBoxButtons.YesNo,
defaultButton: defaultVal ? MessageBoxDefaultButton.Yes : MessageBoxDefaultButton.No) == DialogResult.Yes; defaultButton: defaultVal ? MessageBoxDefaultButton.Yes : MessageBoxDefaultButton.No)) ==
DialogResult.Yes;
public void OkDialog(string text) => MessageBox.Show(text); public void OkDialog(string text) => _application.Invoke(() => MessageBox.Show(text));
public object GetDefaultIcon() => Bitmap.FromResource("UpToolLib.C_64.ico", typeof(UpToolLibMain)).WithSize(ImgSize, ImgSize); public object GetDefaultIcon() => Bitmap.FromResource("UpToolLib.C_64.ico", typeof(UpToolLibMain)).WithSize(ImgSize, ImgSize);
@ -45,7 +50,10 @@ namespace UpToolEto
public void Log(string text) public void Log(string text)
{ {
Console.WriteLine(text);
//TODO implement visual logging //TODO implement visual logging
} }
public UTLibFunctions(Application application) => _application = application;
} }
} }