Cached Http-Request
und gefixxt: tastur überlagert sich mit eingabefeld
This commit is contained in:
@@ -1,10 +1,32 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class FetchMenu {
|
||||
static var lastFetch = DateTime.now();
|
||||
static var fetched = false;
|
||||
|
||||
static Future<List<String>> get() async {
|
||||
List<String> menus = [];
|
||||
|
||||
// TODO: Proto:
|
||||
if (!fetched || DateTime.now().difference(lastFetch).inHours >= 2) {
|
||||
menus = await fromRequest();
|
||||
fetched = true;
|
||||
} else {
|
||||
menus = await fromCache();
|
||||
}
|
||||
|
||||
return menus;
|
||||
}
|
||||
|
||||
static Future<List<String>> fromCache() async {
|
||||
return await loadCache();
|
||||
}
|
||||
|
||||
static Future<List<String>> fromRequest() async {
|
||||
List<String> menus = [];
|
||||
http.Response resp = await http.get(Uri.parse(
|
||||
"https://www.swfr.de/essen/mensen-cafes-speiseplaene/mensa-loerrach"));
|
||||
var content = resp.body.split("<h3>");
|
||||
@@ -15,7 +37,38 @@ class FetchMenu {
|
||||
menus.add(name);
|
||||
}
|
||||
}
|
||||
// End-Proto
|
||||
|
||||
await storeCache(menus);
|
||||
|
||||
lastFetch = DateTime.now();
|
||||
|
||||
return menus;
|
||||
}
|
||||
|
||||
static Future<void> storeCache(List<String> menus) async {
|
||||
var prefs = await SharedPreferences.getInstance();
|
||||
|
||||
for (var i = 0; i < 4; i++) {
|
||||
prefs.setString("menuCache$i", "");
|
||||
if (i < menus.length) {
|
||||
prefs.setString("menuCache$i", menus[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static Future<List<String>> loadCache() async {
|
||||
var prefs = await SharedPreferences.getInstance();
|
||||
|
||||
List<String> menus = [];
|
||||
|
||||
for (var i = 0; i < 4; i++) {
|
||||
var curr = prefs.getString("menuCache$i");
|
||||
if (curr != "") {
|
||||
menus.add(curr!);
|
||||
}
|
||||
}
|
||||
|
||||
return menus;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user