add new data loading algo
This commit is contained in:
@@ -17,15 +17,27 @@ class GlobalModel extends ChangeNotifier {
|
|||||||
|
|
||||||
GlobalModel() {
|
GlobalModel() {
|
||||||
// print("In constructor");
|
// print("In constructor");
|
||||||
GlobalModel.loadData().then((value) {
|
GlobalModel.loadData2().then((value) {
|
||||||
mainData = value;
|
mainData = value;
|
||||||
if (this.mainData["dates"].keys.contains(this.date)) {
|
if (this.mainData["dates"].keys.contains(this.date)) {
|
||||||
this.menuAvailable = DataStatus.Loaded;
|
this.menuAvailable = DataStatus.Loaded;
|
||||||
|
} else {
|
||||||
|
this.menuAvailable = DataStatus.NotFound;
|
||||||
}
|
}
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateCall() async {
|
||||||
|
this.mainData = await GlobalModel.updateLocal();
|
||||||
|
if (this.mainData["dates"].keys.contains(this.date)) {
|
||||||
|
this.menuAvailable = DataStatus.Loaded;
|
||||||
|
} else {
|
||||||
|
this.menuAvailable = DataStatus.NotFound;
|
||||||
|
}
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
static Future<String> getLatestHash() async {
|
static Future<String> getLatestHash() async {
|
||||||
try {
|
try {
|
||||||
Response hashRequest = await get(Uri.parse(hashLink));
|
Response hashRequest = await get(Uri.parse(hashLink));
|
||||||
@@ -52,6 +64,55 @@ class GlobalModel extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<Map> updateLocal() async {
|
||||||
|
// Set main data and return the data to be used in constructor
|
||||||
|
String remoteHash = await GlobalModel.getLatestHash();
|
||||||
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
String? rawData = prefs.getString(storageKey);
|
||||||
|
rawData ??= "";
|
||||||
|
String rawHash = md5.convert(utf8.encode(rawData)).toString();
|
||||||
|
if (remoteHash != rawHash) {
|
||||||
|
print("Local data is out of date, updating local");
|
||||||
|
// Donwload new data and save it.
|
||||||
|
rawData = await getLatestData();
|
||||||
|
Map returnData = jsonDecode(rawData);
|
||||||
|
prefs.setString(storageKey, rawData);
|
||||||
|
return returnData;
|
||||||
|
} else {
|
||||||
|
// return the same data
|
||||||
|
print("Local data is up to date");
|
||||||
|
return jsonDecode(rawData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<Map> loadData2() async {
|
||||||
|
/* New flow for fetching data:
|
||||||
|
Get cached data into a var
|
||||||
|
if theres nothing:
|
||||||
|
get data
|
||||||
|
generate hash and store both the data and hash
|
||||||
|
else:
|
||||||
|
return the current data
|
||||||
|
|
||||||
|
Move the download and get hash part to new method - done
|
||||||
|
|
||||||
|
create a new method for updating the current data.
|
||||||
|
This checks for update and saves new data to sharedprefs if necessary,
|
||||||
|
*/
|
||||||
|
late Map returnData;
|
||||||
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
String? rawData;
|
||||||
|
rawData = prefs.getString(storageKey);
|
||||||
|
if (rawData != null) {
|
||||||
|
print("Some data found");
|
||||||
|
returnData = jsonDecode(rawData);
|
||||||
|
return returnData;
|
||||||
|
} else {
|
||||||
|
print("Data Not Found");
|
||||||
|
return GlobalModel.updateLocal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static Future<Map> loadData() async {
|
static Future<Map> loadData() async {
|
||||||
late Map returnData;
|
late Map returnData;
|
||||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
|||||||
@@ -69,6 +69,13 @@ class HomePage extends StatelessWidget {
|
|||||||
Tab(icon: Icon(Icons.dinner_dining_outlined))
|
Tab(icon: Icon(Icons.dinner_dining_outlined))
|
||||||
]),
|
]),
|
||||||
actions: [
|
actions: [
|
||||||
|
InkWell(
|
||||||
|
splashFactory: NoSplash.splashFactory,
|
||||||
|
onTap: () {
|
||||||
|
data.updateCall();
|
||||||
|
},
|
||||||
|
child: const Icon(Icons.refresh),
|
||||||
|
),
|
||||||
InkWell(
|
InkWell(
|
||||||
splashFactory: NoSplash.splashFactory,
|
splashFactory: NoSplash.splashFactory,
|
||||||
onTap: () {
|
onTap: () {
|
||||||
|
|||||||
Reference in New Issue
Block a user