Syncing issue fixed, Bridge established to server
This commit is contained in:
@@ -36,7 +36,7 @@ class _ActivitiesState extends State<Activities> {
|
||||
// TODO: implement initState
|
||||
super.initState();
|
||||
_focus.addListener(_onFocusChange);
|
||||
UpdateList();
|
||||
//UpdateList();
|
||||
//init(context);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class _CategoriesState extends State<Categories> {
|
||||
// TODO: implement initState
|
||||
super.initState();
|
||||
//init(context);
|
||||
UpdateList();
|
||||
// UpdateList();
|
||||
}
|
||||
|
||||
@override void dispose() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
@@ -137,6 +138,9 @@ class Settings{
|
||||
}else{
|
||||
var uuid = Uuid();
|
||||
String _uuid = uuid.v4();
|
||||
if(Platform.isAndroid){
|
||||
|
||||
}
|
||||
await prefs.setString('uuid',_uuid);
|
||||
return Future.value(_uuid);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class _TasksState extends State<Tasks> {
|
||||
void initState() {
|
||||
// TODO: implement initState
|
||||
super.initState();
|
||||
UpdateList();
|
||||
// UpdateList();
|
||||
// init(context);
|
||||
}
|
||||
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import 'Tasks.dart';
|
||||
import 'Activities.dart';
|
||||
import 'User.dart' as User;
|
||||
import 'package:syncfusion_flutter_charts/charts.dart';
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'Dialogs.dart';
|
||||
final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
|
||||
showAlertDialog(BuildContext context, String title, String message) {
|
||||
@@ -148,6 +147,7 @@ class MyHomePage extends StatefulWidget {
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
var connectivitySub;
|
||||
var refreshSub;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -157,28 +157,33 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
super.initState();
|
||||
print("Initializing refresh stream on main dart");
|
||||
|
||||
connectivitySub=Connectivity().onConnectivityChanged.listen((result) {
|
||||
if (this.mounted) {
|
||||
setState(() {});
|
||||
// connectivitySub=Connectivity().onConnectivityChanged.listen((result) {
|
||||
// if (this.mounted) {
|
||||
// setState(() {});
|
||||
// }
|
||||
// });
|
||||
refreshSub = User.refreshStream.stream.listen((event) {
|
||||
if(event==false){
|
||||
//Update done, Lets go update data
|
||||
LoadStats();
|
||||
}
|
||||
});
|
||||
LoadStats();
|
||||
// User.progressDialog=progressDialog;
|
||||
}
|
||||
var refreshSub;
|
||||
void init(BuildContext context) async{
|
||||
await Future.delayed(Duration(seconds: 1));
|
||||
refreshSub = User.refreshStream.stream.listen((value) {
|
||||
print("Streaming refresh : $value");
|
||||
if(value){
|
||||
Dialogs.waiting("Syncing...");
|
||||
// Dialogs.waiting("Syncing...");
|
||||
print("Opening progress dialog");
|
||||
}else{
|
||||
Dialogs.hide();
|
||||
// Dialogs.hide();
|
||||
print("Closing progress dialog");
|
||||
}
|
||||
});
|
||||
await User.refreshUserData();
|
||||
//await User.refreshUserData();
|
||||
|
||||
}
|
||||
DateTime? lastProductive = null;
|
||||
@@ -187,6 +192,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
// TODO: implement dispose
|
||||
super.dispose();
|
||||
connectivitySub?.cancel();
|
||||
refreshSub?.closeSteam();
|
||||
}
|
||||
void LoadStats() async {
|
||||
// return;
|
||||
@@ -348,7 +354,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
return Scaffold(
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewActivity())).then((value) => {User.refreshUserData().then((va) => LoadStats())});
|
||||
Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewActivity()));
|
||||
},
|
||||
label: Text("New Activity"),
|
||||
icon: Icon(Icons.add)),
|
||||
@@ -369,7 +375,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
: InkWell(
|
||||
onTap: () {
|
||||
setState(() async {
|
||||
await User.refreshUserData();
|
||||
//await User.refreshUserData();
|
||||
LoadStats();
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user