This commit is contained in:
2025-11-17 07:19:43 +01:00
parent 72a81e05ad
commit e584996043
29 changed files with 134 additions and 668 deletions

View File

@@ -1,8 +1,7 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using Avalonia.Controls;
using Avalonia.Interactivity;
using ModpackUpdater.Apps.Manager.Api.Model;
using ModpackUpdater.Apps.Manager.Ui.Models.UpdatesCollectorViewMode;
using ModpackUpdater.Manager;
using Pilz.UI.AvaloniaUI.Dialogs;
@@ -10,85 +9,65 @@ namespace ModpackUpdater.Apps.Manager.Ui;
public partial class UpdatesCollectorView : AvaloniaFlyoutBase
{
public record ModUpdateInfo(KeyValuePair<string, string>[] AvailableVersions, InstallAction Origin)
{
public int NewVersion { get; set; }
public IEnumerable<string> DisplayVersions { get; } = AvailableVersions.Select(n =>
{
if (string.IsNullOrWhiteSpace(n.Value) || n.Value == n.Key)
return n.Key;
return $"{n.Value} ({n.Value})";
});
}
public record ModUpdates(IList<ModUpdateInfo> List);
private readonly IWorkspace workspace;
private readonly ModpackFactory factory = new();
private readonly InstallAction[] actions;
public ModUpdates? CurrentUpdates { get; private set; }
public UpdatesCollectorViewModel Model { get; } = new();
public UpdatesCollectorView(IWorkspace workspace, params InstallAction[] actions)
{
this.workspace = workspace;
this.actions = actions;
DataContext = Model;
InitializeComponent();
}
private async Task<ModUpdates> FindUpdates()
private async Task FindUpdates()
{
var list = new ObservableCollection<ModUpdateInfo>();
ProgressBarStatus.Maximum = actions.Length;
Model.ProgressVisible = true;
Model.ProgressMaxValue = actions.Length;
foreach (var action in actions)
{
{
var updates = await factory.FindUpdates(action, workspace.ModpackConfig?.MinecraftVersion, workspace.ModpackConfig?.ModLoader ?? ModLoader.Any);
ProgressBarStatus.Value += 1;
TextBlockStatus.Text = $"{Math.Round(ProgressBarStatus.Value / ProgressBarStatus.Maximum * 100)}";
Model.ProgressValue += 1;
if (updates == null || updates.Length == 0 || updates[0].Value == action.SourceTag)
continue;
list.Add(new(updates, action));
Model.Updates.Add(new(updates, action));
if (IsClosed)
break;
}
return new ModUpdates(list);
Model.ProgressVisible = false;
}
protected override void OnLoadData(DoWorkEventArgs e)
protected override object GetResult()
{
e.Result = Task.Run(FindUpdates).Result;
return new ModUpdates(Model.Updates);
}
protected override void OnLoadDataCompleted(RunWorkerCompletedEventArgs e)
private async void Me_OnLoaded(object? sender, RoutedEventArgs e)
{
if (e.Result is ModUpdates updates)
ItemsControlUpdates.ItemsSource = (CurrentUpdates = updates).List;
}
private void Me_OnLoaded(object? sender, RoutedEventArgs e)
{
LoadData();
await FindUpdates();
}
private void TextBoxSearch_OnTextChanged(object? sender, TextChangedEventArgs e)
{
var searchString = TextBoxSearch.Text?.Trim().ToLowerInvariant();
var searchString = Model.SearchText?.Trim().ToLowerInvariant();
var hasNoSearch = string.IsNullOrWhiteSpace(searchString);
foreach (var item in ItemsControlUpdates.Items.OfType<Grid>())
{
item.IsVisible = hasNoSearch || (item.Name != null && item.Name.Contains(searchString!, StringComparison.InvariantCultureIgnoreCase));
}
foreach (var item in Model.Updates)
item.Visible = hasNoSearch || (item.Origin.Name != null && item.Origin.Name.Contains(searchString!, StringComparison.InvariantCultureIgnoreCase));
}
private void ButtonRemoveUpdate_Click(object? sender, RoutedEventArgs e)
{
if (sender is Button button && button.DataContext is ModUpdateInfo update)
CurrentUpdates?.List.Remove(update);
Model.Updates.Remove(update);
}
}