Files
Wino-Mail/Wino.Core.UWP/Services/StoreRatingService.cs

133 lines
4.6 KiB
C#
Raw Permalink Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Services.Store;
using Windows.System;
using Wino.Core.Domain;
using Wino.Core.Domain.Interfaces;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.UWP.Services;
public class StoreRatingService : IStoreRatingService
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
private const string RatedStorageKey = nameof(RatedStorageKey);
private const string LatestAskedKey = nameof(LatestAskedKey);
private readonly IConfigurationService _configurationService;
private readonly IMailDialogService _dialogService;
public StoreRatingService(IConfigurationService configurationService, IMailDialogService dialogService)
{
_configurationService = configurationService;
_dialogService = dialogService;
}
private bool IsAskingThresholdExceeded()
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var latestAskedDate = _configurationService.Get(LatestAskedKey, DateTime.MinValue);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Never asked before.
// Set the threshold and wait for the next trigger.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (latestAskedDate == DateTime.MinValue)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
_configurationService.Set(LatestAskedKey, DateTime.UtcNow);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
else if (DateTime.UtcNow >= latestAskedDate.AddMinutes(30))
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
return true;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return false;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task PromptRatingDialogAsync()
{
// Annoying.
if (Debugger.IsAttached) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Swallow all exceptions. App should not crash in any errors.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
try
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
bool isRated = _configurationService.GetRoaming(RatedStorageKey, false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (isRated) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (!isRated)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
if (!IsAskingThresholdExceeded()) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var isRateWinoApproved = await _dialogService.ShowWinoCustomMessageDialogAsync(Translator.StoreRatingDialog_Title,
Translator.StoreRatingDialog_MessageFirstLine,
Translator.Buttons_RateWino,
Domain.Enums.WinoCustomMessageDialogIcon.Question,
Translator.Buttons_No,
RatedStorageKey);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (isRateWinoApproved)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// In case of failure of this call, we will navigate users to Store page directly.
2025-02-16 11:54:23 +01:00
try
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
await ShowPortableRatingDialogAsync();
}
catch (Exception)
{
await Launcher.LaunchUriAsync(new Uri($"ms-windows-store://review/?ProductId=9NCRCVJC50WL"));
2024-04-18 01:44:37 +02:00
}
}
}
}
2025-02-16 11:54:23 +01:00
catch (Exception) { }
finally
{
2025-02-16 11:54:23 +01:00
_configurationService.Set(LatestAskedKey, DateTime.UtcNow);
}
}
private async Task ShowPortableRatingDialogAsync()
{
var _storeContext = StoreContext.GetDefault();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
StoreRateAndReviewResult result = await _storeContext.RequestRateAndReviewAppAsync();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Check status
switch (result.Status)
{
case StoreRateAndReviewStatus.Succeeded:
if (result.WasUpdated)
_dialogService.InfoBarMessage(Translator.Info_ReviewSuccessTitle, Translator.Info_ReviewUpdatedMessage, Domain.Enums.InfoBarMessageType.Success);
else
_dialogService.InfoBarMessage(Translator.Info_ReviewSuccessTitle, Translator.Info_ReviewNewMessage, Domain.Enums.InfoBarMessageType.Success);
_configurationService.Set(RatedStorageKey, true);
break;
case StoreRateAndReviewStatus.CanceledByUser:
break;
case StoreRateAndReviewStatus.NetworkError:
_dialogService.InfoBarMessage(Translator.Info_ReviewNetworkErrorTitle, Translator.Info_ReviewNetworkErrorMessage, Domain.Enums.InfoBarMessageType.Warning);
break;
default:
_dialogService.InfoBarMessage(Translator.Info_ReviewUnknownErrorTitle, string.Format(Translator.Info_ReviewUnknownErrorMessage, result.ExtendedError.Message), Domain.Enums.InfoBarMessageType.Warning);
break;
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task LaunchStorePageForReviewAsync()
{
try
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
await CoreApplication.GetCurrentView()?.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// TODO: Get it from package info.
await Launcher.LaunchUriAsync(new Uri($"ms-windows-store://review/?ProductId=9NCRCVJC50WL"));
});
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
catch (Exception) { }
2024-04-18 01:44:37 +02:00
}
}