This commit is contained in:
Yegor Vialov 2020-05-03 10:30:51 +00:00
parent e9ad612fec
commit 5bc0b0868a
8 changed files with 155 additions and 137 deletions

View File

@ -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 'entities/media_player/widgets/media_player_progress_bar.widget.dart';
part 'pages/whats_new.page.dart'; part 'pages/whats_new.page.dart';
part 'pages/fullscreen.page.dart'; part 'pages/fullscreen.page.dart';
part 'popups.dart';
EventBus eventBus = new EventBus(); EventBus eventBus = new EventBus();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@ -236,10 +237,12 @@ class _HAClientAppState extends State<HAClientApp> {
void _handlePurchaseUpdates(purchase) { void _handlePurchaseUpdates(purchase) {
if (purchase is List<PurchaseDetails>) { if (purchase is List<PurchaseDetails>) {
if (purchase[0].status == PurchaseStatus.purchased) { if (purchase[0].status == PurchaseStatus.purchased) {
eventBus.fire(ShowPopupMessageEvent( eventBus.fire(ShowPopupEvent(
Popup(
title: "Thanks a lot!", title: "Thanks a lot!",
body: "Thank you for supporting HA Client development!", body: "Thank you for supporting HA Client development!",
buttonText: "Ok" positiveText: "Ok"
)
)); ));
} else { } else {
Logger.d("Purchase change handler: ${purchase[0].status}"); Logger.d("Purchase change handler: ${purchase[0].status}");

View File

@ -51,14 +51,16 @@ class MobileAppIntegrationManager {
prefs.setInt('app-integration-version', INTEGRATION_VERSION); prefs.setInt('app-integration-version', INTEGRATION_VERSION);
completer.complete(); completer.complete();
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
title: "Mobile app Integration was created", Popup(
body: "HA Client was registered as MobileApp in your Home Assistant. To start using notifications you need to restart your Home Assistant", title: "Mobile app Integration was created",
positiveText: "Restart now", body: "HA Client was registered as MobileApp in your Home Assistant. To start using notifications you need to restart your Home Assistant",
negativeText: "Later", positiveText: "Restart now",
onPositive: () { negativeText: "Later",
ConnectionManager().callService(domain: "homeassistant", service: "restart"); onPositive: () {
}, ConnectionManager().callService(domain: "homeassistant", service: "restart");
},
)
)); ));
}); });
}).catchError((e) { }).catchError((e) {
@ -93,11 +95,12 @@ class MobileAppIntegrationManager {
} else { } else {
Logger.d('App registration works fine'); Logger.d('App registration works fine');
if (showOkDialog) { if (showOkDialog) {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
Popup(
title: "All good", title: "All good",
body: "HA Client integration with your Home Assistant server works fine", body: "HA Client integration with your Home Assistant server works fine",
positiveText: "Nice!", positiveText: "Ok"
negativeText: "Ok" )
)); ));
} }
} }
@ -109,17 +112,19 @@ class MobileAppIntegrationManager {
_askToRegisterApp(); _askToRegisterApp();
} else { } else {
Logger.e("Error updating app registration: $e"); Logger.e("Error updating app registration: $e");
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
title: "App integration is not working properly", Popup(
body: "Something wrong with HA Client integration on your Home Assistant server. Please report this issue.", title: "App integration is not working properly",
positiveText: "Report to GitHub", body: "Something wrong with HA Client integration on your Home Assistant server. Please report this issue.",
negativeText: "Report to Discord", positiveText: "Report to GitHub",
onPositive: () { negativeText: "Report to Discord",
Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new"); onPositive: () {
}, Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new");
onNegative: () { },
Launcher.launchURLInBrowser("https://discord.gg/AUzEvwn"); onNegative: () {
}, Launcher.launchURLInBrowser("https://discord.gg/AUzEvwn");
},
)
)); ));
} }
completer.complete(); completer.complete();
@ -129,30 +134,34 @@ class MobileAppIntegrationManager {
} }
static void _askToRemoveAndRegisterApp() { static void _askToRemoveAndRegisterApp() {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
title: "Mobile app integration needs to be updated", Popup(
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.", title: "Mobile app integration needs to be updated",
positiveText: "Ok", 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.",
negativeText: "Report an issue", positiveText: "Ok",
onNegative: () { negativeText: "Report an issue",
Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new"); onNegative: () {
}, Launcher.launchURLInBrowser("https://github.com/estevez-dev/ha_client/issues/new");
},
)
)); ));
} }
static void _askToRegisterApp() { static void _askToRegisterApp() {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
title: "App integration is broken", Popup(
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.", title: "App integration is broken",
positiveText: "Register now", 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.",
negativeText: "Cancel", positiveText: "Register now",
onPositive: () { negativeText: "Cancel",
SharedPreferences.getInstance().then((prefs) { onPositive: () {
prefs.remove("app-webhook-id"); SharedPreferences.getInstance().then((prefs) {
ConnectionManager().webhookId = null; prefs.remove("app-webhook-id");
checkAppRegistration(); ConnectionManager().webhookId = null;
}); checkAppRegistration();
}, });
},
)
)); ));
} }

