116 lines
3.2 KiB
Dart
116 lines
3.2 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:aitrainer_app/service/logging.dart';
|
|
import 'package:flutter_inapp_purchase/flutter_inapp_purchase.dart';
|
|
|
|
class PlatformPurchaseApi with Logging {
|
|
static final PlatformPurchaseApi _singleton = PlatformPurchaseApi._internal();
|
|
|
|
StreamSubscription _purchaseUpdatedSubscription;
|
|
StreamSubscription _purchaseErrorSubscription;
|
|
StreamSubscription _conectionSubscription;
|
|
String _platformVersion = 'Unknown';
|
|
List<IAPItem> _items = [];
|
|
List<PurchasedItem> _purchases = [];
|
|
|
|
final List<String> _productLists = List();
|
|
/* Platform.isAndroid
|
|
? [
|
|
'android.test.purchased',
|
|
'point_1000',
|
|
'5000_point',
|
|
'android.test.canceled',
|
|
]
|
|
: ['com.cooni.point1000', 'com.cooni.point5000'];
|
|
*/
|
|
|
|
factory PlatformPurchaseApi() {
|
|
return _singleton;
|
|
}
|
|
|
|
PlatformPurchaseApi._internal();
|
|
|
|
void close() {
|
|
if (_conectionSubscription != null) {
|
|
_conectionSubscription.cancel();
|
|
_conectionSubscription = null;
|
|
}
|
|
}
|
|
|
|
// Platform messages are asynchronous, so we initialize in an async method.
|
|
Future<void> initPurchasePlatformState() async {
|
|
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);
|
|
}
|
|
|
|
// prepare
|
|
var result = await FlutterInappPurchase.instance.initConnection;
|
|
log('result: $result');
|
|
|
|
_platformVersion = platformVersion;
|
|
|
|
// refresh items for android
|
|
try {
|
|
String msg = await FlutterInappPurchase.instance.consumeAllItems;
|
|
log('consumeAllItems: $msg');
|
|
} catch (err) {
|
|
log('consumeAllItems error: $err');
|
|
}
|
|
|
|
_conectionSubscription = FlutterInappPurchase.connectionUpdated.listen((connected) {
|
|
log('connected: $connected');
|
|
});
|
|
|
|
_purchaseUpdatedSubscription = FlutterInappPurchase.purchaseUpdated.listen((productItem) {
|
|
log('purchase-updated: $productItem');
|
|
});
|
|
|
|
_purchaseErrorSubscription = FlutterInappPurchase.purchaseError.listen((purchaseError) {
|
|
log('purchase-error: $purchaseError');
|
|
});
|
|
}
|
|
|
|
void requestPurchase(IAPItem item) {
|
|
//FlutterInappPurchase.instance.requestPurchase(item.productId);
|
|
FlutterInappPurchase.instance.requestPurchase("WT_monthly");
|
|
}
|
|
|
|
Future _getProduct() async {
|
|
List<IAPItem> items = await FlutterInappPurchase.instance.getProducts(_productLists);
|
|
for (var item in items) {
|
|
log('${item.toString()}');
|
|
this._items.add(item);
|
|
}
|
|
|
|
this._items = items;
|
|
this._purchases = [];
|
|
}
|
|
|
|
Future _getPurchases() async {
|
|
List<PurchasedItem> items = await FlutterInappPurchase.instance.getAvailablePurchases();
|
|
for (var item in items) {
|
|
log('${item.toString()}');
|
|
this._purchases.add(item);
|
|
}
|
|
|
|
this._items = [];
|
|
this._purchases = items;
|
|
}
|
|
|
|
Future _getPurchaseHistory() async {
|
|
List<PurchasedItem> items = await FlutterInappPurchase.instance.getPurchaseHistory();
|
|
for (var item in items) {
|
|
log('${item.toString()}');
|
|
this._purchases.add(item);
|
|
}
|
|
|
|
this._items = [];
|
|
this._purchases = items;
|
|
}
|
|
}
|