From 03a984a245a3e6deb2da89f0a993dc735450a6be Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Fri, 28 Jan 2022 17:02:22 +0100 Subject: [PATCH] (#533) Fix remote sources being reloaded on language change This commit fixes an issue where if the remote sources view was selected before going into the settings view, when the language is then changed it would reload the packages that was already acquired which at some rare times could result in an endless loop. This fix changes that, to instead only reload the sources if the remote sources view is active, and as thus checks for the sources when the view gets activated the first time instead of during the initialization phase. --- .../Startup/Internationalization.cs | 2 +- .../ViewModels/RemoteSourceViewModel.cs | 55 +++++++++++++++---- .../ViewModels/ShellViewModel.cs | 3 +- .../Properties/Resources.resx | 3 + 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/Source/ChocolateyGui.Common.Windows/Startup/Internationalization.cs b/Source/ChocolateyGui.Common.Windows/Startup/Internationalization.cs index 20c7b9286..3b58e6121 100644 --- a/Source/ChocolateyGui.Common.Windows/Startup/Internationalization.cs +++ b/Source/ChocolateyGui.Common.Windows/Startup/Internationalization.cs @@ -102,7 +102,7 @@ public static void UpdateLanguage(string languageCode) var culture = GetSupportedCultureInfo(languageCode); - if (culture != existingLanguage) + if (!Equals(culture, existingLanguage)) { TranslationSource.Instance.CurrentCulture = culture; CultureInfo.DefaultThreadCurrentCulture = culture; diff --git a/Source/ChocolateyGui.Common.Windows/ViewModels/RemoteSourceViewModel.cs b/Source/ChocolateyGui.Common.Windows/ViewModels/RemoteSourceViewModel.cs index c1de8ff42..27c56afdd 100644 --- a/Source/ChocolateyGui.Common.Windows/ViewModels/RemoteSourceViewModel.cs +++ b/Source/ChocolateyGui.Common.Windows/ViewModels/RemoteSourceViewModel.cs @@ -105,6 +105,8 @@ public RemoteSourceViewModel( _eventAggregator.Subscribe(this); + AddSortOptions(); + SortSelection = L(nameof(Resources.RemoteSourceViewModel_SortSelectionPopularity)); } @@ -182,11 +184,7 @@ public string SearchQuery set { this.SetPropertyValue(ref _searchQuery, value); } } - public IReadOnlyList SortOptions => new List - { - L(nameof(Resources.RemoteSourceViewModel_SortSelectionPopularity)), - L(nameof(Resources.RemoteSourceViewModel_SortSelectionAtoZ)) - }; + public ObservableCollection SortOptions { get; } = new ObservableCollection(); public string SortSelection { @@ -278,7 +276,7 @@ public async Task LoadPackages(bool forceCheckForOutdatedPackages) { try { - if (!CanLoadRemotePackages() && Packages.Any()) + if (!IsActive || (!CanLoadRemotePackages() && Packages.Any())) { return; } @@ -372,6 +370,14 @@ public async void CheckForOutdatedPackages() await LoadPackages(true); } + protected override async void OnActivate() + { + if (!HasLoaded) + { + await LoadPackages(false); + } + } + protected override void OnViewAttached(object view, object context) { _eventAggregator.Subscribe(view); @@ -400,10 +406,6 @@ protected override void OnInitialize() ShowAdditionalPackageInformation = appConfig.ShowAdditionalPackageInformation ?? false; }); -#pragma warning disable 4014 - LoadPackages(false); -#pragma warning restore 4014 - var immediateProperties = new[] { "IncludeAllVersions", "IncludePrerelease", "MatchWord", "SortSelection" @@ -450,10 +452,41 @@ protected override void OnInitialize() protected override void OnLanguageChanged() { - NotifyOfPropertyChange(nameof(SortOptions)); + AddSortOptions(); + SortSelection = _sortSelectionName == "DownloadCount" ? L(nameof(Resources.RemoteSourceViewModel_SortSelectionPopularity)) : L(nameof(Resources.RemoteSourceViewModel_SortSelectionAtoZ)); + + RemoveOldSortOptions(); + } + + private void AddSortOptions() + { + var downloadCount = L(nameof(Resources.RemoteSourceViewModel_SortSelectionPopularity)); + var title = L(nameof(Resources.RemoteSourceViewModel_SortSelectionAtoZ)); + + var index = SortOptions.IndexOf(downloadCount); + + if (index == -1) + { + SortOptions.Insert(0, downloadCount); + } + + index = SortOptions.IndexOf(title); + + if (index == -1) + { + SortOptions.Insert(1, title); + } + } + + private void RemoveOldSortOptions() + { + var downloadCount = L(nameof(Resources.RemoteSourceViewModel_SortSelectionPopularity)); + var title = L(nameof(Resources.RemoteSourceViewModel_SortSelectionAtoZ)); + + SortOptions.RemoveAll(so => so != downloadCount && so != title); } private void SubscribeToLoadPackagesOnSearchQueryChange() diff --git a/Source/ChocolateyGui.Common.Windows/ViewModels/ShellViewModel.cs b/Source/ChocolateyGui.Common.Windows/ViewModels/ShellViewModel.cs index fe5e5213d..d8e932f7a 100644 --- a/Source/ChocolateyGui.Common.Windows/ViewModels/ShellViewModel.cs +++ b/Source/ChocolateyGui.Common.Windows/ViewModels/ShellViewModel.cs @@ -1,4 +1,4 @@ -// -------------------------------------------------------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- // // Copyright 2017 - Present Chocolatey Software, LLC // Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC @@ -187,6 +187,7 @@ private void SetActiveItem(T newItem) else { _lastActiveItem = ActiveItem; + DeactivateItem(ActiveItem, false); } ActivateItem(newItem); diff --git a/Source/ChocolateyGui.Common/Properties/Resources.resx b/Source/ChocolateyGui.Common/Properties/Resources.resx index 17f783ec6..4fdd003e8 100644 --- a/Source/ChocolateyGui.Common/Properties/Resources.resx +++ b/Source/ChocolateyGui.Common/Properties/Resources.resx @@ -432,6 +432,9 @@ NOTE: Probably only necessary to change in RTL languages. A-Z + The letter 'A' should be translated to the first letter in your language, +and the letter 'Z' to the last letter in your alphabet. +Or if there is a traditional way to show sorting by the Title in your language, that should be used instead. Popularity