Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created April 20, 2024 03:53
Show Gist options
  • Save baba-s/a3d0c1c3c50674c9202543d62aafa72a to your computer and use it in GitHub Desktop.
Save baba-s/a3d0c1c3c50674c9202543d62aafa72a to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace Kogane
{
/// <summary>
/// Purchases 型の拡張メソッド
/// </summary>
public static class PurchasesExtensionMethods
{
//================================================================================
// 関数(static)
//================================================================================
/// <summary>
/// GetProducts を async / await に対応させる拡張メソッド
/// </summary>
public static async UniTask<(List<Purchases.StoreProduct> products, Purchases.Error error)> GetProductsAsync
(
this Purchases self,
string product
)
{
return await self.GetProductsAsync( new[] { product } );
}
/// <summary>
/// GetProducts を async / await に対応させる拡張メソッド
/// </summary>
public static async UniTask<(List<Purchases.StoreProduct> products, Purchases.Error error)> GetProductsAsync
(
this Purchases self,
string[] products
)
{
var tcs = new UniTaskCompletionSource<(List<Purchases.StoreProduct> products, Purchases.Error error)>();
self.GetProducts
(
products: products,
callback: Callback
);
return await tcs.Task;
void Callback
(
List<Purchases.StoreProduct> products,
Purchases.Error error
)
{
tcs.TrySetResult( ( products, error ) );
}
}
/// <summary>
/// GetCustomerInfo を async / await に対応させる拡張メソッド
/// </summary>
public static async UniTask<(Purchases.CustomerInfo customerInfo, Purchases.Error error)> GetCustomerInfoAsync
(
this Purchases self
)
{
var tcs = new UniTaskCompletionSource<(Purchases.CustomerInfo customerInfo, Purchases.Error error)>();
self.GetCustomerInfo( Callback );
return await tcs.Task;
void Callback
(
Purchases.CustomerInfo customerInfo,
Purchases.Error error
)
{
tcs.TrySetResult( ( customerInfo, error ) );
}
}
/// <summary>
/// PurchaseProduct を async / await に対応させる拡張メソッド
/// </summary>
public static async UniTask<(string productIdentifier, Purchases.CustomerInfo customerInfo, bool userCancelled, Purchases.Error error)> PurchaseProductAsync
(
this Purchases self,
string productIdentifier,
string type = "subs",
string oldSku = null,
Purchases.ProrationMode prorationMode = Purchases.ProrationMode.UnknownSubscriptionUpgradeDowngradePolicy,
bool googleIsPersonalizedPrice = false
)
{
var tcs = new UniTaskCompletionSource<(string productIdentifier, Purchases.CustomerInfo customerInfo, bool userCancelled, Purchases.Error error)>();
self.PurchaseProduct
(
productIdentifier: productIdentifier,
callback: Callback,
type: type,
oldSku: oldSku,
prorationMode: prorationMode,
googleIsPersonalizedPrice: googleIsPersonalizedPrice
);
return await tcs.Task;
void Callback
(
string productIdentifier,
Purchases.CustomerInfo customerInfo,
bool userCancelled,
Purchases.Error error
)
{
tcs.TrySetResult( ( productIdentifier, customerInfo, userCancelled, error ) );
}
}
/// <summary>
/// LogIn を async / await に対応させる拡張メソッド
/// </summary>
public static async UniTask<(Purchases.CustomerInfo customerInfo, bool created, Purchases.Error error)> LogInAsync
(
this Purchases self,
string appUserId
)
{
var tcs = new UniTaskCompletionSource<(Purchases.CustomerInfo customerInfo, bool created, Purchases.Error error)>();
self.LogIn
(
appUserId: appUserId,
callback: Callback
);
return await tcs.Task;
void Callback
(
Purchases.CustomerInfo customerInfo,
bool created,
Purchases.Error error
)
{
tcs.TrySetResult( ( customerInfo, created, error ) );
}
}
/// <summary>
/// RestorePurchases を async / await に対応させる拡張メソッド
/// </summary>
public static async UniTask<(Purchases.CustomerInfo customerInfo, Purchases.Error error)> RestorePurchasesAsync
(
this Purchases self
)
{
var tcs = new UniTaskCompletionSource<(Purchases.CustomerInfo customerInfo, Purchases.Error error)>();
self.RestorePurchases( Callback );
return await tcs.Task;
void Callback
(
Purchases.CustomerInfo customerInfo,
Purchases.Error error
)
{
tcs.TrySetResult( ( customerInfo, error ) );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment