Resolves #123 Account details and settings. Get user name from HA

This commit is contained in:
Yegor Vialov 2018-10-07 20:18:14 +03:00
parent 326cd073b9
commit 2fa35d771a
2 changed files with 96 additions and 72 deletions

View File

@ -17,12 +17,14 @@ class HomeAssistant {
EntityCollection _entities;
ViewBuilder _viewBuilder;
Map _instanceConfig = {};
String _userName;
Completer _fetchCompleter;
Completer _statesCompleter;
Completer _servicesCompleter;
Completer _configCompleter;
Completer _connectionCompleter;
Completer _userInfoCompleter;
Timer _connectionTimer;
Timer _fetchTimer;
bool autoReconnect = false;
@ -33,7 +35,8 @@ class HomeAssistant {
Duration fetchTimeout = Duration(seconds: 45);
Duration connectTimeout = Duration(seconds: 15);
String get locationName => _instanceConfig["location_name"] ?? "";
String get locationName => _instanceConfig["location_name"] ?? "...";
String get userName => _userName ?? locationName;
int get viewsCount => _entities.viewList.length ?? 0;
EntityCollection get entities => _entities;
@ -128,7 +131,7 @@ class HomeAssistant {
futures.add(_getStates());
futures.add(_getConfig());
futures.add(_getServices());
//futures.add(_getUserInfo());
futures.add(_getUserInfo());
try {
await Future.wait(futures);
_completeFetching(null);
@ -181,7 +184,7 @@ class HomeAssistant {
} else if (data["id"] == _servicesMessageId) {
_parseServices(data);
} else if (data["id"] == _userInfoMessageId) {
TheLogger.log("Debug","User ingo: $message");
_parseUserInfo(data);
} else if (data["id"] == _currentMessageId) {
TheLogger.log("Debug","Request id:$_currentMessageId was successful");
}
@ -222,13 +225,13 @@ class HomeAssistant {
return _statesCompleter.future;
}
void _getUserInfo() {
//_servicesCompleter = new Completer();
Future _getUserInfo() {
_userInfoCompleter = new Completer();
_incrementMessageId();
_userInfoMessageId = _currentMessageId;
_sendMessageRaw('{"id": $_userInfoMessageId, "type": "auth/current_user"}', false);
//return _servicesCompleter.future;
return _userInfoCompleter.future;
}
Future _getServices() {
@ -295,6 +298,16 @@ class HomeAssistant {
}
}
void _parseUserInfo(Map data) {
TheLogger.log("Debug", "$data");
if (data["success"] == true) {
_userName = data["result"]["name"];
} else {
_userName = null;
}
_userInfoCompleter.complete();
}
void _parseServices(response) {
_servicesCompleter.complete();
/*if (response["success"] == false) {

View File

@ -101,6 +101,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
StreamSubscription _showErrorSubscription;
int _isLoading = 1;
bool _settingsLoaded = false;
bool _accountMenuExpanded = false;
@override
void initState() {
@ -296,74 +297,75 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
}
Drawer _buildAppDrawer() {
List<Widget> menuItems = [];
menuItems.add(
UserAccountsDrawerHeader(
accountName: Text(_homeAssistant.userName),
accountEmail: Text(_instanceHost ?? "Not configured"),
onDetailsPressed: () {
setState(() {
_accountMenuExpanded = !_accountMenuExpanded;
});
},
currentAccountPicture: CircleAvatar(
child: Text(
_homeAssistant.userName[0],
style: TextStyle(
fontSize: 32.0
),
),
),
)
);
if (_accountMenuExpanded) {
menuItems.addAll([
ListTile(
leading: Icon(Icons.settings),
title: Text("Connection settings"),
onTap: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed('/connection-settings');
},
),
Divider(),
]);
} else {
menuItems.addAll([
new ListTile(
leading: Icon(Icons.insert_drive_file),
title: Text("Log"),
onTap: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed('/log-view');
},
),
new ListTile(
leading: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:github-circle")),
title: Text("Report an issue"),
onTap: () {
Navigator.of(context).pop();
HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
},
),
Divider(),
new AboutListTile(
applicationName: appName,
applicationVersion: appVersion,
applicationLegalese: "Keyboard Crumbs | www.keyboardcrumbs.io",
),
new ListTile(
leading: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:coffee")),
title: Text("Buy me a coffee"),
onTap: () {
Navigator.of(context).pop();
HAUtils.launchURL("https://www.buymeacoffee.com/estevez");
},
)
]);
}
return new Drawer(
child: ListView(
children: <Widget>[
new UserAccountsDrawerHeader(
accountName: Text(_homeAssistant != null ? _homeAssistant.locationName : "Unknown"),
accountEmail: Text(_instanceHost ?? "Not configured"),
currentAccountPicture: new Image.asset('images/hassio-192x192.png'),
),
new ListTile(
leading: Icon(Icons.settings),
title: Text("Connection settings"),
onTap: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed('/connection-settings');
},
),
Container(
height: 16.0,
decoration: new BoxDecoration(
border: new Border(
bottom: BorderSide(
width: 1.0,
color: Colors.black26,
)
),
)
),
new ListTile(
leading: Icon(Icons.insert_drive_file),
title: Text("Log"),
onTap: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed('/log-view');
},
),
new ListTile(
leading: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:github-circle")),
title: Text("Report an issue"),
onTap: () {
Navigator.of(context).pop();
HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
},
),
Container(
height: 16.0,
decoration: new BoxDecoration(
border: new Border(
bottom: BorderSide(
width: 1.0,
color: Colors.black26,
)
),
)
),
new AboutListTile(
applicationName: appName,
applicationVersion: appVersion,
applicationLegalese: "Keyboard Crumbs | www.keyboardcrumbs.io",
),
new ListTile(
leading: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:coffee")),
title: Text("Buy me a coffee"),
onTap: () {
Navigator.of(context).pop();
HAUtils.launchURL("https://www.buymeacoffee.com/estevez");
},
)
],
children: menuItems,
),
);
}
@ -451,6 +453,15 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
key: _scaffoldKey,
appBar: AppBar(
title: _buildAppTitle(),
leading: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
setState(() {
_accountMenuExpanded = false;
});
},
),
bottom: empty ? null : TabBar(
tabs: buildUIViewTabs(),
isScrollable: true,