mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-07-05 12:38:51 +02:00
Compare commits
1 Commits
0dc0d6b43b
...
f23e6bec8c
Author | SHA1 | Date | |
---|---|---|---|
![]() |
f23e6bec8c |
@ -1,24 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Threading;
|
||||
|
||||
namespace DHT.Desktop.Dialogs.Progress;
|
||||
|
||||
static class DelayedProgressDialog {
|
||||
public static async ValueTask Await(Func<Task> taskProvider, TimeSpan delay, Window window, string progressDialogTitle, string progressDialogDescription) {
|
||||
Dispatcher.UIThread.VerifyAccess();
|
||||
|
||||
Task task = Task.Run(taskProvider);
|
||||
if (task.IsCompleted) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Freeze the UI thread for a short while in case the task finishes quickly.
|
||||
_ = Task.WhenAny(Task.Delay(delay), task).GetAwaiter().GetResult();
|
||||
|
||||
if (!task.IsCompleted) {
|
||||
await ProgressDialog.ShowIndeterminate(window, progressDialogTitle, progressDialogDescription, _ => task);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,12 +2,13 @@ using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using DHT.Utils.Logging;
|
||||
|
||||
namespace DHT.Desktop.Dialogs.Progress;
|
||||
|
||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||
public sealed partial class ProgressDialog : Window {
|
||||
private static readonly TimeSpan MinimumShowDuration = TimeSpan.FromMilliseconds(500);
|
||||
private static readonly Log Log = Log.ForType<ProgressDialog>();
|
||||
|
||||
internal static async Task Show(Window owner, string title, Func<ProgressDialog, IProgressCallback, Task> action) {
|
||||
var dialog = new ProgressDialog();
|
||||
@ -42,7 +43,6 @@ public sealed partial class ProgressDialog : Window {
|
||||
}
|
||||
|
||||
private bool isFinished = false;
|
||||
private DateTime startTime = DateTime.Now;
|
||||
private Task progressTask = Task.CompletedTask;
|
||||
|
||||
public ProgressDialog() {
|
||||
@ -50,8 +50,6 @@ public sealed partial class ProgressDialog : Window {
|
||||
}
|
||||
|
||||
public void OnOpened(object? sender, EventArgs e) {
|
||||
startTime = DateTime.Now;
|
||||
|
||||
if (DataContext is ProgressDialogModel model) {
|
||||
progressTask = Task.Run(model.StartTask);
|
||||
progressTask.ContinueWith(OnFinished, TaskScheduler.FromCurrentSynchronizationContext());
|
||||
@ -62,14 +60,8 @@ public sealed partial class ProgressDialog : Window {
|
||||
e.Cancel = !isFinished;
|
||||
}
|
||||
|
||||
private async Task OnFinished(Task task) {
|
||||
private void OnFinished(Task task) {
|
||||
isFinished = true;
|
||||
|
||||
TimeSpan elapsedTime = DateTime.Now - startTime;
|
||||
if (elapsedTime < MinimumShowDuration) {
|
||||
await Task.Delay(MinimumShowDuration - elapsedTime);
|
||||
}
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@ using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using DHT.Desktop.Dialogs.Message;
|
||||
using DHT.Desktop.Dialogs.Progress;
|
||||
using DHT.Desktop.Main.Screens;
|
||||
using DHT.Desktop.Server;
|
||||
using DHT.Server;
|
||||
@ -105,33 +104,32 @@ sealed partial class MainWindowModel : IAsyncDisposable {
|
||||
}
|
||||
|
||||
private async void MainContentScreenModelOnDatabaseClosed(object? sender, EventArgs e) {
|
||||
await DisposeContent();
|
||||
|
||||
Title = DefaultTitle;
|
||||
CurrentScreen = welcomeScreen;
|
||||
|
||||
await DisposeState();
|
||||
|
||||
welcomeScreenModel.DatabaseSelected += OnDatabaseSelected;
|
||||
}
|
||||
|
||||
private async Task DisposeContent() {
|
||||
if (mainContentScreenModel != null) {
|
||||
mainContentScreenModel.DatabaseClosed -= MainContentScreenModelOnDatabaseClosed;
|
||||
await mainContentScreenModel.DisposeAsync();
|
||||
mainContentScreenModel = null;
|
||||
}
|
||||
|
||||
await DisposeState();
|
||||
|
||||
Title = DefaultTitle;
|
||||
CurrentScreen = welcomeScreen;
|
||||
|
||||
welcomeScreenModel.DatabaseSelected += OnDatabaseSelected;
|
||||
}
|
||||
|
||||
private async Task DisposeState() {
|
||||
if (state != null) {
|
||||
await DelayedProgressDialog.Await(() => state.DisposeAsync().AsTask(), TimeSpan.FromMilliseconds(200), window, "Close Database", "Please wait for the database to close...");
|
||||
await state.DisposeAsync();
|
||||
state = null;
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync() {
|
||||
await DisposeContent();
|
||||
if (mainContentScreenModel != null) {
|
||||
await mainContentScreenModel.DisposeAsync();
|
||||
}
|
||||
|
||||
await DisposeState();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user