Optimize Loading times

This commit is contained in:
Sewmina
2022-04-15 05:51:39 +05:30
parent 17340890ed
commit 39a9a66f56
5 changed files with 493 additions and 95 deletions

View File

@@ -64,8 +64,15 @@ Future<void> initUserData() async {
if (cacheEnabled) {
await initCacheDatabase();
}
int catCount = Sqflite.firstIntValue(await cacheDb.rawQuery('SELECT COUNT(*) FROM Categories')) ?? 0;
int taskCount = Sqflite.firstIntValue(await cacheDb.rawQuery('SELECT COUNT(*) FROM TaskTypes')) ?? 0;
if(catCount > 0 && taskCount > 0){
await refreshUserData(forceOffline: true);
refreshUserData();
}else{
await refreshUserData();
}
await refreshUserData();
BuildBridgeToServer();
Debug.Log('Initializing UserData...');
@@ -143,19 +150,61 @@ Future<void> refreshUserData({bool forceOffline = false}) async {
refreshStream.add(false);
} else {
Debug.LogTest('updating cats ${DateTime.now()}');
categories = await GetCategories(false);
Debug.LogTest('updating projs ${DateTime.now()}');
projects = await GetProjects(false);
Debug.LogTest('updating tasks ${DateTime.now()}');
taskTypes = await GetTaskTypes(false);
Debug.LogTest('updating acts ${DateTime.now()}');
activities = await GetActivities(false);
Debug.LogTest('updating journals ${DateTime.now()}');
journal = await GetJournals(false);
Debug.LogTest('updating todos ${DateTime.now()}');
todos= await GetTodos(false);
Debug.LogTest('done refreshing ${DateTime.now()}');
if(false) {
Debug.LogTest('updating cats ${DateTime.now()}');
categories = await GetCategories(false);
Debug.LogTest('updating projs ${DateTime.now()}');
projects = await GetProjects(false);
Debug.LogTest('updating tasks ${DateTime.now()}');
taskTypes = await GetTaskTypes(false);
Debug.LogTest('updating acts ${DateTime.now()}');
activities = await GetActivities(false);
Debug.LogTest('updating journals ${DateTime.now()}');
journal = await GetJournals(false);
Debug.LogTest('updating todos ${DateTime.now()}');
todos = await GetTodos(false);
Debug.LogTest('done refreshing ${DateTime.now()}');
}else {
Debug.LogTest('Doing super update ${DateTime.now()}');
refreshStream.add(true);
try {
Debug.LogTest('Requesting all data @ ${DateTime.now()}');
http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/grab_all_data.php'),
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
List<String> data = response.body.split('<tr>');
Debug.LogTest('Got response for all data [${response.contentLength}bytes] @ ${DateTime.now()}');
Debug.LogTest('super update items : ${data.length}\nfilling tables\nActivities : ${data[0].length}\nProjects: ${data[1].length}');
Debug.LogResponse(response.body);
Debug.LogResponse(data[0],src: 'Activity');
Debug.LogResponse(data[1],src: 'Categories');
Debug.LogResponse(data[2],src: 'Projects');
Debug.LogResponse(data[3],src: 'Journals');
Debug.LogResponse(data[4],src: 'TaskTypes');
Debug.LogResponse(data[5],src: 'Todo');
await fillActivityTable(data[0]);
await fillCatsTable(data[1]);
await fillJournalsTable(data[2]);
await fillProjectsTable(data[3]);
await fillTaskTypes(data[4]);
await fillTodosTable(data[5]);
Debug.LogTest('Done filling @ ${DateTime.now()}');
categories = await GetCategories(true);
projects = await GetProjects(true);
taskTypes = await GetTaskTypes(true);
activities = await GetActivities(true);
journal = await GetJournals(true);
todos = await GetTodos(true);
} catch (e) {
Debug.LogError("Error while Super Update $e");
}
refreshStream.add(false);
Debug.LogTest('Done super update ${DateTime.now()}');
}
}
m_refreshing = false;
@@ -343,20 +392,24 @@ Future<void> UpdateCategoriesFromServer() async {
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
Debug.LogResponse(response.body);
List<String> data = response.body.split("<td>");
await cacheDb.delete("Categories");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(catData);
await cacheDb
.rawInsert("INSERT OR REPLACE INTO Categories (${Category.colCatId},${Category.colName},${Category.colProductive},${Category.colColor}) "
"VALUES ('${cat['category_id']}','${cat['name']}',${cat['productive']},'${cat['color']}') ");
}
await fillCatsTable(response.body);
} catch (e) {
Debug.LogError("Error while cats $e");
}
}
Future<void> fillCatsTable(String response) async {
List<String> data = response.split("<td>");
await cacheDb.delete("Categories");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(catData);
await cacheDb
.rawInsert("INSERT OR REPLACE INTO Categories (${Category.colCatId},${Category.colName},${Category.colProductive},${Category.colColor}) "
"VALUES ('${cat['category_id']}','${cat['name']}',${cat['productive']},'${cat['color']}') ");
}
}
Future<List<TaskType>> GetTaskTypes(bool forceOffline) async {
if (cacheEnabled) {
List<TaskType> _taskTypes = [];
@@ -443,22 +496,26 @@ Future<void> UpdateTaskTypesFromServer() async {
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
Debug.LogResponse(response.body);
List<String> data = response.body.split("<td>");
await cacheDb.delete("TaskTypes");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(cat);
await cacheDb
.rawInsert("INSERT OR REPLACE INTO TaskTypes (${TaskType.colId},${TaskType.colName},${TaskType.colCategory},${TaskType.colRelatedProject}) "
"VALUES ('${cat['task_id']}','${cat['name']}','${cat['category_id']}', '${cat['related_project']}') ");
Debug.LogResponse(await cacheDb.query("TaskTypes"));
}
await fillTaskTypes(response.body);
} catch (e) {
Debug.LogError("Error while tasks $e");
}
}
Future<void> fillTaskTypes(String response) async {
List<String> data = response.split("<td>");
await cacheDb.delete("TaskTypes");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(cat);
await cacheDb
.rawInsert("INSERT OR REPLACE INTO TaskTypes (${TaskType.colId},${TaskType.colName},${TaskType.colCategory},${TaskType.colRelatedProject}) "
"VALUES ('${cat['task_id']}','${cat['name']}','${cat['category_id']}', '${cat['related_project']}') ");
Debug.LogResponse(await cacheDb.query("TaskTypes"));
}
}
void StartActivityTimer(String taskType, String metadata, DateTime startTime) async {
final prefs = await SharedPreferences.getInstance();
@@ -618,28 +675,33 @@ Future<void> UpdateActivitiesFromServer() async {
http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_activities.php'),
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
await cacheDb.rawDelete("DELETE FROM Activities");
Debug.Log('Truncate Activity Table before');
await fillActivityTable(response.body);
// Debug.Log("Activity response: ${response.body}");
if (response.body.contains("{")) {
List<String> data = response.body.split("<td>");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(cat);
await cacheDb.rawInsert(
"INSERT OR REPLACE INTO Activities (${Activity.colType}, ${Activity.colStartTime}, ${Activity.colEndTime}, ${Activity.colMetadata}) "
"VALUES ('${cat['task_id']}', '${cat['sTime']}','${cat['eTime']}', '${cat['metadata'].toString().replaceAll("'", "''")}') ");
}
} else {
Debug.Log("No activities for now");
}
} catch (e) {
Debug.Log("Error while acts $e");
}
}
Future<void> fillActivityTable(String response) async{
if (response.contains("{")) {
await cacheDb.rawDelete("DELETE FROM Activities");
Debug.Log('Truncate Activity Table before');
List<String> data = response.split("<td>");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(cat);
await cacheDb.rawInsert(
"INSERT OR REPLACE INTO Activities (${Activity.colType}, ${Activity.colStartTime}, ${Activity.colEndTime}, ${Activity.colMetadata}) "
"VALUES ('${cat['task_id']}', '${cat['sTime']}','${cat['eTime']}', '${cat['metadata'].toString().replaceAll("'", "''")}') ");
}
} else {
Debug.Log("No activities for now");
}
}
Future<List<Project>> GetProjects(bool forceOffline) async {
if (cacheEnabled) {
List<Project> _projects = [];
@@ -740,28 +802,32 @@ Future<void> UpdateProjectsFromServer() async {
http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_projects.php'),
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
await cacheDb.rawDelete("DELETE FROM Projects");
Debug.Log('Truncate Projects Table before');
Debug.LogResponse("Projects response: ${response.body}");
if (response.body.contains("{")) {
List<String> data = response.body.split("<td>");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
Debug.LogResponse(cat, src: 'project data');
await cacheDb.rawInsert(
"INSERT OR REPLACE INTO Projects (${Project.colName}, ${Project.colCat}, ${Project.colSteps}, ${Project.colDeadline}, ${Project.colEta}) "
"VALUES ('${cat['name']}', '${cat['category']}', '${cat['steps']}', '${cat['deadline']}', ${cat['eta']})");
}
} else {
Debug.Log("No activities for now");
}
await fillProjectsTable(response.body);
} catch (e) {
Debug.LogError("Error while acts $e");
}
}
Future<void> fillProjectsTable(String response) async {
await cacheDb.rawDelete("DELETE FROM Projects");
Debug.Log('Truncate Projects Table before');
Debug.LogResponse("Projects response: ${response}");
if (response.contains("{")) {
List<String> data = response.split("<td>");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
Debug.LogResponse(cat, src: 'project data');
await cacheDb.rawInsert(
"INSERT OR REPLACE INTO Projects (${Project.colName}, ${Project.colCat}, ${Project.colSteps}, ${Project.colDeadline}, ${Project.colEta}) "
"VALUES ('${cat['name']}', '${cat['category']}', '${cat['steps']}', '${cat['deadline']}', ${cat['eta']})");
}
} else {
Debug.Log("No projects for now");
}
}
Future<List<Journal>> GetJournals(bool forceOffline) async {
if (cacheEnabled) {
List<Journal> _journals = [];
@@ -834,20 +900,27 @@ Future<void> UpdateJournalsFromServer() async {
http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_journals.php'),
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
Debug.LogResponse(response.body,src:'Journal');
List<String> data = response.body.split("<td>");
await cacheDb.delete("Journal");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(catData);
await cacheDb.rawInsert("INSERT OR REPLACE INTO Journal (id, ${Journal.colTitle},${Journal.colDescription}) "
"VALUES ('${cat['id']}','${cat['title'].toString().replaceAll("'", "''")}','${cat['description'].toString().replaceAll("'", "''")}') ");
}
await fillJournalsTable(response.body);
} catch (e) {
Debug.LogError("Error while cats $e");
}
}
Future<void> fillJournalsTable(String response) async {
Debug.LogResponse(response,src:'Journal');
if(response.isEmpty){
Debug.Log('Empty Journal');
}else{
List<String> data = response.split("<td>");
await cacheDb.delete("Journal");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(catData);
await cacheDb.rawInsert("INSERT OR REPLACE INTO Journal (id, ${Journal.colTitle},${Journal.colDescription}) "
"VALUES ('${cat['id']}','${cat['title'].toString().replaceAll("'", "''")}','${cat['description'].toString().replaceAll("'", "''")}') ");
}}
}
Future<List<Todo>> GetTodos(bool forceOffline) async {
if (cacheEnabled) {
List<Todo> _todos = [];
@@ -927,16 +1000,27 @@ Future<void> UpdateTodosFromServer() async {
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
Debug.LogResponse(response.body);
List<String> data = response.body.split("<td>");
await fillTodosTable(response.body);
} catch (e) {
Debug.LogError("Error while cats $e");
}
}
Future<void> fillTodosTable(String response) async {
if (response.contains("{")) {
List<String> data = response.split("<td>");
await cacheDb.delete("Todos");
for (var value in data) {
Map<String, dynamic> cat = jsonDecode(value);
//Debug.Log(catData);
await cacheDb.rawInsert("INSERT OR REPLACE INTO Todos (id, ${Todo.colCat},${Todo.colMetadata},${Todo.colDueDate},${Todo.colNotificationTime}) "
"VALUES ('${cat['id'].toString().replaceAll("'", "''")}', '${cat['task_id']}', '${cat['metadata'].toString().replaceAll("'", "''")}', '${cat['due_date']}', '${cat['notification_time']}') ");
"VALUES ('${cat['id'].toString().replaceAll("'", "''")}', '${cat['task_id']}', '${cat['metadata'].toString().replaceAll(
"'", "''")}', '${cat['due_date']}', '${cat['notification_time']}') ");
}
} catch (e) {
Debug.LogError("Error while cats $e");
}else{
Debug.Log('Empty Todos');
}
}