From 0f7179b9444fdc9cdaa632675eb2c010626f89fe Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Tue, 11 Feb 2020 09:39:11 +0000 Subject: [PATCH] In app purchase update and optimizations --- android/gradle.properties | 3 ++- lib/main.dart | 43 ++++++++++++++++++++++++++++++++++++++- lib/pages/main.page.dart | 23 --------------------- pubspec.yaml | 18 ++++++++-------- 4 files changed, 53 insertions(+), 34 deletions(-) diff --git a/android/gradle.properties b/android/gradle.properties index aec424e..ba61cee 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -2,4 +2,5 @@ org.gradle.jvmargs=-Xmx2g org.gradle.daemon=true org.gradle.caching=true android.useAndroidX=true -android.enableJetifier=true \ No newline at end of file +android.enableJetifier=true +android.enableR8=true diff --git a/lib/main.dart b/lib/main.dart index 7828587..16c99bf 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -180,7 +180,42 @@ void main() async { }); } -class HAClientApp extends StatelessWidget { +class HAClientApp extends StatefulWidget { + + @override + _HAClientAppState createState() => new _HAClientAppState(); + +} + +class _HAClientAppState extends State { + StreamSubscription> _subscription; + + @override + void initState() { + InAppPurchaseConnection.enablePendingPurchases(); + final Stream purchaseUpdates = + InAppPurchaseConnection.instance.purchaseUpdatedStream; + _subscription = purchaseUpdates.listen((purchases) { + _handlePurchaseUpdates(purchases); + }); + super.initState(); + } + + void _handlePurchaseUpdates(purchase) { + if (purchase is List) { + if (purchase[0].status == PurchaseStatus.purchased) { + eventBus.fire(ShowPopupMessageEvent( + title: "Thanks a lot!", + body: "Thank you for supporting HA Client development!", + buttonText: "Ok" + )); + } else { + Logger.d("Purchase change handler: ${purchase[0].status}"); + } + } else { + Logger.e("Something wrong with purchase handling. Got: $purchase"); + } + } // This widget is the root of your application. @override @@ -233,4 +268,10 @@ class HAClientApp extends StatelessWidget { }, ); } + + @override + void dispose() { + _subscription.cancel(); + super.dispose(); + } } diff --git a/lib/pages/main.page.dart b/lib/pages/main.page.dart index 343a074..fb0343b 100644 --- a/lib/pages/main.page.dart +++ b/lib/pages/main.page.dart @@ -11,7 +11,6 @@ class MainPage extends StatefulWidget { class _MainPageState extends State with WidgetsBindingObserver, TickerProviderStateMixin { - StreamSubscription> _subscription; StreamSubscription _stateSubscription; StreamSubscription _settingsSubscription; StreamSubscription _serviceCallSubscription; @@ -30,11 +29,6 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker @override void initState() { super.initState(); - final Stream purchaseUpdates = - InAppPurchaseConnection.instance.purchaseUpdatedStream; - _subscription = purchaseUpdates.listen((purchases) { - _handlePurchaseUpdates(purchases); - }); workManager.Workmanager.initialize( updateDeviceLocationIsolate, isInDebugMode: false @@ -147,22 +141,6 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker } } - void _handlePurchaseUpdates(purchase) { - if (purchase is List) { - if (purchase[0].status == PurchaseStatus.purchased) { - eventBus.fire(ShowPopupMessageEvent( - title: "Thanks a lot!", - body: "Thank you for supporting HA Client development!", - buttonText: "Ok" - )); - } else { - Logger.d("Purchase change handler: ${purchase[0].status}"); - } - } else { - Logger.e("Something wrong with purchase handling. Got: $purchase"); - } - } - Future _subscribe() { Completer completer = Completer(); if (_stateSubscription == null) { @@ -903,7 +881,6 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker _showEntityPageSubscription?.cancel(); _showErrorSubscription?.cancel(); _startAuthSubscription?.cancel(); - _subscription?.cancel(); _showPageSubscription?.cancel(); _reloadUISubscription?.cancel(); //TODO disconnect diff --git a/pubspec.yaml b/pubspec.yaml index 4347ec7..1533392 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,16 +10,16 @@ environment: dependencies: flutter: sdk: flutter - web_socket_channel: any - shared_preferences: any - progress_indicators: any - event_bus: any - cached_network_image: any - url_launcher: any - date_format: any + web_socket_channel: ^1.1.0 + shared_preferences: ^0.5.6+1 + progress_indicators: ^0.1.4 + event_bus: ^1.1.1 + cached_network_image: ^2.0.0 + url_launcher: ^5.4.1 + date_format: ^1.0.8 charts_flutter: ^0.8.1 - flutter_markdown: 0.3.0 - in_app_purchase: ^0.2.1+4 + flutter_markdown: ^0.3.3 + in_app_purchase: ^0.3.0+3 flutter_custom_tabs: ^0.6.0 flutter_webview_plugin: ^0.3.10+1 firebase_messaging: ^5.1.6