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
|
- name: Checkout code
|
||||||
uses: actions/checkout@v3.6.0
|
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
|
# - name: Cache Flutter dependencies
|
||||||
# uses: actions/cache@v1
|
# uses: actions/cache@v1
|
||||||
# with:
|
# with:
|
||||||
|
|||||||
@@ -14,3 +14,9 @@ const String storageKey = "FullMenu";
|
|||||||
const Center noMenuWidget = Center(
|
const Center noMenuWidget = Center(
|
||||||
child: Text("Menu not available for this session",
|
child: Text("Menu not available for this session",
|
||||||
style: TextStyle(fontSize: 20, fontStyle: FontStyle.italic)));
|
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
|
// ignore_for_file: unnecessary_this, file_names
|
||||||
|
|
||||||
import "dart:convert";
|
import "dart:convert";
|
||||||
import "dart:io";
|
// import "dart:io";
|
||||||
import "package:iiitb_menu/constants.dart";
|
import "package:iiitb_menu/constants.dart";
|
||||||
// import "package:path_provider/path_provider.dart";
|
// import "package:path_provider/path_provider.dart";
|
||||||
// import 'dart:io';
|
// import 'dart:io';
|
||||||
import "package:flutter/material.dart";
|
import "package:flutter/material.dart";
|
||||||
import "package:crypto/crypto.dart";
|
// import "package:crypto/crypto.dart";
|
||||||
import "package:http/http.dart";
|
// import "package:http/http.dart";
|
||||||
import "package:intl/intl.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 {
|
class GlobalModel extends ChangeNotifier {
|
||||||
late Map<dynamic, dynamic> mainData;
|
late Map<dynamic, dynamic> mainData;
|
||||||
@@ -29,76 +31,88 @@ class GlobalModel extends ChangeNotifier {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<String> getLatestHash() async {
|
// static Future<String> getLatestHash() async {
|
||||||
const String hashLink = "https://github.com/kphanipavan/IIITB_Menu/releases/download/latest/out.txt";
|
// try {
|
||||||
try {
|
// // Request hashRequest = Request("get", Uri.parse(hashLink));
|
||||||
Response ret = await get(Uri.parse(hashLink));
|
// Response ret = await Dio().request(hashLink,
|
||||||
if (ret.statusCode == 200) {
|
// options: Options(method: 'GET', headers: {
|
||||||
return ret.body.replaceAll("\n", "");
|
// HttpHeaders.acceptHeader: "text/plain",
|
||||||
} else {
|
// "Access-Control-Allow-Origin": "*",
|
||||||
return "";
|
// "Access-Control-Allow-Methods": "GET",
|
||||||
}
|
// "Access-Control-Allow-Headers": "*",
|
||||||
} on SocketException catch (exce) {
|
// "Access-Control-Max-Age": "1000"
|
||||||
print("Unable to download hash");
|
// }));
|
||||||
print(exce);
|
// if (ret.statusCode == 200) {
|
||||||
return "";
|
// return ret.data.replaceAll("\n", "");
|
||||||
}
|
// } else {
|
||||||
}
|
// return "";
|
||||||
|
// }
|
||||||
|
// } on SocketException catch (exce) {
|
||||||
|
// print("Unable to download hash");
|
||||||
|
// print(exce);
|
||||||
|
// return "";
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
static Future<String> getLatestData() async {
|
// static Future<String> getLatestData() async {
|
||||||
const String dataLink = "https://github.com/kphanipavan/IIITB_Menu/releases/download/latest/out.json";
|
// try {
|
||||||
try {
|
// Response ret = await Dio().request(dataLink,
|
||||||
Response ret = await get(Uri.parse(dataLink));
|
// options: Options(method: "GET", headers: {
|
||||||
return ret.body;
|
// HttpHeaders.acceptHeader: "text/plain",
|
||||||
} on SocketException catch (exce) {
|
// "Access-Control-Allow-Origin": "*"
|
||||||
print("Unable to download any data");
|
// }));
|
||||||
print(exce);
|
// return ret.data;
|
||||||
return "";
|
// } on SocketException catch (exce) {
|
||||||
}
|
// print("Unable to download any data");
|
||||||
}
|
// print(exce);
|
||||||
|
// return "";
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
static Future<Map> loadData() async {
|
static Future<Map> loadData() async {
|
||||||
late Map returnData;
|
late Map returnData;
|
||||||
// Directory appDir = await getApplicationDocumentsDirectory();
|
// Directory appDir = await getApplicationDocumentsDirectory();
|
||||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
// final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
String? rawData;
|
// String? rawData;
|
||||||
String rawHash;
|
// String rawHash;
|
||||||
// String fileName = "${appDir.path}/menu.json";
|
// String fileName = "${appDir.path}/menu.json";
|
||||||
// print(fileName);
|
// print(fileName);
|
||||||
// File fileLink = File(fileName);
|
// File fileLink = File(fileName);
|
||||||
rawData = prefs.getString(storageKey);
|
// rawData = prefs.getString("asdasdasd");
|
||||||
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: ");
|
|
||||||
// print(rawData);
|
// print(rawData);
|
||||||
returnData = jsonDecode(rawData);
|
// if (rawData != null) {
|
||||||
// menuAvailable = true;
|
// print("data found");
|
||||||
} else {
|
// // rawData = localStor.getItem("fullMenu");
|
||||||
print("File Not Found");
|
// rawHash = md5.convert(utf8.encode(rawData)).toString();
|
||||||
returnData = {};
|
// print("Hash of Raw Data:");
|
||||||
rawHash = "";
|
// print(rawHash);
|
||||||
}
|
// // print("RawData: ");
|
||||||
String remoteHash = await GlobalModel.getLatestHash();
|
// // print(rawData);
|
||||||
if (remoteHash == "") {
|
// returnData = jsonDecode(rawData);
|
||||||
} else if (remoteHash == rawHash) {
|
// // 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");
|
// 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("Remote hash $remoteHash is NOT the same as local hash $rawHash");
|
||||||
// print("Getting data from remote");
|
// print("Getting data from remote");
|
||||||
rawData = await getLatestData();
|
// rawData = await getLatestData();
|
||||||
if (rawData == "") {
|
// if (rawData == "") {
|
||||||
return {};
|
// return {};
|
||||||
}
|
// }
|
||||||
returnData = jsonDecode(rawData);
|
// returnData = jsonDecode(rawData);
|
||||||
// fileLink.writeAsStringSync(rawData, mode: FileMode.write);
|
// // fileLink.writeAsStringSync(rawData, mode: FileMode.write);
|
||||||
prefs.setString(storageKey, rawData);
|
// prefs.setString(storageKey, rawData);
|
||||||
}
|
// }
|
||||||
|
returnData = jsonDecode(menuData.data);
|
||||||
return returnData;
|
return returnData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,7 @@ import FlutterMacOS
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
import path_provider_foundation
|
import path_provider_foundation
|
||||||
import shared_preferences_foundation
|
|
||||||
|
|
||||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
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.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.2
|
cupertino_icons: ^1.0.2
|
||||||
provider: ^6.0.5
|
provider: ^6.0.5
|
||||||
crypto: ^3.0.3
|
# crypto: ^3.0.3
|
||||||
intl: ^0.18.1
|
intl: ^0.18.1
|
||||||
http: ^1.1.0
|
# http: ^1.1.0
|
||||||
|
# dio: ^5.3.2
|
||||||
path_provider:
|
path_provider:
|
||||||
# localstorage: ^4.0.1+4
|
# localstorage: ^4.0.1+4
|
||||||
shared_preferences: ^2.2.0
|
# shared_preferences: ^2.2.0
|
||||||
# sembast:
|
# sembast:
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|||||||
@@ -71,7 +71,8 @@ if __name__ == "__main__":
|
|||||||
# print()
|
# print()
|
||||||
finalData["menu"][dayNumber] = currentMenu
|
finalData["menu"][dayNumber] = currentMenu
|
||||||
# print(finalData)
|
# print(finalData)
|
||||||
with open("out.json", "w+") as jsonWriter:
|
with open("data.dart", "w+") as dataWriter:
|
||||||
json.dump(finalData, jsonWriter)
|
allData = "String data = r'" + json.dumps(finalData) + "';"
|
||||||
with open("out.txt", "w+") as hashWriter:
|
dataWriter.write(allData)
|
||||||
hashWriter.write(hashlib.md5(json.dumps(finalData).encode("utf-8")).hexdigest())
|
# 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