From 0e85741a95aec35f07f301401da4a2a4f3739920 Mon Sep 17 00:00:00 2001 From: Ionite Date: Sun, 16 Jul 2023 17:59:26 -0400 Subject: [PATCH] Fix more nullability warnings --- .../Models/ObservableDictionary.cs | 11 +++--- .../ViewModels/CheckpointBrowserViewModel.cs | 38 +++++-------------- .../ViewModels/Dialogs/InstallerViewModel.cs | 27 ++++++------- .../Dialogs/OneClickInstallViewModel.cs | 2 +- .../ViewModels/PackageManagerViewModel.cs | 2 + .../Models/Packages/BaseGitPackage.cs | 2 +- .../Models/Packages/BasePackage.cs | 2 +- .../Models/Packages/VladAutomatic.cs | 2 +- 8 files changed, 34 insertions(+), 52 deletions(-) diff --git a/StabilityMatrix.Avalonia/Models/ObservableDictionary.cs b/StabilityMatrix.Avalonia/Models/ObservableDictionary.cs index 49ccde95..d5831460 100644 --- a/StabilityMatrix.Avalonia/Models/ObservableDictionary.cs +++ b/StabilityMatrix.Avalonia/Models/ObservableDictionary.cs @@ -2,14 +2,14 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; namespace StabilityMatrix.Avalonia.Models; -public class ObservableDictionary : ICollection>, - IDictionary, +public class ObservableDictionary : IDictionary, INotifyCollectionChanged, INotifyPropertyChanged where TKey : notnull { - protected readonly IDictionary dictionary; + private readonly IDictionary dictionary; public event NotifyCollectionChangedEventHandler? CollectionChanged; public event PropertyChangedEventHandler? PropertyChanged; @@ -91,7 +91,7 @@ public class ObservableDictionary : ICollection(key, value))); + new KeyValuePair(key, value!))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Keys))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Values))); @@ -99,7 +99,8 @@ public class ObservableDictionary : ICollection dictionary.TryGetValue(key, out value); + public bool TryGetValue([NotNull] TKey key, [MaybeNullWhen(false)] out TValue value) + => dictionary.TryGetValue(key, out value); public TValue this[TKey key] { diff --git a/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowserViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowserViewModel.cs index 1c786661..f27f9b85 100644 --- a/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowserViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowserViewModel.cs @@ -58,7 +58,7 @@ public partial class CheckpointBrowserViewModel : PageViewModelBase [ObservableProperty] private bool canGoToPreviousPage; [ObservableProperty] private bool isIndeterminate; [ObservableProperty] private bool noResultsFound; - [ObservableProperty] private string noResultsText; + [ObservableProperty] private string noResultsText = string.Empty; private List allModelCards = new(); @@ -320,57 +320,39 @@ public partial class CheckpointBrowserViewModel : PageViewModelBase CurrentPageNumber++; await TrySearchAgain(false); } - - // On changes to ModelCards, update the view source - partial void OnModelCardsChanged(ObservableCollection? value) - { - // if (value is null) - // { - // ModelCardsView = null; - // } - // // Create new view - // var view = new DataGridCollectionView(value) - // { - // Filter = FilterModelCardsPredicate, - // }; - // ModelCardsView = view; - } partial void OnShowNsfwChanged(bool value) { - settingsManager.Transaction(s => s.ModelBrowserNsfwEnabled = value); + settingsManager.Transaction(s => s.ModelBrowserNsfwEnabled, value); // ModelCardsView?.Refresh(); var updateCards = allModelCards - .Select(model => new CheckpointBrowserCardViewModel(model.CivitModel, - downloadService, settingsManager, dialogFactory, notificationService)) .Where(FilterModelCardsPredicate); ModelCards = new ObservableCollection(updateCards); - if (!HasSearched) - return; + if (!HasSearched) return; UpdateResultsText(); } - partial void OnSelectedPeriodChanged(CivitPeriod oldValue, CivitPeriod newValue) + partial void OnSelectedPeriodChanged(CivitPeriod value) { TrySearchAgain().SafeFireAndForget(); settingsManager.Transaction(s => s.ModelSearchOptions = new ModelSearchOptions( - newValue, SortMode, SelectedModelType)); + value, SortMode, SelectedModelType)); } - partial void OnSortModeChanged(CivitSortMode oldValue, CivitSortMode newValue) + partial void OnSortModeChanged(CivitSortMode value) { TrySearchAgain().SafeFireAndForget(); settingsManager.Transaction(s => s.ModelSearchOptions = new ModelSearchOptions( - SelectedPeriod, newValue, SelectedModelType)); + SelectedPeriod, value, SelectedModelType)); } - partial void OnSelectedModelTypeChanged(CivitModelType oldValue, CivitModelType newValue) + partial void OnSelectedModelTypeChanged(CivitModelType value) { TrySearchAgain().SafeFireAndForget(); settingsManager.Transaction(s => s.ModelSearchOptions = new ModelSearchOptions( - SelectedPeriod, SortMode, newValue)); + SelectedPeriod, SortMode, value)); } private async Task TrySearchAgain(bool shouldUpdatePageNumber = true) @@ -390,7 +372,7 @@ public partial class CheckpointBrowserViewModel : PageViewModelBase private void UpdateResultsText() { NoResultsFound = ModelCards?.Count <= 0; - NoResultsText = allModelCards?.Count > 0 + NoResultsText = allModelCards.Count > 0 ? $"{allModelCards.Count} results hidden by filters" : "No results found"; } diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/InstallerViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/InstallerViewModel.cs index 19f64ca5..eb07be0d 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/InstallerViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/InstallerViewModel.cs @@ -18,8 +18,8 @@ using NLog; using Octokit; using StabilityMatrix.Avalonia.Services; using StabilityMatrix.Core.Helper; +using StabilityMatrix.Core.Helper.Factory; using StabilityMatrix.Core.Models; -using StabilityMatrix.Core.Models.FileInterfaces; using StabilityMatrix.Core.Models.Packages; using StabilityMatrix.Core.Models.Progress; using StabilityMatrix.Core.Processes; @@ -52,7 +52,6 @@ public partial class InstallerViewModel : ContentDialogViewModelBase [ObservableProperty] private GitHubCommit? selectedCommit; [ObservableProperty] private string? releaseNotes; - // [ObservableProperty] private bool isReleaseMode; // Version types (release or commit) [ObservableProperty] @@ -78,14 +77,12 @@ public partial class InstallerViewModel : ContentDialogViewModelBase [ObservableProperty] private string? installName; internal event EventHandler? PackageInstalled; - - public ProgressViewModel InstallProgress { get; } = new() - { - }; + public ProgressViewModel InstallProgress { get; } = new(); public InstallerViewModel( ISettingsManager settingsManager, + IPackageFactory packageFactory, IPyRunner pyRunner, IDownloadService downloadService, INotificationService notificationService, ISharedFolders sharedFolders, @@ -98,7 +95,9 @@ public partial class InstallerViewModel : ContentDialogViewModelBase this.sharedFolders = sharedFolders; this.prerequisiteHelper = prerequisiteHelper; - // AvailablePackages and SelectedPackage need to be set in init + // AvailablePackages and SelectedPackage + AvailablePackages = new ObservableCollection(packageFactory.GetAllAvailablePackages()); + SelectedPackage = AvailablePackages[0]; } public override void OnLoaded() @@ -148,13 +147,13 @@ public partial class InstallerViewModel : ContentDialogViewModelBase { notificationService.Show(new Notification("Package name is empty", "Please enter a name for the package", NotificationType.Error)); + return; } await InstallGitIfNecessary(); - - var libraryDir = new DirectoryPath(settingsManager.LibraryDir, "Packages", InstallName); - SelectedPackage.InstallLocation = $"{settingsManager.LibraryDir}\\Packages\\{InstallName}"; - // SelectedPackage.DisplayName = InstallName; + + SelectedPackage.InstallLocation = Path.Combine( + settingsManager.LibraryDir, "Packages", InstallName); if (!PyRunner.PipInstalled || !PyRunner.VenvInstalled) { @@ -211,7 +210,7 @@ public partial class InstallerViewModel : ContentDialogViewModelBase return branch == null ? version : $"{branch}@{version[..7]}"; } - private Task DownloadPackage(string version, bool isCommitHash) + private Task DownloadPackage(string version, bool isCommitHash) { InstallProgress.Text = "Downloading package..."; @@ -292,10 +291,8 @@ public partial class InstallerViewModel : ContentDialogViewModelBase // ReSharper disable once UnusedParameterInPartialMethod partial void OnSelectedVersionTypeChanged(PackageVersionType value) => OnSelectedPackageChanged(SelectedPackage); - partial void OnSelectedPackageChanged(BasePackage? value) + partial void OnSelectedPackageChanged(BasePackage value) { - if (value == null) return; - ReleaseNotes = string.Empty; AvailableVersions?.Clear(); AvailableCommits?.Clear(); diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/OneClickInstallViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/OneClickInstallViewModel.cs index 6219ba24..01f209e8 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/OneClickInstallViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/OneClickInstallViewModel.cs @@ -26,7 +26,7 @@ public partial class OneClickInstallViewModel : ViewModelBase [ObservableProperty] private string headerText; [ObservableProperty] private string subHeaderText; - [ObservableProperty] private string subSubHeaderText; + [ObservableProperty] private string subSubHeaderText = string.Empty; [ObservableProperty] private bool showInstallButton; [ObservableProperty] private bool isIndeterminate; [ObservableProperty] private ObservableCollection allPackages; diff --git a/StabilityMatrix.Avalonia/ViewModels/PackageManagerViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/PackageManagerViewModel.cs index 71209a7a..22daaecb 100644 --- a/StabilityMatrix.Avalonia/ViewModels/PackageManagerViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/PackageManagerViewModel.cs @@ -270,6 +270,8 @@ public partial class PackageManagerViewModel : PageViewModelBase private async Task UpdateSelectedPackage() { + if (SelectedPackage == null) return; + var package = packageFactory.FindPackageByName(SelectedPackage.PackageName); if (package == null) { diff --git a/StabilityMatrix.Core/Models/Packages/BaseGitPackage.cs b/StabilityMatrix.Core/Models/Packages/BaseGitPackage.cs index b43398cc..51649c6e 100644 --- a/StabilityMatrix.Core/Models/Packages/BaseGitPackage.cs +++ b/StabilityMatrix.Core/Models/Packages/BaseGitPackage.cs @@ -97,7 +97,7 @@ public abstract class BaseGitPackage : BasePackage return allReleases; } - public override async Task DownloadPackage(string version, bool isCommitHash, + public override async Task DownloadPackage(string version, bool isCommitHash, IProgress? progress = null) { var downloadUrl = GetDownloadUrl(version, isCommitHash); diff --git a/StabilityMatrix.Core/Models/Packages/BasePackage.cs b/StabilityMatrix.Core/Models/Packages/BasePackage.cs index eacbac17..0799587d 100644 --- a/StabilityMatrix.Core/Models/Packages/BasePackage.cs +++ b/StabilityMatrix.Core/Models/Packages/BasePackage.cs @@ -18,7 +18,7 @@ public abstract class BasePackage public virtual bool ShouldIgnoreReleases => false; public virtual bool UpdateAvailable { get; set; } - public abstract Task DownloadPackage(string version, bool isCommitHash, + public abstract Task DownloadPackage(string version, bool isCommitHash, IProgress? progress = null); public abstract Task InstallPackage(IProgress? progress = null); public abstract Task RunPackage(string installedPackagePath, string arguments); diff --git a/StabilityMatrix.Core/Models/Packages/VladAutomatic.cs b/StabilityMatrix.Core/Models/Packages/VladAutomatic.cs index 8be9085c..12e1fbeb 100644 --- a/StabilityMatrix.Core/Models/Packages/VladAutomatic.cs +++ b/StabilityMatrix.Core/Models/Packages/VladAutomatic.cs @@ -176,7 +176,7 @@ public class VladAutomatic : BaseGitPackage progress?.Report(new ProgressReport(1, isIndeterminate: false)); } - public override async Task DownloadPackage(string version, bool isCommitHash, IProgress? progress = null) + public override async Task DownloadPackage(string version, bool isCommitHash, IProgress? progress = null) { progress?.Report(new ProgressReport(0.1f, message: "Downloading package...", isIndeterminate: true, type: ProgressType.Download));