This commit is contained in:
estevez-dev
2019-03-20 23:38:57 +02:00
parent 67d7bb45f5
commit 20d3498bfd
3 changed files with 33 additions and 28 deletions

View File

@ -35,6 +35,10 @@ class EntityCollection {
}); });
} }
void clear() {
_allEntities.clear();
}
Entity _createEntityInstance(rawEntityData) { Entity _createEntityInstance(rawEntityData) {
switch (rawEntityData["entity_id"].split(".")[0]) { switch (rawEntityData["entity_id"].split(".")[0]) {
case 'sun': { case 'sun': {

View File

@ -242,7 +242,7 @@ class HomeAssistant {
_sendSubscribe(); _sendSubscribe();
} else if (data["type"] == "auth_invalid") { } else if (data["type"] == "auth_invalid") {
Logger.d("[Received] <== ${data.toString()}"); Logger.d("[Received] <== ${data.toString()}");
_logout(); logout();
_completeConnecting({"errorCode": 62, "errorMessage": "${data["message"]}"}); _completeConnecting({"errorCode": 62, "errorMessage": "${data["message"]}"});
} else if (data["type"] == "result") { } else if (data["type"] == "result") {
Logger.d("[Received] <== id: ${data['id']}, success: ${data['success']}"); Logger.d("[Received] <== id: ${data['id']}, success: ${data['success']}");
@ -262,10 +262,11 @@ class HomeAssistant {
} }
} }
void _logout() { Future logout() async {
_token = null; _token = null;
_tempToken = null; _tempToken = null;
SharedPreferences.getInstance().then((prefs) => prefs.remove("hassio-token")); //TODO proper clear
await SharedPreferences.getInstance().then((prefs) => prefs.remove("hassio-token"));
} }
void _sendSubscribe() { void _sendSubscribe() {
@ -283,19 +284,19 @@ class HomeAssistant {
} }
Future _getLongLivedToken() async { Future _getLongLivedToken() async {
await _sendSocketMessage(type: "auth/long_lived_access_token", additionalData: {"client_name": "HA Client app", "lifespan": 365}).then((data) { await _sendSocketMessage(type: "auth/long_lived_access_token", additionalData: {"client_name": "HA Client app ${DateTime.now().millisecondsSinceEpoch}", "lifespan": 365}).then((data) {
if (data['success']) { if (data['success']) {
Logger.d("Got long-lived token: ${data['result']}"); Logger.d("Got long-lived token: ${data['result']}");
_token = data['result']; _token = data['result'];
_tempToken = null; _tempToken = null;
SharedPreferences.getInstance().then((prefs) => prefs.setString("hassio-token", _token)); SharedPreferences.getInstance().then((prefs) => prefs.setString("hassio-token", _token));
} else { } else {
_logout(); logout();
Logger.e("Error getting long-lived token: ${data['error'].toString()}"); Logger.e("Error getting long-lived token: ${data['error'].toString()}");
} }
}).catchError((e) { }).catchError((e) {
Logger.e("Error getting long-lived token: ${e.toString()}"); Logger.e("Error getting long-lived token: ${e.toString()}");
_logout(); logout();
}); });
} }
@ -361,7 +362,7 @@ class HomeAssistant {
Logger.d("Firing event to reload UI"); Logger.d("Firing event to reload UI");
eventBus.fire(ReloadUIEvent()); eventBus.fire(ReloadUIEvent());
}).catchError((e) { }).catchError((e) {
_logout(); logout();
disconnect(); disconnect();
flutterWebviewPlugin.close(); flutterWebviewPlugin.close();
_completeFetching({"errorCode": 61, "errorMessage": "Error getting temp token"}); _completeFetching({"errorCode": 61, "errorMessage": "Error getting temp token"});
@ -377,7 +378,7 @@ class HomeAssistant {
_hassioChannel.sink.add('{"type": "auth","access_token": "$_tempToken"}'); _hassioChannel.sink.add('{"type": "auth","access_token": "$_tempToken"}');
} else { } else {
Logger.e("General login error"); Logger.e("General login error");
_logout(); logout();
disconnect(); disconnect();
_completeFetching({"errorCode": 61, "errorMessage": "General login error"}); _completeFetching({"errorCode": 61, "errorMessage": "General login error"});
} }

View File

@ -164,7 +164,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
StreamSubscription _showErrorSubscription; StreamSubscription _showErrorSubscription;
StreamSubscription _startAuthSubscription; StreamSubscription _startAuthSubscription;
StreamSubscription _reloadUISubscription; StreamSubscription _reloadUISubscription;
bool _accountMenuExpanded = false;
int _previousViewCount; int _previousViewCount;
//final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); //final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@ -353,11 +352,11 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
UserAccountsDrawerHeader( UserAccountsDrawerHeader(
accountName: Text(widget.homeAssistant.userName), accountName: Text(widget.homeAssistant.userName),
accountEmail: Text(widget.homeAssistant.hostname ?? "Not configured"), accountEmail: Text(widget.homeAssistant.hostname ?? "Not configured"),
onDetailsPressed: () { /*onDetailsPressed: () {
setState(() { setState(() {
_accountMenuExpanded = !_accountMenuExpanded; _accountMenuExpanded = !_accountMenuExpanded;
}); });
}, },*/
currentAccountPicture: CircleAvatar( currentAccountPicture: CircleAvatar(
child: Text( child: Text(
widget.homeAssistant.userAvatarText, widget.homeAssistant.userAvatarText,
@ -368,19 +367,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
), ),
) )
); );
if (_accountMenuExpanded) {
menuItems.addAll([
ListTile(
leading: Icon(Icons.settings),
title: Text("Settings"),
onTap: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed('/connection-settings', arguments: {"homeAssistant", widget.homeAssistant});
},
),
Divider(),
]);
} else {
if (widget.homeAssistant != null && widget.homeAssistant.panels.isNotEmpty) { if (widget.homeAssistant != null && widget.homeAssistant.panels.isNotEmpty) {
widget.homeAssistant.panels.forEach((Panel panel) { widget.homeAssistant.panels.forEach((Panel panel) {
if (!panel.isHidden) { if (!panel.isHidden) {
@ -403,6 +389,24 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
]); ]);
} }
menuItems.addAll([ menuItems.addAll([
ListTile(
leading: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:login-variant")),
title: Text("Connection settings"),
onTap: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed('/connection-settings', arguments: {"homeAssistant", widget.homeAssistant});
},
),
ListTile(
leading: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:logout-variant")),
title: Text("Logout"),
onTap: () {
widget.homeAssistant.logout().then((_) {
widget.homeAssistant.disconnect().then((__) => _refreshData());
});
},
),
Divider(),
new ListTile( new ListTile(
leading: Icon(Icons.insert_drive_file), leading: Icon(Icons.insert_drive_file),
title: Text("Log"), title: Text("Log"),
@ -448,7 +452,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
applicationVersion: appVersion applicationVersion: appVersion
) )
]); ]);
}
return new Drawer( return new Drawer(
child: ListView( child: ListView(
children: menuItems, children: menuItems,
@ -632,9 +635,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
icon: Icon(Icons.menu), icon: Icon(Icons.menu),
onPressed: () { onPressed: () {
_scaffoldKey.currentState.openDrawer(); _scaffoldKey.currentState.openDrawer();
setState(() {
_accountMenuExpanded = false;
});
}, },
), ),
bottom: empty ? null : TabBar( bottom: empty ? null : TabBar(