View File

@ -29,7 +29,8 @@ class StartupUserMessagesManager {
} }
void _showSupportAppDevelopmentMessage() { void _showSupportAppDevelopmentMessage() {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
Popup(
title: "Hi!", 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.", 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", positiveText: "Show options",
@ -45,6 +46,7 @@ class StartupUserMessagesManager {
prefs.setBool(_supportAppDevelopmentMessageKey, true); prefs.setBool(_supportAppDevelopmentMessageKey, true);
}); });
} }
)
)); ));
} }

View File

@ -18,8 +18,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
StreamSubscription _showEntityPageSubscription; StreamSubscription _showEntityPageSubscription;
StreamSubscription _showErrorSubscription; StreamSubscription _showErrorSubscription;
StreamSubscription _startAuthSubscription; StreamSubscription _startAuthSubscription;
StreamSubscription _showPopupDialogSubscription; StreamSubscription _showPopupSubscription;
StreamSubscription _showPopupMessageSubscription;
StreamSubscription _showTokenLoginPopupSubscription; StreamSubscription _showTokenLoginPopupSubscription;
StreamSubscription _reloadUISubscription; StreamSubscription _reloadUISubscription;
StreamSubscription _fullReloadSubscription; StreamSubscription _fullReloadSubscription;
@ -183,27 +182,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_fullLoad(); _fullLoad();
}); });
} }
if (_showPopupDialogSubscription == null) { if (_showPopupSubscription == null) {
_showPopupDialogSubscription = eventBus.on<ShowPopupDialogEvent>().listen((event){ _showPopupSubscription = eventBus.on<ShowPopupEvent>().listen((event){
_showPopupDialog( event.popup.show(context);
title: event.title,
body: event.body,
onPositive: event.onPositive,
onNegative: event.onNegative,
positiveText: event.positiveText,
negativeText: event.negativeText
);
});
}
if (_showPopupMessageSubscription == null) {
_showPopupMessageSubscription = eventBus.on<ShowPopupMessageEvent>().listen((event){
_showPopupDialog(
title: event.title,
body: event.body,
onPositive: event.onButtonClick,
positiveText: event.buttonText,
negativeText: null
);
}); });
} }
if (_showTokenLoginPopupSubscription == null) { if (_showTokenLoginPopupSubscription == null) {
@ -278,43 +259,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
} }
} }
void _showPopupDialog({String title, String body, var onPositive, var onNegative, String positiveText, String negativeText}) {
List<Widget> 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<FormState>(); final _tokenLoginFormKey = GlobalKey<FormState>();
void _showTokenLoginDialog() { void _showTokenLoginDialog() {
@ -360,9 +304,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
Row( Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
RaisedButton( FlatButton(
child: Text('Login', style: Theme.of(context).textTheme.button.copyWith(fontSize: 20)), child: Text('Login'),
color: Theme.of(context).primaryColor,
onPressed: () { onPressed: () {
if (_tokenLoginFormKey.currentState.validate()) { if (_tokenLoginFormKey.currentState.validate()) {
_tokenLoginFormKey.currentState.save(); _tokenLoginFormKey.currentState.save();
@ -370,8 +313,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
}, },
), ),
Container(width: 10), Container(width: 10),
RaisedButton( FlatButton(
child: Text('Cancel', style: Theme.of(context).textTheme.button.copyWith(fontSize: 20)), child: Text('Cancel'),
onPressed: () { onPressed: () {
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
@ -793,8 +736,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_lovelaceSubscription?.cancel(); _lovelaceSubscription?.cancel();
_settingsSubscription?.cancel(); _settingsSubscription?.cancel();
_serviceCallSubscription?.cancel(); _serviceCallSubscription?.cancel();
_showPopupDialogSubscription?.cancel(); _showPopupSubscription?.cancel();
_showPopupMessageSubscription?.cancel();
_showEntityPageSubscription?.cancel(); _showEntityPageSubscription?.cancel();
_showErrorSubscription?.cancel(); _showErrorSubscription?.cancel();
_startAuthSubscription?.cancel(); _startAuthSubscription?.cancel();

View File

@ -57,26 +57,30 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
} }
restart() { restart() {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
title: "Are you sure you want to restart Home Assistant?", Popup(
body: "This will restart your Home Assistant server.", title: "Are you sure you want to restart Home Assistant?",
positiveText: "Sure. Make it so", body: "This will restart your Home Assistant server.",
negativeText: "What?? No!", positiveText: "Sure. Make it so",
onPositive: () { negativeText: "What?? No!",
ConnectionManager().callService(domain: "homeassistant", service: "restart"); onPositive: () {
}, ConnectionManager().callService(domain: "homeassistant", service: "restart");
}
),
)); ));
} }
stop() { stop() {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
title: "Are you sure you want to STOP Home Assistant?", Popup(
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.", title: "Are you sure you want to STOP Home Assistant?",
positiveText: "Sure. Make it so", 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.",
negativeText: "What?? No!", positiveText: "Sure. Make it so",
onPositive: () { negativeText: "What?? No!",
ConnectionManager().callService(domain: "homeassistant", service: "stop"); onPositive: () {
}, ConnectionManager().callService(domain: "homeassistant", service: "stop");
},
)
)); ));
} }
@ -85,14 +89,16 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
} }
resetRegistration() { resetRegistration() {
eventBus.fire(ShowPopupDialogEvent( eventBus.fire(ShowPopupEvent(
title: "Waaaait", Popup(
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.", title: "Waaaait",
positiveText: "Done it already", 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.",
negativeText: "Ok, I will", positiveText: "Done it already",
onPositive: () { negativeText: "Ok, I will",
MobileAppIntegrationManager.checkAppRegistration(showOkDialog: true, forceRegister: true); onPositive: () {
}, MobileAppIntegrationManager.checkAppRegistration(showOkDialog: true, forceRegister: true);
},
)
)); ));
} }

49
lib/popups.dart Normal file
View File

@ -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<Widget> 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,
);
},
);
}
}

View File

@ -56,6 +56,12 @@ class NotifyServiceCallEvent {
NotifyServiceCallEvent(this.domain, this.service, this.entityId); NotifyServiceCallEvent(this.domain, this.service, this.entityId);
} }
class ShowPopupEvent {
final Popup popup;
ShowPopupEvent(this.popup);
}
/*
class ShowPopupDialogEvent { class ShowPopupDialogEvent {
final String title; final String title;
final String body; final String body;
@ -75,6 +81,7 @@ class ShowPopupMessageEvent {
ShowPopupMessageEvent({this.title, this.body, this.buttonText: "Ok", this.onButtonClick}); ShowPopupMessageEvent({this.title, this.body, this.buttonText: "Ok", this.onButtonClick});
} }
*/
class ShowTokenLoginPopupEvent { class ShowTokenLoginPopupEvent {
final bool goBackFirst; final bool goBackFirst;

View File

@ -1,7 +1,7 @@
name: hass_client name: hass_client
description: Home Assistant Android Client description: Home Assistant Android Client
version: 1.0.0+1001 version: 1.0.0+1002
environment: environment: