Skip to content

Commit

Permalink
Merge pull request #124 from AdamGuidarini/bug-crash-on-launch
Browse files Browse the repository at this point in the history
Bug crash on launch
  • Loading branch information
AdamGuidarini authored Oct 25, 2024
2 parents d508dae + 39fa7de commit 632d969
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ void DatabaseController::upgrade(int currentVersion) {
);
}

if (currentVersion < 15) {
repairImportErrors();
}

manager.execSql("PRAGMA schema_version = " + to_string(DB_VERSION));
}

Expand Down Expand Up @@ -551,3 +555,111 @@ vector<Notification> DatabaseController::getStashedNotifications() {
void DatabaseController::deleteNotification(long id) {
manager.deleteRecord(NOTIFICATIONS, {pair(DOSE_ID, to_string(id))});
}

void DatabaseController::repairImportErrors() {
auto doses = manager.execSqlWithReturn("SELECT * FROM " + MEDICATION_TRACKER_TABLE);
auto meds = manager.execSqlWithReturn("SELECT * FROM " + MEDICATION_TABLE);
auto times = manager.execSqlWithReturn("SELECT * FROM " + MEDICATION_TIMES);
auto notes = manager.execSqlWithReturn("SELECT * FROM " + NOTES_TABLE);
regex dateRegex("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$");

while (!doses->isAfterLast() && doses->getCount() > 0) {
auto doseTime = doses->getItem(DOSE_TIME);
auto timeTaken = doses->getItem(TIME_TAKEN);
bool updateRequired = false;

// scheduled datetime is wrong
if (!regex_match(doseTime, dateRegex)) {
if (doseTime.length() == DateFormats::DB_DATE_FORMAT.length() - 1) {
doseTime += "0";

updateRequired = true;
}
}

// take datetime is wrong
if (!regex_match(timeTaken, dateRegex)) {
if (timeTaken.length() == DateFormats::DB_DATE_FORMAT.length() - 1) {
timeTaken += "0";

updateRequired = true;
}
}

if (updateRequired) {
map<string, string> vals = {
pair(DOSE_TIME, doseTime),
pair(TIME_TAKEN, timeTaken)
};

manager.update(
MEDICATION_TRACKER_TABLE,
vals,
{pair(DOSE_ID, doses->getItem(DOSE_ID))}
);
}

doses->moveToNext();
}

while (!meds->isAfterLast() && meds->getCount() > 0) {
auto start = meds->getItem(START_DATE);

// scheduled datetime is wrong
if (!regex_match(start, dateRegex)) {
manager.update(
MEDICATION_TABLE,
{pair(START_DATE, start + "0")},
{pair(MED_ID, meds->getItem(MED_ID))}
);
}

meds->moveToNext();
}

while (!times->isAfterLast() && times->getCount() > 0) {
auto schedTime = times->getItem(DRUG_TIME);

bool match = regex_match(schedTime, dateRegex);

// scheduled datetime is wrong
if (!match && schedTime.length() == DateFormats::DB_DATE_FORMAT.length() - 1) {
manager.update(
MEDICATION_TIMES,
{pair(DRUG_TIME, schedTime + "0")},
{pair(TIME_ID, times->getItem(TIME_ID))}
);
}

times->moveToNext();
}

while (!notes->isAfterLast() && notes->getCount() > 0) {
string editTime = notes->getItem(TIME_EDITED);
string timeEdited = TIME_EDITED;
bool match = regex_match(editTime, dateRegex);

timeEdited.pop_back();

if (timeEdited == editTime) {
manager.update(
NOTES_TABLE,
{pair(TIME_EDITED, "")},
{pair(NOTE_ID, notes->getItem(NOTE_ID))}
);
} else if (!match && timeEdited.length() == DateFormats::DB_DATE_FORMAT.length() - 1) {
manager.update(
NOTES_TABLE,
{pair(TIME_EDITED, timeEdited += "0")},
{pair(NOTE_ID, notes->getItem(NOTE_ID))}
);
}

notes->moveToNext();
}

delete doses;
delete meds;
delete times;;
delete notes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include <memory>
#include <regex>
#include "DbManager.h"
#include "../Medication/Medication.h"
#include "../Dose/Dose.h"
Expand All @@ -23,11 +24,12 @@ namespace TimeFormats {
namespace DateFormats {
const string MM_DD_YYYY = "MM/dd/yyyy";
const string DD_MM_YYYY = "dd/MM/yyyy";
const string DB_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
}

class DatabaseController {
private:
const int DB_VERSION = 14;
const int DB_VERSION = 15;
const string DATABASE_NAME = "Medications.db";
vector<string> tablesToIgnore;
DbManager manager;
Expand Down Expand Up @@ -102,6 +104,10 @@ class DatabaseController {
*/
Dose* setDose(Table* table);

/**
* Resolves issues in caused by imports where the last character of the last record was removed
*/
void repairImportErrors();
public:
const string NOTES_TABLE = "Notes";
const string SETTINGS_TABLE = "Settings";
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/Sqlite3Database/DbManager/DbManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ void DbManager::importData(string &inData, const vector<string> &ignoreTables) {

while ((pos = tblStr.find(',')) != string::npos || tblStr.length() > 0) {
unsigned int end =
tblStr.find(',') != string::npos ? tblStr.find(',') : tblStr.length() - 1;
tblStr.find(',') != string::npos ? tblStr.find(',') : tblStr.length();
string token = tblStr.substr(0, end);
bool incrementInd = false;

Expand Down
19 changes: 14 additions & 5 deletions app/src/main/java/projects/medicationtracker/Helpers/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -882,11 +883,19 @@ public ArrayList<Note> getNotes(long medId) {

Note n = new Note(noteId, medId, note, entryTime);

if (cursor.getString(cursor.getColumnIndexOrThrow(TIME_EDITED)) != null && !cursor.getString((cursor.getColumnIndexOrThrow(TIME_EDITED))).isEmpty()) {
LocalDateTime editTime = TimeFormatting.stringToLocalDateTime(
cursor.getString((cursor.getColumnIndexOrThrow(TIME_EDITED)))
);
n.setModifiedTime(editTime);
if (cursor.getString(cursor.getColumnIndexOrThrow(TIME_EDITED)) != null && !cursor.getString(cursor.getColumnIndexOrThrow(TIME_EDITED)).isEmpty()) {

try {
LocalDateTime editTime = TimeFormatting.stringToLocalDateTime(
cursor.getString(cursor.getColumnIndexOrThrow(TIME_EDITED))
);
n.setModifiedTime(editTime);
} catch (Exception e) {
Log.e(
"Notes",
e.getMessage()
);
}
}

notes.add(n);
Expand Down
18 changes: 0 additions & 18 deletions app/src/main/java/projects/medicationtracker/Models/Dose.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ public Dose(long id, long medicationId, boolean isTaken, @Nullable String timeTa
final String dateFormat = DBHelper.DateFormats.DB_DATE_FORMAT;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat, Locale.getDefault());

// Some times seem to be 1 character short, this protects against that
if (timeTaken.length() < dateFormat.length()) {
timeTaken += "0";
}

if (doseTime.length() < dateFormat.length()) {
doseTime += "0";
}

doseId = id;
medId = medicationId;
taken = isTaken;
Expand All @@ -72,15 +63,6 @@ public Dose(long id, long medicationId, boolean isTaken, String timeTaken, Strin
final String dateFormat = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat, Locale.getDefault());

// Some times seem to be 1 character short, this protects against that
if (timeTaken.length() < dateFormat.length()) {
timeTaken += "0";
}

if (doseTime.length() < dateFormat.length()) {
doseTime += "0";
}

doseId = id;
medId = medicationId;
taken = isTaken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,7 @@ public Medication(String thisMed, String patient, String units, String[] times,
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat, Locale.getDefault());
LocalDateTime[] medTimes = new LocalDateTime[times.length];

// Some times seem to be 1 character short, this protects against that
if (firstDate.length() < dateFormat.length()) {
firstDate += "0";
}

for (int i = 0; i < times.length; i++) {
// Some times seem to be 1 character short, this protects against that
if (times[i].length() < dateFormat.length()) {
times[i] += "0";
}

medTimes[i] = LocalDateTime.parse(times[i], formatter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public Notification(long rowId, long medicationId, long notificationId, LocalDat
public Notification(long rowId, long medicationId, long notificationId, String dosageTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DBHelper.DateFormats.DB_DATE_FORMAT, Locale.getDefault());

if (dosageTime.length() < DBHelper.DateFormats.DB_DATE_FORMAT.length()) {
dosageTime += "0";
}

id = rowId;
medId = medicationId;
this.notificationId = notificationId;
Expand Down

0 comments on commit 632d969

Please sign in to comment.