WT1.1.4 build 45 android fb, iap
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
import 'package:aitrainer_app/model/product.dart';
|
||||
import 'package:aitrainer_app/service/logging.dart';
|
||||
import 'package:flutter_inapp_purchase/flutter_inapp_purchase.dart';
|
||||
|
||||
@@ -11,10 +14,14 @@ class PlatformPurchaseApi with Logging {
|
||||
StreamSubscription _conectionSubscription;
|
||||
String _platformVersion = 'Unknown';
|
||||
List<IAPItem> _items = [];
|
||||
List getIAPItems() => _items;
|
||||
List<PurchasedItem> _purchases = [];
|
||||
|
||||
final List<String> _productLists = List();
|
||||
/* Platform.isAndroid
|
||||
final List<String> _productList = List();
|
||||
|
||||
List getProductList() => _productList;
|
||||
|
||||
/* Platform.isAndroid
|
||||
? [
|
||||
'android.test.purchased',
|
||||
'point_1000',
|
||||
@@ -22,7 +29,7 @@ class PlatformPurchaseApi with Logging {
|
||||
'android.test.canceled',
|
||||
]
|
||||
: ['com.cooni.point1000', 'com.cooni.point5000'];
|
||||
*/
|
||||
*/
|
||||
|
||||
factory PlatformPurchaseApi() {
|
||||
return _singleton;
|
||||
@@ -30,27 +37,32 @@ class PlatformPurchaseApi with Logging {
|
||||
|
||||
PlatformPurchaseApi._internal();
|
||||
|
||||
void close() {
|
||||
Future<void> close() async {
|
||||
if (_conectionSubscription != null) {
|
||||
_conectionSubscription.cancel();
|
||||
_conectionSubscription = null;
|
||||
}
|
||||
await FlutterInappPurchase.instance.endConnection;
|
||||
}
|
||||
|
||||
// Platform messages are asynchronous, so we initialize in an async method.
|
||||
Future<void> initPurchasePlatformState() async {
|
||||
if (_productList.length > 0) {
|
||||
return;
|
||||
}
|
||||
log(" --- Init PurchasePlatform");
|
||||
String platformVersion;
|
||||
// Platform messages may fail, so we use a try/catch PlatformException.
|
||||
try {
|
||||
platformVersion = await FlutterInappPurchase.instance.platformVersion;
|
||||
} on Exception {
|
||||
platformVersion = 'Failed to get platform version for InAppPurchase.';
|
||||
log(platformVersion);
|
||||
log("PlatformVersion" + platformVersion);
|
||||
}
|
||||
|
||||
// prepare
|
||||
var result = await FlutterInappPurchase.instance.initConnection;
|
||||
log('result: $result');
|
||||
log(' FlutterInappPurchase init result: $result');
|
||||
|
||||
_platformVersion = platformVersion;
|
||||
|
||||
@@ -73,15 +85,57 @@ class PlatformPurchaseApi with Logging {
|
||||
_purchaseErrorSubscription = FlutterInappPurchase.purchaseError.listen((purchaseError) {
|
||||
log('purchase-error: $purchaseError');
|
||||
});
|
||||
|
||||
getSubscriptions();
|
||||
}
|
||||
|
||||
void getSubscriptions() {
|
||||
Cache().products.forEach((element) {
|
||||
Product product = element as Product;
|
||||
if (Platform.isAndroid) {
|
||||
if (product.productIdAndroid != null && product.productIdAndroid.isNotEmpty) {
|
||||
_productList.add(product.productIdAndroid);
|
||||
}
|
||||
} else {
|
||||
if (product.productIdIos != null && product.productIdIos.isNotEmpty) {
|
||||
_productList.add(product.productIdIos);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
_productList.forEach((element) {
|
||||
print(element);
|
||||
});
|
||||
_getSubscription();
|
||||
}
|
||||
|
||||
void purchase(Product product) {
|
||||
if (product == null) {
|
||||
throw Exception("No product to purchase");
|
||||
}
|
||||
String productId = Platform.isAndroid ? product.productIdAndroid : product.productIdIos;
|
||||
IAPItem selected;
|
||||
for (var item in _items) {
|
||||
if (item.productId == productId) {
|
||||
selected = item;
|
||||
log("Item to purchase: " + item.toString());
|
||||
}
|
||||
}
|
||||
|
||||
if (selected != null) {
|
||||
requestPurchase(selected);
|
||||
} else {
|
||||
throw Exception("product " + productId + " not defined in the PlatformStore");
|
||||
}
|
||||
}
|
||||
|
||||
void requestPurchase(IAPItem item) {
|
||||
//FlutterInappPurchase.instance.requestPurchase(item.productId);
|
||||
FlutterInappPurchase.instance.requestPurchase("WT_monthly");
|
||||
FlutterInappPurchase.instance.requestPurchase(item.productId);
|
||||
}
|
||||
|
||||
Future _getProduct() async {
|
||||
List<IAPItem> items = await FlutterInappPurchase.instance.getProducts(_productLists);
|
||||
Future _getSubscription() async {
|
||||
List<IAPItem> items = await FlutterInappPurchase.instance.getSubscriptions(_productList);
|
||||
for (var item in items) {
|
||||
log('${item.toString()}');
|
||||
this._items.add(item);
|
||||
|
||||
Reference in New Issue
Block a user