diff --git a/lib/main.dart b/lib/main.dart index 639c47c..cce898a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -152,6 +152,7 @@ part 'entities/media_player/widgets/media_player_seek_bar.widget.dart'; part 'entities/media_player/widgets/media_player_progress_bar.widget.dart'; part 'pages/whats_new.page.dart'; part 'pages/fullscreen.page.dart'; +part 'popups.dart'; EventBus eventBus = new EventBus(); final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); @@ -236,10 +237,12 @@ class _HAClientAppState extends State { void _handlePurchaseUpdates(purchase) { if (purchase is List) { if (purchase[0].status == PurchaseStatus.purchased) { - eventBus.fire(ShowPopupMessageEvent( + eventBus.fire(ShowPopupEvent( + Popup( title: "Thanks a lot!", body: "Thank you for supporting HA Client development!", - buttonText: "Ok" + positiveText: "Ok" + ) )); } else { Logger.d("Purchase change handler: ${purchase[0].status}"); diff --git a/lib/managers/mobile_app_integration_manager.class.dart b/lib/managers/mobile_app_integration_manager.class.dart index fc1d6d9..97ffc42 100644 --- a/lib/managers/mobile_app_integration_manager.class.dart +++ b/lib/managers/mobile_app_integration_manager.class.dart @@ -51,14 +51,16 @@ class MobileAppIntegrationManager { prefs.setInt('app-integration-version', INTEGRATION_VERSION); completer.complete(); - eventBus.fire(ShowPopupDialogEvent( - title: "Mobile app Integration was created", - body: "HA Client was registered as MobileApp in your Home Assistant. To start using notifications you need to restart your Home Assistant", - positiveText: "Restart now", - negativeText: "Later", - onPositive: () { - ConnectionManager().callService(domain: "homeassistant", service: "restart"); - }, + eventBus.fire(ShowPopupEvent( + Popup( + title: "Mobile app Integration was created", + body: "HA Client was registered as MobileApp in your Home Assistant. To start using notifications you need to restart your Home Assistant", + positiveText: "Restart now", + negativeText: "Later", + onPositive: () { + ConnectionManager().callService(domain: "homeassistant", service: "restart"); + }, + ) )); }); }).catchError((e) { @@ -93,11 +95,12 @@ class MobileAppIntegrationManager { } else { Logger.d('App registration works fine'); if (showOkDialog) { - eventBus.fire(ShowPopupDialogEvent( + eventBus.fire(ShowPopupEvent( + Popup( title: "All good", body: "HA Client integration with your Home Assistant server works fine", - positiveText: "Nice!", - negativeText: "Ok" + positiveText: "Ok" + ) )); } } @@ -109,17 +112,19 @@ class MobileAppIntegrationManager { _askToRegisterApp(); } else { Logger.e("Error updating app registration: $e"); - eventBus.fire(ShowPopupDialogEvent( - title: "App integration is not working properly", - body: "Something wrong with HA Client integration on your Home Assistant server. Please report this issue.", - positiveText: "Report to GitHub", - negativeText: "Report to Discord", - onPositive: () { - Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new"); - }, - onNegative: () { - Launcher.launchURLInBrowser("https://discord.gg/AUzEvwn"); - }, + eventBus.fire(ShowPopupEvent( + Popup( + title: "App integration is not working properly", + body: "Something wrong with HA Client integration on your Home Assistant server. Please report this issue.", + positiveText: "Report to GitHub", + negativeText: "Report to Discord", + onPositive: () { + Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new"); + }, + onNegative: () { + Launcher.launchURLInBrowser("https://discord.gg/AUzEvwn"); + }, + ) )); } completer.complete(); @@ -129,30 +134,34 @@ class MobileAppIntegrationManager { } static void _askToRemoveAndRegisterApp() { - eventBus.fire(ShowPopupDialogEvent( - title: "Mobile app integration needs to be updated", - body: "You need to update HA Client integration to continue using notifications and location tracking. Please remove 'Mobile App' integration for this device from your Home Assistant and restart Home Assistant. Then go back to HA Client to create app integration again.", - positiveText: "Ok", - negativeText: "Report an issue", - onNegative: () { - Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new"); - }, + eventBus.fire(ShowPopupEvent( + Popup( + title: "Mobile app integration needs to be updated", + body: "You need to update HA Client integration to continue using notifications and location tracking. Please remove 'Mobile App' integration for this device from your Home Assistant and restart Home Assistant. Then go back to HA Client to create app integration again.", + positiveText: "Ok", + negativeText: "Report an issue", + onNegative: () { + Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new"); + }, + ) )); } static void _askToRegisterApp() { - eventBus.fire(ShowPopupDialogEvent( - title: "App integration is broken", - body: "Looks like app integration was removed from your Home Assistant or it needs to be updated. HA Client needs to be registered on your Home Assistant server to make it possible to use notifications and location tracking. Please remove 'Mobile App' integration for this device from your Home Assistant before registering and restart Home Assistant. Then go back here.", - positiveText: "Register now", - negativeText: "Cancel", - onPositive: () { - SharedPreferences.getInstance().then((prefs) { - prefs.remove("app-webhook-id"); - ConnectionManager().webhookId = null; - checkAppRegistration(); - }); - }, + eventBus.fire(ShowPopupEvent( + Popup( + title: "App integration is broken", + body: "Looks like app integration was removed from your Home Assistant or it needs to be updated. HA Client needs to be registered on your Home Assistant server to make it possible to use notifications and location tracking. Please remove 'Mobile App' integration for this device from your Home Assistant before registering and restart Home Assistant. Then go back here.", + positiveText: "Register now", + negativeText: "Cancel", + onPositive: () { + SharedPreferences.getInstance().then((prefs) { + prefs.remove("app-webhook-id"); + ConnectionManager().webhookId = null; + checkAppRegistration(); + }); + }, + ) )); } diff --git a/lib/managers/startup_user_messages_manager.class.dart b/lib/managers/startup_user_messages_manager.class.dart index cf441f4..c0e854d 100644 --- a/lib/managers/startup_user_messages_manager.class.dart +++ b/lib/managers/startup_user_messages_manager.class.dart @@ -29,7 +29,8 @@ class StartupUserMessagesManager { } void _showSupportAppDevelopmentMessage() { - eventBus.fire(ShowPopupDialogEvent( + eventBus.fire(ShowPopupEvent( + Popup( title: "Hi!", body: "As you may have noticed this app contains no ads. Also all app features are available for you for free. I'm not planning to change this in nearest future, but still you can support this application development materially. There is one-time payment available as well as several subscription options. Thanks.", positiveText: "Show options", @@ -45,6 +46,7 @@ class StartupUserMessagesManager { prefs.setBool(_supportAppDevelopmentMessageKey, true); }); } + ) )); } diff --git a/lib/pages/main/main.page.dart b/lib/pages/main/main.page.dart index 0c661d7..b61f42c 100644 --- a/lib/pages/main/main.page.dart +++ b/lib/pages/main/main.page.dart @@ -18,8 +18,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker StreamSubscription _showEntityPageSubscription; StreamSubscription _showErrorSubscription; StreamSubscription _startAuthSubscription; - StreamSubscription _showPopupDialogSubscription; - StreamSubscription _showPopupMessageSubscription; + StreamSubscription _showPopupSubscription; StreamSubscription _showTokenLoginPopupSubscription; StreamSubscription _reloadUISubscription; StreamSubscription _fullReloadSubscription; @@ -183,27 +182,9 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker _fullLoad(); }); } - if (_showPopupDialogSubscription == null) { - _showPopupDialogSubscription = eventBus.on().listen((event){ - _showPopupDialog( - title: event.title, - body: event.body, - onPositive: event.onPositive, - onNegative: event.onNegative, - positiveText: event.positiveText, - negativeText: event.negativeText - ); - }); - } - if (_showPopupMessageSubscription == null) { - _showPopupMessageSubscription = eventBus.on().listen((event){ - _showPopupDialog( - title: event.title, - body: event.body, - onPositive: event.onButtonClick, - positiveText: event.buttonText, - negativeText: null - ); + if (_showPopupSubscription == null) { + _showPopupSubscription = eventBus.on().listen((event){ + event.popup.show(context); }); } if (_showTokenLoginPopupSubscription == null) { @@ -278,43 +259,6 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker } } - void _showPopupDialog({String title, String body, var onPositive, var onNegative, String positiveText, String negativeText}) { - List buttons = []; - buttons.add(FlatButton( - child: new Text("$positiveText"), - onPressed: () { - Navigator.of(context).pop(); - if (onPositive != null) { - onPositive(); - } - }, - )); - if (negativeText != null) { - buttons.add(FlatButton( - child: new Text("$negativeText"), - onPressed: () { - Navigator.of(context).pop(); - if (onNegative != null) { - onNegative(); - } - }, - )); - } - // flutter defined function - showDialog( - barrierDismissible: false, - context: context, - builder: (BuildContext context) { - // return object of type Dialog - return AlertDialog( - title: new Text("$title"), - content: new Text("$body"), - actions: buttons, - ); - }, - ); - } - final _tokenLoginFormKey = GlobalKey(); void _showTokenLoginDialog() { @@ -360,9 +304,8 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker Row( mainAxisSize: MainAxisSize.min, children: [ - RaisedButton( - child: Text('Login', style: Theme.of(context).textTheme.button.copyWith(fontSize: 20)), - color: Theme.of(context).primaryColor, + FlatButton( + child: Text('Login'), onPressed: () { if (_tokenLoginFormKey.currentState.validate()) { _tokenLoginFormKey.currentState.save(); @@ -370,8 +313,8 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker }, ), Container(width: 10), - RaisedButton( - child: Text('Cancel', style: Theme.of(context).textTheme.button.copyWith(fontSize: 20)), + FlatButton( + child: Text('Cancel'), onPressed: () { Navigator.of(context).pop(); }, @@ -793,8 +736,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker _lovelaceSubscription?.cancel(); _settingsSubscription?.cancel(); _serviceCallSubscription?.cancel(); - _showPopupDialogSubscription?.cancel(); - _showPopupMessageSubscription?.cancel(); + _showPopupSubscription?.cancel(); _showEntityPageSubscription?.cancel(); _showErrorSubscription?.cancel(); _startAuthSubscription?.cancel(); diff --git a/lib/pages/settings/integration_settings.part.dart b/lib/pages/settings/integration_settings.part.dart index 12888d2..5049576 100644 --- a/lib/pages/settings/integration_settings.part.dart +++ b/lib/pages/settings/integration_settings.part.dart @@ -57,26 +57,30 @@ class _IntegrationSettingsPageState extends State { } restart() { - eventBus.fire(ShowPopupDialogEvent( - title: "Are you sure you want to restart Home Assistant?", - body: "This will restart your Home Assistant server.", - positiveText: "Sure. Make it so", - negativeText: "What?? No!", - onPositive: () { - ConnectionManager().callService(domain: "homeassistant", service: "restart"); - }, + eventBus.fire(ShowPopupEvent( + Popup( + title: "Are you sure you want to restart Home Assistant?", + body: "This will restart your Home Assistant server.", + positiveText: "Sure. Make it so", + negativeText: "What?? No!", + onPositive: () { + ConnectionManager().callService(domain: "homeassistant", service: "restart"); + } + ), )); } stop() { - eventBus.fire(ShowPopupDialogEvent( - title: "Are you sure you want to STOP Home Assistant?", - body: "This will STOP your Home Assistant server. It means that your web interface as well as HA Client will not work untill you'll find a way to start your server using ssh or something.", - positiveText: "Sure. Make it so", - negativeText: "What?? No!", - onPositive: () { - ConnectionManager().callService(domain: "homeassistant", service: "stop"); - }, + eventBus.fire(ShowPopupEvent( + Popup( + title: "Are you sure you want to STOP Home Assistant?", + body: "This will STOP your Home Assistant server. It means that your web interface as well as HA Client will not work untill you'll find a way to start your server using ssh or something.", + positiveText: "Sure. Make it so", + negativeText: "What?? No!", + onPositive: () { + ConnectionManager().callService(domain: "homeassistant", service: "stop"); + }, + ) )); } @@ -85,14 +89,16 @@ class _IntegrationSettingsPageState extends State { } resetRegistration() { - eventBus.fire(ShowPopupDialogEvent( - title: "Waaaait", - body: "If you don't whant to have duplicate integrations and entities in your HA for your current device, first you need to remove MobileApp integration from Integration settings in HA and restart server.", - positiveText: "Done it already", - negativeText: "Ok, I will", - onPositive: () { - MobileAppIntegrationManager.checkAppRegistration(showOkDialog: true, forceRegister: true); - }, + eventBus.fire(ShowPopupEvent( + Popup( + title: "Waaaait", + body: "If you don't whant to have duplicate integrations and entities in your HA for your current device, first you need to remove MobileApp integration from Integration settings in HA and restart server.", + positiveText: "Done it already", + negativeText: "Ok, I will", + onPositive: () { + MobileAppIntegrationManager.checkAppRegistration(showOkDialog: true, forceRegister: true); + }, + ) )); } diff --git a/lib/popups.dart b/lib/popups.dart new file mode 100644 index 0000000..3fc9444 --- /dev/null +++ b/lib/popups.dart @@ -0,0 +1,49 @@ +part of 'main.dart'; + +class Popup { + final String title; + final String body; + final String positiveText; + final String negativeText; + final onPositive; + final onNegative; + + Popup({@required this.title, @required this.body, this.positiveText, this.negativeText, this.onPositive, this.onNegative}); + + void show(BuildContext context) { + List buttons = []; + buttons.add(FlatButton( + child: new Text("$positiveText"), + onPressed: () { + Navigator.of(context).pop(); + if (onPositive != null) { + onPositive(); + } + }, + )); + if (negativeText != null) { + buttons.add(FlatButton( + child: new Text("$negativeText"), + onPressed: () { + Navigator.of(context).pop(); + if (onNegative != null) { + onNegative(); + } + }, + )); + } + // flutter defined function + showDialog( + barrierDismissible: false, + context: context, + builder: (BuildContext context) { + // return object of type Dialog + return AlertDialog( + title: new Text("$title"), + content: new Text("$body"), + actions: buttons, + ); + }, + ); + } +} \ No newline at end of file diff --git a/lib/types/event_bus_events.dart b/lib/types/event_bus_events.dart index 24f880d..fe368c0 100644 --- a/lib/types/event_bus_events.dart +++ b/lib/types/event_bus_events.dart @@ -56,6 +56,12 @@ class NotifyServiceCallEvent { NotifyServiceCallEvent(this.domain, this.service, this.entityId); } +class ShowPopupEvent { + final Popup popup; + + ShowPopupEvent(this.popup); +} +/* class ShowPopupDialogEvent { final String title; final String body; @@ -75,6 +81,7 @@ class ShowPopupMessageEvent { ShowPopupMessageEvent({this.title, this.body, this.buttonText: "Ok", this.onButtonClick}); } +*/ class ShowTokenLoginPopupEvent { final bool goBackFirst; diff --git a/pubspec.yaml b/pubspec.yaml index 5c074eb..033a34a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: hass_client description: Home Assistant Android Client -version: 1.0.0+1001 +version: 1.0.0+1002 environment: