Skip to content

Instantly share code, notes, and snippets.

@colinkiama
Last active December 21, 2019 02:03
Show Gist options
  • Save colinkiama/e56bb527191dfc89a9bb00ffb76e3f8b to your computer and use it in GitHub Desktop.
Save colinkiama/e56bb527191dfc89a9bb00ffb76e3f8b to your computer and use it in GitHub Desktop.
Review and (email) feedback helper class for UWP apps. Everything you need to ask for review and feedback prompts.
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml.Controls;
namespace PomoLibrary.Helpers
{
public static class ReviewHelper
{
public const string emailValue = "colinkiama@gmail.com";
public const string StoreID = "9PJSR2QK1V1V";
public static readonly string ReviewString = $"ms-windows-store://review/?ProductId={StoreID}";
public static readonly string FeedbackString;
const string launchCountSettingsValue = "launchCount";
const string noMorePromptsSettingsValue = "noMorePrompts";
static string appDisplayName = Package.Current.DisplayName;
static ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
static ReviewHelper()
{
TryUpdateLaunchCount();
FeedbackString = $"mailto:{emailValue}?subject={appDisplayName}%20Feedback&body=<Write%20your%20feedback%20here>";
}
private static void TryUpdateLaunchCount()
{
if (localSettings.Values[noMorePromptsSettingsValue] == null)
{
localSettings.Values[noMorePromptsSettingsValue] = false;
localSettings.Values[launchCountSettingsValue] = (byte)1;
}
else if ((bool)localSettings.Values[noMorePromptsSettingsValue] == false)
{
{
byte oldValue = (byte)localSettings.Values[launchCountSettingsValue];
localSettings.Values[launchCountSettingsValue] = (byte)(oldValue + 1);
}
}
}
// Recommended: Run this when you navigate app's "Home Page" or "Shell".
public static async Task TryRequestReviewAsync()
{
if (CheckIfTimeForReview())
{
var reviewDialog = new ContentDialog()
{
Title = "Enjoying the app?",
Content = "It would be lovely if you left a review! 😊",
PrimaryButtonText = "Review",
CloseButtonText = "Later"
};
reviewDialog.Closed += ReviewDialog_Closed;
await reviewDialog.ShowAsync();
}
}
private static async void ReviewDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
{
if (args.Result == ContentDialogResult.Primary)
{
RemovePromptsForever();
await Launcher.LaunchUriAsync(new Uri(ReviewString));
}
else
{
await ShowFeedbackDialog();
}
}
private static void RemovePromptsForever()
{
localSettings.Values[noMorePromptsSettingsValue] = true;
}
private async static Task ShowFeedbackDialog()
{
try
{
var feedbackDialog = new ContentDialog()
{
Title = "Feedback",
Content = "Your feedback is valuable. It improves your experience!",
PrimaryButtonText = "Give Feedback",
CloseButtonText = "Later",
};
feedbackDialog.Closed += FeedbackDialog_Closed;
await feedbackDialog.ShowAsync();
}
catch (Exception)
{
}
}
private async static void FeedbackDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
{
if (args.Result == ContentDialogResult.Primary)
{
await Launcher.LaunchUriAsync(new Uri(FeedbackString));
}
}
private static bool CheckIfTimeForReview()
{
bool isTimeToReview = false;
if ((bool)localSettings.Values[noMorePromptsSettingsValue] == false)
{
if ((byte)localSettings.Values[launchCountSettingsValue] == (byte)2)
{
isTimeToReview = true;
localSettings.Values[launchCountSettingsValue] = (byte)0;
}
}
return isTimeToReview;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment