appmate_flutter 1.0.1
appmate_flutter: ^1.0.1 copied to clipboard
Appmate flutter plugin project.
example/lib/main.dart
import 'dart:async';
import 'dart:developer';
import 'package:appmate_flutter/PurchaseClient.dart';
import 'package:appmate_flutter/helpers/BaseResponse.dart';
import 'package:appmate_flutter/helpers/EntitlementResponse.dart';
import 'package:appmate_flutter/helpers/GenericError.dart';
import 'package:appmate_flutter/helpers/ProductsResponse.dart';
import 'package:appmate_flutter/helpers/UserEventType.dart';
import 'package:appmate_flutter/models/Offerwall.dart';
import 'package:appmate_flutter/models/Product.dart';
import 'package:appmate_flutter_example/offerwall_detail.dart';
import 'package:appmate_flutter_example/settings.dart';
import 'package:appmate_flutter_example/user_id_relation.dart';
import 'package:appmate_flutter_example/util.dart';
import 'package:appmate_flutter_example/widgets/ProductRow.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'purchase.dart';
void main() {
runApp(MaterialApp(
home: ProductList(),
));
}
class ProductList extends StatefulWidget {
const ProductList({Key? key}) : super(key: key);
@override
_ProductListState createState() => _ProductListState();
}
class _ProductListState extends State<ProductList> {
List<Product> _products = [];
String _environment = "";
@override
void initState() {
super.initState();
PurchaseClient.setApiKey("p3PHN2ANQb2XpAV7A-uvyQ").then((value) => {
PurchaseClient.getUserId().then((userId) {
PurchaseClient.setOneSignalId(userId);
PurchaseClient.setAdvertiseId(userId);
PurchaseClient.setAppsFlyerId(userId);
PurchaseClient.setUserAttributes(
{"userName": "example", "Email": "example@example.com"});
})
});
PurchaseClient.setSandboxActive(true);
PurchaseClient.enableDebugLogs(true);
setEnvironment();
//AppmateFlutter.setUserId("fatih1905");
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text('AppMate - ' + _environment),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.settings,
color: Colors.white,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Settings()),
).then((completion) {
setEnvironment();
});
},
),
IconButton(
icon: Icon(
Icons.supervised_user_circle,
color: Colors.white,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => UserIdRelation()),
);
},
),
IconButton(
icon: Icon(
Icons.history,
color: Colors.white,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => PurchaseList()),
);
},
)
],
centerTitle: false,
backgroundColor: Colors.redAccent,
),
body: SingleChildScrollView(child: _buildProducts()),
);
}
void setEnvironment() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
var env = prefs.getString('_environment');
_environment = (env != null && env.length > 0) ? env : "prod";
});
}
void showProductsByIdAlert() {
TextEditingController _productIdField = TextEditingController();
showCupertinoDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: const Text('Get Product(s) By Id'),
content: Column(children: [
Text(
"Enter Ids, you can seperate them with commas. ie: test_consumable,test_non_consumable"),
CupertinoTextField(controller: _productIdField)
]),
actions: <Widget>[
CupertinoActionSheetAction(
child: const Text('Cancel'),
onPressed: () {
Navigator.pop(context, 'Cancel');
},
),
CupertinoActionSheetAction(
child: const Text('Get'),
onPressed: () {
getProductsWithIdList(_productIdField.text.split(","));
Navigator.pop(context, '0');
},
)
]),
);
}
void getOfferwalDetail({required String offerwallId}) {
PurchaseClient.getOfferwall(offerwallId).then((value) {
Offerwall? offerwall = value.offerwall;
GenericError? error = value.error;
String errorMsg = '';
if (error != null) {
errorMsg = error.message ?? '';
}
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
OfferwallDetail(offerwall: offerwall, errorMsg: errorMsg)),
);
});
}
void showOfferwallByIdAlert() {
TextEditingController _offerwallField = TextEditingController();
showCupertinoDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: const Text('Enter Offerwall Id'),
content: Column(children: [
CupertinoTextField(controller: _offerwallField),
]),
actions: <Widget>[
CupertinoActionSheetAction(
child: const Text('Get'),
onPressed: () {
String? offerwallId = _offerwallField.text;
if (offerwallId.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Enter id please"),
));
} else {
getOfferwalDetail(offerwallId: offerwallId);
}
},
),
CupertinoActionSheetAction(
child: const Text('Get Default Offer'),
onPressed: () {
getOfferwalDetail(offerwallId: '');
},
),
CupertinoActionSheetAction(
child: const Text('Cancel'),
onPressed: () {
Navigator.pop(context, 'Cancel');
},
)
]),
);
}
void isProductPurchased() {
TextEditingController _productIdField = TextEditingController();
showCupertinoDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: const Text('is Product Purchased'),
content: Column(children: [
Text("Enter Product Id, ie: test_consumable"),
CupertinoTextField(controller: _productIdField)
]),
actions: <Widget>[
CupertinoActionSheetAction(
child: const Text('Cancel'),
onPressed: () {
Navigator.pop(context, 'Cancel');
},
),
CupertinoActionSheetAction(
child: const Text('Get'),
onPressed: () async {
BaseResponse response = await PurchaseClient.isProductPurchased(
_productIdField.text);
if (response.error != null) {
Util.showDialog(
context, response.error!.message ?? "Unknown Error");
} else {
Util.showDialog(
context,
response.success
? "Product Purchased"
: "Product Not Purchased");
}
},
)
]),
);
}
void showProductsWithTypeAlert() {
showCupertinoModalPopup(
context: context,
builder: (BuildContext context) => CupertinoActionSheet(
title: const Text('Get Products With Type'),
message: const Text('Select Product Type'),
actions: <Widget>[
CupertinoActionSheetAction(
child: const Text('Consumable'),
onPressed: () {
getProductsWithType("0");
Navigator.pop(context, '0');
},
),
CupertinoActionSheetAction(
child: const Text('Nonconsumable'),
onPressed: () {
getProductsWithType("1");
Navigator.pop(context, '1');
},
),
CupertinoActionSheetAction(
child: const Text('Subscription'),
onPressed: () {
getProductsWithType("2");
Navigator.pop(context, '2');
},
)
],
cancelButton: CupertinoActionSheetAction(
child: const Text('Cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context, 'Cancel');
},
)),
);
}
Future<void> getProducts() async {
setState(() {
_products = [];
});
List<Product> products = [];
try {
ProductsResponse response = await PurchaseClient.getProducts();
if (response.error != null) {
Util.showDialog(context, response.error!.message ?? "Unknown Error");
} else {
products = response.products ?? [];
}
} on Exception catch (e) {
print('Unknown exception: $e');
}
if (!mounted) return;
setState(() {
_products = products;
});
}
Future<void> getEntitlements() async {
try {
EntitlementResponse response = await PurchaseClient.getEntitlements();
showCupertinoDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: const Text('Entitlements'),
content: Column(children: [
Text(response.entitlements.toString()),
]),
actions: <Widget>[
CupertinoActionSheetAction(
child: const Text('Cancel'),
onPressed: () {
Navigator.pop(context, 'Cancel');
},
),
]),
);
} on Exception catch (err) {
log(err.toString());
}
}
Future<void> sendAppmateEvvent(
String productId, UserEventType eventType) async {
try {
BaseResponse response =
await PurchaseClient.setAppmateEvent(productId, eventType);
if (response.error != null) {
Util.showDialog(context, response.error!.message ?? "Unknown Error");
} else {
Util.showDialog(
context, response.success ? "Set Event Success" : "Unknown Error");
}
} on Exception catch (e) {
// Anything else that is an exception
print('Unknown exception: $e');
}
}
Future<void> setAppmateEvent() async {
TextEditingController _productIdField = TextEditingController();
showCupertinoDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: const Text('setAppmateEvent'),
content: Column(children: [
Text("Enter Product Id, ie: test_consumable"),
CupertinoTextField(controller: _productIdField),
Column(
children: [
OutlinedButton(
child: Text('FIRST_LAUNCH'),
onPressed: () async {
sendAppmateEvvent(
_productIdField.text, UserEventType.FIRST_LAUNCH);
}),
OutlinedButton(
child: Text('VIEW'),
onPressed: () async {
sendAppmateEvvent(
_productIdField.text, UserEventType.VIEW);
}),
OutlinedButton(
child: Text('PURCHASE'),
onPressed: () async {
sendAppmateEvvent(
_productIdField.text, UserEventType.PURCHASE);
}),
],
)
]),
actions: <Widget>[
CupertinoActionSheetAction(
child: const Text('Cancel'),
onPressed: () {
Navigator.pop(context, 'Cancel');
},
)
]),
);
}
Future<void> getProductsWithType(String type) async {
List<Product> products = [];
try {
ProductsResponse response =
await PurchaseClient.getProductsWithType(type);
if (response.error != null) {
Util.showDialog(context, response.error!.message ?? "Unknown Error");
} else {
products = response.products ?? [];
}
} on PlatformException {}
if (!mounted) return;
setState(() {
_products = products;
});
}
Future<void> getProductsWithIdList(List<String> ids) async {
List<Product> products = [];
try {
ProductsResponse response =
await PurchaseClient.getProductsWithIdList(ids);
if (response.error != null) {
Util.showDialog(context, response.error!.message ?? "Unknown Error");
} else {
products = response.products ?? [];
}
} on PlatformException {}
if (!mounted) return;
setState(() {
_products = products;
});
}
Widget _buildProducts() {
return Column(
children: [
TextButton(
onPressed: () {
getProducts();
},
child: Text('Get Products'),
),
TextButton(
onPressed: () {
showProductsWithTypeAlert();
},
child: Text('Get Products With Type'),
),
TextButton(
onPressed: () {
showProductsByIdAlert();
},
child: Text('Get Products By Id'),
),
TextButton(
onPressed: () {
isProductPurchased();
},
child: Text('is Product Purchased'),
),
TextButton(
onPressed: () {
getEntitlements();
},
child: Text('Get Entitlements'),
),
TextButton(
onPressed: () {
setAppmateEvent();
},
child: Text('Appmate Event'),
),
TextButton(
onPressed: () {
showOfferwallByIdAlert();
},
child: Text('Get Offerwall'),
),
ListView.builder(
itemCount: _products.length * 2,
padding: const EdgeInsets.all(10),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext _context, int i) {
if (i.isOdd) {
return Divider();
}
final int index = i ~/ 2;
if (index >= _products.length) {
return Divider();
}
return ProductRow(p: _products[index]);
})
],
);
}
}