Changed data loading strategy
This commit is contained in:
17
.github/workflows/gh-pages.yml
vendored
17
.github/workflows/gh-pages.yml
vendored
@@ -25,6 +25,23 @@ jobs:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3.6.0
|
||||
|
||||
- name: Setup Python
|
||||
uses: actions/setup-python@v4.7.0
|
||||
with:
|
||||
python-version: "3.11"
|
||||
|
||||
- name: Install Packages
|
||||
working-directory: ./scripts
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
|
||||
- name: Compile Menu
|
||||
working-directory: ./scripts
|
||||
run: python scrapeToJson.py
|
||||
|
||||
- name: Copy Data File
|
||||
run: cp ./scripts/data.dart ./lib/data.dart
|
||||
# - name: Cache Flutter dependencies
|
||||
# uses: actions/cache@v1
|
||||
# with:
|
||||
|
||||
@@ -14,3 +14,9 @@ const String storageKey = "FullMenu";
|
||||
const Center noMenuWidget = Center(
|
||||
child: Text("Menu not available for this session",
|
||||
style: TextStyle(fontSize: 20, fontStyle: FontStyle.italic)));
|
||||
|
||||
const String hashLink =
|
||||
"http://github.com/kphanipavan/IIITB_Menu/releases/download/latest/out.txt";
|
||||
|
||||
const String dataLink =
|
||||
"http://github.com/kphanipavan/IIITB_Menu/releases/download/latest/out.json";
|
||||
|
||||
1
lib/data.dart
Normal file
1
lib/data.dart
Normal file
File diff suppressed because one or more lines are too long
@@ -1,15 +1,17 @@
|
||||
// ignore_for_file: unnecessary_this, file_names
|
||||
|
||||
import "dart:convert";
|
||||
import "dart:io";
|
||||
// import "dart:io";
|
||||
import "package:iiitb_menu/constants.dart";
|
||||
// import "package:path_provider/path_provider.dart";
|
||||
// import 'dart:io';
|
||||
import "package:flutter/material.dart";
|
||||
import "package:crypto/crypto.dart";
|
||||
import "package:http/http.dart";
|
||||
// import "package:crypto/crypto.dart";
|
||||
// import "package:http/http.dart";
|
||||
import "package:intl/intl.dart";
|
||||
import "package:shared_preferences/shared_preferences.dart";
|
||||
// import "package:shared_preferences/shared_preferences.dart";
|
||||
// import "package:dio/dio.dart";
|
||||
import "package:iiitb_menu/data.dart" as menuData;
|
||||
|
||||
class GlobalModel extends ChangeNotifier {
|
||||
late Map<dynamic, dynamic> mainData;
|
||||
@@ -29,76 +31,88 @@ class GlobalModel extends ChangeNotifier {
|
||||
});
|
||||
}
|
||||
|
||||
static Future<String> getLatestHash() async {
|
||||
const String hashLink = "https://github.com/kphanipavan/IIITB_Menu/releases/download/latest/out.txt";
|
||||
try {
|
||||
Response ret = await get(Uri.parse(hashLink));
|
||||
if (ret.statusCode == 200) {
|
||||
return ret.body.replaceAll("\n", "");
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
} on SocketException catch (exce) {
|
||||
print("Unable to download hash");
|
||||
print(exce);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
// static Future<String> getLatestHash() async {
|
||||
// try {
|
||||
// // Request hashRequest = Request("get", Uri.parse(hashLink));
|
||||
// Response ret = await Dio().request(hashLink,
|
||||
// options: Options(method: 'GET', headers: {
|
||||
// HttpHeaders.acceptHeader: "text/plain",
|
||||
// "Access-Control-Allow-Origin": "*",
|
||||
// "Access-Control-Allow-Methods": "GET",
|
||||
// "Access-Control-Allow-Headers": "*",
|
||||
// "Access-Control-Max-Age": "1000"
|
||||
// }));
|
||||
// if (ret.statusCode == 200) {
|
||||
// return ret.data.replaceAll("\n", "");
|
||||
// } else {
|
||||
// return "";
|
||||
// }
|
||||
// } on SocketException catch (exce) {
|
||||
// print("Unable to download hash");
|
||||
// print(exce);
|
||||
// return "";
|
||||
// }
|
||||
// }
|
||||
|
||||
static Future<String> getLatestData() async {
|
||||
const String dataLink = "https://github.com/kphanipavan/IIITB_Menu/releases/download/latest/out.json";
|
||||
try {
|
||||
Response ret = await get(Uri.parse(dataLink));
|
||||
return ret.body;
|
||||
} on SocketException catch (exce) {
|
||||
print("Unable to download any data");
|
||||
print(exce);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
// static Future<String> getLatestData() async {
|
||||
// try {
|
||||
// Response ret = await Dio().request(dataLink,
|
||||
// options: Options(method: "GET", headers: {
|
||||
// HttpHeaders.acceptHeader: "text/plain",
|
||||
// "Access-Control-Allow-Origin": "*"
|
||||
// }));
|
||||
// return ret.data;
|
||||
// } on SocketException catch (exce) {
|
||||
// print("Unable to download any data");
|
||||
// print(exce);
|
||||
// return "";
|
||||
// }
|
||||
// }
|
||||
|
||||
static Future<Map> loadData() async {
|
||||
late Map returnData;
|
||||
// Directory appDir = await getApplicationDocumentsDirectory();
|
||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
String? rawData;
|
||||
String rawHash;
|
||||
// final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
// String? rawData;
|
||||
// String rawHash;
|
||||
// String fileName = "${appDir.path}/menu.json";
|
||||
// print(fileName);
|
||||
// File fileLink = File(fileName);
|
||||
rawData = prefs.getString(storageKey);
|
||||
print(rawData);
|
||||
if (rawData != null) {
|
||||
print("data found");
|
||||
// rawData = localStor.getItem("fullMenu");
|
||||
rawHash = md5.convert(utf8.encode(rawData)).toString();
|
||||
print("Hash of Raw Data:");
|
||||
print(rawHash);
|
||||
// print("RawData: ");
|
||||
// rawData = prefs.getString("asdasdasd");
|
||||
// print(rawData);
|
||||
returnData = jsonDecode(rawData);
|
||||
// menuAvailable = true;
|
||||
} else {
|
||||
print("File Not Found");
|
||||
returnData = {};
|
||||
rawHash = "";
|
||||
}
|
||||
String remoteHash = await GlobalModel.getLatestHash();
|
||||
if (remoteHash == "") {
|
||||
} else if (remoteHash == rawHash) {
|
||||
// if (rawData != null) {
|
||||
// print("data found");
|
||||
// // rawData = localStor.getItem("fullMenu");
|
||||
// rawHash = md5.convert(utf8.encode(rawData)).toString();
|
||||
// print("Hash of Raw Data:");
|
||||
// print(rawHash);
|
||||
// // print("RawData: ");
|
||||
// // print(rawData);
|
||||
// returnData = jsonDecode(rawData);
|
||||
// // menuAvailable = true;
|
||||
// } else {
|
||||
// print("Data Not Found");
|
||||
// returnData = {};
|
||||
// rawHash = "";
|
||||
// }
|
||||
// // String remoteHash = await GlobalModel.getLatestHash();
|
||||
// String remoteHash = "asd";
|
||||
// print("Remote Hash: $remoteHash");
|
||||
// if (remoteHash == "") {
|
||||
// } else if (remoteHash == rawHash) {
|
||||
// print("Remote hash $remoteHash is same as local hash $rawHash");
|
||||
} else {
|
||||
// } else {
|
||||
// print("Remote hash $remoteHash is NOT the same as local hash $rawHash");
|
||||
// print("Getting data from remote");
|
||||
rawData = await getLatestData();
|
||||
if (rawData == "") {
|
||||
return {};
|
||||
}
|
||||
returnData = jsonDecode(rawData);
|
||||
// fileLink.writeAsStringSync(rawData, mode: FileMode.write);
|
||||
prefs.setString(storageKey, rawData);
|
||||
}
|
||||
|
||||
// rawData = await getLatestData();
|
||||
// if (rawData == "") {
|
||||
// return {};
|
||||
// }
|
||||
// returnData = jsonDecode(rawData);
|
||||
// // fileLink.writeAsStringSync(rawData, mode: FileMode.write);
|
||||
// prefs.setString(storageKey, rawData);
|
||||
// }
|
||||
returnData = jsonDecode(menuData.data);
|
||||
return returnData;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,7 @@ import FlutterMacOS
|
||||
import Foundation
|
||||
|
||||
import path_provider_foundation
|
||||
import shared_preferences_foundation
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||
}
|
||||
|
||||
@@ -36,12 +36,13 @@ dependencies:
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^1.0.2
|
||||
provider: ^6.0.5
|
||||
crypto: ^3.0.3
|
||||
# crypto: ^3.0.3
|
||||
intl: ^0.18.1
|
||||
http: ^1.1.0
|
||||
# http: ^1.1.0
|
||||
# dio: ^5.3.2
|
||||
path_provider:
|
||||
# localstorage: ^4.0.1+4
|
||||
shared_preferences: ^2.2.0
|
||||
# shared_preferences: ^2.2.0
|
||||
# sembast:
|
||||
|
||||
dev_dependencies:
|
||||
|
||||
@@ -71,7 +71,8 @@ if __name__ == "__main__":
|
||||
# print()
|
||||
finalData["menu"][dayNumber] = currentMenu
|
||||
# print(finalData)
|
||||
with open("out.json", "w+") as jsonWriter:
|
||||
json.dump(finalData, jsonWriter)
|
||||
with open("out.txt", "w+") as hashWriter:
|
||||
hashWriter.write(hashlib.md5(json.dumps(finalData).encode("utf-8")).hexdigest())
|
||||
with open("data.dart", "w+") as dataWriter:
|
||||
allData = "String data = r'" + json.dumps(finalData) + "';"
|
||||
dataWriter.write(allData)
|
||||
# with open("out.txt", "w+") as hashWriter:
|
||||
# hashWriter.write(hashlib.md5(json.dumps(finalData).encode("utf-8")).hexdigest())
|
||||
|
||||
Reference in New Issue
Block a user