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

Commit 64f6130

Browse files
mattleibowjamesmontemagno
authored andcommitted
[Proposal] Add an API to Track App Versions (#124)
* Add an API to track app versions * Using the new Preferences model * Made a few changes and updated the sample * Added docs
1 parent 267edd1 commit 64f6130

File tree

5 files changed

+378
-0
lines changed

5 files changed

+378
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The following cross-platform APIs are planned for our first release:
4545
- [x] Secure Storage
4646
- [x] SMS
4747
- [ ] Text-to-Speech
48+
- [x] Version Tracking
4849
- [x] Vibrate
4950

5051
## Contributing

Samples/Caboodle.Samples/ViewModel/HomeViewModel.cs

+24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using System.Collections.ObjectModel;
2+
using System.Linq;
23
using Caboodle.Samples.Model;
34
using Caboodle.Samples.View;
5+
using Microsoft.Caboodle;
46

57
namespace Caboodle.Samples.ViewModel
68
{
79
public class HomeViewModel : BaseViewModel
810
{
11+
private bool alreadyAppeared;
12+
913
public HomeViewModel()
1014
{
1115
Items = new ObservableCollection<SampleItem>
@@ -35,5 +39,25 @@ public HomeViewModel()
3539
}
3640

3741
public ObservableCollection<SampleItem> Items { get; }
42+
43+
public override void OnAppearing()
44+
{
45+
base.OnAppearing();
46+
47+
if (!alreadyAppeared)
48+
{
49+
alreadyAppeared = true;
50+
51+
if (VersionTracking.IsFirstLaunchEver)
52+
{
53+
DisplayAlert("Welcome to the Caboodle samples!");
54+
}
55+
else if (VersionTracking.IsFirstLaunchForCurrentVersion)
56+
{
57+
var count = VersionTracking.VersionHistory.Count();
58+
DisplayAlert($"Welcome to the NEW Caboodle samples! You have tried {count} versions.");
59+
}
60+
}
61+
}
3862
}
3963
}

0 commit comments

Comments
 (0)