|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using DevToys.Api.Core; |
| 4 | +using DevToys.Core.Threading; |
| 5 | +using DevToys.Models; |
| 6 | +using Newtonsoft.Json; |
| 7 | +using System; |
| 8 | +using System.Composition; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Windows.Services.Store; |
| 12 | +using Windows.Storage; |
| 13 | + |
| 14 | +namespace DevToys.Core |
| 15 | +{ |
| 16 | + [Export(typeof(IMarketingService))] |
| 17 | + [Shared] |
| 18 | + internal sealed class MarketingService : IMarketingService, IDisposable |
| 19 | + { |
| 20 | + private const string StoredFileName = "marketingInfo.json"; |
| 21 | + |
| 22 | + private readonly INotificationService _notificationService; |
| 23 | + private readonly DisposableSempahore _semaphore = new DisposableSempahore(); |
| 24 | + private readonly object _lock = new object(); |
| 25 | + |
| 26 | + private bool _rateOfferInProgress; |
| 27 | + private AsyncLazy<MarketingState> _marketingState; |
| 28 | + |
| 29 | + [ImportingConstructor] |
| 30 | + public MarketingService(INotificationService notificationService) |
| 31 | + { |
| 32 | + _notificationService = notificationService; |
| 33 | + _marketingState = new AsyncLazy<MarketingState>(LoadStateAsync); |
| 34 | + } |
| 35 | + |
| 36 | + public void Dispose() |
| 37 | + { |
| 38 | + _semaphore.Dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + public async Task NotifyAppEncounteredAProblemAsync() |
| 42 | + { |
| 43 | + await UpdateMarketingStateAsync(state => |
| 44 | + { |
| 45 | + state.LastProblemEncounteredDate = DateTime.Now; |
| 46 | + state.StartSinceLastProblemEncounteredCount = 0; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + public void NotifyAppJustUpdated() |
| 51 | + { |
| 52 | + UpdateMarketingStateAsync(state => |
| 53 | + { |
| 54 | + state.LastUpdateDate = DateTime.Now; |
| 55 | + }).ForgetSafely(); |
| 56 | + } |
| 57 | + |
| 58 | + public void NotifyAppStarted() |
| 59 | + { |
| 60 | + UpdateMarketingStateAsync(state => |
| 61 | + { |
| 62 | + state.StartSinceLastProblemEncounteredCount++; |
| 63 | + }).ForgetSafely(); |
| 64 | + } |
| 65 | + |
| 66 | + public void NotifySmartDetectionWorked() |
| 67 | + { |
| 68 | + UpdateMarketingStateAsync(state => |
| 69 | + { |
| 70 | + state.SmartDetectionCount++; |
| 71 | + }).ContinueWith(_ => |
| 72 | + { |
| 73 | + TryOfferUserToRateApp(); |
| 74 | + }).ForgetSafely(); |
| 75 | + } |
| 76 | + |
| 77 | + public void NotifyToolSuccessfullyWorked() |
| 78 | + { |
| 79 | + UpdateMarketingStateAsync(state => |
| 80 | + { |
| 81 | + state.ToolSuccessfulyWorkedCount++; |
| 82 | + }).ContinueWith(_ => |
| 83 | + { |
| 84 | + TryOfferUserToRateApp(); |
| 85 | + }).ForgetSafely(); |
| 86 | + } |
| 87 | + |
| 88 | + private void TryOfferUserToRateApp() |
| 89 | + { |
| 90 | + lock (_lock) |
| 91 | + { |
| 92 | + if (_rateOfferInProgress) |
| 93 | + { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + if (_marketingState.IsValueCreated |
| 98 | + && DetermineWhetherAppRatingShouldBeOffered(_marketingState.GetValueAsync().Result)) |
| 99 | + { |
| 100 | + _rateOfferInProgress = true; |
| 101 | + |
| 102 | + _notificationService.ShowInAppNotification( |
| 103 | + LanguageManager.Instance.MainPage.NotificationRateAppTitle, |
| 104 | + LanguageManager.Instance.MainPage.NotificationRateAppActionableActionText, |
| 105 | + () => |
| 106 | + { |
| 107 | + RateAsync().ForgetSafely(); |
| 108 | + }, |
| 109 | + LanguageManager.Instance.MainPage.NotificationRateAppMessage); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private async Task RateAsync() |
| 115 | + { |
| 116 | + await UpdateMarketingStateAsync(state => |
| 117 | + { |
| 118 | + state.AppRatingOfferCount++; |
| 119 | + state.LastAppRatingOfferDate = DateTime.Now; |
| 120 | + }); |
| 121 | + |
| 122 | + StoreContext storeContext = StoreContext.GetDefault(); |
| 123 | + |
| 124 | + StoreRateAndReviewResult result = await ThreadHelper.RunOnUIThreadAsync(async () => |
| 125 | + { |
| 126 | + return await storeContext.RequestRateAndReviewAppAsync(); |
| 127 | + }).ConfigureAwait(false); |
| 128 | + |
| 129 | + if (result.Status == StoreRateAndReviewStatus.Succeeded) |
| 130 | + { |
| 131 | + await UpdateMarketingStateAsync(state => |
| 132 | + { |
| 133 | + state.AppGotRated = true; |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + lock (_lock) |
| 138 | + { |
| 139 | + _rateOfferInProgress = false; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private bool DetermineWhetherAppRatingShouldBeOffered(MarketingState state) |
| 144 | + { |
| 145 | + // The user already rated the app. Let's not offer him to rate it again. |
| 146 | + if (state.AppGotRated) |
| 147 | + { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + // We already offered the user to rate the app many times. |
| 152 | + // It's very unlikely that he will rate it at this point. Let's stop asking. |
| 153 | + if (state.AppRatingOfferCount >= 10) |
| 154 | + { |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + // If it's been less than 8 days since the last time the app crashed or that the app |
| 159 | + // has been installed on the machine. Let's not ask the user to rate the app. |
| 160 | + if (DateTime.Now - state.LastProblemEncounteredDate < TimeSpan.FromDays(8)) |
| 161 | + { |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + // If the app have been started less than 4 times since the last crash or since the app |
| 166 | + // got installed on the machine, let's not ask the user to rate the app. |
| 167 | + if (state.StartSinceLastProblemEncounteredCount < 4) |
| 168 | + { |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + // The app got updated 2 days ago. Potentially, we introduced some instability (not necessarily crash, |
| 173 | + // but maybe visual issues, inconsistencies...etc). |
| 174 | + // Let's make sure we don't offer the user to rate the app as soon as it got updated, just in case |
| 175 | + // if the app is completely broken. |
| 176 | + if (DateTime.Now - state.LastUpdateDate < TimeSpan.FromDays(2)) |
| 177 | + { |
| 178 | + return false; |
| 179 | + } |
| 180 | + |
| 181 | + // Let's make sure we don't offer to rate the app more than once within 2 days. |
| 182 | + if (state.AppRatingOfferCount > 0 && DateTime.Now - state.LastAppRatingOfferDate < TimeSpan.FromDays(2)) |
| 183 | + { |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + // If we already offered to rate the app more than 2 times, let's make sure we |
| 188 | + // don't offer it again before the next 5 days. |
| 189 | + if (state.AppRatingOfferCount > 2 && DateTime.Now - state.LastAppRatingOfferDate < TimeSpan.FromDays(5)) |
| 190 | + { |
| 191 | + return false; |
| 192 | + } |
| 193 | + |
| 194 | + // If we already offered to rate the app more than 5 times, let's make sure we |
| 195 | + // don't offer it again before the next 10 days. |
| 196 | + if (state.AppRatingOfferCount > 5 && DateTime.Now - state.LastAppRatingOfferDate < TimeSpan.FromDays(10)) |
| 197 | + { |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + // If we already offered to rate the app more than 7 times, let's make sure we |
| 202 | + // don't offer it again before the next 60 days. |
| 203 | + if (state.AppRatingOfferCount > 7 && DateTime.Now - state.LastAppRatingOfferDate < TimeSpan.FromDays(60)) |
| 204 | + { |
| 205 | + return false; |
| 206 | + } |
| 207 | + |
| 208 | + // Smart Detection has been used at least twice. Let's offer the use to rate the app. |
| 209 | + if (state.SmartDetectionCount > 3) |
| 210 | + { |
| 211 | + return true; |
| 212 | + } |
| 213 | + |
| 214 | + // The user used tools at least 10 times already. Let's offer the use to rate the app. |
| 215 | + if (state.ToolSuccessfulyWorkedCount > 10) |
| 216 | + { |
| 217 | + return true; |
| 218 | + } |
| 219 | + |
| 220 | + return false; |
| 221 | + } |
| 222 | + |
| 223 | + private async Task UpdateMarketingStateAsync(Action<MarketingState> updateAction) |
| 224 | + { |
| 225 | + await TaskScheduler.Default; |
| 226 | + |
| 227 | + MarketingState state = await _marketingState.GetValueAsync(); |
| 228 | + |
| 229 | + using (await _semaphore.WaitAsync(CancellationToken.None)) |
| 230 | + { |
| 231 | + updateAction(state); |
| 232 | + } |
| 233 | + |
| 234 | + await SaveStateAsync(state); |
| 235 | + } |
| 236 | + |
| 237 | + private async Task SaveStateAsync(MarketingState state) |
| 238 | + { |
| 239 | + await TaskScheduler.Default; |
| 240 | + |
| 241 | + try |
| 242 | + { |
| 243 | + using (await _semaphore.WaitAsync(CancellationToken.None)) |
| 244 | + { |
| 245 | + StorageFolder localCacheFolder = ApplicationData.Current.LocalCacheFolder; |
| 246 | + |
| 247 | + StorageFile file = await localCacheFolder.CreateFileAsync(StoredFileName, CreationCollisionOption.ReplaceExisting); |
| 248 | + |
| 249 | + string fileContent |
| 250 | + = JsonConvert.SerializeObject( |
| 251 | + state, |
| 252 | + Formatting.Indented); |
| 253 | + |
| 254 | + await FileIO.WriteTextAsync(file, fileContent); |
| 255 | + } |
| 256 | + } |
| 257 | + catch (Exception) |
| 258 | + { |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + private async Task<MarketingState> LoadStateAsync() |
| 263 | + { |
| 264 | + await TaskScheduler.Default; |
| 265 | + |
| 266 | + try |
| 267 | + { |
| 268 | + using (await _semaphore.WaitAsync(CancellationToken.None)) |
| 269 | + { |
| 270 | + StorageFolder localCacheFolder = ApplicationData.Current.LocalCacheFolder; |
| 271 | + |
| 272 | + IStorageItem? file = await localCacheFolder.TryGetItemAsync(StoredFileName); |
| 273 | + |
| 274 | + if (file is not null && file is StorageFile storageFile) |
| 275 | + { |
| 276 | + string fileContent = await FileIO.ReadTextAsync(storageFile); |
| 277 | + var result = JsonConvert.DeserializeObject<MarketingState>(fileContent); |
| 278 | + if (result is not null) |
| 279 | + { |
| 280 | + return result; |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | + } |
| 285 | + catch (Exception) |
| 286 | + { |
| 287 | + } |
| 288 | + |
| 289 | + return new MarketingState |
| 290 | + { |
| 291 | + LastAppRatingOfferDate = DateTime.Now, |
| 292 | + LastProblemEncounteredDate = DateTime.Now, |
| 293 | + LastUpdateDate = DateTime.Now |
| 294 | + }; |
| 295 | + } |
| 296 | + } |
| 297 | +} |
0 commit comments