[#30] Snackbar with connection error. New initial loading screen
This commit is contained in:
		| @@ -42,7 +42,7 @@ class HassioDataModel { | |||||||
|       //Fetch timeout timer |       //Fetch timeout timer | ||||||
|       _fetchingTimer = Timer(Duration(seconds: 10), () { |       _fetchingTimer = Timer(Duration(seconds: 10), () { | ||||||
|         closeConnection(); |         closeConnection(); | ||||||
|         _fetchCompleter.completeError({"message": "Data fetching timeout."}); |         _fetchCompleter.completeError({"errorCode" : 1,"errorMessage": "Connection timeout"}); | ||||||
|       }); |       }); | ||||||
|       _fetchCompleter = new Completer(); |       _fetchCompleter = new Completer(); | ||||||
|       _reConnectSocket().then((r) { |       _reConnectSocket().then((r) { | ||||||
|   | |||||||
| @@ -1,15 +1,12 @@ | |||||||
| import 'dart:convert'; | import 'dart:convert'; | ||||||
| import 'dart:async'; | import 'dart:async'; | ||||||
| import 'package:flutter/rendering.dart'; | import 'package:flutter/rendering.dart'; | ||||||
| import 'package:http/http.dart' as http; |  | ||||||
| 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:web_socket_channel/status.dart' as socketStatus; |  | ||||||
| import 'package:progress_indicators/progress_indicators.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:package_info/package_info.dart'; |  | ||||||
|  |  | ||||||
| part 'settings.dart'; | part 'settings.dart'; | ||||||
| part 'data_model.dart'; | part 'data_model.dart'; | ||||||
| @@ -53,8 +50,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver { | |||||||
|   Map _uiStructure; |   Map _uiStructure; | ||||||
|   Map _instanceConfig; |   Map _instanceConfig; | ||||||
|   int _uiViewsCount = 0; |   int _uiViewsCount = 0; | ||||||
|   String _dataModelErrorMessage = ""; |  | ||||||
|   String _instanceHost; |   String _instanceHost; | ||||||
|  |   int _fetchErrorCode = 0; | ||||||
|   bool loading = true; |   bool loading = true; | ||||||
|   Map _stateIconColors = { |   Map _stateIconColors = { | ||||||
|     "on": Colors.amber, |     "on": Colors.amber, | ||||||
| @@ -100,7 +97,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver { | |||||||
|     setState(() { |     setState(() { | ||||||
|       loading = true; |       loading = true; | ||||||
|     }); |     }); | ||||||
|     _dataModelErrorMessage = null; |     _fetchErrorCode = 0; | ||||||
|     if (_dataModel != null) { |     if (_dataModel != null) { | ||||||
|       await _dataModel.fetch().then((result) { |       await _dataModel.fetch().then((result) { | ||||||
|         setState(() { |         setState(() { | ||||||
| @@ -112,7 +109,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver { | |||||||
|         }); |         }); | ||||||
|       }).catchError((e) { |       }).catchError((e) { | ||||||
|         setState(() { |         setState(() { | ||||||
|           _dataModelErrorMessage = e.toString(); |           _fetchErrorCode = e["errorCode"] != null ? e["errorCode"] : 2; | ||||||
|           loading = false; |           loading = false; | ||||||
|         }); |         }); | ||||||
|       }); |       }); | ||||||
| @@ -258,7 +255,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver { | |||||||
|     return result; |     return result; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Widget _buildTitle() { |   Widget _buildAppTitle() { | ||||||
|     Row titleRow = Row( |     Row titleRow = Row( | ||||||
|       children: [Text(_instanceConfig != null ? _instanceConfig["location_name"] : "")], |       children: [Text(_instanceConfig != null ? _instanceConfig["location_name"] : "")], | ||||||
|     ); |     ); | ||||||
| @@ -300,39 +297,77 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver { | |||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   _getErrorMessageByCode(int code, bool short) { | ||||||
|  |     String message = short ? "Unknown error" : "Unknown error"; | ||||||
|  |     switch (code) { | ||||||
|  |       case 1: { | ||||||
|  |         message = short ? "Unable to connect" : "Unable to connect\n Please check your internet connection and Home Assistant instance state"; | ||||||
|  |         break; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return message; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   _checkShowInfo(BuildContext context) { | ||||||
|  |     if (_fetchErrorCode > 0) { | ||||||
|  |       String text = _getErrorMessageByCode(_fetchErrorCode, true); | ||||||
|  |       SnackBarAction action; | ||||||
|  |       switch (_fetchErrorCode) { | ||||||
|  |         case 1: { | ||||||
|  |             action = SnackBarAction( | ||||||
|  |                 label: "Retry", | ||||||
|  |                 onPressed: _refreshData, | ||||||
|  |             ); | ||||||
|  |             break; | ||||||
|  |           } | ||||||
|  |       } | ||||||
|  |       Timer(Duration(seconds: 1), () { | ||||||
|  |         _scaffoldKey.currentState.hideCurrentSnackBar(); | ||||||
|  |         _scaffoldKey.currentState.showSnackBar( | ||||||
|  |             SnackBar( | ||||||
|  |                 content: Text("$text"), | ||||||
|  |                 action: action, | ||||||
|  |                 duration: Duration(hours: 1), | ||||||
|  |             ) | ||||||
|  |         ); | ||||||
|  |       }); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|  |     _checkShowInfo(context); | ||||||
|     // This method is rerun every time setState is called. |     // This method is rerun every time setState is called. | ||||||
|     // |     // | ||||||
|     if (_entitiesData == null) { |     if (_entitiesData == null) { | ||||||
|       return new Scaffold( |       return new Scaffold( | ||||||
|  |         key: _scaffoldKey, | ||||||
|         appBar: new AppBar( |         appBar: new AppBar( | ||||||
|           title: _buildTitle() |           title: _buildAppTitle() | ||||||
|         ), |         ), | ||||||
|         drawer: _buildAppDrawer(), |         drawer: _buildAppDrawer(), | ||||||
|         body: Center( |         body: Center( | ||||||
|           child: Column( |           child: Column( | ||||||
|             mainAxisAlignment: MainAxisAlignment.center, |             mainAxisAlignment: MainAxisAlignment.center, | ||||||
|             children: [ |             children: [ | ||||||
|               Padding( |               /*Padding( | ||||||
|                 padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 10.0), |                 padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 10.0), | ||||||
|                 child: Text( |                 child: Text( | ||||||
|                     _dataModelErrorMessage != null ? "Well... no.\n\nThere was an error: $_dataModelErrorMessage\n\nCheck your internet connection or restart the app" : "Loading... I hope...", |                     _fetchErrorCode > 0 ? "Well... no.\n\nThere was an error [$_fetchErrorCode]: ${_getErrorMessageByCode(_fetchErrorCode, false)}" : "Loading...", | ||||||
|                     textAlign: TextAlign.center, |                     textAlign: TextAlign.center, | ||||||
|                     style: TextStyle(fontSize: 16.0), |                     style: TextStyle(fontSize: 16.0), | ||||||
|                 ), |                 ), | ||||||
|               ), |               ),*/ | ||||||
|               GlowingProgressIndicator( |               Icon( | ||||||
|                 child: Icon( |                 _createMDIfromCode(MaterialDesignIcons.getCustomIconByName("mdi:home-assistant")), | ||||||
|                     _createMDIfromCode(MaterialDesignIcons.getCustomIconByName("mdi:home-assistant")), |                 size: 100.0, | ||||||
|                     size: 40.0, |                 color: _fetchErrorCode == 0 ? Colors.blue : Colors.redAccent, | ||||||
|                     color: _dataModelErrorMessage == null ? Colors.blue : Colors.redAccent, |  | ||||||
|                 ), |  | ||||||
|               ), |               ), | ||||||
|             ] |             ] | ||||||
|           ), |           ), | ||||||
|         ), |         ), | ||||||
|         //Text(_dataModelErrorMessage != null ? "Well... no.\n\nThere was an error:\n$_dataModelErrorMessage" : "Loading... or not...\n\nJust wait 10 seconds"), |  | ||||||
|         floatingActionButton: new FloatingActionButton( |         floatingActionButton: new FloatingActionButton( | ||||||
|           onPressed: _refreshData, |           onPressed: _refreshData, | ||||||
|           tooltip: 'Increment', |           tooltip: 'Increment', | ||||||
| @@ -343,10 +378,11 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver { | |||||||
|       return DefaultTabController( |       return DefaultTabController( | ||||||
|           length: _uiViewsCount, |           length: _uiViewsCount, | ||||||
|           child: new Scaffold( |           child: new Scaffold( | ||||||
|  |             key: _scaffoldKey, | ||||||
|             appBar: new AppBar( |             appBar: new AppBar( | ||||||
|               // Here we take the value from the MyHomePage object that was created by |               // Here we take the value from the MyHomePage object that was created by | ||||||
|               // the App.build method, and use it to set our appbar title. |               // the App.build method, and use it to set our appbar title. | ||||||
|               title: _buildTitle(), |               title: _buildAppTitle(), | ||||||
|               bottom: TabBar( |               bottom: TabBar( | ||||||
|                   tabs: buildUIViewTabs() |                   tabs: buildUIViewTabs() | ||||||
|               ), |               ), | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								pubspec.yaml
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								pubspec.yaml
									
									
									
									
									
								
							| @@ -1,13 +1,7 @@ | |||||||
| name: hass_client | name: hass_client | ||||||
| description: Home Assistant Android Client | description: Home Assistant Android Client | ||||||
|  |  | ||||||
| # The following defines the version and build number for your application. | version: 0.0.8 | ||||||
| # A version number is three numbers separated by dots, like 1.2.43 |  | ||||||
| # followed by an optional build number separated by a +. |  | ||||||
| # Both the version and the builder number may be overridden in flutter |  | ||||||
| # build by specifying --build-name and --build-number, respectively. |  | ||||||
| # Read more about versioning at semver.org. |  | ||||||
| version: 0.0.7 |  | ||||||
|  |  | ||||||
| environment: | environment: | ||||||
|   sdk: ">=2.0.0-dev.68.0 <3.0.0" |   sdk: ">=2.0.0-dev.68.0 <3.0.0" | ||||||
| @@ -22,7 +16,7 @@ dependencies: | |||||||
|  |  | ||||||
|   # The following adds the Cupertino Icons font to your application. |   # The following adds the Cupertino Icons font to your application. | ||||||
|   # Use with the CupertinoIcons class for iOS style icons. |   # Use with the CupertinoIcons class for iOS style icons. | ||||||
|   cupertino_icons: ^0.1.2 |   #cupertino_icons: ^0.1.2 | ||||||
|  |  | ||||||
| dev_dependencies: | dev_dependencies: | ||||||
|   flutter_test: |   flutter_test: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user