Todo basics added
This commit is contained in:
437
lib/User.dart
437
lib/User.dart
@@ -29,6 +29,7 @@ List<TaskType> taskTypes = [];
|
||||
List<Activity> activities = [];
|
||||
List<Project> projects = [];
|
||||
List<Journal> journal = [];
|
||||
List<Todo> todos = [];
|
||||
|
||||
bool offline = true;
|
||||
bool registered = false;
|
||||
@@ -80,83 +81,81 @@ Future<void> initUserData() async {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> BuildBridgeToServer() async{
|
||||
Future<void> BuildBridgeToServer() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
while(true){
|
||||
while (true) {
|
||||
await UserOperations.executeQueries();
|
||||
try {
|
||||
http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/bridge.php'),
|
||||
body: <String, String>{"username": username}));
|
||||
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(data[4].contains('<td>')){
|
||||
if (data[4].contains('<td>')) {
|
||||
List<String> ongoingData = data[4].split("<td>");
|
||||
if(!prefs.containsKey('current_activity')) {
|
||||
if (!prefs.containsKey('current_activity')) {
|
||||
StartActivityTimer(ongoingData[0], ongoingData[1], DateTime.parse(ongoingData[2]));
|
||||
}
|
||||
}else{
|
||||
if(prefs.containsKey('current_activity'))CancelOngoingActivity();
|
||||
} else {
|
||||
if (prefs.containsKey('current_activity')) CancelOngoingActivity();
|
||||
}
|
||||
|
||||
if(prefs.containsKey('rev')){
|
||||
if(prefs.getString('rev') == response.body){
|
||||
if (prefs.containsKey('rev')) {
|
||||
if (prefs.getString('rev') == response.body) {
|
||||
print('We are on the same page');
|
||||
}else{
|
||||
} else {
|
||||
print('Gotta update');
|
||||
await refreshUserData();
|
||||
prefs.setString('rev',response.body);
|
||||
prefs.setString('rev', response.body);
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
prefs.setString('rev', response.body);
|
||||
await refreshUserData();
|
||||
}
|
||||
|
||||
}catch(e){
|
||||
} catch (e) {
|
||||
print("Error with bridge : $e");
|
||||
}
|
||||
await Future.delayed(Duration(seconds: 5));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
bool m_refreshing = false;
|
||||
Future<void> refreshUserData({bool forceOffline = false}) async {
|
||||
print('refreshing user data');
|
||||
if(m_refreshing){
|
||||
print('Called while refreshing. Return');return;
|
||||
if (m_refreshing) {
|
||||
print('Called while refreshing. Return');
|
||||
return;
|
||||
}
|
||||
m_refreshing=true;
|
||||
m_refreshing = true;
|
||||
|
||||
if(forceOffline) {
|
||||
if (forceOffline) {
|
||||
refreshStream.add(true);
|
||||
categories = await GetCategories(true);
|
||||
projects= await GetProjects(true);
|
||||
projects = await GetProjects(true);
|
||||
taskTypes = await GetTaskTypes(true);
|
||||
activities = await GetActivities(true);
|
||||
journal = await GetJournals(true);
|
||||
todos = await GetTodos(true);
|
||||
|
||||
refreshStream.add(false);
|
||||
}else{
|
||||
|
||||
} else {
|
||||
categories = await GetCategories(false);
|
||||
projects= await GetProjects(false);
|
||||
projects = await GetProjects(false);
|
||||
taskTypes = await GetTaskTypes(false);
|
||||
activities = await GetActivities(false);
|
||||
journal = await GetJournals(false);
|
||||
|
||||
todos= await GetTodos(false);
|
||||
}
|
||||
|
||||
m_refreshing=false;
|
||||
m_refreshing = false;
|
||||
if (cacheEnabled) {
|
||||
NotificationManager.RescheduleNotifications();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<bool> cacheDbExist() async {
|
||||
if (Platform.isAndroid || Platform.isIOS) {
|
||||
Directory directory = await getApplicationDocumentsDirectory();
|
||||
@@ -179,7 +178,7 @@ Future<void> updateActList() async {
|
||||
activities = await GetActivities(false);
|
||||
}
|
||||
|
||||
Future<void> updateProjectsList() async{
|
||||
Future<void> updateProjectsList() async {
|
||||
projects = await GetProjects(false);
|
||||
}
|
||||
|
||||
@@ -202,7 +201,8 @@ void onCacheDatabaseCreate(Database db, int newVersion) async {
|
||||
await db.execute(CategoriesTableSQL);
|
||||
print("Initiated Categories Table");
|
||||
|
||||
String TaskTableSQL = 'CREATE TABLE TaskTypes(id TEXT PRIMARY KEY, ${TaskType.colName} TEXT, ${TaskType.colCategory} TEXT, ${TaskType.colRelatedProject} TEXT, '
|
||||
String TaskTableSQL =
|
||||
'CREATE TABLE TaskTypes(id TEXT PRIMARY KEY, ${TaskType.colName} TEXT, ${TaskType.colCategory} TEXT, ${TaskType.colRelatedProject} TEXT, '
|
||||
'FOREIGN KEY (${TaskType.colCategory}) REFERENCES Categories(${Category.colCatId}))';
|
||||
// print(TaskTableSQL);
|
||||
await db.execute(TaskTableSQL);
|
||||
@@ -219,9 +219,13 @@ void onCacheDatabaseCreate(Database db, int newVersion) async {
|
||||
|
||||
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)';
|
||||
await db.execute(TodoTableSQL);
|
||||
|
||||
String QueriesTableSQL = 'CREATE TABLE Queries(id INTEGER PRIMARY KEY AUTOINCREMENT, ${Queries.colLink} TEXT,${Queries.colData} TEXT)';
|
||||
// print(QueriesTableSQL);
|
||||
await db.execute(QueriesTableSQL);
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
if (prefs.getBool("registered") ?? false) {
|
||||
@@ -232,7 +236,7 @@ void onCacheDatabaseCreate(Database db, int newVersion) async {
|
||||
}
|
||||
|
||||
Future<void> addInitialDataToCache() async {
|
||||
Dialogs.syncingMessage= "Adding initial data";
|
||||
Dialogs.syncingMessage = "Adding initial data";
|
||||
print("adding init data");
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
|
||||
@@ -240,7 +244,7 @@ Future<void> addInitialDataToCache() async {
|
||||
for (Category element in InitialData.getCategories(username)) {
|
||||
await UserOperations.addCategory(element.name, element.color, element.productive, bulk: true);
|
||||
}
|
||||
Dialogs.syncingMessage= "Just a minute";
|
||||
Dialogs.syncingMessage = "Just a minute";
|
||||
for (TaskType element in InitialData.getTaskTypes(username)) {
|
||||
await UserOperations.addTaskType(element.name, element.category, bulk: true);
|
||||
// Map<String,Object> data = {
|
||||
@@ -249,9 +253,9 @@ Future<void> addInitialDataToCache() async {
|
||||
// };
|
||||
// await cacheDb.insert('TaskTypes', data);
|
||||
}
|
||||
Dialogs.syncingMessage= "Syncing";
|
||||
Dialogs.syncingMessage = "Syncing";
|
||||
await UserOperations.executeQueries();
|
||||
// await refreshUserData();
|
||||
// await refreshUserData();
|
||||
}
|
||||
|
||||
void onCacheDatabaseUpgrade(Database db, int oldVersion, int newVersion) async {
|
||||
@@ -333,7 +337,7 @@ Future<void> UpdateCategoriesFromServer() async {
|
||||
|
||||
print(response.body);
|
||||
List<String> data = response.body.split("<td>");
|
||||
await cacheDb.delete("Categories");
|
||||
await cacheDb.delete("Categories");
|
||||
for (var value in data) {
|
||||
Map<String, dynamic> cat = jsonDecode(value);
|
||||
//print(catData);
|
||||
@@ -390,13 +394,13 @@ Future<List<TaskType>> GetTaskTypes(bool forceOffline) async {
|
||||
continue;
|
||||
}
|
||||
Project? relatedProject;
|
||||
if(related_project.isNotEmpty){
|
||||
if (related_project.isNotEmpty) {
|
||||
relatedProject = await getProjectFromId(related_project);
|
||||
print('got a tasktype with project');
|
||||
}
|
||||
|
||||
// print("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 {
|
||||
@@ -412,10 +416,10 @@ Future<List<TaskType>> GetTaskTypes(bool forceOffline) async {
|
||||
for (var value in data) {
|
||||
Map<String, dynamic> 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']));
|
||||
//print(cat);
|
||||
}
|
||||
}catch(e){
|
||||
} catch (e) {
|
||||
print("Error while tasks NC $e");
|
||||
}
|
||||
|
||||
@@ -437,8 +441,9 @@ Future<void> UpdateTaskTypesFromServer() async {
|
||||
for (var value in data) {
|
||||
Map<String, dynamic> cat = jsonDecode(value);
|
||||
//print(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']}') ");
|
||||
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']}') ");
|
||||
|
||||
print(await cacheDb.query("TaskTypes"));
|
||||
}
|
||||
@@ -447,41 +452,37 @@ Future<void> UpdateTaskTypesFromServer() async {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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<td>$metadata<td>${UserOperations.dFormat.format(startTime)}");
|
||||
NotificationManager.RescheduleNotifications();
|
||||
|
||||
UserOperations.startOngoing(prefs.getString('current_activity')!);
|
||||
|
||||
}
|
||||
|
||||
Future<List<String>?> getOngoingData() async{
|
||||
Future<List<String>?> getOngoingData() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if(prefs.containsKey('current_activity')){
|
||||
if (prefs.containsKey('current_activity')) {
|
||||
List<String> data = [];
|
||||
try{
|
||||
try {
|
||||
data = prefs.getString('current_activity')!.split('<td>');
|
||||
return data;
|
||||
}catch(e){
|
||||
}
|
||||
}else{
|
||||
}
|
||||
} catch (e) {}
|
||||
} else {}
|
||||
}
|
||||
|
||||
void StopActivityTimer() async{
|
||||
void StopActivityTimer() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
try {
|
||||
List<String> data = prefs.getString('current_activity')!.split("<td>");
|
||||
UserOperations.addActivity(data[0], DateTime.parse(data[2]), DateTime.now(),metadata: data[1]);
|
||||
}catch(e){}
|
||||
UserOperations.addActivity(data[0], DateTime.parse(data[2]), DateTime.now(), metadata: data[1]);
|
||||
} catch (e) {}
|
||||
prefs.remove('current_activity');
|
||||
UserOperations.stopOngoing();
|
||||
}
|
||||
|
||||
void CancelOngoingActivity() async{
|
||||
void CancelOngoingActivity() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.remove('current_activity');
|
||||
UserOperations.stopOngoing();
|
||||
@@ -623,7 +624,7 @@ Future<void> UpdateActivitiesFromServer() async {
|
||||
//print(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']}') ");
|
||||
"VALUES ('${cat['task_id']}', '${cat['sTime']}','${cat['eTime']}', '${cat['metadata'].toString().replaceAll("'", "''")}') ");
|
||||
}
|
||||
} else {
|
||||
print("No activities for now");
|
||||
@@ -661,16 +662,20 @@ Future<List<Project>> GetProjects(bool forceOffline) async {
|
||||
String? category = element[Project.colCat];
|
||||
String? stepsJson = element[Project.colSteps];
|
||||
String? deadline = element[Project.colDeadline];
|
||||
int? eta= element[Project.colEta];
|
||||
int? eta = element[Project.colEta];
|
||||
print(name);
|
||||
|
||||
if (name == null || category == null || stepsJson == null || deadline == null || eta==null) {
|
||||
if (name == null || category == null || stepsJson == null || deadline == null || eta == null) {
|
||||
print("Something is null!\nname:${name == null}, cat:${category == null}, steps:${stepsJson == null}, deadline${deadline == null}");
|
||||
print("TaskType:{$name}, Start Time:{$category}, endTime:{$stepsJson}, metadata:${deadline}");
|
||||
continue;
|
||||
}
|
||||
Category? cat = await getCatFromId(category);
|
||||
if(cat==null){print('couldnt find cat');continue;}
|
||||
print('searching for $category');
|
||||
if (cat == null) {
|
||||
print('couldnt find cat');
|
||||
continue;
|
||||
}
|
||||
print('steps : $stepsJson');
|
||||
List<dynamic> _steps = jsonDecode(stepsJson);
|
||||
List<ProjectStep> steps = [];
|
||||
@@ -682,9 +687,9 @@ Future<List<Project>> GetProjects(bool forceOffline) async {
|
||||
print(element);
|
||||
});
|
||||
eta = (m_eta > 0) ? m_eta : eta;
|
||||
// print(steps);
|
||||
// print(steps);
|
||||
|
||||
_projects.add(Project(name.replaceAll(username, ""),category,steps,eta,DateTime.parse(deadline),cat: cat));
|
||||
_projects.add(Project(name.replaceAll(username, ""), category, steps, eta, DateTime.parse(deadline), cat: cat));
|
||||
}
|
||||
projects = _projects;
|
||||
} else {
|
||||
@@ -710,7 +715,6 @@ Future<List<Project>> GetProjects(bool forceOffline) async {
|
||||
print('null found');
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
projects = _projects;
|
||||
@@ -745,7 +749,7 @@ Future<void> UpdateProjectsFromServer() async {
|
||||
print(cat);
|
||||
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']})");
|
||||
"VALUES ('${cat['name']}', '${cat['category']}', '${cat['steps']}', '${cat['deadline']}', ${cat['eta']})");
|
||||
}
|
||||
} else {
|
||||
print("No activities for now");
|
||||
@@ -795,7 +799,7 @@ Future<List<Journal>> GetJournals(bool forceOffline) async {
|
||||
}
|
||||
DateTime day = DateTime.parse(id.replaceAll(username, ''));
|
||||
// print("name:{$catName}, color:{$catColor}, prod:{$catProductive}");
|
||||
_journals.add(Journal(id,day,title: title,description: text));
|
||||
_journals.add(Journal(id, day, title: title, description: text));
|
||||
}
|
||||
journal = _journals;
|
||||
} else {
|
||||
@@ -810,14 +814,15 @@ Future<List<Journal>> GetJournals(bool forceOffline) async {
|
||||
for (var value in data) {
|
||||
Map<String, dynamic> cat = jsonDecode(value);
|
||||
//print(catData);
|
||||
_categories.add(Journal(cat['id'],DateTime.parse(cat['id'].toString().replaceAll(username, '')), title:cat['title'], description:cat['text']));
|
||||
_categories
|
||||
.add(Journal(cat['id'], DateTime.parse(cat['id'].toString().replaceAll(username, '')), title: cat['title'], description: cat['text']));
|
||||
}
|
||||
journal = _categories;
|
||||
} catch (e) {
|
||||
print("Error while cats NC: $e");
|
||||
}
|
||||
}
|
||||
journal.sort((a,b)=> b.day.compareTo(a.day));
|
||||
journal.sort((a, b) => b.day.compareTo(a.day));
|
||||
return journal;
|
||||
}
|
||||
|
||||
@@ -833,8 +838,7 @@ Future<void> UpdateJournalsFromServer() async {
|
||||
for (var value in data) {
|
||||
Map<String, dynamic> cat = jsonDecode(value);
|
||||
//print(catData);
|
||||
await cacheDb
|
||||
.rawInsert("INSERT OR REPLACE INTO Journal (id, ${Journal.colTitle},${Journal.colDescription}) "
|
||||
await cacheDb.rawInsert("INSERT OR REPLACE INTO Journal (id, ${Journal.colTitle},${Journal.colDescription}) "
|
||||
"VALUES ('${cat['id']}','${cat['title'].toString().replaceAll("'", "''")}','${cat['description'].toString().replaceAll("'", "''")}') ");
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -842,6 +846,100 @@ Future<void> UpdateJournalsFromServer() async {
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Todo>> GetTodos(bool forceOffline) async {
|
||||
if (cacheEnabled) {
|
||||
List<Todo> _todos = [];
|
||||
if (offline || forceOffline) {
|
||||
//Retreive from cacheDB
|
||||
|
||||
} else {
|
||||
//Check if server got updated, If not go for cache
|
||||
|
||||
//Validate device_id to check updates
|
||||
|
||||
bool catsUpdated = false;
|
||||
// try {
|
||||
// http.Response update_response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/check_update.php'), body: <String, String>{"username": username, "device_id": android_id}));
|
||||
// final data = update_response.body.split(',');
|
||||
// catsUpdated = data[0] == '1';
|
||||
// } catch (e) {
|
||||
// print(e);
|
||||
// }
|
||||
|
||||
//Update CacheDB
|
||||
if (!catsUpdated) {
|
||||
await UpdateTodosFromServer();
|
||||
}
|
||||
}
|
||||
|
||||
List<Map> cats = await cacheDb.query('Todos');
|
||||
print(cats.length);
|
||||
for (Map element in cats) {
|
||||
String? id = element['id'].toString();
|
||||
String? task_id = element[Todo.colCat];
|
||||
String? metadata = element[Todo.colMetadata];
|
||||
String? due_date = element[Todo.colDueDate];
|
||||
String? notification_time = element[Todo.colNotificationTime];
|
||||
|
||||
if (id == null || task_id == null || metadata == null || due_date == null) {
|
||||
print("Something is null!");
|
||||
print("id:{$id}, task:{$task_id}, metadata:${metadata}, due_date: ${due_date}");
|
||||
continue;
|
||||
}
|
||||
TaskType? taskType = await getTaskFromId(task_id);
|
||||
if(taskType == null){print('got null taask for this todo!');print("id:{$id}, task:{$task_id}, metadata:${metadata}, due_date: ${due_date}");}
|
||||
DateTime dueDate = DateTime.parse(due_date);
|
||||
DateTime? notificationTime = (notification_time == null) ? null : ((notification_time.isEmpty || notification_time =='null') ? null :DateTime.parse(notification_time));
|
||||
// print("name:{$catName}, color:{$catColor}, prod:{$catProductive}");
|
||||
_todos.add(Todo(id,task_id,metadata,dueDate, notificationTime: notificationTime, task:taskType));
|
||||
}
|
||||
todos = _todos;
|
||||
} else {
|
||||
print("NC: Updating todos as $username");
|
||||
try {
|
||||
http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_todos.php'),
|
||||
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
|
||||
|
||||
print(response.body);
|
||||
List<String> data = response.body.split("<td>");
|
||||
List<Journal> _categories = [];
|
||||
for (var value in data) {
|
||||
Map<String, dynamic> cat = jsonDecode(value);
|
||||
//print(catData);
|
||||
_categories
|
||||
.add(Journal(cat['id'], DateTime.parse(cat['id'].toString().replaceAll(username, '')), title: cat['title'], description: cat['text']));
|
||||
}
|
||||
journal = _categories;
|
||||
} catch (e) {
|
||||
print("Error while cats NC: $e");
|
||||
}
|
||||
}
|
||||
// journal.sort((a, b) => b.day.compareTo(a.day));
|
||||
return todos;
|
||||
}
|
||||
|
||||
Future<void> UpdateTodosFromServer() async {
|
||||
print("Updating Todos as $username");
|
||||
try {
|
||||
http.Response response = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/get_todos.php'),
|
||||
body: <String, String>{"username": username, "device_id": await Settings.UUID()}));
|
||||
|
||||
print(response.body);
|
||||
List<String> data = response.body.split("<td>");
|
||||
await cacheDb.delete("Todos");
|
||||
for (var value in data) {
|
||||
Map<String, dynamic> cat = jsonDecode(value);
|
||||
//print(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']}') ");
|
||||
}
|
||||
} catch (e) {
|
||||
print("Error while cats $e");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Future<TaskType?> getTaskFromId(String taskId) async {
|
||||
// await GetTaskTypes(false);
|
||||
TaskType? cat = null;
|
||||
@@ -874,7 +972,7 @@ Future<Project?> getProjectFromId(String projectId) async {
|
||||
// await GetTaskTypes(false);
|
||||
Project? project = null;
|
||||
for (var element in projects) {
|
||||
if (element.getName() ==projectId.replaceAll(username, "")) {
|
||||
if (element.getName() == projectId.replaceAll(username, "")) {
|
||||
project = element;
|
||||
}
|
||||
}
|
||||
@@ -885,6 +983,19 @@ Future<Project?> getProjectFromId(String projectId) async {
|
||||
return project;
|
||||
}
|
||||
|
||||
bool journalExists(DateTime date){
|
||||
int journalId = -1;
|
||||
for (int i =0; i < journal.length; i++) {
|
||||
// print('${journal[i].day } : $date');
|
||||
if(DateFormat('yyyy-MM-dd').format(journal[i].day) == DateFormat('yyyy-MM-dd').format(date)){
|
||||
journalId = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (journalId > 0);
|
||||
}
|
||||
|
||||
//Helpers
|
||||
class Helpers {
|
||||
Future<String?> _getId() async {
|
||||
@@ -957,7 +1068,7 @@ class UserOperations {
|
||||
'device_id': await Settings.UUID(),
|
||||
'name': name,
|
||||
'category': username + category,
|
||||
'related_project' :(relatedProject==null) ? '' : (username + relatedProject)
|
||||
'related_project': (relatedProject == null) ? '' : (username + relatedProject)
|
||||
};
|
||||
|
||||
if (cacheEnabled) {
|
||||
@@ -969,8 +1080,14 @@ class UserOperations {
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
Map<String, Object> data = {TaskType.colId: username + name, Category.colName: name, Category.colCatId: username + category,};
|
||||
if(relatedProject!=null || relatedProject =='None'){data.putIfAbsent(TaskType.colRelatedProject, () => relatedProject.toString());}
|
||||
Map<String, Object> data = {
|
||||
TaskType.colId: username + name,
|
||||
Category.colName: name,
|
||||
Category.colCatId: username + category,
|
||||
};
|
||||
if (relatedProject != null || relatedProject == 'None') {
|
||||
data.putIfAbsent(TaskType.colRelatedProject, () => relatedProject.toString());
|
||||
}
|
||||
await cacheDb.insert('TaskTypes', data);
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
@@ -996,7 +1113,7 @@ class UserOperations {
|
||||
'username': username,
|
||||
'name': name,
|
||||
'category': username + category,
|
||||
'related_project' :(relatedProject==null) ? '' : (username + relatedProject)
|
||||
'related_project': (relatedProject == null) ? '' : (username + relatedProject)
|
||||
};
|
||||
|
||||
if (cacheEnabled) {
|
||||
@@ -1008,7 +1125,8 @@ class UserOperations {
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
await cacheDb.rawUpdate("UPDATE TaskTypes SET ${TaskType.colId}='${username+name}', ${TaskType.colName}='$name', ${TaskType.colCategory}='${username+category}', ${TaskType.colRelatedProject}='${(relatedProject == 'None') ? '' : relatedProject}' WHERE id='${username+oldName}'");
|
||||
await cacheDb.rawUpdate(
|
||||
"UPDATE TaskTypes SET ${TaskType.colId}='${username + name}', ${TaskType.colName}='$name', ${TaskType.colCategory}='${username + category}', ${TaskType.colRelatedProject}='${(relatedProject == 'None') ? '' : relatedProject}' WHERE id='${username + oldName}'");
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
try {
|
||||
@@ -1193,7 +1311,7 @@ class UserOperations {
|
||||
await executeQueries();
|
||||
}
|
||||
|
||||
static Future<void> addProject(String name, String category, List<ProjectStep> steps,int eta, DateTime deadline) async {
|
||||
static Future<void> addProject(String name, String category, List<ProjectStep> steps, int eta, DateTime deadline) async {
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'name': username + name,
|
||||
'username': username,
|
||||
@@ -1213,7 +1331,13 @@ class UserOperations {
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
Map<String, Object> data = {Project.colName: username+name, Project.colCat: category, Project.colSteps: jsonEncode(steps),Project.colEta: eta, Project.colDeadline: deadline.toString()};
|
||||
Map<String, Object> data = {
|
||||
Project.colName: username + name,
|
||||
Project.colCat: category,
|
||||
Project.colSteps: jsonEncode(steps),
|
||||
Project.colEta: eta,
|
||||
Project.colDeadline: deadline.toString()
|
||||
};
|
||||
await cacheDb.insert('Projects', data);
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
@@ -1231,16 +1355,60 @@ class UserOperations {
|
||||
await executeQueries();
|
||||
}
|
||||
|
||||
static Future<void> CompleteProjectStep(Project project, ProjectStep step, DateTime finishedDate) async {
|
||||
static Future<void> editProject(String oldName, String name, String category, List<ProjectStep> steps, int eta, DateTime deadline) async {
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'oldName': username + oldName,
|
||||
'name': username + name,
|
||||
'username': username,
|
||||
'category_id': username + category,
|
||||
'steps': jsonEncode(steps),
|
||||
'eta': eta.toString(),
|
||||
'deadline': DateFormat("yyyy-MM-dd").format(deadline)
|
||||
};
|
||||
|
||||
if (cacheEnabled) {
|
||||
//Add Query
|
||||
Map<String, Object> query = {Queries.colLink: 'edit_project', Queries.colData: jsonEncode(queryBody)};
|
||||
|
||||
print("adding new query ${query[Queries.colLink]} : ${jsonEncode(queryBody)}");
|
||||
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
Map<String, Object> data = {
|
||||
Project.colName: username + name,
|
||||
Project.colCat: category,
|
||||
Project.colSteps: jsonEncode(steps),
|
||||
Project.colEta: eta,
|
||||
Project.colDeadline: deadline.toString()
|
||||
};
|
||||
await cacheDb.rawUpdate(
|
||||
"UPDATE Projects SET ${Project.colName}='${username + name}', ${Project.colCat}='${username+category}', ${Project.colSteps}='${jsonEncode(steps)}', ${Project.colEta}='${eta}', ${Project.colDeadline}='${deadline.toString()}' WHERE ${Project.colName}='${username+oldName}'");
|
||||
await cacheDb.rawUpdate("UPDATE TaskTypes SET ${TaskType.colRelatedProject}='${username+name}' WHERE ${TaskType.colRelatedProject}='${username+oldName}'");
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
try {
|
||||
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/edit_project.php'), body: queryBody));
|
||||
print("Query executed : Results{${queryResponse.body}");
|
||||
if (queryResponse.body.toLowerCase().contains("success")) {
|
||||
//Success
|
||||
}
|
||||
} catch (e) {
|
||||
print('NC: Error adding prjct $e}');
|
||||
}
|
||||
}
|
||||
await executeQueries();
|
||||
}
|
||||
|
||||
static Future<void> CompleteProjectStep(Project project, ProjectStep step, DateTime finishedDate) async {
|
||||
project.steps.forEach((element) {
|
||||
if(element.stepName == step.stepName){
|
||||
if (element.stepName == step.stepName) {
|
||||
element.finishedDate = finishedDate;
|
||||
}
|
||||
});
|
||||
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'name': username+project.name,
|
||||
'name': username + project.name,
|
||||
'username': username,
|
||||
'steps': jsonEncode(project.steps),
|
||||
};
|
||||
@@ -1273,15 +1441,14 @@ class UserOperations {
|
||||
}
|
||||
|
||||
static Future<void> UndoProjectStep(Project project, ProjectStep step) async {
|
||||
|
||||
project.steps.forEach((element) {
|
||||
if(element.stepName == step.stepName){
|
||||
if (element.stepName == step.stepName) {
|
||||
element.finishedDate = null;
|
||||
}
|
||||
});
|
||||
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'name': username+project.name,
|
||||
'name': username + project.name,
|
||||
'username': username,
|
||||
'steps': jsonEncode(project.steps),
|
||||
};
|
||||
@@ -1314,23 +1481,19 @@ class UserOperations {
|
||||
}
|
||||
|
||||
static Future<void> addJournal(DateTime day, String title, String text) async {
|
||||
|
||||
bool exist =false;
|
||||
bool exist = false;
|
||||
for (var element in journal) {
|
||||
if(element.day== day){
|
||||
if (element.day == day) {
|
||||
//Wat!
|
||||
exist=true;
|
||||
exist = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(exist){return;}
|
||||
if (exist) {
|
||||
return;
|
||||
}
|
||||
String id = username + DateFormat('yyyy-MM-dd').format(day);
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'username': username,
|
||||
'id': id,
|
||||
'title': title,
|
||||
'description': text
|
||||
};
|
||||
Map<String, String> queryBody = <String, String>{'username': username, 'id': id, 'title': title, 'description': text};
|
||||
if (cacheEnabled) {
|
||||
//Add Query
|
||||
Map<String, Object> query = {Queries.colLink: 'add_journal', Queries.colData: jsonEncode(queryBody)};
|
||||
@@ -1340,11 +1503,7 @@ class UserOperations {
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
Map<String, Object> data = {
|
||||
'id':id,
|
||||
Journal.colTitle: title,
|
||||
Journal.colDescription:text
|
||||
};
|
||||
Map<String, Object> data = {'id': id, Journal.colTitle: title, Journal.colDescription: text};
|
||||
await cacheDb.insert('Journal', data);
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
@@ -1359,20 +1518,14 @@ class UserOperations {
|
||||
}
|
||||
//executeQueries();
|
||||
}
|
||||
//Add to server and refresh Cache
|
||||
await executeQueries();
|
||||
|
||||
//Add to server and refresh Cache
|
||||
await executeQueries();
|
||||
}
|
||||
|
||||
static Future<void> editJournal(DateTime oldDay, DateTime day, String title, String text) async {
|
||||
String oldId = username + DateFormat('yyyy-MM-dd').format(oldDay);
|
||||
String id = username + DateFormat('yyyy-MM-dd').format(day);
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'username': username,
|
||||
'old_id':oldId,
|
||||
'id': id,
|
||||
'title': title,
|
||||
'description': text
|
||||
};
|
||||
Map<String, String> queryBody = <String, String>{'username': username, 'old_id': oldId, 'id': id, 'title': title, 'description': text};
|
||||
if (cacheEnabled) {
|
||||
//Add Query
|
||||
Map<String, Object> query = {Queries.colLink: 'edit_journal', Queries.colData: jsonEncode(queryBody)};
|
||||
@@ -1380,14 +1533,11 @@ class UserOperations {
|
||||
print("adding new query ${query[Queries.colLink]} : ${jsonEncode(queryBody)}");
|
||||
|
||||
await cacheDb.insert('Queries', query);
|
||||
await cacheDb.rawUpdate("UPDATE Journal SET id='$id', ${Journal.colTitle}='${title.toString().replaceAll("'", "''")}', ${Journal.colDescription}='${text.toString().replaceAll("'", "''")}' WHERE id='$oldId'");
|
||||
await cacheDb.rawUpdate(
|
||||
"UPDATE Journal SET id='$id', ${Journal.colTitle}='${title.toString().replaceAll("'", "''")}', ${Journal.colDescription}='${text.toString().replaceAll("'", "''")}' WHERE id='$oldId'");
|
||||
//update Cache
|
||||
Map<String, Object> data = {
|
||||
'id':id,
|
||||
Journal.colTitle: title,
|
||||
Journal.colDescription:text
|
||||
};
|
||||
// await cacheDb.insert('Journal', data);
|
||||
Map<String, Object> data = {'id': id, Journal.colTitle: title, Journal.colDescription: text};
|
||||
// await cacheDb.insert('Journal', data);
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
try {
|
||||
@@ -1403,9 +1553,54 @@ class UserOperations {
|
||||
}
|
||||
//Add to server and refresh Cache
|
||||
await executeQueries();
|
||||
|
||||
}
|
||||
|
||||
static Future<void> addTodo(String taskType, String metadata, DateTime dueDate, DateTime? notificationTime) async {
|
||||
|
||||
String taskId = username + taskType;
|
||||
if(taskId.contains('[') && taskId.contains(']')){
|
||||
//has a project related
|
||||
taskId = taskId.substring(0, taskId.indexOf('[') - 1);
|
||||
}
|
||||
|
||||
String id = username + taskId + metadata;
|
||||
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'username': username,
|
||||
'task': taskId,
|
||||
'metadata': metadata,
|
||||
'due_date': DateFormat('yyyy-MM-dd').format(dueDate),
|
||||
if(notificationTime!=null)'notification_time':DateFormat('yyyy-MM-dd HH:mm').format(notificationTime)
|
||||
};
|
||||
if (cacheEnabled) {
|
||||
//Add Query
|
||||
Map<String, Object> query = {Queries.colLink: 'add_todo', Queries.colData: jsonEncode(queryBody)};
|
||||
|
||||
print("adding new query ${query[Queries.colLink]} : ${jsonEncode(queryBody)}");
|
||||
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
Map<String, Object> data = {'id': id, Todo.colCat: username+taskType, Todo.colMetadata: metadata, Todo.colDueDate: DateFormat('yyyy-MM-dd').format(dueDate), if(notificationTime!=null)Todo.colNotificationTime:DateFormat('yyyy-MM-dd HH:mm').format(notificationTime)};
|
||||
await cacheDb.insert('Todos', data);
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
try {
|
||||
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/add_todo.php'), body: queryBody));
|
||||
print("Query executed : Results{${queryResponse.body}");
|
||||
if (queryResponse.body.toLowerCase().contains("success")) {
|
||||
//Success
|
||||
}
|
||||
} catch (e) {
|
||||
print('NC: Error adding journal entry $e}');
|
||||
}
|
||||
//executeQueries();
|
||||
}
|
||||
//Add to server and refresh Cache
|
||||
await executeQueries();
|
||||
}
|
||||
|
||||
|
||||
static Future<void> deleteTask(String name, {bulk = false}) async {
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'id': username + name,
|
||||
@@ -1523,7 +1718,7 @@ class UserOperations {
|
||||
static Future<void> deleteProject(String project, {bulk = false}) async {
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'username': username,
|
||||
'name': username+project,
|
||||
'name': username + project,
|
||||
};
|
||||
//Add Query
|
||||
Map<String, Object> query = {Queries.colLink: 'delete_project', Queries.colData: jsonEncode(queryBody)};
|
||||
@@ -1534,8 +1729,7 @@ class UserOperations {
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
String deleteQuery =
|
||||
"DELETE FROM Projects WHERE ${Project.colName}='${username+project}'";
|
||||
String deleteQuery = "DELETE FROM Projects WHERE ${Project.colName}='${username + project}'";
|
||||
print("delteQuery : $deleteQuery");
|
||||
|
||||
await cacheDb.rawDelete(deleteQuery);
|
||||
@@ -1571,8 +1765,7 @@ class UserOperations {
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
String deleteQuery =
|
||||
"DELETE FROM Journal WHERE id='$id'";
|
||||
String deleteQuery = "DELETE FROM Journal WHERE id='$id'";
|
||||
print("delteQuery : $deleteQuery");
|
||||
|
||||
await cacheDb.rawDelete(deleteQuery);
|
||||
@@ -1617,7 +1810,7 @@ class UserOperations {
|
||||
try {
|
||||
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/$file.php'), body: body));
|
||||
print("Query executed : Results{${queryResponse.body}");
|
||||
if (queryResponse.body.toLowerCase().contains("success")) {
|
||||
if (queryResponse.body.toLowerCase().contains("+")) {
|
||||
await cacheDb.rawDelete('DELETE FROM Queries WHERE id=$id');
|
||||
}
|
||||
offline = false;
|
||||
|
||||
Reference in New Issue
Block a user