|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Microsoft.Caboodle |
| 7 | +{ |
| 8 | + public static class VersionTracking |
| 9 | + { |
| 10 | + const string versionTrailKey = "VersionTracking.Trail"; |
| 11 | + const string versionsKey = "VersionTracking.Versions"; |
| 12 | + const string buildsKey = "VersionTracking.Builds"; |
| 13 | + |
| 14 | + static readonly string sharedName = $"{AppInfo.PackageName}.caboodle"; |
| 15 | + |
| 16 | + static Dictionary<string, List<string>> versionTrail; |
| 17 | + |
| 18 | + static VersionTracking() |
| 19 | + { |
| 20 | + IsFirstLaunchEver = !Preferences.ContainsKey(versionsKey, sharedName) || !Preferences.ContainsKey(buildsKey, sharedName); |
| 21 | + if (IsFirstLaunchEver) |
| 22 | + { |
| 23 | + versionTrail = new Dictionary<string, List<string>> |
| 24 | + { |
| 25 | + { versionsKey, new List<string>() }, |
| 26 | + { buildsKey, new List<string>() } |
| 27 | + }; |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + versionTrail = new Dictionary<string, List<string>> |
| 32 | + { |
| 33 | + { versionsKey, ReadHistory(versionsKey).ToList() }, |
| 34 | + { buildsKey, ReadHistory(buildsKey).ToList() } |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + IsFirstLaunchForCurrentVersion = !versionTrail[versionsKey].Contains(CurrentVersion); |
| 39 | + if (IsFirstLaunchForCurrentVersion) |
| 40 | + { |
| 41 | + versionTrail[versionsKey].Add(CurrentVersion); |
| 42 | + } |
| 43 | + |
| 44 | + IsFirstLaunchForCurrentBuild = !versionTrail[buildsKey].Contains(CurrentBuild); |
| 45 | + if (IsFirstLaunchForCurrentBuild) |
| 46 | + { |
| 47 | + versionTrail[buildsKey].Add(CurrentBuild); |
| 48 | + } |
| 49 | + |
| 50 | + if (IsFirstLaunchForCurrentVersion || IsFirstLaunchForCurrentBuild) |
| 51 | + { |
| 52 | + WriteHistory(versionsKey, versionTrail[versionsKey]); |
| 53 | + WriteHistory(buildsKey, versionTrail[buildsKey]); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static bool IsFirstLaunchEver { get; private set; } |
| 58 | + |
| 59 | + public static bool IsFirstLaunchForCurrentVersion { get; private set; } |
| 60 | + |
| 61 | + public static bool IsFirstLaunchForCurrentBuild { get; private set; } |
| 62 | + |
| 63 | + public static string CurrentVersion => AppInfo.VersionString; |
| 64 | + |
| 65 | + public static string CurrentBuild => AppInfo.BuildString; |
| 66 | + |
| 67 | + public static string PreviousVersion => GetPrevious(versionsKey); |
| 68 | + |
| 69 | + public static string PreviousBuild => GetPrevious(buildsKey); |
| 70 | + |
| 71 | + public static string FirstInstalledVersion => versionTrail[versionsKey].FirstOrDefault(); |
| 72 | + |
| 73 | + public static string FirstInstalledBuild => versionTrail[buildsKey].FirstOrDefault(); |
| 74 | + |
| 75 | + public static IEnumerable<string> VersionHistory => versionTrail[versionsKey].ToArray(); |
| 76 | + |
| 77 | + public static IEnumerable<string> BuildHistory => versionTrail[buildsKey].ToArray(); |
| 78 | + |
| 79 | + public static bool IsFirstLaunchForVersion(string version) |
| 80 | + => CurrentVersion == version && IsFirstLaunchForCurrentVersion; |
| 81 | + |
| 82 | + public static bool IsFirstLaunchForBuild(string build) |
| 83 | + => CurrentBuild == build && IsFirstLaunchForCurrentBuild; |
| 84 | + |
| 85 | + internal static string GetStatus() |
| 86 | + { |
| 87 | + var sb = new StringBuilder(); |
| 88 | + sb.AppendLine(); |
| 89 | + sb.AppendLine("VersionTracking"); |
| 90 | + sb.AppendLine($" IsFirstLaunchEver: {IsFirstLaunchEver}"); |
| 91 | + sb.AppendLine($" IsFirstLaunchForCurrentVersion: {IsFirstLaunchForCurrentVersion}"); |
| 92 | + sb.AppendLine($" IsFirstLaunchForCurrentBuild: {IsFirstLaunchForCurrentBuild}"); |
| 93 | + sb.AppendLine(); |
| 94 | + sb.AppendLine($" CurrentVersion: {CurrentVersion}"); |
| 95 | + sb.AppendLine($" PreviousVersion: {PreviousVersion}"); |
| 96 | + sb.AppendLine($" FirstInstalledVersion: {FirstInstalledVersion}"); |
| 97 | + sb.AppendLine($" VersionHistory: [{string.Join(", ", VersionHistory)}]"); |
| 98 | + sb.AppendLine(); |
| 99 | + sb.AppendLine($" CurrentBuild: {CurrentBuild}"); |
| 100 | + sb.AppendLine($" PreviousBuild: {PreviousBuild}"); |
| 101 | + sb.AppendLine($" FirstInstalledBuild: {FirstInstalledBuild}"); |
| 102 | + sb.AppendLine($" BuildHistory: [{string.Join(", ", BuildHistory)}]"); |
| 103 | + return sb.ToString(); |
| 104 | + } |
| 105 | + |
| 106 | + static string[] ReadHistory(string key) |
| 107 | + => Preferences.Get(key, null, sharedName)?.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0]; |
| 108 | + |
| 109 | + static void WriteHistory(string key, IEnumerable<string> history) |
| 110 | + => Preferences.Set(key, string.Join("|", history), sharedName); |
| 111 | + |
| 112 | + static string GetPrevious(string key) |
| 113 | + { |
| 114 | + var trail = versionTrail[key]; |
| 115 | + return (trail.Count >= 2) ? trail[trail.Count - 2] : null; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments