Resolves #123 Account details and settings. Get user name from HA
This commit is contained in:
parent
326cd073b9
commit
2fa35d771a
@ -17,12 +17,14 @@ class HomeAssistant {
|
|||||||
EntityCollection _entities;
|
EntityCollection _entities;
|
||||||
ViewBuilder _viewBuilder;
|
ViewBuilder _viewBuilder;
|
||||||
Map _instanceConfig = {};
|
Map _instanceConfig = {};
|
||||||
|
String _userName;
|
||||||
|
|
||||||
Completer _fetchCompleter;
|
Completer _fetchCompleter;
|
||||||
Completer _statesCompleter;
|
Completer _statesCompleter;
|
||||||
Completer _servicesCompleter;
|
Completer _servicesCompleter;
|
||||||
Completer _configCompleter;
|
Completer _configCompleter;
|
||||||
Completer _connectionCompleter;
|
Completer _connectionCompleter;
|
||||||
|
Completer _userInfoCompleter;
|
||||||
Timer _connectionTimer;
|
Timer _connectionTimer;
|
||||||
Timer _fetchTimer;
|
Timer _fetchTimer;
|
||||||
bool autoReconnect = false;
|
bool autoReconnect = false;
|
||||||
@ -33,7 +35,8 @@ class HomeAssistant {
|
|||||||
Duration fetchTimeout = Duration(seconds: 45);
|
Duration fetchTimeout = Duration(seconds: 45);
|
||||||
Duration connectTimeout = Duration(seconds: 15);
|
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;
|
int get viewsCount => _entities.viewList.length ?? 0;
|
||||||
|
|
||||||
EntityCollection get entities => _entities;
|
EntityCollection get entities => _entities;
|
||||||
@ -128,7 +131,7 @@ class HomeAssistant {
|
|||||||
futures.add(_getStates());
|
futures.add(_getStates());
|
||||||
futures.add(_getConfig());
|
futures.add(_getConfig());
|
||||||
futures.add(_getServices());
|
futures.add(_getServices());
|
||||||
//futures.add(_getUserInfo());
|
futures.add(_getUserInfo());
|
||||||
try {
|
try {
|
||||||
await Future.wait(futures);
|
await Future.wait(futures);
|
||||||
_completeFetching(null);
|
_completeFetching(null);
|
||||||
@ -181,7 +184,7 @@ class HomeAssistant {
|
|||||||
} else if (data["id"] == _servicesMessageId) {
|
} else if (data["id"] == _servicesMessageId) {
|
||||||
_parseServices(data);
|
_parseServices(data);
|
||||||
} else if (data["id"] == _userInfoMessageId) {
|
} else if (data["id"] == _userInfoMessageId) {
|
||||||
TheLogger.log("Debug","User ingo: $message");
|
_parseUserInfo(data);
|
||||||
} else if (data["id"] == _currentMessageId) {
|
} else if (data["id"] == _currentMessageId) {
|
||||||
TheLogger.log("Debug","Request id:$_currentMessageId was successful");
|
TheLogger.log("Debug","Request id:$_currentMessageId was successful");
|
||||||
}
|
}
|
||||||
@ -222,13 +225,13 @@ class HomeAssistant {
|
|||||||
return _statesCompleter.future;
|
return _statesCompleter.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _getUserInfo() {
|
Future _getUserInfo() {
|
||||||
//_servicesCompleter = new Completer();
|
_userInfoCompleter = new Completer();
|
||||||
_incrementMessageId();
|
_incrementMessageId();
|
||||||
_userInfoMessageId = _currentMessageId;
|
_userInfoMessageId = _currentMessageId;
|
||||||
_sendMessageRaw('{"id": $_userInfoMessageId, "type": "auth/current_user"}', false);
|
_sendMessageRaw('{"id": $_userInfoMessageId, "type": "auth/current_user"}', false);
|
||||||
|
|
||||||
//return _servicesCompleter.future;
|
return _userInfoCompleter.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future _getServices() {
|
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) {
|
void _parseServices(response) {
|
||||||
_servicesCompleter.complete();
|
_servicesCompleter.complete();
|
||||||
/*if (response["success"] == false) {
|
/*if (response["success"] == false) {
|
||||||
|
@ -101,6 +101,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
StreamSubscription _showErrorSubscription;
|
StreamSubscription _showErrorSubscription;
|
||||||
int _isLoading = 1;
|
int _isLoading = 1;
|
||||||
bool _settingsLoaded = false;
|
bool _settingsLoaded = false;
|
||||||
|
bool _accountMenuExpanded = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -296,15 +297,29 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Drawer _buildAppDrawer() {
|
Drawer _buildAppDrawer() {
|
||||||
return new Drawer(
|
List<Widget> menuItems = [];
|
||||||
child: ListView(
|
menuItems.add(
|
||||||
children: <Widget>[
|
UserAccountsDrawerHeader(
|
||||||
new UserAccountsDrawerHeader(
|
accountName: Text(_homeAssistant.userName),
|
||||||
accountName: Text(_homeAssistant != null ? _homeAssistant.locationName : "Unknown"),
|
|
||||||
accountEmail: Text(_instanceHost ?? "Not configured"),
|
accountEmail: Text(_instanceHost ?? "Not configured"),
|
||||||
currentAccountPicture: new Image.asset('images/hassio-192x192.png'),
|
onDetailsPressed: () {
|
||||||
|
setState(() {
|
||||||
|
_accountMenuExpanded = !_accountMenuExpanded;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
currentAccountPicture: CircleAvatar(
|
||||||
|
child: Text(
|
||||||
|
_homeAssistant.userName[0],
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 32.0
|
||||||
),
|
),
|
||||||
new ListTile(
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
if (_accountMenuExpanded) {
|
||||||
|
menuItems.addAll([
|
||||||
|
ListTile(
|
||||||
leading: Icon(Icons.settings),
|
leading: Icon(Icons.settings),
|
||||||
title: Text("Connection settings"),
|
title: Text("Connection settings"),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
@ -312,17 +327,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
Navigator.of(context).pushNamed('/connection-settings');
|
Navigator.of(context).pushNamed('/connection-settings');
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Container(
|
Divider(),
|
||||||
height: 16.0,
|
]);
|
||||||
decoration: new BoxDecoration(
|
} else {
|
||||||
border: new Border(
|
menuItems.addAll([
|
||||||
bottom: BorderSide(
|
|
||||||
width: 1.0,
|
|
||||||
color: Colors.black26,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
)
|
|
||||||
),
|
|
||||||
new ListTile(
|
new ListTile(
|
||||||
leading: Icon(Icons.insert_drive_file),
|
leading: Icon(Icons.insert_drive_file),
|
||||||
title: Text("Log"),
|
title: Text("Log"),
|
||||||
@ -339,17 +347,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
|
HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Container(
|
Divider(),
|
||||||
height: 16.0,
|
|
||||||
decoration: new BoxDecoration(
|
|
||||||
border: new Border(
|
|
||||||
bottom: BorderSide(
|
|
||||||
width: 1.0,
|
|
||||||
color: Colors.black26,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
)
|
|
||||||
),
|
|
||||||
new AboutListTile(
|
new AboutListTile(
|
||||||
applicationName: appName,
|
applicationName: appName,
|
||||||
applicationVersion: appVersion,
|
applicationVersion: appVersion,
|
||||||
@ -363,7 +361,11 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
HAUtils.launchURL("https://www.buymeacoffee.com/estevez");
|
HAUtils.launchURL("https://www.buymeacoffee.com/estevez");
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
],
|
]);
|
||||||
|
}
|
||||||
|
return new Drawer(
|
||||||
|
child: ListView(
|
||||||
|
children: menuItems,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -451,6 +453,15 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
key: _scaffoldKey,
|
key: _scaffoldKey,
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: _buildAppTitle(),
|
title: _buildAppTitle(),
|
||||||
|
leading: IconButton(
|
||||||
|
icon: Icon(Icons.menu),
|
||||||
|
onPressed: () {
|
||||||
|
_scaffoldKey.currentState.openDrawer();
|
||||||
|
setState(() {
|
||||||
|
_accountMenuExpanded = false;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
bottom: empty ? null : TabBar(
|
bottom: empty ? null : TabBar(
|
||||||
tabs: buildUIViewTabs(),
|
tabs: buildUIViewTabs(),
|
||||||
isScrollable: true,
|
isScrollable: true,
|
||||||
|
Reference in New Issue
Block a user