data = response.body.split(",");
- Debug.LogResponse('Update :\nactivities_rev=${data[0]} tasks_rev=${data[1]} cats_rev=${data[2]} projects_rev=${data[3]}');
- bool changedOffline = offline==true;
- offline=false;
- if(changedOffline){refreshStream.add(false);}
+ Debug.LogResponse(
+ 'Update :\nactivities_rev=${data[0]} tasks_rev=${data[1]} cats_rev=${data[2]} projects_rev=${data[3]}');
+ bool changedOffline = offline == true;
+ offline = false;
+ if (changedOffline) {
+ refreshStream.add(false);
+ }
if (data[4].contains('')) {
List ongoingData = data[4].split("");
if (!prefs.containsKey('current_activity')) {
- StartActivityTimer(ongoingData[0], ongoingData[1], DateTime.parse(ongoingData[2]));
+ StartActivityTimer(
+ ongoingData[0], ongoingData[1], DateTime.parse(ongoingData[2]));
}
} else {
if (prefs.containsKey('current_activity')) CancelOngoingActivity();
@@ -131,12 +145,33 @@ Future BuildBridgeToServer() async {
} catch (e) {
Debug.LogError("Error with bridge : $e");
bool changedOffline = offline == false;
- offline=true;
- if(changedOffline){refreshStream.add(false);}
+ offline = true;
+ if (changedOffline) {
+ refreshStream.add(false);
+ }
}
await Future.delayed(Duration(seconds: 5));
}
}
+const APP_USAGE_KEY_NAME = "app_usage_data";
+Future InitUsageListener() async {
+ final prefs = await SharedPreferences.getInstance();
+ var delay = Duration(seconds: 10);
+ if(!prefs.containsKey(APP_USAGE_KEY_NAME)){
+ prefs.setString(APP_USAGE_KEY_NAME, "");
+ }
+
+ List app_usage = [];
+ while (true) {
+ List appUsage = await AppUsage().getAppUsage(DateTime.now().subtract(delay), DateTime.now());
+
+ List prev_appUsage = jsonDecode(prefs.getString(APP_USAGE_KEY_NAME)!);
+
+
+ Debug.Log("Listening to usage");
+ await Future.delayed(delay);
+ }
+}
bool m_refreshing = false;
Future refreshUserData({bool forceOffline = false}) async {
@@ -158,7 +193,7 @@ Future refreshUserData({bool forceOffline = false}) async {
refreshStream.add(false);
} else {
- if(K.kIsWeb) {
+ if (K.kIsWeb) {
Debug.LogTest('updating cats ${DateTime.now()}');
categories = await GetCategories(false);
Debug.LogTest('updating projs ${DateTime.now()}');
@@ -172,23 +207,29 @@ Future refreshUserData({bool forceOffline = false}) async {
Debug.LogTest('updating todos ${DateTime.now()}');
todos = await GetTodos(false);
Debug.LogTest('done refreshing ${DateTime.now()}');
- }else {
+ } 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: {"username": username, "device_id": await Settings.UUID()}));
+ http.Response response = (await http.post(
+ Uri.parse(API_ENDPOINT + '/task_tracker/grab_all_data.php'),
+ body: {
+ "username": username,
+ "device_id": await Settings.UUID()
+ }));
List data = response.body.split('');
- 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.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');
+ 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]);
@@ -199,21 +240,42 @@ Future refreshUserData({bool forceOffline = false}) async {
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);
-
-
+ 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()}');
}
+ DateTime now = DateTime.now();
+ List appUsage =
+ await AppUsage().getAppUsage(now.subtract(Duration(days: 2)), now);
+
+ Debug.Log("Adding auto logs");
+ appUsage.forEach((task) {
+ bool foundConflict = false;
+ for (int i = 1; i < activities.length; i++) {
+ Activity act = activities[activities.length - i];
+
+ if (isConflicting(
+ act.startTime, act.endTime, task.startDate, task.endDate)) {
+ //Got something here
+ continue;
+ }
+
+ // UserOperations.addActivity("At work", task.startDate, task.endDate);
+ Debug.LogTest(task);
+
+ if (act.startTime.isBefore(now.subtract(Duration(days: 2)))) {
+ break;
+ }
+ }
+ });
}
m_refreshing = false;
@@ -222,6 +284,30 @@ Future refreshUserData({bool forceOffline = false}) async {
}
}
+bool isConflicting(
+ DateTime start1, DateTime end1, DateTime start2, DateTime end2) {
+ if (start1.isBefore(end2) && end1.isAfter(end2)) {
+ // 1 starts before 2 and continues after 2 is done
+ return true;
+ }
+
+ if (end1.isBefore(end2) && end1.isAfter(start2)) {
+ // 1 ends within 2
+ return true;
+ }
+
+ if (start1.isAfter(start2) && start1.isBefore(end2)) {
+ //1 starts within 2
+ return true;
+ }
+
+ if (start1 == start2 || end1 == end2) {
+ return true;
+ }
+
+ return false;
+}
+
Future cacheDbExist() async {
if (!K.kIsWeb && Platform.isAndroid || Platform.isIOS) {
Directory directory = await getApplicationDocumentsDirectory();
@@ -252,10 +338,15 @@ Future initCacheDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
Debug.LogResponse('database at ' + directory.path + '/cache.db');
if (!K.kIsWeb && Platform.isAndroid || Platform.isIOS) {
- cacheDb = await openDatabase(directory.path + 'cache.db', version: 1, onCreate: onCacheDatabaseCreate, onUpgrade: onCacheDatabaseUpgrade);
+ cacheDb = await openDatabase(directory.path + 'cache.db',
+ version: 1,
+ onCreate: onCacheDatabaseCreate,
+ onUpgrade: onCacheDatabaseUpgrade);
} else {
- cacheDb = await SqlFF.databaseFactoryFfi
- .openDatabase(directory.path + 'cache.db', options: OpenDatabaseOptions(version: 1, onCreate: onCacheDatabaseCreate));
+ cacheDb = await SqlFF.databaseFactoryFfi.openDatabase(
+ directory.path + 'cache.db',
+ options:
+ OpenDatabaseOptions(version: 1, onCreate: onCacheDatabaseCreate));
}
await UserOperations.executeQueries();
}
@@ -283,13 +374,16 @@ void onCacheDatabaseCreate(Database db, int newVersion) async {
'CREATE TABLE Projects(id TEXT PRIMARY KEY, ${Project.colName} TEXT, ${Project.colCat} TEXT, ${Project.colSteps} TEXT, ${Project.colEta} INT, ${Project.colDeadline} DATETIME)';
await db.execute(ProjectsTableSQL);
- String JournalTableSQL = 'CREATE TABLE Journal(id TEXT PRIMARY KEY, ${Journal.colTitle} TEXT, ${Journal.colDescription})';
+ String JournalTableSQL =
+ 'CREATE TABLE Journal(id TEXT PRIMARY KEY, ${Journal.colTitle} TEXT, ${Journal.colDescription})';
await db.execute(JournalTableSQL);
- String TodoTableSQL = 'CREATE TABLE Todos(id TEXT PRIMARY KEY, ${Todo.colCat} TEXT, ${Todo.colMetadata} TEXT, ${Todo.colDueDate} DATE, ${Todo.colNotificationTime} DATETIME)';
+ String TodoTableSQL =
+ 'CREATE TABLE Todos(id TEXT PRIMARY KEY, ${Todo.colCat} TEXT, ${Todo.colMetadata} TEXT, ${Todo.colDueDate} DATE, ${Todo.colNotificationTime} DATETIME)';
await db.execute(TodoTableSQL);
- String QueriesTableSQL = 'CREATE TABLE Queries(id INTEGER PRIMARY KEY AUTOINCREMENT, ${Queries.colLink} TEXT,${Queries.colData} TEXT)';
+ String QueriesTableSQL =
+ 'CREATE TABLE Queries(id INTEGER PRIMARY KEY AUTOINCREMENT, ${Queries.colLink} TEXT,${Queries.colData} TEXT)';
await db.execute(QueriesTableSQL);
final prefs = await SharedPreferences.getInstance();
@@ -308,11 +402,14 @@ Future addInitialDataToCache() async {
//Insert Initial Entries
for (Category element in InitialData.getCategories(username)) {
- await UserOperations.addCategory(element.name, element.color, element.productive, bulk: true);
+ await UserOperations.addCategory(
+ element.name, element.color, element.productive,
+ bulk: true);
}
Dialogs.syncingMessage = "Just a minute";
for (TaskType element in InitialData.getTaskTypes(username)) {
- await UserOperations.addTaskType(element.name, element.category, bulk: true);
+ await UserOperations.addTaskType(element.name, element.category,
+ bulk: true);
// Map data = {
// TaskType.colName: element.name,
// TaskType.colCategory: element.category
@@ -326,7 +423,8 @@ Future addInitialDataToCache() async {
void onCacheDatabaseUpgrade(Database db, int oldVersion, int newVersion) async {
//ValidateCacheDB();
- Debug.LogResponse('Upgrading CacheDB from ver.$oldVersion to ver.$newVersion');
+ Debug.LogResponse(
+ 'Upgrading CacheDB from ver.$oldVersion to ver.$newVersion');
}
Future> GetCategories(bool forceOffline) async {
@@ -343,7 +441,7 @@ Future> GetCategories(bool forceOffline) async {
bool catsUpdated = false;
// try {
- // http.Response update_response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/check_update.php'), body: {"username": username, "device_id": android_id}));
+ // http.Response update_response = (await http.post(Uri.parse(API_ENDPOINT+'/task_tracker/check_update.php'), body: {"username": username, "device_id": android_id}));
// final data = update_response.body.split(',');
// catsUpdated = data[0] == '1';
// } catch (e) {
@@ -366,25 +464,32 @@ Future> GetCategories(bool forceOffline) async {
String? catProductive = element[Category.colProductive].toString();
if (catName == null || catColor == null || catProductive == null) {
Debug.LogError("Something is null!");
- Debug.LogError("name:{$catName}, color:{$catColor}, prod:{$Category.colProductive}");
+ Debug.LogError(
+ "name:{$catName}, color:{$catColor}, prod:{$Category.colProductive}");
continue;
}
// Debug.Log("name:{$catName}, color:{$catColor}, prod:{$catProductive}");
- _categories.add(Category(username + catName, catName, catColor, ParseBool(catProductive)));
+ _categories.add(Category(
+ username + catName, catName, catColor, ParseBool(catProductive)));
}
categories = _categories;
} else {
Debug.Log("NC: Updating Categories as $username");
try {
- http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_categories.php'),
- body: {"username": username, "device_id": await Settings.UUID()}));
+ http.Response response = (await http.post(
+ Uri.parse(API_ENDPOINT + '/task_tracker/get_categories.php'),
+ body: {
+ "username": username,
+ "device_id": await Settings.UUID()
+ }));
- Debug.LogResponse(response.body,src:'get_categories');
+ Debug.LogResponse(response.body, src: 'get_categories');
List data = response.body.split("");
List _categories = [];
for (var value in data) {
Map cat = jsonDecode(value);
- _categories.add(Category(cat['category_id'], cat['name'], cat['color'], ParseBool(cat['productive'])));
+ _categories.add(Category(cat['category_id'], cat['name'], cat['color'],
+ ParseBool(cat['productive'])));
}
categories = _categories;
} catch (e) {
@@ -397,8 +502,12 @@ Future> GetCategories(bool forceOffline) async {
Future UpdateCategoriesFromServer() async {
Debug.Log("Updating Categories as $username");
try {
- http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_categories.php'),
- body: {"username": username, "device_id": await Settings.UUID()}));
+ http.Response response = (await http.post(
+ Uri.parse(API_ENDPOINT + '/task_tracker/get_categories.php'),
+ body: {
+ "username": username,
+ "device_id": await Settings.UUID()
+ }));
Debug.LogResponse(response.body);
await fillCatsTable(response.body);
@@ -413,8 +522,8 @@ Future fillCatsTable(String response) async {
for (var value in data) {
Map cat = jsonDecode(value);
//Debug.Log(catData);
- await cacheDb
- .rawInsert("INSERT OR REPLACE INTO Categories (${Category.colCatId},${Category.colName},${Category.colProductive},${Category.colColor}) "
+ 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']}') ");
}
}
@@ -432,7 +541,7 @@ Future> GetTaskTypes(bool forceOffline) async {
bool updated = false;
// try {
// //Validate device_id to check updates
- // http.Response update_response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/check_update.php'), body: {"username": username, "device_id": android_id}));
+ // http.Response update_response = (await http.post(Uri.parse(API_ENDPOINT+'/task_tracker/check_update.php'), body: {"username": username, "device_id": android_id}));
// final data = update_response.body.split(',');
// updated = data[1] == '1';
// } catch (e) {
@@ -469,15 +578,20 @@ Future> GetTaskTypes(bool forceOffline) async {
}
// Debug.Log("name:{$name}, cat:{$category}, prod:{$id}");
- _taskTypes.add(TaskType(id, name, category, cat: cat, relatedProject: relatedProject));
+ _taskTypes.add(TaskType(id, name, category,
+ cat: cat, relatedProject: relatedProject));
}
taskTypes = _taskTypes;
} else {
List _taskTypes = [];
Debug.Log("NC: Updating TaskTypes as $username");
try {
- http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_taskTypes.php'),
- body: {"username": username, "device_id": await Settings.UUID()}));
+ http.Response response = (await http.post(
+ Uri.parse(API_ENDPOINT + '/task_tracker/get_taskTypes.php'),
+ body: {
+ "username": username,
+ "device_id": await Settings.UUID()
+ }));
Debug.LogResponse("taskType retreive (try): ${response.body}");
List data = response.body.split("");
@@ -485,7 +599,9 @@ Future> GetTaskTypes(bool forceOffline) async {
for (var value in data) {
Map data = jsonDecode(value);
Category? cat = await getCatFromId(data['category_id']);
- _taskTypes.add(TaskType(data['task_id'], data['name'], data['category_id'], cat: cat, relatedProject: data['related_project']));
+ _taskTypes.add(TaskType(
+ data['task_id'], data['name'], data['category_id'],
+ cat: cat, relatedProject: data['related_project']));
//Debug.Log(cat);
}
} catch (e) {
@@ -501,8 +617,12 @@ Future UpdateTaskTypesFromServer() async {
// await GetCategories(true);
Debug.Log("Updating TaskTypes as $username");
try {
- http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_taskTypes.php'),
- body: {"username": username, "device_id": await Settings.UUID()}));
+ http.Response response = (await http.post(
+ Uri.parse(API_ENDPOINT + '/task_tracker/get_taskTypes.php'),
+ body: {
+ "username": username,
+ "device_id": await Settings.UUID()
+ }));
Debug.LogResponse(response.body);
await fillTaskTypes(response.body);
@@ -517,18 +637,20 @@ Future fillTaskTypes(String response) async {
for (var value in data) {
Map cat = jsonDecode(value);
//Debug.Log(cat);
- await cacheDb
- .rawInsert("INSERT OR REPLACE INTO TaskTypes (${TaskType.colId},${TaskType.colName},${TaskType.colCategory},${TaskType.colRelatedProject}) "
+ 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 {
+void StartActivityTimer(
+ String taskType, String metadata, DateTime startTime) async {
final prefs = await SharedPreferences.getInstance();
- prefs.setString('current_activity', "$taskType| $metadata | ${UserOperations.dFormat.format(startTime)}");
+ prefs.setString('current_activity',
+ "$taskType | $metadata | ${UserOperations.dFormat.format(startTime)}");
NotificationManager.RescheduleNotifications();
UserOperations.startOngoing(prefs.getString('current_activity')!);
@@ -549,7 +671,8 @@ void StopActivityTimer() async {
final prefs = await SharedPreferences.getInstance();
try {
List data = prefs.getString('current_activity')!.split("");
- UserOperations.addActivity(data[0], DateTime.parse(data[2]), DateTime.now(), metadata: data[1]);
+ UserOperations.addActivity(data[0], DateTime.parse(data[2]), DateTime.now(),
+ metadata: data[1]);
} catch (e) {}
prefs.remove('current_activity');
UserOperations.stopOngoing();
@@ -574,7 +697,7 @@ Future> GetActivities(bool forceOffline) async {
bool updated = false;
// try {
// //Validate device_id to check updates
- // http.Response update_response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/check_update.php'), body: {"username": username, "device_id": android_id}));
+ // http.Response update_response = (await http.post(Uri.parse(API_ENDPOINT+'/task_tracker/check_update.php'), body: {"username": username, "device_id": android_id}));
// final data = update_response.body.split(',');
// updated = data[2] == '1';
// } catch (e) {
@@ -589,7 +712,8 @@ Future> GetActivities(bool forceOffline) async {
}
}
- List | | | | | |