Files
minecraft-modpack-updater/ModpackUpdater.Apps.Manager/Ui/UpdatesCollectorView.axaml.cs
2025-11-17 19:02:40 +01:00

72 lines
2.2 KiB
C#

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;
namespace ModpackUpdater.Apps.Manager.Ui;
public partial class UpdatesCollectorView : AvaloniaFlyoutBase
{
private readonly IWorkspace workspace;
private readonly ModpackFactory factory = new();
private readonly InstallAction[] actions;
public UpdatesCollectorViewModel Model { get; } = new();
public UpdatesCollectorView(IWorkspace workspace, params InstallAction[] actions)
{
this.workspace = workspace;
this.actions = actions;
DataContext = Model;
InitializeComponent();
}
private async Task FindUpdates()
{
Model.Progress.Start(actions.Length);
foreach (var action in actions)
{
var updates = await factory.FindUpdates(action, workspace.ModpackConfig?.MinecraftVersion, workspace.ModpackConfig?.ModLoader ?? ModLoader.Any);
Model.Progress.Increment();
if (updates == null || updates.Length == 0 || updates[0].Value == action.SourceTag)
continue;
Model.Updates.Add(new(updates, action));
if (IsClosed)
break;
}
Model.Progress.Stop();
}
protected override object GetResult()
{
return new ModUpdates(Model.Updates);
}
private async void Me_OnLoaded(object? sender, RoutedEventArgs e)
{
await FindUpdates();
}
private void TextBoxSearch_OnTextChanged(object? sender, TextChangedEventArgs e)
{
var searchString = Model.SearchText?.Trim().ToLowerInvariant();
var hasNoSearch = string.IsNullOrWhiteSpace(searchString);
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)
Model.Updates.Remove(update);
}
}