Syncing issue fixed, Bridge established to server

This commit is contained in:
Sewmina
2022-03-20 04:59:53 +05:30
parent 7d8eea7375
commit b09d74bba3
10 changed files with 83 additions and 87 deletions

View File

@@ -1,7 +1,6 @@
import 'dart:async';
import 'dart:io';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -32,7 +31,7 @@ List<Project> projects = [];
bool offline = true;
bool registered = false;
StreamController<bool> refreshStream = StreamController<bool>();
StreamController<bool> refreshStream = StreamController.broadcast();
Future<http.Response> login(String _username, String password) async {
final prefs = await SharedPreferences.getInstance();
@@ -64,28 +63,68 @@ Future<void> initUserData() async {
}
await refreshUserData();
BuildBridgeToServer();
print('Initializing UserData...');
if (cacheEnabled) {
if (Platform.isAndroid || Platform.isIOS) {
Connectivity().onConnectivityChanged.listen((result) {
offline = (result == ConnectivityResult.none);
if (!offline) {
UserOperations.executeQueries();
refreshUserData();
}
});
}
// if (Platform.isAndroid || Platform.isIOS) {
// Connectivity().onConnectivityChanged.listen((result) {
// offline = (result == ConnectivityResult.none);
// if (!offline) {
// UserOperations.executeQueries();
// refreshUserData();
// }
// });
// }
}
}
Future<void> BuildBridgeToServer() async{
final prefs = await SharedPreferences.getInstance();
while(true){
try {
http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/bridge.php'),
body: <String, String>{"username": username}));
print("bridge retreive (try): ${response.body}");
List<String> data = response.body.split(",");
print('Update :\nactivities_rev=${data[0]} tasks_rev=${data[1]} cats_rev=${data[2]} projects_rev=${data[3]}');
if(prefs.containsKey('rev')){
if(prefs.getString('rev') == response.body){
print('We are on the same page');
}else{
print('Gotta update');
await refreshUserData();
prefs.setString('rev',response.body);
}
}else{
prefs.setString('rev', response.body);
await refreshUserData();
}
}catch(e){
print("Error with bridge : $e");
}
await Future.delayed(Duration(seconds: 5));
}
}
bool m_refreshing = false;
Future<void> refreshUserData() async {
print('refreshing user data');
if(m_refreshing){
print('Called while refreshing. Return');return;
}
m_refreshing=true;
refreshStream.add(true);
await updateCatsList();
await updateTasksList();
await updateActList();
await updateProjectsList();
refreshStream.add(false);
m_refreshing=false;
if (cacheEnabled) {
NotificationManager.RescheduleNotifications();
}
@@ -710,7 +749,7 @@ class UserOperations {
Category.colProductive: productive
};
await cacheDb.insert('Categories', data);
await refreshUserData();
// await refreshUserData();
} else {
try {
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/add_category.php'), body: queryBody));
@@ -749,7 +788,7 @@ class UserOperations {
//update Cache
Map<String, Object> data = {TaskType.colId: username + name, Category.colName: name, Category.colCatId: username + category};
await cacheDb.insert('TaskTypes', data);
await refreshUserData();
//await refreshUserData();
} else {
try {
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/add_taskType.php'), body: queryBody));
@@ -805,7 +844,7 @@ class UserOperations {
Activity.colMetadata: metadata
};
await cacheDb.insert('Activities', data);
await refreshUserData();
// await refreshUserData();
} else {
try {
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/add_activity.php'), body: queryBody));
@@ -859,7 +898,7 @@ class UserOperations {
// Map<String, Object> data = {Activity.colType: username + type, Activity.colStartTime: dFormat.format(sTime), Activity.colEndTime: dFormat.format(eTime), Activity.colMetadata: metadata};
// String updateActQuery = "UPDATE Activities SET ${Activity.colType}=";
// await cacheDb.insert('Activities', data);
await refreshUserData();
// await refreshUserData();
} else {
// queries.add(Queries('edit_activity', jsonEncode(queryBody)));
try {
@@ -899,7 +938,7 @@ class UserOperations {
//update Cache
Map<String, Object> data = {Project.colName: username+name, Project.colCat: category, Project.colSteps: jsonEncode(steps), Project.colDeadline: deadline.toString()};
await cacheDb.insert('Projects', data);
await refreshUserData();
//await refreshUserData();
} else {
try {
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/add_project.php'), body: queryBody));
@@ -935,7 +974,7 @@ class UserOperations {
};
await cacheDb.rawDelete("DELETE FROM TaskTypes WHERE id='${username + name}'");
await refreshUserData();
// await refreshUserData();
//Add to server and refresh Cache
} else {
try {
@@ -972,7 +1011,7 @@ class UserOperations {
Category.colName: name,
};
await cacheDb.rawDelete("DELETE FROM Categories WHERE ${Category.colCatId}='${username + name}'");
await refreshUserData();
//await refreshUserData();
//Add to server and refresh Cache
} else {
try {
@@ -1011,7 +1050,7 @@ class UserOperations {
print("delteQuery : $deleteQuery");
await cacheDb.rawDelete(deleteQuery);
await refreshUserData();
// await refreshUserData();
//Add to server and refresh Cache
} else {
try {
@@ -1060,8 +1099,9 @@ class UserOperations {
print("Error while query $e");
}
}
await refreshUserData();
//await refreshUserData();
} else {
await refreshUserData();
print('Trying to execute queries in no cahce mode, We dont do that here');
}
}