Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Correctly handle version/build downgrades, fixes #1960
Browse files Browse the repository at this point in the history
  • Loading branch information
cpraehaus committed Feb 20, 2022
1 parent ae271c4 commit 0495f86
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions Xamarin.Essentials/VersionTracking/VersionTracking.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ public static class VersionTracking

static readonly string sharedName = Preferences.GetPrivatePreferencesSharedName("versiontracking");

static readonly Dictionary<string, List<string>> versionTrail;
static Dictionary<string, List<string>> versionTrail;

static VersionTracking()
{
InitVersionTracking();
}

/// <summary>
/// Initialize VersionTracking module, load data and track current version
/// </summary>
/// <remarks>
/// For internal use. Usually only called once in production code, but multiple times in unit tests
/// </remarks>
internal static void InitVersionTracking()
{
IsFirstLaunchEver = !Preferences.ContainsKey(versionsKey, sharedName) || !Preferences.ContainsKey(buildsKey, sharedName);
if (IsFirstLaunchEver)
Expand All @@ -35,15 +46,19 @@ static VersionTracking()
};
}

IsFirstLaunchForCurrentVersion = !versionTrail[versionsKey].Contains(CurrentVersion);
IsFirstLaunchForCurrentVersion = !versionTrail[versionsKey].Contains(CurrentVersion) || CurrentVersion != LastInstalledVersion;
if (IsFirstLaunchForCurrentVersion)
{
// Avoid duplicates and move current version to end of list if already present
versionTrail[versionsKey].RemoveAll(v => v == CurrentVersion);
versionTrail[versionsKey].Add(CurrentVersion);
}

IsFirstLaunchForCurrentBuild = !versionTrail[buildsKey].Contains(CurrentBuild);
IsFirstLaunchForCurrentBuild = !versionTrail[buildsKey].Contains(CurrentBuild) || CurrentBuild != LastInstalledBuild;
if (IsFirstLaunchForCurrentBuild)
{
// Avoid duplicates and move current build to end of list if already present
versionTrail[buildsKey].RemoveAll(b => b == CurrentBuild);
versionTrail[buildsKey].Add(CurrentBuild);
}

Expand Down Expand Up @@ -119,5 +134,9 @@ static string GetPrevious(string key)
var trail = versionTrail[key];
return (trail.Count >= 2) ? trail[trail.Count - 2] : null;
}

static string LastInstalledVersion => versionTrail[versionsKey].LastOrDefault();

static string LastInstalledBuild => versionTrail[buildsKey].LastOrDefault();
}
}

0 comments on commit 0495f86

Please sign in to comment.