Skip to content

Commit

Permalink
Added Windows implementation for the plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriBertani committed May 6, 2023
1 parent 1a0740b commit 3485ce6
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 10 deletions.
7 changes: 4 additions & 3 deletions Plugin.Maui.AppRating/IAppRating.cs
Expand Up @@ -10,7 +10,8 @@ public interface IAppRating
/// <summary>
/// Perform rating on the current OS store app or open the store page on browser.
/// </summary>
/// <param name="packageName">Use this for Android.</param>
/// <param name="applicationId">Use this for iOS.</param>
Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "");
/// <param name="packageName">Use <b>packageName</b> for Android.</param>
/// <param name="applicationId">Use <b>applicationId</b> for iOS.</param>
/// <param name="productId">Use <b>productId</b> for Windows</param>
Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "", string productId = "");
}
7 changes: 4 additions & 3 deletions Plugin.Maui.AppRating/Platforms/Android/AppRating.android.cs
Expand Up @@ -47,9 +47,10 @@ public async Task PerformInAppRateAsync()
/// <summary>
/// Perform rating on the current OS store app or open the store page on browser.
/// </summary>
/// <param name="packageName">Use this for Android.</param>
/// <param name="applicationId">Use this for iOS.</param>
public Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "")
/// <param name="packageName">Use <b>packageName</b> for Android.</param>
/// <param name="applicationId">Use <b>applicationId</b> for iOS.</param>
/// <param name="productId">Use <b>productId</b> for Windows</param>
public Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "", string productId = "")
{
var tcs = new TaskCompletionSource<bool>();

Expand Down
87 changes: 87 additions & 0 deletions Plugin.Maui.AppRating/Platforms/Windows/AppRating.windows.cs
@@ -0,0 +1,87 @@
using Microsoft.UI.Xaml.Controls;
using Windows.ApplicationModel.Core;
using Windows.Services.Store;
using Windows.UI.Core;
using Launcher = Windows.System.Launcher;

namespace Plugin.Maui.AppRating;

partial class AppRatingImplementation : IAppRating
{
/// <summary>
/// Open an in-app review popup of your current application.
/// </summary>
/// <remarks>To use this method the <b>Target Version</b> must be 10.0.17763 or above.</remarks>
public async Task PerformInAppRateAsync()
{
var dispatcher = CoreApplication.MainView?.CoreWindow?.Dispatcher;

await dispatcher?.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
StoreContext storeContext = StoreContext.GetDefault();
var result = await storeContext.RequestRateAndReviewAppAsync();
switch (result.Status)
{
case StoreRateAndReviewStatus.Error:
await ShowErrorMessage("ERROR", "There was an error trying to opening in-app rating.");
break;
case StoreRateAndReviewStatus.CanceledByUser:
await ShowErrorMessage("ACTION CANCELED", "In-app rating action canceled by user.");
break;
case StoreRateAndReviewStatus.NetworkError:
await ShowErrorMessage("ERROR", "Please check your internet connection first.");
break;
}
});
}

/// <summary>
/// Perform rating on the current OS store app or open the store page on browser.
/// </summary>
/// <param name="packageName">Use <b>packageName</b> for Android.</param>
/// <param name="applicationId">Use <b>applicationId</b> for iOS.</param>
/// <param name="productId">Use <b>productId</b> for Windows</param>
public async Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "", string productId = "")
{
if (string.IsNullOrEmpty(productId))
{
await ShowErrorMessage("ERROR", "Please, provide the application ProductId for Microsoft Store.");

return;
}

try
{
await Launcher.LaunchUriAsync(new Uri($"ms-windows-store://review/?ProductId={productId}"));
}
catch (Exception)
{
await ShowErrorMessage("ERROR", "Cannot open rating because Microsoft Store was unable to launch.");
}
}

private static async Task ShowErrorMessage(string title, string message)
{
// To create a native dialog there is an issue with the XamlRoot on WinUI,
// so we have to call the MainPage of the MAUI application and use the
// DisplayAlert method.
// In case something goes wrong, we throw an exception.
try
{
if (Application.Current != null)
{
if (Application.Current.MainPage != null)
await Application.Current.MainPage.DisplayAlert(title, message, "OK");
}
}
catch (Exception)
{
throw;
}
}
}
7 changes: 4 additions & 3 deletions Plugin.Maui.AppRating/Platforms/iOS/AppRating.ios.cs
Expand Up @@ -48,9 +48,10 @@ public Task PerformInAppRateAsync()
/// <summary>
/// Perform rating on the current OS store app or open the store page on browser.
/// </summary>
/// <param name="packageName">Use this for Android.</param>
/// <param name="applicationId">Use this for iOS/macOS</param>
public Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "")
/// <param name="packageName">Use <b>packageName</b> for Android.</param>
/// <param name="applicationId">Use <b>applicationId</b> for iOS.</param>
/// <param name="productId">Use <b>productId</b> for Windows</param>
public Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "", string productId = "")
{
var tcs = new TaskCompletionSource<bool>();

Expand Down
2 changes: 1 addition & 1 deletion Plugin.Maui.AppRating/Plugin.Maui.AppRating.csproj
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!--<TargetFrameworks>net7.0;net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>-->
<TargetFrameworks>net7.0-android;net7.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
<UseMaui>true</UseMaui>
Expand Down

0 comments on commit 3485ce6

Please sign in to comment.