Cache Jarvis app config on startup
This commit is contained in:
@@ -15,6 +15,7 @@ class _OtthonAppState extends State<OtthonApp> {
|
||||
List<CalendarEvent> syncedEvents = [];
|
||||
List<String> todoSuggestions = [];
|
||||
JarvisSettings jarvisSettings = JarvisSettings.defaults();
|
||||
String googleServerClientId = googleCalendarServerClientId;
|
||||
WeatherSnapshot? weather;
|
||||
String themeId = 'sage';
|
||||
bool loaded = false;
|
||||
@@ -40,9 +41,11 @@ class _OtthonAppState extends State<OtthonApp> {
|
||||
syncedEvents = settings.syncedEvents;
|
||||
todoSuggestions = settings.todoSuggestions;
|
||||
jarvisSettings = settings.jarvisSettings;
|
||||
googleServerClientId = settings.googleServerClientId;
|
||||
themeId = settings.themeId;
|
||||
loaded = true;
|
||||
});
|
||||
_loadRemoteAppConfig();
|
||||
}
|
||||
|
||||
Future<void> _saveSettings() async {
|
||||
@@ -54,10 +57,39 @@ class _OtthonAppState extends State<OtthonApp> {
|
||||
syncedEvents: syncedEvents,
|
||||
todoSuggestions: todoSuggestions,
|
||||
jarvisSettings: jarvisSettings,
|
||||
googleServerClientId: googleServerClientId,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _loadRemoteAppConfig() async {
|
||||
final apiToken = jarvisSettings.apiToken.trim();
|
||||
if (apiToken.isEmpty) return;
|
||||
|
||||
try {
|
||||
final baseUri = Uri.parse(normalizeJarvisUrl(jarvisSettings.apiUrl));
|
||||
final uri = baseUri.replace(path: '/api/app-config', query: '');
|
||||
final response = await http
|
||||
.get(uri, headers: {'X-Otthon-Token': apiToken})
|
||||
.timeout(const Duration(seconds: 10));
|
||||
if (response.statusCode != 200) return;
|
||||
final payload = jsonDecode(response.body);
|
||||
if (payload is! Map<String, dynamic>) return;
|
||||
final remoteGoogleClientId =
|
||||
payload['googleCalendarServerClientId'] as String?;
|
||||
if (remoteGoogleClientId == null ||
|
||||
remoteGoogleClientId.trim().isEmpty ||
|
||||
remoteGoogleClientId == googleServerClientId) {
|
||||
return;
|
||||
}
|
||||
if (!mounted) return;
|
||||
setState(() => googleServerClientId = remoteGoogleClientId.trim());
|
||||
_saveSettings();
|
||||
} catch (_) {
|
||||
// Offline mode keeps the locally cached app configuration.
|
||||
}
|
||||
}
|
||||
|
||||
void _updatePeople(List<Person> nextPeople) {
|
||||
setState(() => people = nextPeople);
|
||||
_saveSettings();
|
||||
@@ -88,6 +120,7 @@ class _OtthonAppState extends State<OtthonApp> {
|
||||
void _updateJarvisSettings(JarvisSettings nextSettings) {
|
||||
setState(() => jarvisSettings = nextSettings);
|
||||
_saveSettings();
|
||||
_loadRemoteAppConfig();
|
||||
}
|
||||
|
||||
Future<void> _loadWeather() async {
|
||||
@@ -132,6 +165,7 @@ class _OtthonAppState extends State<OtthonApp> {
|
||||
syncedEvents: syncedEvents,
|
||||
todoSuggestions: todoSuggestions,
|
||||
jarvisSettings: jarvisSettings,
|
||||
googleServerClientId: googleServerClientId,
|
||||
weather: weather,
|
||||
themeId: themeId,
|
||||
loaded: loaded,
|
||||
@@ -154,6 +188,7 @@ class HomeShell extends StatefulWidget {
|
||||
required this.syncedEvents,
|
||||
required this.todoSuggestions,
|
||||
required this.jarvisSettings,
|
||||
required this.googleServerClientId,
|
||||
required this.weather,
|
||||
required this.themeId,
|
||||
required this.loaded,
|
||||
@@ -170,6 +205,7 @@ class HomeShell extends StatefulWidget {
|
||||
final List<CalendarEvent> syncedEvents;
|
||||
final List<String> todoSuggestions;
|
||||
final JarvisSettings jarvisSettings;
|
||||
final String googleServerClientId;
|
||||
final WeatherSnapshot? weather;
|
||||
final String themeId;
|
||||
final bool loaded;
|
||||
@@ -352,6 +388,7 @@ class _HomeShellState extends State<HomeShell> {
|
||||
cachedEvents: widget.syncedEvents,
|
||||
people: widget.people,
|
||||
jarvisSettings: widget.jarvisSettings,
|
||||
serverClientId: widget.googleServerClientId,
|
||||
onChanged: widget.onGoogleConnectionsChanged,
|
||||
onSyncedEventsChanged: widget.onSyncedEventsChanged,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user