From ca13f575a23bad0d2927366bfa56671457d834a3 Mon Sep 17 00:00:00 2001 From: Phani Pavan K Date: Fri, 25 Oct 2024 15:20:36 +0530 Subject: [PATCH] add new data loading algo --- lib/models/globalModel.dart | 63 ++++++++++++++++++++++++++++++++++++- lib/views/homePage.dart | 7 +++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/models/globalModel.dart b/lib/models/globalModel.dart index c41d96b..b0a7a5d 100644 --- a/lib/models/globalModel.dart +++ b/lib/models/globalModel.dart @@ -17,15 +17,27 @@ class GlobalModel extends ChangeNotifier { GlobalModel() { // print("In constructor"); - GlobalModel.loadData().then((value) { + GlobalModel.loadData2().then((value) { mainData = value; if (this.mainData["dates"].keys.contains(this.date)) { this.menuAvailable = DataStatus.Loaded; + } else { + this.menuAvailable = DataStatus.NotFound; } 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 getLatestHash() async { try { Response hashRequest = await get(Uri.parse(hashLink)); @@ -52,6 +64,55 @@ class GlobalModel extends ChangeNotifier { } } + static Future 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 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 loadData() async { late Map returnData; final SharedPreferences prefs = await SharedPreferences.getInstance(); diff --git a/lib/views/homePage.dart b/lib/views/homePage.dart index be2981d..342d9df 100644 --- a/lib/views/homePage.dart +++ b/lib/views/homePage.dart @@ -69,6 +69,13 @@ class HomePage extends StatelessWidget { Tab(icon: Icon(Icons.dinner_dining_outlined)) ]), actions: [ + InkWell( + splashFactory: NoSplash.splashFactory, + onTap: () { + data.updateCall(); + }, + child: const Icon(Icons.refresh), + ), InkWell( splashFactory: NoSplash.splashFactory, onTap: () {