Fix caching

This commit is contained in:
Yegor Vialov 2020-03-15 15:26:03 +00:00
parent 14ce608bbb
commit bc642f81ad
2 changed files with 15 additions and 21 deletions

View File

@ -12,7 +12,6 @@ class HomeAssistant {
HomeAssistantUI ui; HomeAssistantUI ui;
Map _instanceConfig = {}; Map _instanceConfig = {};
String _userName; String _userName;
bool childMode;
HSVColor savedColor; HSVColor savedColor;
int savedPlayerPosition; int savedPlayerPosition;
String sendToPlayerId; String sendToPlayerId;
@ -68,7 +67,7 @@ class HomeAssistant {
futures.add(_getServices(null)); futures.add(_getServices(null));
Future.wait(futures).then((_) { Future.wait(futures).then((_) {
if (isMobileAppEnabled) { if (isMobileAppEnabled) {
if (!childMode) _createUI(); _createUI();
_fetchCompleter.complete(); _fetchCompleter.complete();
MobileAppIntegrationManager.checkAppRegistration(); MobileAppIntegrationManager.checkAppRegistration();
} else { } else {
@ -80,14 +79,11 @@ class HomeAssistant {
return _fetchCompleter.future; return _fetchCompleter.future;
} }
Future fetchDataFromCache() async { Future<void> fetchDataFromCache() async {
if (_fetchCompleter != null && !_fetchCompleter.isCompleted) { Logger.d('Loading cached data');
Logger.w("Previous cached data fetch is not completed yet");
return _fetchCompleter.future;
}
_fetchCompleter = Completer();
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getBool('cached') != null && prefs.getBool('cached')) { bool cached = prefs.getBool('cached');
if (cached != null && cached) {
if (entities == null) entities = EntityCollection(ConnectionManager().httpWebHost); if (entities == null) entities = EntityCollection(ConnectionManager().httpWebHost);
try { try {
_getStates(prefs); _getStates(prefs);
@ -99,13 +95,12 @@ class HomeAssistant {
_getPanels(prefs); _getPanels(prefs);
_getServices(prefs); _getServices(prefs);
if (isMobileAppEnabled) { if (isMobileAppEnabled) {
if (!childMode) _createUI(); _createUI();
} }
} catch (e) { } catch (e) {
Logger.d('Didnt get cached data: $e'); Logger.d('Didnt get cached data: $e');
} }
} }
_fetchCompleter.complete();
} }
void saveCache() async { void saveCache() async {
@ -230,7 +225,6 @@ class HomeAssistant {
void _parseUserInfo(data) { void _parseUserInfo(data) {
_rawUserInfo = data; _rawUserInfo = data;
_userName = data["name"]; _userName = data["name"];
childMode = _userName.startsWith("[child]");
} }
Future _getPanels(SharedPreferences sharedPrefs) async { Future _getPanels(SharedPreferences sharedPrefs) async {

View File

@ -94,7 +94,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_showInfoBottomBar(progress: true,); _showInfoBottomBar(progress: true,);
_subscribe().then((_) { _subscribe().then((_) {
ConnectionManager().init(loadSettings: true, forceReconnect: true).then((__){ ConnectionManager().init(loadSettings: true, forceReconnect: true).then((__){
_fetchData(); _fetchData(true);
LocationManager(); LocationManager();
StartupUserMessagesManager().checkMessagesToShow(); StartupUserMessagesManager().checkMessagesToShow();
}, onError: (e) { }, onError: (e) {
@ -107,18 +107,18 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_hideBottomBar(); _hideBottomBar();
_showInfoBottomBar(progress: true,); _showInfoBottomBar(progress: true,);
ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){ ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){
_fetchData(); _fetchData(false);
}, onError: (e) { }, onError: (e) {
_setErrorState(e); _setErrorState(e);
}); });
} }
_fetchData() async { _fetchData(bool useCache) async {
await HomeAssistant().fetchDataFromCache().then((_) { if (useCache) {
if (_entityToShow != null) { HomeAssistant().fetchDataFromCache().then((_) {
_entityToShow = HomeAssistant().entities.get(_entityToShow.entityId); setState((){});
} });
}); }
await HomeAssistant().fetchData().then((_) { await HomeAssistant().fetchData().then((_) {
_hideBottomBar(); _hideBottomBar();
if (_entityToShow != null) { if (_entityToShow != null) {