127 lines
3.9 KiB
C#
127 lines
3.9 KiB
C#
using ModpackUpdater.Apps.Manager.Api.Model;
|
|
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(List<ModUpdateInfo> List);
|
|
|
|
private readonly IWorkspace workspace;
|
|
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 => radListView_VersionTags.SelectedIndex;
|
|
|
|
public UpdatesCollectorUi(IWorkspace workspace, params InstallAction[] actions)
|
|
{
|
|
this.workspace = workspace;
|
|
this.actions = actions;
|
|
|
|
InitializeComponent();
|
|
|
|
radListView_Updates.AutoSizeColumnsMode = ListViewAutoSizeColumnsMode.Fill;
|
|
radListView_VersionTags.AutoSizeColumnsMode = ListViewAutoSizeColumnsMode.Fill;
|
|
}
|
|
|
|
private async Task<ModUpdates> FindUpdates()
|
|
{
|
|
var list = new List<ModUpdateInfo>();
|
|
|
|
foreach (var action in actions)
|
|
{
|
|
var updates = await factory.FindUpdates(action, workspace.ModpackConfig?.MinecraftVersion, workspace.ModpackConfig?.ModLoader ?? ModLoader.Any);
|
|
|
|
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)
|
|
{
|
|
radListView_VersionTags.BeginUpdate();
|
|
radListView_VersionTags.Items.Clear();
|
|
|
|
foreach (var kvp in updates.AvailableVersions)
|
|
{
|
|
var item = new ListViewDataItem();
|
|
item[0] = kvp.Key;
|
|
item[1] = kvp.Value;
|
|
radListView_VersionTags.Items.Add(item);
|
|
}
|
|
|
|
radListView_VersionTags.SelectedIndex = updates.NewVersion;
|
|
radListView_VersionTags.EndUpdate();
|
|
}
|
|
|
|
private async void UpdatesCollectorUi_Shown(object sender, EventArgs e)
|
|
{
|
|
radWaitingBar1.StartWaiting();
|
|
CurrentUpdates = await FindUpdates();
|
|
LoadUpdates(CurrentUpdates);
|
|
radWaitingBar1.StopWaiting();
|
|
}
|
|
|
|
private void RadListView_Updates_SelectedItemChanged(object sender, EventArgs e)
|
|
{
|
|
if (SelectedUpdate is ModUpdateInfo updates)
|
|
LoadVersions(updates);
|
|
}
|
|
|
|
private void RadListView_VersionTags_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
var newIndex = SelectedVersion;
|
|
|
|
if (newIndex != -1 && SelectedUpdate is ModUpdateInfo updates)
|
|
{
|
|
updates.NewVersion = newIndex;
|
|
UpdateUpdatesItem(radListView_Updates.SelectedItem);
|
|
}
|
|
}
|
|
|
|
private void RadListView_Updates_ItemRemoved(object sender, ListViewItemEventArgs e)
|
|
{
|
|
if (e.Item?.Value is ModUpdateInfo update && CurrentUpdates is not null && CurrentUpdates.List.Contains(update))
|
|
CurrentUpdates.List.Remove(update);
|
|
}
|
|
}
|