diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index de26384..6503a5d 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -48,6 +48,9 @@ class HomeAssistant { } String get userName => _userName ?? locationName; String get userAvatarText => userName.length > 0 ? userName[0] : ""; + bool get isNoEntities => entities == null || entities.isEmpty; + bool get isNoViews => ui == null || ui.isEmpty; + bool get isAuthenticated => _token != null; //int get viewsCount => entities.views.length ?? 0; HomeAssistant() { @@ -106,14 +109,14 @@ class HomeAssistant { return _fetchCompleter.future; } - disconnect() async { + Future disconnect() async { Logger.d( "Socket disconnecting..."); await _socketSubscription?.cancel(); - await _hassioChannel?.sink?.close()?.timeout(Duration(seconds: 3), - onTimeout: () => Logger.d( "Socket sink closed") + await _hassioChannel?.sink?.close()?.timeout(Duration(seconds: 4), + onTimeout: () => Logger.d( "Socket sink close timeout") ); _hassioChannel = null; - + Logger.d( "..Disconnected"); } Future _connection() { @@ -263,10 +266,12 @@ class HomeAssistant { } Future logout() async { + Logger.d("Logging out..."); _token = null; _tempToken = null; - //TODO proper clear await SharedPreferences.getInstance().then((prefs) => prefs.remove("hassio-token")); + ui?.clear(); + entities?.clear(); } void _sendSubscribe() { diff --git a/lib/main.dart b/lib/main.dart index f999c4f..88b3347 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -367,7 +367,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker ), ) ); - if (widget.homeAssistant != null && widget.homeAssistant.panels.isNotEmpty) { + if (widget.homeAssistant.panels.isNotEmpty) { widget.homeAssistant.panels.forEach((Panel panel) { if (!panel.isHidden) { menuItems.add( @@ -379,16 +379,18 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker ); } }); - menuItems.addAll([ + } + if (widget.homeAssistant.isSettingsLoaded) { + menuItems.add( new ListTile( leading: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:home-assistant")), title: Text("Open Web UI"), onTap: () => HAUtils.launchURL(widget.homeAssistant.httpWebHost), - ), - Divider() - ]); + ) + ); } menuItems.addAll([ + Divider(), ListTile( leading: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:login-variant")), title: Text("Connection settings"), @@ -396,16 +398,9 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker 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()); - }); - }, - ), + ) + ]); + menuItems.addAll([ Divider(), new ListTile( leading: Icon(Icons.insert_drive_file), @@ -603,6 +598,24 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker final GlobalKey _scaffoldKey = new GlobalKey(); Widget _buildScaffoldBody(bool empty) { + List> popupMenuItems = []; + if (widget.homeAssistant.isAuthenticated) { + popupMenuItems.addAll([ + PopupMenuItem( + child: new Text("Reload"), + value: "reload", + ), + PopupMenuItem( + child: new Text("Logout"), + value: "logout", + )]); + } else { + popupMenuItems.addAll([ + PopupMenuItem( + child: new Text("Connect"), + value: "reload", + )]); + } return NestedScrollView( headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { return [ @@ -610,7 +623,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker floating: true, pinned: true, primary: true, - title: Text(widget.homeAssistant != null ? widget.homeAssistant.locationName : ""), + title: Text(widget.homeAssistant.locationName ?? ""), actions: [ IconButton( icon: Icon(MaterialDesignIcons.getIconDataFromIconName( @@ -619,13 +632,18 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker showMenu( position: RelativeRect.fromLTRB(MediaQuery.of(context).size.width, 70.0, 0.0, 0.0), context: context, - items: [PopupMenuItem( - child: new Text("Reload"), - value: "reload", - )] + items: popupMenuItems ).then((String val) { if (val == "reload") { _refreshData(); + } else if (val == "logout") { + widget.homeAssistant.disconnect().then((_) { + widget.homeAssistant.logout().then((_) { + setState(() { + _refreshData(); + }); + }); + }); } }); } @@ -652,9 +670,9 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( - MaterialDesignIcons.getIconDataFromIconName("mdi:home-assistant"), + MaterialDesignIcons.getIconDataFromIconName("mdi:border-none-variant"), size: 100.0, - color: Colors.blue, + color: Colors.black26, ), ] ), @@ -717,7 +735,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker } } // This method is rerun every time setState is called. - if (widget.homeAssistant.ui == null || widget.homeAssistant.ui.views == null) { + if (widget.homeAssistant.isNoViews) { return Scaffold( key: _scaffoldKey, primary: false, diff --git a/lib/ui_class/ui.dart b/lib/ui_class/ui.dart index 4e696b3..543b5bd 100644 --- a/lib/ui_class/ui.dart +++ b/lib/ui_class/ui.dart @@ -4,6 +4,8 @@ class HomeAssistantUI { List views; String title; + bool get isEmpty => views == null || views.isEmpty; + HomeAssistantUI() { views = []; } @@ -25,4 +27,8 @@ class HomeAssistantUI { return result; } + void clear() { + views.clear(); + } + } \ No newline at end of file