From 618007b20624aba3047b5c7302d793aca7807317 Mon Sep 17 00:00:00 2001 From: Maria Bordunova Date: Wed, 18 Aug 2021 19:19:17 +0300 Subject: [PATCH 1/3] Update sample app --- example/lib/home.dart | 74 ++++++++++++++++++---------------- example/lib/params_view.dart | 11 +++-- example/lib/products_view.dart | 45 ++++++++++++--------- 3 files changed, 70 insertions(+), 60 deletions(-) diff --git a/example/lib/home.dart b/example/lib/home.dart index b7de8025..a1515c71 100644 --- a/example/lib/home.dart +++ b/example/lib/home.dart @@ -10,12 +10,13 @@ class HomeView extends StatefulWidget { } class _HomeViewState extends State { - QLaunchResult _qLaunchResult; + Map _permissions; + Map _products; @override void initState() { super.initState(); - initPlatformState(); + _initPlatformState(); } @override @@ -25,25 +26,15 @@ class _HomeViewState extends State { title: const Text('Qonversion example app'), ), body: Center( - child: _qLaunchResult == null + child: _products == null && _permissions == null ? CircularProgressIndicator() : ListView( children: [ Padding(padding: EdgeInsets.only(top: 20)), - ListTile( - title: Text('UID'), - subtitle: Text(_qLaunchResult.uid), - ), - ListTile( - title: Text('DateTime'), - subtitle: Text(_qLaunchResult.date?.toString() ?? 'n/a'), - ), ListTile(title: Text('PRODUCTS:')), - ...productsFromMap(_qLaunchResult.products), + ..._productsFromMap(_products ?? {}), ListTile(title: Text('PERMISSIONS:')), - ..._permissionsFromMap(_qLaunchResult.permissions), - ListTile(title: Text('USER PRODUCTS:')), - ...productsFromMap(_qLaunchResult.userProducts), + ..._permissionsFromMap(_permissions ?? {}), Padding( padding: const EdgeInsets.all(8.0), child: FlatButton( @@ -101,16 +92,29 @@ class _HomeViewState extends State { ); } - Future initPlatformState() async { + Future _initPlatformState() async { if (kDebugMode) { await Qonversion.setDebugMode(); } - _qLaunchResult = await Qonversion.launch( + Qonversion.launch( 'PV77YHL7qnGvsdmpTs7gimsxUvY-Znl2', isObserveMode: false, ); + _loadUI(); + } + + Future _loadUI() async { + try { + _products = await Qonversion.products(); + _permissions = await Qonversion.checkPermissions(); + } catch (e) { + print(e); + _products = {}; + _permissions = {}; + } + setState(() {}); } @@ -136,24 +140,24 @@ class _HomeViewState extends State { ) .toList(); } -} -List productsFromMap(Map products) { - return products.entries - .map( - (e) => ListTile( - title: Text(e.key), - subtitle: Text( - e.value.qonversionId ?? - '' + '\n' + e.value.storeId ?? - '' + - '\n' + - e.value.duration.toString() + - '\n' + - e.value.type.toString() + - '\n', + List _productsFromMap(Map products) { + return products.entries + .map( + (e) => ListTile( + title: Text(e.key), + subtitle: Text( + e.value.qonversionId ?? + '' + '\n' + e.value.storeId ?? + '' + + '\n' + + e.value.duration.toString() + + '\n' + + e.value.type.toString() + + '\n', + ), ), - ), - ) - .toList(); + ) + .toList(); + } } diff --git a/example/lib/params_view.dart b/example/lib/params_view.dart index 633ec137..91442d32 100644 --- a/example/lib/params_view.dart +++ b/example/lib/params_view.dart @@ -22,7 +22,7 @@ class ParamsView extends StatelessWidget { color: Colors.green, textColor: Colors.white, onPressed: () async { - await Qonversion.setUserId('customId'); + Qonversion.setUserId('customId'); print('did set user id'); }, ), @@ -30,9 +30,8 @@ class ParamsView extends StatelessWidget { child: Text('Set User Property'), color: Colors.blue, textColor: Colors.white, - onPressed: () async { - await Qonversion.setUserProperty( - 'customProperty', 'customValue'); + onPressed: () { + Qonversion.setUserProperty('customProperty', 'customValue'); print('did set user property'); }, ), @@ -41,8 +40,8 @@ class ParamsView extends StatelessWidget { child: Text('Set ${describeEnum(v)}'), color: Colors.purple, textColor: Colors.white, - onPressed: () async { - await Qonversion.setProperty(v, 'email@email.com'); + onPressed: () { + Qonversion.setProperty(v, 'email@email.com'); print('did set property'); }, ), diff --git a/example/lib/products_view.dart b/example/lib/products_view.dart index f9d36286..09c1b207 100644 --- a/example/lib/products_view.dart +++ b/example/lib/products_view.dart @@ -37,7 +37,9 @@ class _ProductsViewState extends State { orElse: () => null); print(permission?.isActive); - } catch (e) { + } on QPurchaseException catch (e) { + // check if a user canceled the purchase + // e.isUserCancelled print(e); } }); @@ -77,11 +79,15 @@ class _ProductsViewState extends State { color: Colors.yellow, textColor: Colors.black, onPressed: () async { - final res = - await Qonversion.checkTrialIntroEligibility( - _products.keys.toList()); - print(res.map( - (key, value) => MapEntry(key, value.status))); + try { + final res = + await Qonversion.checkTrialIntroEligibility( + _products.keys.toList()); + print(res.map( + (key, value) => MapEntry(key, value.status))); + } catch (e) { + print(e); + } }, ), ), @@ -116,10 +122,7 @@ class _ProductsViewState extends State { children: [ ListTile( title: Text('Store ID: ${product.storeId}'), - subtitle: Text('Q ID: ${product.qonversionId}'), - trailing: product.skProduct != null - ? Text(product.skProduct.localizedTitle) - : null, + subtitle: Text('Q ID: ${product.qonversionId}.\nStore Title: ${product.storeTitle}'), onTap: () => print(product.toJson()), ), Padding( @@ -129,12 +132,19 @@ class _ProductsViewState extends State { color: Colors.blue, textColor: Colors.white, onPressed: () async { - final permissions = await Qonversion.purchase(product.qonversionId); - final permission = permissions.values.firstWhere( - (element) => element.productId == product.qonversionId, - orElse: () => null); - - print(permission?.isActive); + try { + final permissions = + await Qonversion.purchaseProduct(product); + final permission = permissions.values.firstWhere( + (element) => element.productId == product.qonversionId, + orElse: () => null); + + print(permission?.isActive); + } on QPurchaseException catch (e) { + // check if a user canceled the purchase + // e.isUserCancelled + print(e); + } }, ), ), @@ -176,9 +186,6 @@ class _ProductsViewState extends State { ListTile( title: Text('Store ID: ${product.storeId}'), subtitle: Text('Q ID: ${product.qonversionId}'), - trailing: product.skProduct != null - ? Text(product.skProduct.localizedTitle) - : null, ) ]; } From 12d62014cb0096934b4b65232f1e3f6bd3c51967 Mon Sep 17 00:00:00 2001 From: Maria Bordunova Date: Wed, 18 Aug 2021 19:28:50 +0300 Subject: [PATCH 2/3] Delete useless keywords --- example/lib/home.dart | 2 +- example/lib/params_view.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/example/lib/home.dart b/example/lib/home.dart index a1515c71..edb29627 100644 --- a/example/lib/home.dart +++ b/example/lib/home.dart @@ -94,7 +94,7 @@ class _HomeViewState extends State { Future _initPlatformState() async { if (kDebugMode) { - await Qonversion.setDebugMode(); + Qonversion.setDebugMode(); } Qonversion.launch( diff --git a/example/lib/params_view.dart b/example/lib/params_view.dart index 91442d32..27b98cc2 100644 --- a/example/lib/params_view.dart +++ b/example/lib/params_view.dart @@ -21,7 +21,7 @@ class ParamsView extends StatelessWidget { child: Text('Set User ID'), color: Colors.green, textColor: Colors.white, - onPressed: () async { + onPressed: () { Qonversion.setUserId('customId'); print('did set user id'); }, From 13fb6b95525bbf8ea0b99e7afb91b94c2cb36990 Mon Sep 17 00:00:00 2001 From: Maria Bordunova Date: Thu, 19 Aug 2021 12:18:49 +0300 Subject: [PATCH 3/3] Apply suggestions from code review --- example/lib/home.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/lib/home.dart b/example/lib/home.dart index edb29627..cc3a8457 100644 --- a/example/lib/home.dart +++ b/example/lib/home.dart @@ -102,10 +102,10 @@ class _HomeViewState extends State { isObserveMode: false, ); - _loadUI(); + _loadQonversionObjects(); } - Future _loadUI() async { + Future _loadQonversionObjects() async { try { _products = await Qonversion.products(); _permissions = await Qonversion.checkPermissions();