Skip to content

Commit

Permalink
Merge pull request #50 from jamesmontemagno/windows-updates
Browse files Browse the repository at this point in the history
Introduce ReviewStatus enum and update RequestReview method
  • Loading branch information
jamesmontemagno authored Mar 9, 2024
2 parents d1035ac + 6d7b3a2 commit ae458a6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 198 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Store Review Plugin for Xamarin
## Store Review Plugin for .NET MAUI, Windows, & Xamarin

**Platform Support**

Expand Down Expand Up @@ -57,9 +57,20 @@ Read for Android: [In-app reviews for your Android apps](https://devblogs.micros
/// <summary>
/// Requests the review.
/// </summary>
void RequestReview();
Task<ReviewStatus> RequestReview(bool testMode)
```

If you are on .NET 6 Windows you will need to set the Window handle before calling the method:

```csharp
#if WINDOWS
var windowObject = (MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView;
StoreReviewImplementation.Window = windowObject;
#endif
```

Test mode is only used on Android.

### Android setup

Ensure that you follow the [Xamarin.Essentials setup steps](https://docs.microsoft.com/xamarin/essentials/get-started?WT.mc_id=friends-0000-jamont). And follow the steps below if you linker behavior is not set to `Don't Link`.
Expand Down
8 changes: 8 additions & 0 deletions src/StoreReview.Plugin/CrossStoreReview.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

namespace Plugin.StoreReview
{
public enum ReviewStatus
{
Succeeded,
Error,
CanceledByUser,
NetworkError,
Unknown
}
/// <summary>
/// Cross platform StoreReview implemenations
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/StoreReview.Plugin/IStoreReview.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public interface IStoreReview
/// <summary>
/// Requests an app review.
/// </summary>
Task RequestReview(bool testMode);
Task<ReviewStatus> RequestReview(bool testMode);
}
}
6 changes: 4 additions & 2 deletions src/StoreReview.Plugin/StoreReviewImplementation.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void OpenStoreReviewPage(string appId)
/// <summary>
/// Requests an app review.
/// </summary>
public async Task RequestReview(bool testMode)
public async Task<ReviewStatus> RequestReview(bool testMode)
{
tcs?.TrySetCanceled();
tcs = new TaskCompletionSource<bool>();
Expand All @@ -99,9 +99,11 @@ public async Task RequestReview(bool testMode)
forceReturn = false;
var request = manager.RequestReviewFlow();
request.AddOnCompleteListener(this);
await tcs.Task;
var status = await tcs.Task;
manager.Dispose();
request.Dispose();

return status ? ReviewStatus.Succeeded : ReviewStatus.Error;
}

Activity Activity =>
Expand Down
8 changes: 4 additions & 4 deletions src/StoreReview.Plugin/StoreReviewImplementation.apple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void OpenStoreReviewPage(string appId)
/// <summary>
/// Requests an app review.
/// </summary>
public Task RequestReview(bool testMode)
public Task<ReviewStatus> RequestReview(bool testMode)
{
#if __IOS__
if (IsiOS103)
Expand All @@ -86,7 +86,7 @@ public Task RequestReview(bool testMode)
if (windowScene != null)
{
SKStoreReviewController.RequestReview(windowScene);
return Task.CompletedTask;
return Task.FromResult(ReviewStatus.Unknown);
}
}
SKStoreReviewController.RequestReview();
Expand All @@ -99,8 +99,8 @@ public Task RequestReview(bool testMode)
}
#endif

return Task.CompletedTask;
}
return Task.FromResult(ReviewStatus.Unknown);
}

internal static Version ParseVersion(string version)
{
Expand Down
51 changes: 47 additions & 4 deletions src/StoreReview.Plugin/StoreReviewImplementation.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Services.Store;
using Windows.ApplicationModel.Store;
using System.Collections.Generic;
using Windows.System;

namespace Plugin.StoreReview
{
Expand All @@ -27,10 +30,50 @@ public void OpenStoreListing(string appId) =>
public void OpenStoreReviewPage(string appId) =>
OpenUrl($"ms-windows-store://review/?ProductId={appId}");

/// <summary>
/// Requests an app review.
/// </summary>
public async Task RequestReview(bool testMode) => _ = await StoreRequestHelper.SendRequestAsync(StoreContext.GetDefault(), 16, string.Empty).AsTask();
#if NET6_0_OR_GREATER
public static object Window { get; set; }
#endif
/// <summary>
/// Requests an app review.
/// </summary>
public async Task<ReviewStatus> RequestReview(bool testMode)
{
try
{

var context = StoreContext.GetDefault();

#if NET6_0_OR_GREATER
if(Window is null)
throw new NullReferenceException("WindowObject is null. Please set the WindowObject property before calling RequestReview.");

var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(Window);

WinRT.Interop.InitializeWithWindow.Initialize(context, hwnd);
#endif

var result = await context.RequestRateAndReviewAppAsync();
return result.Status switch
{
StoreRateAndReviewStatus.Succeeded => ReviewStatus.Succeeded,
StoreRateAndReviewStatus.CanceledByUser => ReviewStatus.CanceledByUser,
StoreRateAndReviewStatus.Error => ReviewStatus.Error,
StoreRateAndReviewStatus.NetworkError => ReviewStatus.NetworkError,
_ => ReviewStatus.Error,
};
}
catch(Exception ex)
{
Debug.WriteLine(ex);
return ReviewStatus.Error;
}
finally
{
#if NET6_0_OR_GREATER
Window = null;
#endif
}
}

void OpenUrl(string url)
{
Expand Down
Loading

0 comments on commit ae458a6

Please sign in to comment.