using System.Collections.ObjectModel; using System.ComponentModel; using Avalonia.Controls; using Avalonia.Interactivity; using ModpackUpdater.Apps.Manager.Api.Model; using ModpackUpdater.Manager; using Pilz.UI.AvaloniaUI.Dialogs; namespace ModpackUpdater.Apps.Manager.Ui; public partial class UpdatesCollectorView : AvaloniaFlyoutBase { public record ModUpdateInfo(KeyValuePair[] AvailableVersions, InstallAction Origin) { public int NewVersion { get; set; } public IEnumerable 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 List); private readonly IWorkspace workspace; private readonly ModpackFactory factory = new(); private readonly InstallAction[] actions; public ModUpdates? CurrentUpdates { get; private set; } public UpdatesCollectorView(IWorkspace workspace, params InstallAction[] actions) { this.workspace = workspace; this.actions = actions; InitializeComponent(); } private async Task FindUpdates() { var list = new ObservableCollection(); ProgressBarStatus.Maximum = 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)}"; if (updates == null || updates.Length == 0 || updates[0].Value == action.SourceTag) continue; list.Add(new(updates, action)); } return new ModUpdates(list); } protected override void OnLoadData(DoWorkEventArgs e) { e.Result = Task.Run(FindUpdates).Result; } protected override void OnLoadDataCompleted(RunWorkerCompletedEventArgs e) { if (e.Result is ModUpdates updates) ItemsControlUpdates.ItemsSource = (CurrentUpdates = updates).List; } private void Me_OnLoaded(object? sender, RoutedEventArgs e) { LoadData(); } private void TextBoxSearch_OnTextChanged(object? sender, TextChangedEventArgs e) { var searchString = TextBoxSearch.Text?.Trim().ToLowerInvariant(); var hasNoSearch = string.IsNullOrWhiteSpace(searchString); foreach (var item in ItemsControlUpdates.Items.OfType()) { item.IsVisible = hasNoSearch || (item.Name != null && item.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); } }