In app purchase update and optimizations

This commit is contained in:
Yegor Vialov 2020-02-11 09:39:11 +00:00
parent 1e3bfa8ff7
commit 0f7179b944
4 changed files with 53 additions and 34 deletions

View File

@ -3,3 +3,4 @@ org.gradle.daemon=true
org.gradle.caching=true org.gradle.caching=true
android.useAndroidX=true android.useAndroidX=true
android.enableJetifier=true android.enableJetifier=true
android.enableR8=true

View File

@ -180,7 +180,42 @@ void main() async {
}); });
} }
class HAClientApp extends StatelessWidget { class HAClientApp extends StatefulWidget {
@override
_HAClientAppState createState() => new _HAClientAppState();
}
class _HAClientAppState extends State<HAClientApp> {
StreamSubscription<List<PurchaseDetails>> _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<PurchaseDetails>) {
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. // This widget is the root of your application.
@override @override
@ -233,4 +268,10 @@ class HAClientApp extends StatelessWidget {
}, },
); );
} }
@override
void dispose() {
_subscription.cancel();
super.dispose();
}
} }

View File

@ -11,7 +11,6 @@ class MainPage extends StatefulWidget {
class _MainPageState extends State<MainPage> with WidgetsBindingObserver, TickerProviderStateMixin { class _MainPageState extends State<MainPage> with WidgetsBindingObserver, TickerProviderStateMixin {
StreamSubscription<List<PurchaseDetails>> _subscription;
StreamSubscription _stateSubscription; StreamSubscription _stateSubscription;
StreamSubscription _settingsSubscription; StreamSubscription _settingsSubscription;
StreamSubscription _serviceCallSubscription; StreamSubscription _serviceCallSubscription;
@ -30,11 +29,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
@override @override
void initState() { void initState() {
super.initState(); super.initState();
final Stream purchaseUpdates =
InAppPurchaseConnection.instance.purchaseUpdatedStream;
_subscription = purchaseUpdates.listen((purchases) {
_handlePurchaseUpdates(purchases);
});
workManager.Workmanager.initialize( workManager.Workmanager.initialize(
updateDeviceLocationIsolate, updateDeviceLocationIsolate,
isInDebugMode: false isInDebugMode: false
@ -147,22 +141,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
} }
} }
void _handlePurchaseUpdates(purchase) {
if (purchase is List<PurchaseDetails>) {
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() { Future _subscribe() {
Completer completer = Completer(); Completer completer = Completer();
if (_stateSubscription == null) { if (_stateSubscription == null) {
@ -903,7 +881,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_showEntityPageSubscription?.cancel(); _showEntityPageSubscription?.cancel();
_showErrorSubscription?.cancel(); _showErrorSubscription?.cancel();
_startAuthSubscription?.cancel(); _startAuthSubscription?.cancel();
_subscription?.cancel();
_showPageSubscription?.cancel(); _showPageSubscription?.cancel();
_reloadUISubscription?.cancel(); _reloadUISubscription?.cancel();
//TODO disconnect //TODO disconnect

View File

@ -10,16 +10,16 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
web_socket_channel: any web_socket_channel: ^1.1.0
shared_preferences: any shared_preferences: ^0.5.6+1
progress_indicators: any progress_indicators: ^0.1.4
event_bus: any event_bus: ^1.1.1
cached_network_image: any cached_network_image: ^2.0.0
url_launcher: any url_launcher: ^5.4.1
date_format: any date_format: ^1.0.8
charts_flutter: ^0.8.1 charts_flutter: ^0.8.1
flutter_markdown: 0.3.0 flutter_markdown: ^0.3.3
in_app_purchase: ^0.2.1+4 in_app_purchase: ^0.3.0+3
flutter_custom_tabs: ^0.6.0 flutter_custom_tabs: ^0.6.0
flutter_webview_plugin: ^0.3.10+1 flutter_webview_plugin: ^0.3.10+1
firebase_messaging: ^5.1.6 firebase_messaging: ^5.1.6