Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Nov 25, 2023
1 parent 3fbbf82 commit 895fa4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/OneWare.PackageManager/Models/PackageCategoryModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace OneWare.PackageManager.Models;

public class PackageCategoryModel : ObservableObject
{
public List<PackageViewModel> Packages { get; } = new ();
public List<PackageViewModel> Packages { get; } = [];

public ObservableCollection<PackageViewModel> VisiblePackages { get; } = new();
public ObservableCollection<PackageViewModel> VisiblePackages { get; } = [];

public IObservable<object?>? IconObservable { get; }
public string Header { get; }
Expand All @@ -20,6 +20,17 @@ public PackageCategoryModel(string header, IObservable<object?>? iconObservable
IconObservable = iconObservable;
}

public void Add(PackageViewModel model)
{
Packages.Add(model);
}

public void Remove(PackageViewModel model)
{
Packages.Remove(model);
VisiblePackages.Remove(model);
}

public void Filter(string filter, bool showInstalled, bool showAvailable)
{
var filtered =
Expand All @@ -29,6 +40,6 @@ public void Filter(string filter, bool showInstalled, bool showAvailable)
if (!showAvailable) filtered = filtered.Where(x => x.Status != PackageStatus.Available);

VisiblePackages.Clear();
VisiblePackages.AddRange(filtered);
VisiblePackages.AddRange(filtered.OrderBy(x => x.Package.Name));
}
}
13 changes: 7 additions & 6 deletions src/OneWare.PackageManager/ViewModels/PackageManagerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public PackageManagerViewModel(IHttpService httpService, ILogger logger, IPaths

private void FilterPackages()
{
foreach (var cat in PackageCategories)
foreach (var categoryModel in PackageCategories)
{
cat.Filter(Filter, _showInstalled, _showAvailable);
categoryModel.Filter(Filter, _showInstalled, _showAvailable);
}
}

Expand Down Expand Up @@ -142,9 +142,11 @@ private void RegisterPackage(Package package, string? installedVersion = null)
var category = PackageCategories.FirstOrDefault(x =>
x.Header.Equals(package.Category, StringComparison.OrdinalIgnoreCase)) ?? PackageCategories.Last();

PackageCategories.First().Packages.Add(model);
if(category != PackageCategories.First())
PackageCategories.First().Add(model);
Packages.Add(package.Id, model);
category.Packages.Add(model);
category.Add(model);
FilterPackages();

Observable.FromEventPattern(model, nameof(model.Installed))
.Subscribe(x => _ = SaveInstalledPackagesDatabaseAsync())
Expand All @@ -170,8 +172,7 @@ public async Task LoadPackagesAsync()
Packages.Remove(rm.Key);
foreach (var category in PackageCategories)
{
category.Packages.Remove(rm.Value);
category.VisiblePackages.Remove(rm.Value);
category.Remove(rm.Value);
}
}

Expand Down

0 comments on commit 895fa4b

Please sign in to comment.