Skip to content

Commit

Permalink
feat: In-app rating request on iOS
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Uno.UWP/Generated/3.0.0.0/Windows.Services.Store/StoreRateAndReviewResult.cs
#	src/Uno.UWP/Generated/3.0.0.0/Windows.Services.Store/StoreRateAndReviewStatus.cs
#	src/Uno.UWP/Services/Store/StoreContext.cs
#	src/Uno.UWP/Services/Store/StoreContext.iOS.cs
  • Loading branch information
MartinZikmund committed Sep 18, 2024
1 parent e4d13bc commit 672148d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/Uno.UWP/Services/Store/StoreContext.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation;

namespace Windows.Services.Store
{
public sealed partial class StoreContext
{
private StoreContext() { }

public static StoreContext GetDefault()
private StoreContext()
{
return new StoreContext();
}

/// <summary>
/// Gets a StoreContext object that can be used to access
/// and manage store-related data for the current
/// user in the context of the current app.
/// </summary>
/// <returns>An object that you can use to access and manage
/// store-related data for the current user.</returns>
public static StoreContext GetDefault() => new StoreContext();

/// <summary>
/// Requests the user to rate and review the app. This method will
/// display the UI for the user to select a store rating and add
/// an optional store review for the product.
/// </summary>
/// <returns>Store rate and review result.</returns>
/// <remarks>
/// On iOS and macOS, it is not possible to know whether the user
/// actually rated the app or whether the system allowed the dialog
/// to be displayed.
/// </remarks>
public IAsyncOperation<StoreRateAndReviewResult> RequestRateAndReviewAppAsync() =>
AsyncOperation.FromTask(ct => RequestRateAndReviewAppTaskAsync(ct));
}
}
23 changes: 23 additions & 0 deletions src/Uno.UWP/Services/Store/StoreContext.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
using Uno.Foundation.Logging;

using Foundation;
using System.Threading.Tasks;
using System.Threading;
using Windows.System;
using Windows.UI.Core;
using CoreFoundation;
using StoreKit;

namespace Windows.Services.Store
{
Expand Down Expand Up @@ -56,5 +62,22 @@ public IAsyncOperation<StoreProductResult> GetStoreProductForCurrentAppAsync()

[GeneratedRegex("trackId[^0-9]*([0-9]*)")]
private static partial Regex TrackParser();

private async Task<StoreRateAndReviewResult> RequestRateAndReviewAppTaskAsync(CancellationToken cancellationToken)
{
try
{
await CoreDispatcher.Main.RunAsync(CoreDispatcherPriority.Normal, (cancellationToken) =>
{
SKStoreReviewController.RequestReview(UIApplication.SharedApplication.KeyWindow.WindowScene);
});

return new StoreRateAndReviewResult(StoreRateAndReviewStatus.Succeeded);
}
catch (Exception ex)
{
return new StoreRateAndReviewResult(StoreRateAndReviewStatus.Error, ex);
}
}
}
}
28 changes: 28 additions & 0 deletions src/Uno.UWP/Services/Store/StoreRateAndReviewResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#nullable enable

using System;

namespace Windows.Services.Store
{
/// <summary>
/// Provides response data for a request to rate and review the product.
/// </summary>
public partial class StoreRateAndReviewResult
{
internal StoreRateAndReviewResult(StoreRateAndReviewStatus status, Exception? extendedError = null)
{
Status = status;
ExtendedError = extendedError;
}

/// <summary>
/// Gets the status for the rate and review request for the product.
/// </summary>
public StoreRateAndReviewStatus Status { get; }

/// <summary>
/// Gets the error code for the request, if the operation encountered an error.
/// </summary>
public Exception? ExtendedError { get; }
}
}
28 changes: 28 additions & 0 deletions src/Uno.UWP/Services/Store/StoreRateAndReviewStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Windows.Services.Store
{
/// <summary>
/// Gets the result status for the rate and review request for the product.
/// </summary>
public enum StoreRateAndReviewStatus
{
/// <summary>
/// The request was successful.
/// </summary>
Succeeded,

/// <summary>
/// The request was canceled by the user.
/// </summary>
CanceledByUser,

/// <summary>
/// The request encountered a network error.
/// </summary>
NetworkError,

/// <summary>
/// The request encountered an error.
/// </summary>
Error,
}
}

0 comments on commit 672148d

Please sign in to comment.