New bottom info bar

This commit is contained in:
Yegor Vialov 2018-11-18 12:46:54 +02:00
parent 8ee096595c
commit f248268984
3 changed files with 89 additions and 88 deletions

View File

@ -23,21 +23,12 @@ class _EntityViewPageState extends State<EntityViewPage> {
} }
}); });
_prepareData(); _prepareData();
_getHistory();
} }
void _prepareData() async { void _prepareData() async {
_title = widget.entity.displayName; _title = widget.entity.displayName;
} }
void _getHistory() {
/* widget.homeAssistant.getHistory(widget.entity.entityId).then((List history) {
if (history != null) {
}
});*/
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {

View File

@ -4,7 +4,6 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:web_socket_channel/io.dart'; import 'package:web_socket_channel/io.dart';
import 'package:progress_indicators/progress_indicators.dart';
import 'package:event_bus/event_bus.dart'; import 'package:event_bus/event_bus.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
@ -82,7 +81,7 @@ part 'ui_widgets/card_header_widget.dart';
EventBus eventBus = new EventBus(); EventBus eventBus = new EventBus();
const String appName = "HA Client"; const String appName = "HA Client";
const appVersion = "0.3.9"; const appVersion = "0.3.9-68";
String homeAssistantWebHost; String homeAssistantWebHost;
@ -147,15 +146,14 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
StreamSubscription _showEntityPageSubscription; StreamSubscription _showEntityPageSubscription;
StreamSubscription _refreshDataSubscription; StreamSubscription _refreshDataSubscription;
StreamSubscription _showErrorSubscription; StreamSubscription _showErrorSubscription;
int _isLoading = 1; bool _settingsLoaded = false;
//bool _settingsLoaded = false;
bool _accountMenuExpanded = false; bool _accountMenuExpanded = false;
bool _useLovelaceUI; bool _useLovelaceUI;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
//_settingsLoaded = false; _settingsLoaded = false;
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
_homeAssistant = HomeAssistant(); _homeAssistant = HomeAssistant();
@ -176,20 +174,16 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_subscribe(); _subscribe();
_refreshData(); _refreshData();
}, onError: (_) { }, onError: (_) {
setState(() { _showErrorBottomBar(message: _, errorCode: 5);
_isLoading = 2;
});
_showErrorSnackBar(message: _, errorCode: 5);
}); });
} }
@override @override
void didChangeAppLifecycleState(AppLifecycleState state) { void didChangeAppLifecycleState(AppLifecycleState state) {
TheLogger.debug("$state"); TheLogger.debug("$state");
/*if (state == AppLifecycleState.resumed && _settingsLoaded && (_homeAssistant == null || _homeAssistant.entities == null)) { if (state == AppLifecycleState.resumed && _settingsLoaded) {
TheLogger.debug("No data. Fetching...");
_refreshData(); _refreshData();
}*/ }
} }
_loadConnectionSettings() async { _loadConnectionSettings() async {
@ -205,9 +199,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
if ((domain == null) || (port == null) || (_password == null) || if ((domain == null) || (port == null) || (_password == null) ||
(domain.length == 0) || (port.length == 0) || (_password.length == 0)) { (domain.length == 0) || (port.length == 0) || (_password.length == 0)) {
throw("Check connection settings"); throw("Check connection settings");
}/* else { } else {
_settingsLoaded = true; _settingsLoaded = true;
}*/ }
} }
_subscribe() { _subscribe() {
@ -239,21 +233,17 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
if (_showErrorSubscription == null) { if (_showErrorSubscription == null) {
_showErrorSubscription = eventBus.on<ShowErrorEvent>().listen((event){ _showErrorSubscription = eventBus.on<ShowErrorEvent>().listen((event){
_showErrorSnackBar(message: event.text, errorCode: event.errorCode); _showErrorBottomBar(message: event.text, errorCode: event.errorCode);
}); });
} }
} }
_refreshData() async { _refreshData() async {
_homeAssistant.updateSettings(_webSocketApiEndpoint, _password, _authType, _useLovelaceUI); _homeAssistant.updateSettings(_webSocketApiEndpoint, _password, _authType, _useLovelaceUI);
setState(() { _hideBottomBar();
_hideErrorSnackBar(); _showInfoBottomBar(message: "Refreshing data...");
_isLoading = 1;
});
await _homeAssistant.fetch().then((result) { await _homeAssistant.fetch().then((result) {
setState(() { _hideBottomBar();
_isLoading = 0;
});
}).catchError((e) { }).catchError((e) {
_setErrorState(e); _setErrorState(e);
}); });
@ -261,18 +251,15 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
} }
_setErrorState(e) { _setErrorState(e) {
setState(() {
_isLoading = 2;
});
if (e is Error) { if (e is Error) {
TheLogger.error(e.toString()); TheLogger.error(e.toString());
TheLogger.error("${e.stackTrace}"); TheLogger.error("${e.stackTrace}");
_showErrorSnackBar( _showErrorBottomBar(
message: "There was some error", message: "There was some error",
errorCode: 13 errorCode: 13
); );
} else { } else {
_showErrorSnackBar( _showErrorBottomBar(
message: e != null ? e["errorMessage"] ?? "$e" : "Unknown error", message: e != null ? e["errorMessage"] ?? "$e" : "Unknown error",
errorCode: e["errorCode"] != null ? e["errorCode"] : 99 errorCode: e["errorCode"] != null ? e["errorCode"] : 99
); );
@ -304,31 +291,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
return result; return result;
} }
Widget _buildAppTitle() {
Row titleRow = Row(
children: [Text(_homeAssistant != null ? _homeAssistant.locationName : "")],
);
if (_isLoading == 1) {
titleRow.children.add(Padding(
child: JumpingDotsProgressIndicator(
fontSize: 26.0,
color: Colors.white,
),
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;
}
Drawer _buildAppDrawer() { Drawer _buildAppDrawer() {
List<Widget> menuItems = []; List<Widget> menuItems = [];
menuItems.add( menuItems.add(
@ -410,21 +372,42 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
); );
} }
void _hideErrorSnackBar() { void _hideBottomBar() {
_scaffoldKey?.currentState?.hideCurrentSnackBar(); //_scaffoldKey?.currentState?.hideCurrentSnackBar();
setState(() {
_showBottomBar = false;
});
} }
void _showErrorSnackBar({Key key, @required String message, @required int errorCode}) { Widget _bottomBarAction;
SnackBarAction action; bool _showBottomBar = false;
String _bottomBarText;
Color _bottomBarColor;
void _showInfoBottomBar({@required String message}) {
_bottomBarAction = Container(height: 0.0, width: 0.0,);
_bottomBarColor = Colors.blue.shade100;
setState(() {
_bottomBarText = "$message";
_showBottomBar = true;
});
}
void _showErrorBottomBar({Key key, @required String message, @required int errorCode}) {
TextStyle textStyle = TextStyle(
color: Colors.blue,
fontSize: Sizes.nameFontSize
);
_bottomBarColor = Colors.red.shade100;
switch (errorCode) { switch (errorCode) {
case 9: case 9:
case 11: case 11:
case 7: case 7:
case 1: { case 1: {
action = SnackBarAction( _bottomBarAction = FlatButton(
label: "Retry", child: Text("Retry", style: textStyle),
onPressed: () { onPressed: () {
_scaffoldKey?.currentState?.hideCurrentSnackBar(); //_scaffoldKey?.currentState?.hideCurrentSnackBar();
_refreshData(); _refreshData();
}, },
); );
@ -433,10 +416,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
case 5: { case 5: {
message = "Check connection settings"; message = "Check connection settings";
action = SnackBarAction( _bottomBarAction = FlatButton(
label: "Open", child: Text("Open", style: textStyle),
onPressed: () { onPressed: () {
_scaffoldKey?.currentState?.hideCurrentSnackBar(); //_scaffoldKey?.currentState?.hideCurrentSnackBar();
Navigator.pushNamed(context, '/connection-settings'); Navigator.pushNamed(context, '/connection-settings');
}, },
); );
@ -444,10 +427,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
} }
case 6: { case 6: {
action = SnackBarAction( _bottomBarAction = FlatButton(
label: "Settings", child: Text("Settings", style: textStyle),
onPressed: () { onPressed: () {
_scaffoldKey?.currentState?.hideCurrentSnackBar(); //_scaffoldKey?.currentState?.hideCurrentSnackBar();
Navigator.pushNamed(context, '/connection-settings'); Navigator.pushNamed(context, '/connection-settings');
}, },
); );
@ -455,10 +438,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
} }
case 10: { case 10: {
action = SnackBarAction( _bottomBarAction = FlatButton(
label: "Refresh", child: Text("Refresh", style: textStyle),
onPressed: () { onPressed: () {
_scaffoldKey?.currentState?.hideCurrentSnackBar(); //_scaffoldKey?.currentState?.hideCurrentSnackBar();
_refreshData(); _refreshData();
}, },
); );
@ -466,10 +449,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
} }
case 8: { case 8: {
action = SnackBarAction( _bottomBarAction = FlatButton(
label: "Reconnect", child: Text("Reconnect", style: textStyle),
onPressed: () { onPressed: () {
_scaffoldKey?.currentState?.hideCurrentSnackBar(); //_scaffoldKey?.currentState?.hideCurrentSnackBar();
_refreshData(); _refreshData();
}, },
); );
@ -477,24 +460,28 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
} }
default: { default: {
action = SnackBarAction( _bottomBarAction = FlatButton(
label: "Reload", child: Text("Reload", style: textStyle),
onPressed: () { onPressed: () {
_scaffoldKey?.currentState?.hideCurrentSnackBar(); //_scaffoldKey?.currentState?.hideCurrentSnackBar();
_refreshData(); _refreshData();
}, },
); );
break; break;
} }
} }
_scaffoldKey.currentState.hideCurrentSnackBar(); setState(() {
_bottomBarText = "$message (code: $errorCode)";
_showBottomBar = true;
});
/*_scaffoldKey.currentState.hideCurrentSnackBar();
_scaffoldKey.currentState.showSnackBar( _scaffoldKey.currentState.showSnackBar(
SnackBar( SnackBar(
content: Text("$message (code: $errorCode)"), content: Text("$message (code: $errorCode)"),
action: action, action: action,
duration: Duration(hours: 1), duration: Duration(hours: 1),
) )
); );*/
} }
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@ -507,7 +494,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
floating: true, floating: true,
pinned: true, pinned: true,
primary: true, primary: true,
title: _buildAppTitle(), title: Text(_homeAssistant != null ? _homeAssistant.locationName : ""),
leading: IconButton( leading: IconButton(
icon: Icon(Icons.menu), icon: Icon(Icons.menu),
onPressed: () { onPressed: () {
@ -533,7 +520,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: _isLoading == 2 ? Colors.redAccent : Colors.blue, color: Colors.blue,
), ),
] ]
), ),
@ -545,12 +532,34 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Widget bottomBar;
if (_showBottomBar) {
bottomBar = Container(
color: _bottomBarColor,
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, Sizes.rowPadding, 0.0, Sizes.rowPadding),
child: Text(
"$_bottomBarText",
softWrap: true,
),
),
),
_bottomBarAction
],
),
);
}
// This method is rerun every time setState is called. // This method is rerun every time setState is called.
if (_homeAssistant.ui == null || _homeAssistant.ui.views == null) { if (_homeAssistant.ui == null || _homeAssistant.ui.views == null) {
return Scaffold( return Scaffold(
key: _scaffoldKey, key: _scaffoldKey,
primary: false, primary: false,
drawer: _buildAppDrawer(), drawer: _buildAppDrawer(),
bottomNavigationBar: bottomBar,
body: _buildScaffoldBody(true) body: _buildScaffoldBody(true)
); );
} else { } else {
@ -558,6 +567,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
key: _scaffoldKey, key: _scaffoldKey,
drawer: _buildAppDrawer(), drawer: _buildAppDrawer(),
primary: false, primary: false,
bottomNavigationBar: bottomBar,
body: DefaultTabController( body: DefaultTabController(
length: _homeAssistant.ui?.views?.length ?? 0, length: _homeAssistant.ui?.views?.length ?? 0,
child: _buildScaffoldBody(false), child: _buildScaffoldBody(false),

View File

@ -1,7 +1,7 @@
name: hass_client name: hass_client
description: Home Assistant Android Client description: Home Assistant Android Client
version: 0.3.9+67 version: 0.3.9+68
environment: environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0" sdk: ">=2.0.0-dev.68.0 <3.0.0"