Journal section added
This commit is contained in:
220
lib/User.dart
220
lib/User.dart
@@ -28,6 +28,7 @@ List<Category> categories = [];
|
||||
List<TaskType> taskTypes = [];
|
||||
List<Activity> activities = [];
|
||||
List<Project> projects = [];
|
||||
List<Journal> journal = [];
|
||||
|
||||
bool offline = true;
|
||||
bool registered = false;
|
||||
@@ -136,14 +137,17 @@ Future<void> refreshUserData({bool forceOffline = false}) async {
|
||||
taskTypes = await GetTaskTypes(true);
|
||||
activities = await GetActivities(true);
|
||||
projects= await GetProjects(true);
|
||||
journal = await GetJournals(true);
|
||||
|
||||
refreshStream.add(false);
|
||||
}else{
|
||||
|
||||
await updateCatsList();
|
||||
await updateTasksList();
|
||||
await updateActList();
|
||||
await updateProjectsList();
|
||||
categories = await GetCategories(false);
|
||||
taskTypes = await GetTaskTypes(false);
|
||||
activities = await GetActivities(false);
|
||||
projects= await GetProjects(false);
|
||||
journal = await GetJournals(false);
|
||||
|
||||
}
|
||||
|
||||
m_refreshing=false;
|
||||
@@ -213,6 +217,8 @@ 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})';
|
||||
await db.execute(JournalTableSQL);
|
||||
String QueriesTableSQL = 'CREATE TABLE Queries(id INTEGER PRIMARY KEY AUTOINCREMENT, ${Queries.colLink} TEXT,${Queries.colData} TEXT)';
|
||||
// print(QueriesTableSQL);
|
||||
await db.execute(QueriesTableSQL);
|
||||
@@ -750,6 +756,92 @@ Future<void> UpdateProjectsFromServer() async {
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Journal>> GetJournals(bool forceOffline) async {
|
||||
if (cacheEnabled) {
|
||||
List<Journal> _journals = [];
|
||||
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 UpdateJournalsFromServer();
|
||||
}
|
||||
}
|
||||
|
||||
List<Map> cats = await cacheDb.query('Journal');
|
||||
print(cats.length);
|
||||
for (Map element in cats) {
|
||||
String? id = element['id'].toString();
|
||||
String? title = element[Journal.colTitle].toString();
|
||||
String? text = element[Journal.colDescription].toString();
|
||||
if (id == null || title == null || text == null) {
|
||||
print("Something is null!");
|
||||
print("id:{$id}, title:{$title}, text:${text}");
|
||||
continue;
|
||||
}
|
||||
DateTime day = DateTime.parse(id.replaceAll(username, ''));
|
||||
// print("name:{$catName}, color:{$catColor}, prod:{$catProductive}");
|
||||
_journals.add(Journal(id,day,title: title,description: text));
|
||||
}
|
||||
journal = _journals;
|
||||
} else {
|
||||
print("NC: Updating Categories as $username");
|
||||
try {
|
||||
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()}));
|
||||
|
||||
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 journal;
|
||||
}
|
||||
|
||||
Future<void> UpdateJournalsFromServer() async {
|
||||
print("Updating Journal as $username");
|
||||
try {
|
||||
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()}));
|
||||
|
||||
print(response.body);
|
||||
List<String> data = response.body.split("<td>");
|
||||
await cacheDb.delete("Journal");
|
||||
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}) "
|
||||
"VALUES ('${cat['id']}','${cat['title'].toString().replaceAll("'", "''")}','${cat['description'].toString().replaceAll("'", "''")}') ");
|
||||
}
|
||||
} catch (e) {
|
||||
print("Error while cats $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<TaskType?> getTaskFromId(String taskId) async {
|
||||
// await GetTaskTypes(false);
|
||||
TaskType? cat = null;
|
||||
@@ -1221,6 +1313,89 @@ class UserOperations {
|
||||
await executeQueries();
|
||||
}
|
||||
|
||||
static Future<void> addJournal(DateTime day, String title, String text) async {
|
||||
String id = username + DateFormat('yyyy-MM-dd').format(day);
|
||||
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)};
|
||||
|
||||
print("adding new query ${query[Queries.colLink]} : ${jsonEncode(queryBody)}");
|
||||
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
Map<String, Object> data = {
|
||||
'id':id,
|
||||
Journal.colTitle: title,
|
||||
Journal.colDescription:text
|
||||
};
|
||||
await cacheDb.insert('Journal', data);
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
try {
|
||||
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/add_journal.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> 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
|
||||
};
|
||||
if (cacheEnabled) {
|
||||
//Add Query
|
||||
Map<String, Object> query = {Queries.colLink: 'edit_journal', Queries.colData: jsonEncode(queryBody)};
|
||||
|
||||
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'");
|
||||
//update Cache
|
||||
Map<String, Object> data = {
|
||||
'id':id,
|
||||
Journal.colTitle: title,
|
||||
Journal.colDescription:text
|
||||
};
|
||||
// await cacheDb.insert('Journal', data);
|
||||
await refreshUserData(forceOffline: true);
|
||||
} else {
|
||||
try {
|
||||
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/edit_journal.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,
|
||||
@@ -1372,6 +1547,43 @@ class UserOperations {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> deleteJournal(String id, {bulk = false}) async {
|
||||
Map<String, String> queryBody = <String, String>{
|
||||
'username': username,
|
||||
'id': id,
|
||||
};
|
||||
//Add Query
|
||||
Map<String, Object> query = {Queries.colLink: 'delete_journal', Queries.colData: jsonEncode(queryBody)};
|
||||
|
||||
print("adding new query ${query[Queries.colLink]} : ${jsonEncode(queryBody)}");
|
||||
|
||||
if (cacheEnabled) {
|
||||
await cacheDb.insert('Queries', query);
|
||||
|
||||
//update Cache
|
||||
String deleteQuery =
|
||||
"DELETE FROM Journal WHERE id='$id'";
|
||||
print("delteQuery : $deleteQuery");
|
||||
|
||||
await cacheDb.rawDelete(deleteQuery);
|
||||
await refreshUserData(forceOffline: true);
|
||||
//Add to server and refresh Cache
|
||||
} else {
|
||||
try {
|
||||
http.Response queryResponse = (await http.post(Uri.parse('http://161.97.127.136/task_tracker/delete_journal.php'), body: queryBody));
|
||||
print("Query executed : Results{${queryResponse.body}");
|
||||
if (queryResponse.body.toLowerCase().contains("success")) {
|
||||
//Success
|
||||
}
|
||||
} catch (e) {
|
||||
print('NC: Error deleting journal $e}');
|
||||
}
|
||||
}
|
||||
if (!bulk) {
|
||||
await executeQueries();
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> executeQueries() async {
|
||||
if (cacheEnabled) {
|
||||
if (offline) {
|
||||
|
||||
Reference in New Issue
Block a user