|
| 1 | +using System; |
| 2 | +using TestExtension.ToolWindows; |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Globalization; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Community.VisualStudio.Toolkit; |
| 8 | +using System.Windows.Controls; |
| 9 | +using Microsoft.VisualStudio.Imaging; |
| 10 | +using Microsoft.VisualStudio.PlatformUI; |
| 11 | +using Microsoft.VisualStudio.Shell; |
| 12 | +using Task = System.Threading.Tasks.Task; |
| 13 | + |
| 14 | +namespace TestExtension.ToolWindows |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A standardized way to prompt the user to rate and review the extension |
| 18 | + /// </summary> |
| 19 | + public class RatingPrompt |
| 20 | + { |
| 21 | + private const string _urlFormat = "https://marketplace.visualstudio.com/items?itemName={0}&ssr=false#review-details"; |
| 22 | + private const int _minutesVisible = 2; |
| 23 | + private static readonly ConcurrentDictionary<string, bool> _hasAlreadyRequested = new(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Creates a new instance of the rating prompt. |
| 27 | + /// </summary> |
| 28 | + /// <param name="marketplaceId">The unique Marketplace id found at the end of the Marketplace URL. For instance: "MyName.MyExtensions".</param> |
| 29 | + /// <param name="extensionName">The name of the extension to show in the prompt.</param> |
| 30 | + /// <param name="config">Likely a options page that implements the <see cref="IRatingConfig"/> interface. This is used to keep track of how many times the prompt was requested and if the user has already rated.</param> |
| 31 | + /// <param name="requestsBeforePrompt">Indicates how many successful requests it takes before the user is prompted to rate.</param> |
| 32 | + /// <exception cref="ArgumentNullException">None of the parameters passed in can be null.</exception> |
| 33 | + /// <exception cref="ArgumentException">The Marketplace ID has to be valid so an absolute URI can be constructed.</exception> |
| 34 | + public RatingPrompt(string marketplaceId, string extensionName, IRatingConfig config = null, int requestsBeforePrompt = 5) |
| 35 | + { |
| 36 | + MarketplaceId = marketplaceId ?? throw new ArgumentNullException(nameof(marketplaceId)); |
| 37 | + ExtensionName = extensionName ?? throw new ArgumentNullException(nameof(extensionName)); |
| 38 | + Config = config; |
| 39 | + RequestsBeforePrompt = requestsBeforePrompt; |
| 40 | + |
| 41 | + string ratingUrl = string.Format(CultureInfo.InvariantCulture, _urlFormat, MarketplaceId); |
| 42 | + |
| 43 | + if (!Uri.TryCreate(ratingUrl, UriKind.Absolute, out Uri parsedUrl)) |
| 44 | + { |
| 45 | + throw new ArgumentException($"{RatingUrl} is not a valid URL", nameof(marketplaceId)); |
| 46 | + } |
| 47 | + |
| 48 | + RatingUrl = parsedUrl; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// The Marketplace ID is the unique last part of the URL. For instance: "MyName.MyExtension". |
| 53 | + /// </summary> |
| 54 | + public virtual string MarketplaceId { get; } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// The name of the extension. It's shown in the prompt so the user knows which extension to rate. |
| 58 | + /// </summary> |
| 59 | + public virtual string ExtensionName { get; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// The configuration/options object used to store the information related to the rating prompt. |
| 63 | + /// </summary> |
| 64 | + public virtual IRatingConfig? Config { get; } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// The Marketplace URL the users are taken to when prompted. |
| 68 | + /// </summary> |
| 69 | + public virtual Uri RatingUrl { get; } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Indicates how many successful requests it takes before the user is prompted to rate. |
| 73 | + /// </summary> |
| 74 | + public virtual int RequestsBeforePrompt { get; set; } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Registers successful usage of the extension. Only one is registered per Visual Studio session. |
| 78 | + /// When the number of usages matches <see cref="RequestsBeforePrompt"/>, the prompt will be shown. |
| 79 | + /// </summary> |
| 80 | + public virtual void RegisterSuccessfulUsage() |
| 81 | + { |
| 82 | + if (Config == null) |
| 83 | + { |
| 84 | + throw new NullReferenceException("The Config property has not been set."); |
| 85 | + } |
| 86 | + |
| 87 | + if (_hasAlreadyRequested.TryAdd(MarketplaceId, true) && Config.RatingRequests < RequestsBeforePrompt) |
| 88 | + { |
| 89 | + IncrementAsync().FireAndForget(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Resets the count of successful usages and starts over. |
| 95 | + /// </summary> |
| 96 | + public virtual async Task ResetAsync() |
| 97 | + { |
| 98 | + _hasAlreadyRequested.TryRemove(MarketplaceId, out _); |
| 99 | + |
| 100 | + if (Config != null) |
| 101 | + { |
| 102 | + Config.RatingRequests = 0; |
| 103 | + await Config.SaveAsync(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private async Task IncrementAsync() |
| 108 | + { |
| 109 | + await Task.Yield(); // Yield to allow any shutdown procedure to continue |
| 110 | + |
| 111 | + if (VsShellUtilities.ShellIsShuttingDown || Config == null) |
| 112 | + { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + Config.RatingRequests += 1; |
| 117 | + await Config.SaveAsync(); |
| 118 | + |
| 119 | + if (Config.RatingRequests == RequestsBeforePrompt) |
| 120 | + { |
| 121 | + PromptAsync().FireAndForget(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Prompts the user to rate the extension. |
| 127 | + /// </summary> |
| 128 | + public async Task PromptAsync() |
| 129 | + { |
| 130 | + var window = new RatingPromptWindow(this); |
| 131 | + var result = window.ShowDialog(); |
| 132 | + |
| 133 | + if (window.Choice == RateChoice.RateNow) |
| 134 | + { |
| 135 | + Process.Start(RatingUrl.OriginalString); |
| 136 | + } |
| 137 | + else if (window.Choice == RateChoice.RateLater) |
| 138 | + { |
| 139 | + ResetAsync().FireAndForget(); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments