add options to update and clear direct links
This commit is contained in:
108
ModpackUpdater.Apps.Manager/Ui/UpdatesCollectorUi.cs
Normal file
108
ModpackUpdater.Apps.Manager/Ui/UpdatesCollectorUi.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using ModpackUpdater.Manager;
|
||||
using Telerik.WinControls.UI;
|
||||
|
||||
namespace ModpackUpdater.Apps.Manager.Ui;
|
||||
|
||||
public partial class UpdatesCollectorUi : RadForm
|
||||
{
|
||||
public record ModUpdateInfo(KeyValuePair<string, string>[] AvailableVersions, InstallAction Origin)
|
||||
{
|
||||
public int NewVersion { get; set; } = 0;
|
||||
}
|
||||
|
||||
public record ModUpdates(IEnumerable<ModUpdateInfo> List);
|
||||
|
||||
private readonly ModpackFactory factory = new();
|
||||
private readonly InstallAction[] actions;
|
||||
|
||||
public ModUpdates? CurrentUpdates { get; private set; }
|
||||
|
||||
public ModUpdateInfo? SelectedUpdate => radListView_Updates.SelectedItem?.Value as ModUpdateInfo;
|
||||
|
||||
public int SelectedVersion => radListControl_VersionTags.SelectedValue as int? ?? -1;
|
||||
|
||||
public UpdatesCollectorUi(params InstallAction[] actions)
|
||||
{
|
||||
this.actions = actions;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private async Task<ModUpdates> FindUpdates()
|
||||
{
|
||||
var list = new List<ModUpdateInfo>();
|
||||
|
||||
foreach (var action in actions)
|
||||
{
|
||||
var updates = await factory.FindUpdates(action);
|
||||
|
||||
if (updates == null || updates.Length == 0 || updates[0].Value == action.SourceTag)
|
||||
continue;
|
||||
|
||||
list.Add(new(updates, action));
|
||||
}
|
||||
|
||||
return new ModUpdates(list);
|
||||
}
|
||||
|
||||
private void LoadUpdates(ModUpdates updates)
|
||||
{
|
||||
radListView_Updates.BeginUpdate();
|
||||
radListView_Updates.Items.Clear();
|
||||
|
||||
foreach (var update in updates.List)
|
||||
{
|
||||
var item = new ListViewDataItem(update);
|
||||
UpdateUpdatesItem(item);
|
||||
radListView_Updates.Items.Add(item);
|
||||
}
|
||||
|
||||
radListView_Updates.EndUpdate();
|
||||
}
|
||||
|
||||
private static void UpdateUpdatesItem(ListViewDataItem? item)
|
||||
{
|
||||
if (item?.Value is not ModUpdateInfo updates)
|
||||
return;
|
||||
|
||||
item[0] = updates.Origin.Name;
|
||||
item[1] = updates.Origin.SourceTag;
|
||||
item[2] = updates.AvailableVersions[updates.NewVersion].Value;
|
||||
}
|
||||
|
||||
private void LoadVersions(ModUpdateInfo updates)
|
||||
{
|
||||
radListControl_VersionTags.BeginUpdate();
|
||||
radListControl_VersionTags.Items.Clear();
|
||||
|
||||
foreach (var kvp in updates.AvailableVersions)
|
||||
radListControl_VersionTags.Items.Add(new RadListDataItem($"{kvp.Key} | ({kvp.Value})", kvp.Value));
|
||||
|
||||
radListControl_VersionTags.SelectedValue = updates.NewVersion;
|
||||
radListControl_VersionTags.EndUpdate();
|
||||
}
|
||||
|
||||
private async void UpdatesCollectorUi_Shown(object sender, EventArgs e)
|
||||
{
|
||||
radWaitingBar1.StartWaiting();
|
||||
var updates = await FindUpdates();
|
||||
LoadUpdates(updates);
|
||||
radWaitingBar1.StopWaiting();
|
||||
}
|
||||
|
||||
private void RadListView_Updates_SelectedItemChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (SelectedUpdate is ModUpdateInfo updates)
|
||||
LoadVersions(updates);
|
||||
}
|
||||
|
||||
private void RadListControl_VersionTags_SelectedValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
var newIndex = SelectedVersion;
|
||||
|
||||
if (newIndex != -1 && SelectedUpdate is ModUpdateInfo updates)
|
||||
{
|
||||
updates.NewVersion = newIndex;
|
||||
UpdateUpdatesItem(radListView_Updates.SelectedItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user