Skip to content

Commit

Permalink
feat: add notification progress
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Mar 20, 2024
1 parent daea7a3 commit 9d1c344
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class Configuration : IPluginConfiguration {
public bool WarnAboutBreakingChanges = true;
public bool ReplaceSortName = true;
public bool HideDefaultVariant = true;
public bool UseNotificationProgress;
public string TitlePrefix = "[HS] ";
public string PenumbraFolder = "Heliosphere";
public string? DefaultCollection;
Expand All @@ -45,6 +46,7 @@ internal Configuration(Configuration other) {
this.WarnAboutBreakingChanges = other.WarnAboutBreakingChanges;
this.ReplaceSortName = other.ReplaceSortName;
this.HideDefaultVariant = other.HideDefaultVariant;
this.UseNotificationProgress = other.UseNotificationProgress;
this.TitlePrefix = other.TitlePrefix;
this.PenumbraFolder = other.PenumbraFolder;
this.DefaultCollection = other.DefaultCollection;
Expand Down
2 changes: 2 additions & 0 deletions DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class DownloadTask : IDisposable {
internal const string ApiBase = "https://heliosphere.app/api";
#endif

internal Guid TaskId { get; } = Guid.NewGuid();
private Plugin Plugin { get; }
private string ModDirectory { get; }
internal Guid Version { get; }
Expand Down Expand Up @@ -1452,6 +1453,7 @@ internal static string Name(this State state) {
return state switch {
State.NotStarted => "Not started",
State.DownloadingPackageInfo => "Downloading package info",
State.CheckingExistingFiles => "Checking existing files",
State.DownloadingFiles => "Downloading files",
State.ConstructingModPack => "Constructing mod pack",
State.AddingMod => "Adding mod",
Expand Down
3 changes: 3 additions & 0 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class Plugin : IDalamudPlugin {
internal PackageState State { get; }
internal Guard<List<DownloadTask>> Downloads { get; } = new([]);
internal PluginUi PluginUi { get; }
private NotificationProgressManager ProgressManager { get; }
internal Server Server { get; }
internal LinkPayloads LinkPayloads { get; }
private CommandHandler CommandHandler { get; }
Expand Down Expand Up @@ -206,6 +207,7 @@ public Plugin() {
this.Penumbra = new PenumbraIpc(this);
this.State = new PackageState(this);
this.PluginUi = new PluginUi(this);
this.ProgressManager = new NotificationProgressManager(this);
this.Server = new Server(this);
this.LinkPayloads = new LinkPayloads(this);
this.CommandHandler = new CommandHandler(this);
Expand All @@ -232,6 +234,7 @@ public void Dispose() {
this.CommandHandler.Dispose();
this.LinkPayloads.Dispose();
this.Server.Dispose();
this.ProgressManager.Dispose();
this.PluginUi.Dispose();
SentrySdk.EndSession();
this.Sentry.Dispose();
Expand Down
90 changes: 90 additions & 0 deletions Ui/NotificationProgressManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Dalamud.Interface.ImGuiNotification;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Plugin.Services;
using Heliosphere.Util;

namespace Heliosphere.Ui;

internal class NotificationProgressManager : IDisposable {
private Plugin Plugin { get; }
private Dictionary<Guid, IActiveNotification> Notifications { get; } = [];

internal NotificationProgressManager(Plugin plugin) {
this.Plugin = plugin;
this.Plugin.Framework.Update += this.FrameworkUpdate;
}

public void Dispose() {
this.Plugin.Framework.Update -= this.FrameworkUpdate;

foreach (var (_, notif) in this.Notifications) {
notif.DismissNow();
}

this.Notifications.Clear();
}

private void FrameworkUpdate(IFramework _) {
this.Update();
}

internal void Update() {
using var guard = this.Plugin.Downloads.Wait(0);
if (guard == null) {
return;
}

foreach (var task in guard.Data) {
if (!this.Notifications.TryGetValue(task.TaskId, out var notif)) {
notif = this.Plugin.NotificationManager.AddNotification(new Notification {
InitialDuration = TimeSpan.MaxValue,
});

notif.Dismiss += args => {
if (args.Reason != NotificationDismissReason.Manual) {
return;
}
task.CancellationToken.Cancel();
};

this.Notifications[task.TaskId] = notif;
}

this.UpdateNotif(notif, task);
}

// remove old notifs
foreach (var id in this.Notifications.Keys.ToList()) {
if (guard.Data.Any(task => task.TaskId == id)) {
continue;
}

var notif = this.Notifications[id];
if (notif.InitialDuration == TimeSpan.MaxValue) {
notif.InitialDuration = TimeSpan.FromSeconds(3);
}

this.Notifications.Remove(id);
}
}

private void UpdateNotif(IActiveNotification notif, DownloadTask task) {
notif.Content = task.StateDataMax == 0
? $"{task.State.Name()} ({task.StateData:N0}"
: $"{task.State.Name()} ({task.StateData:N0} / {task.StateDataMax:N0})";
notif.Progress = task.StateDataMax == 0
? 0
: (float) task.StateData / task.StateDataMax;

if (task.State.IsDone()) {
notif.InitialDuration = TimeSpan.FromSeconds(task.State == State.Errored ? 5 : 3);
notif.Type = task.State switch {
State.Finished => NotificationType.Success,
State.Cancelled => NotificationType.Warning,
State.Errored => NotificationType.Error,
_ => NotificationType.Info,
};
}
}
}

0 comments on commit 9d1c344

Please sign in to comment.