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

View File

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