Showing error snakbar improvements. Error icon in header

This commit is contained in:
Yegor Vialov 2018-10-07 10:28:28 +03:00
parent 4085006446
commit bc1f4eab2e
2 changed files with 46 additions and 35 deletions

View File

@ -97,7 +97,9 @@ class HomeAssistant {
onDone: () { onDone: () {
TheLogger.log("Debug","Disconnect detected. Reconnecting..."); TheLogger.log("Debug","Disconnect detected. Reconnecting...");
disconnect().then((_) { disconnect().then((_) {
_connection(); _connection().catchError((e){
_completeConnecting(e);
});
}); });
}, },
onError: (e) { onError: (e) {
@ -148,6 +150,8 @@ class HomeAssistant {
} else { } else {
_connectionCompleter.complete(); _connectionCompleter.complete();
} }
} else if (error != null) {
eventBus.fire(ShowErrorEvent(error["errorMessage"], error["errorCode"]));
} }
} }

View File

@ -92,14 +92,13 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
String _authType; String _authType;
int _uiViewsCount = 0; int _uiViewsCount = 0;
String _instanceHost; String _instanceHost;
int _errorCodeToBeShown = 0;
String _lastErrorMessage = "";
StreamSubscription _stateSubscription; StreamSubscription _stateSubscription;
StreamSubscription _settingsSubscription; StreamSubscription _settingsSubscription;
StreamSubscription _serviceCallSubscription; StreamSubscription _serviceCallSubscription;
StreamSubscription _showEntityPageSubscription; StreamSubscription _showEntityPageSubscription;
StreamSubscription _refreshDataSubscription; StreamSubscription _refreshDataSubscription;
bool _isLoading = true; StreamSubscription _showErrorSubscription;
int _isLoading = 1;
bool _settingsLoaded = false; bool _settingsLoaded = false;
@override @override
@ -118,9 +117,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_refreshData(); _refreshData();
}, onError: (_) { }, onError: (_) {
setState(() { setState(() {
_lastErrorMessage = _; _isLoading = 2;
_errorCodeToBeShown = 5;
}); });
_showErrorSnackBar(message: _, errorCode: 5);
} }
); );
}); });
@ -130,9 +129,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_createConnection(); _createConnection();
}, onError: (_) { }, onError: (_) {
setState(() { setState(() {
_lastErrorMessage = _; _isLoading = 2;
_errorCodeToBeShown = 5;
}); });
_showErrorSnackBar(message: _, errorCode: 5);
}); });
} }
@ -194,21 +193,25 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_refreshData(); _refreshData();
}); });
} }
if (_showErrorSubscription == null) {
_showErrorSubscription = eventBus.on<ShowErrorEvent>().listen((event){
_showErrorSnackBar(message: event.text, errorCode: event.errorCode);
});
}
} }
_refreshData() async { _refreshData() async {
_homeAssistant.updateConnectionSettings(_apiEndpoint, _apiPassword, _authType); _homeAssistant.updateConnectionSettings(_apiEndpoint, _apiPassword, _authType);
setState(() { setState(() {
_isLoading = true; _isLoading = 1;
}); });
_errorCodeToBeShown = 0;
_lastErrorMessage = "";
await _homeAssistant.fetch().then((result) { await _homeAssistant.fetch().then((result) {
setState(() { setState(() {
//_instanceConfig = _homeAssistant.instanceConfig; //_instanceConfig = _homeAssistant.instanceConfig;
_entities = _homeAssistant.entities; _entities = _homeAssistant.entities;
_uiViewsCount = _homeAssistant.viewsCount; _uiViewsCount = _homeAssistant.viewsCount;
_isLoading = false; _isLoading = 0;
}); });
}).catchError((e) { }).catchError((e) {
_setErrorState(e); _setErrorState(e);
@ -218,10 +221,12 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_setErrorState(e) { _setErrorState(e) {
setState(() { setState(() {
_errorCodeToBeShown = e["errorCode"] != null ? e["errorCode"] : 99; _isLoading = 2;
_lastErrorMessage = e["errorMessage"] ?? "Unknown error";
_isLoading = false;
}); });
_showErrorSnackBar(
message: e != null ? e["errorMessage"] ?? "$e" : "Unknown error",
errorCode: e["errorCode"] != null ? e["errorCode"] : 99
);
} }
void _callService(String domain, String service, String entityId, Map<String, String> additionalParams) { void _callService(String domain, String service, String entityId, Map<String, String> additionalParams) {
@ -271,7 +276,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
Row titleRow = Row( Row titleRow = Row(
children: [Text(_homeAssistant != null ? _homeAssistant.locationName : "")], children: [Text(_homeAssistant != null ? _homeAssistant.locationName : "")],
); );
if (_isLoading) { if (_isLoading == 1) {
titleRow.children.add(Padding( titleRow.children.add(Padding(
child: JumpingDotsProgressIndicator( child: JumpingDotsProgressIndicator(
fontSize: 26.0, fontSize: 26.0,
@ -279,6 +284,15 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
), ),
padding: const EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 30.0), padding: const EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 30.0),
)); ));
} else if (_isLoading == 2) {
titleRow.children.add(Padding(
child: Icon(
Icons.error_outline,
size: 20.0,
color: Colors.red,
),
padding: const EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 0.0),
));
} }
return titleRow; return titleRow;
} }
@ -356,11 +370,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
); );
} }
_checkShowInfo() { _showErrorSnackBar({Key key, @required String message, @required int errorCode}) {
if (_errorCodeToBeShown > 0) {
String message = _lastErrorMessage;
SnackBarAction action; SnackBarAction action;
switch (_errorCodeToBeShown) { switch (errorCode) {
case 9: case 9:
case 11: case 11:
case 7: case 7:
@ -420,19 +432,14 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
break; break;
} }
} }
Timer(Duration(seconds: 1), () {
_scaffoldKey.currentState.hideCurrentSnackBar(); _scaffoldKey.currentState.hideCurrentSnackBar();
_scaffoldKey.currentState.showSnackBar( _scaffoldKey.currentState.showSnackBar(
SnackBar( SnackBar(
content: Text("$message (code: $_errorCodeToBeShown)"), content: Text("$message (code: $errorCode)"),
action: action, action: action,
duration: Duration(hours: 1), duration: Duration(hours: 1),
) )
); );
});
} else {
_scaffoldKey?.currentState?.hideCurrentSnackBar();
}
} }
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@ -453,7 +460,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
Icon( Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"), MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"),
size: 100.0, size: 100.0,
color: _errorCodeToBeShown == 0 ? Colors.blue : Colors.redAccent, color: _isLoading == 2 ? Colors.redAccent : Colors.blue,
), ),
] ]
), ),
@ -465,7 +472,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
_checkShowInfo();
// This method is rerun every time setState is called. // This method is rerun every time setState is called.
if (_entities == null) { if (_entities == null) {
return _buildScaffold(true); return _buildScaffold(true);
@ -485,6 +491,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
if (_serviceCallSubscription != null) _serviceCallSubscription.cancel(); if (_serviceCallSubscription != null) _serviceCallSubscription.cancel();
if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel(); if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel();
if (_refreshDataSubscription != null) _refreshDataSubscription.cancel(); if (_refreshDataSubscription != null) _refreshDataSubscription.cancel();
if (_showErrorSubscription != null) _showErrorSubscription.cancel();
_homeAssistant.disconnect(); _homeAssistant.disconnect();
super.dispose(); super.dispose();
} }