Skip to content

Commit

Permalink
(chocolatey#533) Fix remote sources being reloaded on language change
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AdmiringWorm committed Jan 28, 2022
1 parent d903e54 commit 03a984a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public RemoteSourceViewModel(

_eventAggregator.Subscribe(this);

AddSortOptions();

SortSelection = L(nameof(Resources.RemoteSourceViewModel_SortSelectionPopularity));
}

Expand Down Expand Up @@ -182,11 +184,7 @@ public string SearchQuery
set { this.SetPropertyValue(ref _searchQuery, value); }
}

public IReadOnlyList<string> SortOptions => new List<string>
{
L(nameof(Resources.RemoteSourceViewModel_SortSelectionPopularity)),
L(nameof(Resources.RemoteSourceViewModel_SortSelectionAtoZ))
};
public ObservableCollection<string> SortOptions { get; } = new ObservableCollection<string>();

public string SortSelection
{
Expand Down Expand Up @@ -278,7 +276,7 @@ public async Task LoadPackages(bool forceCheckForOutdatedPackages)
{
try
{
if (!CanLoadRemotePackages() && Packages.Any())
if (!IsActive || (!CanLoadRemotePackages() && Packages.Any()))
{
return;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="ShellViewModel.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
Expand Down Expand Up @@ -187,6 +187,7 @@ private void SetActiveItem<T>(T newItem)
else
{
_lastActiveItem = ActiveItem;
DeactivateItem(ActiveItem, false);
}

ActivateItem(newItem);
Expand Down
3 changes: 3 additions & 0 deletions Source/ChocolateyGui.Common/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ NOTE: Probably only necessary to change in RTL languages.</comment>
</data>
<data name="RemoteSourceViewModel_SortSelectionAtoZ" xml:space="preserve">
<value>A-Z</value>
<comment>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.</comment>
</data>
<data name="RemoteSourceViewModel_SortSelectionPopularity" xml:space="preserve">
<value>Popularity</value>
Expand Down

0 comments on commit 03a984a

Please sign in to comment.