Add timeout for data fetching. [#34] MDI class fixes
This commit is contained in:
		| @@ -13,6 +13,7 @@ class HassioDataModel { | |||||||
|   Completer _fetchCompleter; |   Completer _fetchCompleter; | ||||||
|   Completer _statesCompleter; |   Completer _statesCompleter; | ||||||
|   Completer _servicesCompleter; |   Completer _servicesCompleter; | ||||||
|  |   Timer _fetchingTimer; | ||||||
|  |  | ||||||
|   Map get entities => _entitiesData; |   Map get entities => _entitiesData; | ||||||
|   Map get services => _servicesData; |   Map get services => _servicesData; | ||||||
| @@ -27,10 +28,14 @@ class HassioDataModel { | |||||||
|     if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) { |     if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) { | ||||||
|       debugPrint("Previous fetch is not complited"); |       debugPrint("Previous fetch is not complited"); | ||||||
|     } else { |     } else { | ||||||
|  |       _fetchingTimer = new Timer(new Duration(seconds: 10), () { | ||||||
|  |         _fetchCompleter.completeError({"message": "Data fetching timeout."}); | ||||||
|  |       }); | ||||||
|       _fetchCompleter = new Completer(); |       _fetchCompleter = new Completer(); | ||||||
|       _reConnectSocket().then((r) { |       _reConnectSocket().then((r) { | ||||||
|         _getData(); |         _getData(); | ||||||
|       }).catchError((e) { |       }).catchError((e) { | ||||||
|  |         _fetchingTimer.cancel(); | ||||||
|         _fetchCompleter.completeError(e); |         _fetchCompleter.completeError(e); | ||||||
|       }); |       }); | ||||||
|     } |     } | ||||||
| @@ -56,11 +61,14 @@ class HassioDataModel { | |||||||
|   _getData() { |   _getData() { | ||||||
|     _getStates().then((result) { |     _getStates().then((result) { | ||||||
|       _getServices().then((result) { |       _getServices().then((result) { | ||||||
|  |         _fetchingTimer.cancel(); | ||||||
|         _fetchCompleter.complete(); |         _fetchCompleter.complete(); | ||||||
|       }).catchError((e) { |       }).catchError((e) { | ||||||
|  |         _fetchingTimer.cancel(); | ||||||
|         _fetchCompleter.completeError(e); |         _fetchCompleter.completeError(e); | ||||||
|       }); |       }); | ||||||
|     }).catchError((e) { |     }).catchError((e) { | ||||||
|  |       _fetchingTimer.cancel(); | ||||||
|       _fetchCompleter.completeError(e); |       _fetchCompleter.completeError(e); | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
| @@ -133,8 +141,6 @@ class HassioDataModel { | |||||||
|     }).catchError((e){ |     }).catchError((e){ | ||||||
|       debugPrint("Unable to connect for sending =("); |       debugPrint("Unable to connect for sending =("); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|  |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void _parseServices(Map data) { |   void _parseServices(Map data) { | ||||||
| @@ -234,16 +240,16 @@ class HassioDataModel { | |||||||
|  |  | ||||||
| class MaterialDesignIcons { | class MaterialDesignIcons { | ||||||
|   static Map _defaultIconsByDomains = { |   static Map _defaultIconsByDomains = { | ||||||
|     "light": 0xf335, |     "light": "mdi:lightbulb", | ||||||
|     "switch": 0xf241, |     "switch": "mdi:flash", | ||||||
|     "binary_sensor": 0xf130, |     "binary_sensor": "mdi:checkbox-blank-circle-outline", | ||||||
|     "group": 0xf2b1, |     "group": "mdi:google-circles-communities", | ||||||
|     "sensor": 0xf208, |     "sensor": "mdi:eye", | ||||||
|     "automation": 0xf411, |     "automation": "mdi:playlist-play", | ||||||
|     "script": 0xf219, |     "script": "mdi:file-document", | ||||||
|     "input_boolean": 0xf1de, |     "input_boolean": "mdi:drawing", | ||||||
|     "input_datetime": 0xf953, |     "input_datetime": "mdi:clock", | ||||||
|     "sun": 0xf5a8 |     "sun": "mdi:white-balance-sunny" | ||||||
|   }; |   }; | ||||||
|   static Map _iconsDataMap = { |   static Map _iconsDataMap = { | ||||||
|     "mdi:access-point": 0xf002, |     "mdi:access-point": 0xf002, | ||||||
|   | |||||||
| @@ -77,7 +77,7 @@ class _MainPageState extends State<MainPage> { | |||||||
|     setState(() { |     setState(() { | ||||||
|       loading = true; |       loading = true; | ||||||
|     }); |     }); | ||||||
|     _dataModelErrorMessage = ""; |     _dataModelErrorMessage = null; | ||||||
|     if (_dataModel != null) { |     if (_dataModel != null) { | ||||||
|       await _dataModel.fetch().then((result) { |       await _dataModel.fetch().then((result) { | ||||||
|         setState(() { |         setState(() { | ||||||
| @@ -189,18 +189,14 @@ class _MainPageState extends State<MainPage> { | |||||||
|  |  | ||||||
|   List<Widget> buildSingleView(structure) { |   List<Widget> buildSingleView(structure) { | ||||||
|       List<Widget> result = []; |       List<Widget> result = []; | ||||||
|       if (_dataModelErrorMessage.length == 0) { |       structure["standalone"].forEach((entityId) { | ||||||
|         structure["standalone"].forEach((entityId) { |         result.add(_buildCard([entityId], "")); | ||||||
|           result.add(_buildCard([entityId], "")); |       }); | ||||||
|         }); |       structure["groups"].forEach((group) { | ||||||
|         structure["groups"].forEach((group) { |         result.add(_buildCard( | ||||||
|           result.add(_buildCard( |             group["children"], group["friendly_name"].toString())); | ||||||
|               group["children"], group["friendly_name"].toString())); |       }); | ||||||
|         }); |  | ||||||
|       } else { |  | ||||||
|         //TODO |  | ||||||
|         //result.add(Text(_dataModelErrorMessage)); |  | ||||||
|       } |  | ||||||
|       return result; |       return result; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -283,7 +279,7 @@ class _MainPageState extends State<MainPage> { | |||||||
|           title: _buildTitle() |           title: _buildTitle() | ||||||
|         ), |         ), | ||||||
|         drawer: _buildAppDrawer(), |         drawer: _buildAppDrawer(), | ||||||
|         body: Text("Loading... or not...\n\nPlease, restart the app in case of three dots in header starts to freaking you out."), |         body: Text(_dataModelErrorMessage != null ? "Well... no. There was an error: $_dataModelErrorMessage" : "Loading... or not..."), | ||||||
|         floatingActionButton: new FloatingActionButton( |         floatingActionButton: new FloatingActionButton( | ||||||
|           onPressed: _refreshData, |           onPressed: _refreshData, | ||||||
|           tooltip: 'Increment', |           tooltip: 'Increment', | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user