Skip to content

Commit b9b063c

Browse files
committed
Finalize LegacyVersionTracking
1 parent eadb6c2 commit b9b063c

3 files changed

Lines changed: 63 additions & 15 deletions

File tree

samples/Plugin.Maui.FormsMigration.Sample/MainPage.xaml.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ public MainPage()
1010
// SecureStorage Example
1111
async void SecureStorage_Clicked(object sender, EventArgs e)
1212
{
13+
// More information: https://learn.microsoft.com/dotnet/maui/migration/secure-storage
14+
1315
// Xamarin.Essentials SecureStorage stores its data in $(AppInfo.PackageName).xamarinessentials
1416
// MAUI SecureStorage stores its data in $(AppInfo.PackageName).microsoft.maui.essentials.preferences
15-
// MAUI also uses different encryption for SecureStorage than used in Xamarin.Essentials (I think)
17+
// MAUI also uses different encryption for SecureStorage than used in Xamarin.Essentials.
1618

1719
// $(AppInfo.PackageName) is referring to the Microsoft.Maui.ApplicationModel.AppInfo APIs and
1820
// will be replaced with your actual application ID. These should be the same for both your old app and new app.
@@ -28,7 +30,11 @@ async void SecureStorage_Clicked(object sender, EventArgs e)
2830
// VersionTracking Example
2931
void VersionTracking_Clicked(object sender, EventArgs e)
3032
{
31-
// TBD
33+
// More information: https://learn.microsoft.com/dotnet/maui/migration/version-tracking
34+
35+
// Retrieve values from your legacy Xamarin application through the LegacyVersionTracking class.
36+
37+
// Read legacy version/build number information
3238
labelIsFirst.Text = LegacyVersionTracking.IsFirstLaunchEver.ToString();
3339
labelCurrentVersionIsFirst.Text = LegacyVersionTracking.IsFirstLaunchForCurrentVersion.ToString();
3440
labelCurrentBuildIsFirst.Text = LegacyVersionTracking.IsFirstLaunchForCurrentBuild.ToString();
@@ -43,20 +49,22 @@ void VersionTracking_Clicked(object sender, EventArgs e)
4349
labelPreviousVersion.Text = LegacyVersionTracking.PreviousVersion?.ToString() ?? "none";
4450
labelPreviousBuild.Text = LegacyVersionTracking.PreviousBuild?.ToString() ?? "none";
4551

46-
List<string> vhistory = new() { "1.0", "2.0", "3.0", "4.0", "5.0" };
47-
List<string> bhistory = new() { "1", "2", "3", "4", "5" };
52+
// Transfer the full legacy Xamarin app version tracking information (version & build numbers)
53+
// to the .NET MAUI Version Tracking data store. Before doing so the *new* .NET MAUI Version Tracking
54+
// store will be cleared.
55+
LegacyVersionTracking.TransferHistory(true, true, true);
4856
}
4957

5058
// Properties Example
5159
void AppProperties_Clicked(object sender, EventArgs e)
5260
{
61+
// More information: https://learn.microsoft.com/dotnet/maui/migration/app-properties
62+
5363
// Retrieve values from your legacy Xamarin application through the LegacyApplication class.
5464

5565
// The code below assumes that there is a property value saved with the key "id". Replace this key
5666
// with any value(s) you have stored in your legacy Xamarin app to get them out.
5767

58-
// For more information, see: https://learn.microsoft.com/dotnet/maui/migration/app-properties
59-
6068
int id;
6169
if (LegacyApplication.Current.Properties.ContainsKey("id"))
6270
{

src/Plugin.Maui.FormsMigration/VersionTracking/LegacyPreferences.shared.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ namespace Plugin.Maui.FormsMigration;
44

55
static partial class LegacyPreferences
66
{
7-
internal static string GetPrivatePreferencesSharedName(string feature) => $"{AppInfo.PackageName}.xamarinessentials.{feature}";
7+
internal static string GetPrivatePreferencesSharedName(string feature) =>
8+
$"{AppInfo.PackageName}.xamarinessentials.{feature}";
89

910
internal static bool ContainsKey(string key, string sharedName) => PlatformContainsKey(key, sharedName);
1011
internal static void Remove(string key, string sharedName) => PlatformRemove(key, sharedName);
11-
internal static string Get(string key, string defaultValue, string sharedName) => PlatformGet<string>(key, defaultValue, sharedName);
12+
internal static string Get(string key, string? defaultValue, string sharedName) => PlatformGet<string>(key, defaultValue, sharedName);
1213
}

src/Plugin.Maui.FormsMigration/VersionTracking/LegacyVersionTracking.shared.cs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
using Microsoft.Maui.ApplicationModel;
2+
using Microsoft.Maui.Storage;
23

34
namespace Plugin.Maui.FormsMigration;
45

6+
/// <summary>
7+
/// The LegacyVersionTracking API can be used to access the legacy Xamarin application version tracking information in your .NET MAUI app.
8+
/// </summary>
9+
/// <remarks>Important: make sure that your .NET MAUI app version is higher than the legacy application version number. Not doing so might have unexpected results.</remarks>
510
public static class LegacyVersionTracking
611
{
12+
static string PrivateVersionTrackingSharedNameMaui =>
13+
$"{AppInfo.Current.PackageName}.microsoft.maui.essentials.versiontracking";
14+
715
const string versionsKey = "VersionTracking.Versions";
816
const string buildsKey = "VersionTracking.Builds";
917

@@ -20,6 +28,31 @@ static LegacyVersionTracking()
2028
InitVersionTracking();
2129
}
2230

31+
/// <summary>
32+
/// Transfers the version and/or build number information from the legacy Xamarin app to the .NET MAUI Version Tracking data store.
33+
/// </summary>
34+
/// <param name="includeVersionInfo">Determines whether or not version number information should be included in the transfer.</param>
35+
/// <param name="includeBuildInfo">Determines whether or not build number information should be included in the transfer.</param>
36+
/// <param name="clearMauiVersionHistory">Determines whether or not to clear the current .NET MAUI app version history before transferring the legacy Xamarin app history.</param>
37+
/// <remarks>When not setting <paramref name="clearMauiVersionHistory"/> to <see langword="true"/>, newer version/build numbers might appear before the legacy version/build number history.</remarks>
38+
public static void TransferHistory(bool includeVersionInfo, bool includeBuildInfo, bool clearMauiVersionHistory)
39+
{
40+
if (clearMauiVersionHistory)
41+
{
42+
Preferences.Default.Clear(PrivateVersionTrackingSharedNameMaui);
43+
}
44+
45+
if (includeVersionInfo)
46+
{
47+
WriteVersionTrackingHistory(VersionsKey, LegacyVersionTracking.VersionHistory);
48+
}
49+
50+
if (includeBuildInfo)
51+
{
52+
WriteVersionTrackingHistory(BuildsKey, LegacyVersionTracking.BuildHistory);
53+
}
54+
}
55+
2356
/// <summary>
2457
/// Initialize VersionTracking module, load data and track current version.
2558
/// </summary>
@@ -96,22 +129,22 @@ internal static void InitVersionTracking()
96129
/// <summary>
97130
/// Gets the version number for the previously run version of the legacy Xamarin app.
98131
/// </summary>
99-
public static string PreviousVersion => GetPrevious(versionsKey);
132+
public static string? PreviousVersion => GetPrevious(versionsKey);
100133

101134
/// <summary>
102135
/// Gets the build number for the previously run version of the legacy Xamarin app.
103136
/// </summary>
104-
public static string PreviousBuild => GetPrevious(buildsKey);
137+
public static string? PreviousBuild => GetPrevious(buildsKey);
105138

106139
/// <summary>
107140
/// Gets the version number of the first version of the app legacy Xamarin that was installed on this device.
108141
/// </summary>
109-
public static string FirstInstalledVersion => versionTrail[versionsKey].FirstOrDefault();
142+
public static string? FirstInstalledVersion => versionTrail[versionsKey].FirstOrDefault();
110143

111144
/// <summary>
112145
/// Gets the build number of first version of the legacy Xamarin app that was installed on this device.
113146
/// </summary>
114-
public static string FirstInstalledBuild => versionTrail[buildsKey].FirstOrDefault();
147+
public static string? FirstInstalledBuild => versionTrail[buildsKey].FirstOrDefault();
115148

116149
/// <summary>
117150
/// Gets the collection of version numbers of the legacy Xamarin app that ran on this device.
@@ -143,13 +176,19 @@ static string[] ReadHistory(string key)
143176
=> LegacyPreferences.Get(key, null, sharedName)?.Split(new[] { '|' },
144177
StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
145178

146-
static string GetPrevious(string key)
179+
static string? GetPrevious(string key)
147180
{
148181
var trail = versionTrail[key];
149182
return (trail.Count >= 2) ? trail[trail.Count - 2] : null;
150183
}
151184

152-
static string LastInstalledVersion => versionTrail[versionsKey].LastOrDefault();
185+
static string? LastInstalledVersion => versionTrail[versionsKey].LastOrDefault();
186+
187+
static string? LastInstalledBuild => versionTrail[buildsKey].LastOrDefault();
153188

154-
static string LastInstalledBuild => versionTrail[buildsKey].LastOrDefault();
189+
static void WriteVersionTrackingHistory(string key, IEnumerable<string> history)
190+
{
191+
Preferences.Default.Set(key, string.Join("|", history),
192+
PrivateVersionTrackingSharedNameMaui);
193+
}
155194
}

0 commit comments

Comments
 (0)