Skip to content

Commit

Permalink
Merge pull request #131 from AdamGuidarini/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AdamGuidarini authored Nov 16, 2024
2 parents 3c52ad9 + bc73852 commit 2daee17
Show file tree
Hide file tree
Showing 17 changed files with 573 additions and 111 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ android {

defaultConfig {
applicationId "projects.medicationtracker"
minSdkVersion 28
minSdkVersion 29
targetSdkVersion 34
compileSdk 34

versionCode 31
versionName "0.13.2"
versionName "0.13.3"
resourceConfigurations += ['en', 'de', 'es', 'it', 'tr']

ndk {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ void DatabaseController::create() {
+ "FOREIGN KEY (" + PARENT_ID + ") REFERENCES "
+ MEDICATION_TABLE + "(" + MED_ID + ") ON DELETE CASCADE,"
+ "FOREIGN KEY (" + CHILD_ID + ") REFERENCES "
+ MEDICATION_TABLE + "(" + MED_ID + ") ON DELETE CASCADE"
+ ");"
+ MEDICATION_TABLE + "(" + MED_ID + ") ON DELETE CASCADE);"
);

manager.execSql("CREATE TABLE IF NOT EXISTS " + MEDICATION_TRACKER_TABLE + "("
Expand All @@ -72,8 +71,7 @@ void DatabaseController::create() {
+ OVERRIDE_DOSE_AMOUNT + " REAL,"
+ OVERRIDE_DOSE_UNIT + " TEXT,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" + MED_ID +
") ON DELETE CASCADE"
+ ");"
") ON DELETE CASCADE);"
);

manager.execSql(
Expand All @@ -82,8 +80,7 @@ void DatabaseController::create() {
+ MED_ID + " INTEGER,"
+ DRUG_TIME + " TEXT,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" + MED_ID +
") ON DELETE CASCADE"
+ ");"
") ON DELETE CASCADE);"
);

manager.execSql(
Expand All @@ -94,8 +91,7 @@ void DatabaseController::create() {
+ ENTRY_TIME + " DATETIME,"
+ TIME_EDITED + " DATETIME,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" + MED_ID +
") ON DELETE CASCADE"
+ ");"
") ON DELETE CASCADE);"
);

manager.execSql(
Expand All @@ -106,7 +102,10 @@ void DatabaseController::create() {
+ AGREED_TO_TERMS + " BOOLEAN DEFAULT 0,"
+ DATE_FORMAT + " TEXT DEFAULT '" + DateFormats::MM_DD_YYYY + "',"
+ TIME_FORMAT + " TEXT DEFAULT '" + TimeFormats::_12_HOUR + "',"
+ SEEN_NOTIFICATION_REQUEST + " BOOLEAN DEFAULT 0);"
+ SEEN_NOTIFICATION_REQUEST + " BOOLEAN DEFAULT 0,"
+ EXPORT_FREQUENCY + " INTEGER DEFAULT -1,"
+ EXPORT_START + " DATETIME,"
+ EXPORT_FILE_NAME + " TEXT);"
);

manager.execSql(
Expand All @@ -121,8 +120,7 @@ void DatabaseController::create() {
+ CHANGE_DATE + " DATETIME,"
+ PAUSED + " BOOLEAN,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" + MED_ID +
") ON DELETE CASCADE"
+ ");"
") ON DELETE CASCADE);"
);

manager.execSql(
Expand All @@ -132,8 +130,7 @@ void DatabaseController::create() {
+ DOSE_ID + " INTEGER, "
+ SCHEDULED_TIME + " DATETIME,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" + MED_ID +
") ON DELETE CASCADE"
+ ");"
") ON DELETE CASCADE);"
);

manager.execSql("PRAGMA schema_version = " + to_string(DB_VERSION));
Expand All @@ -157,8 +154,7 @@ void DatabaseController::upgrade(int currentVersion) {
+ CHANGE_DATE + " DATETIME,"
+ PAUSED + " BOOLEAN,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" + MED_ID +
") ON DELETE CASCADE"
+ ");"
") ON DELETE CASCADE);"
);
}

Expand Down Expand Up @@ -221,9 +217,8 @@ void DatabaseController::upgrade(int currentVersion) {
+ DOSE_ID + " INTEGER, "
+ SCHEDULED_TIME + " DATETIME,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" + MED_ID +
") ON DELETE CASCADE"
+ ");"
+ "END TRANSACTION;"
") ON DELETE CASCADE);"
+ "COMMIT;"
);
}

Expand All @@ -250,12 +245,10 @@ void DatabaseController::upgrade(int currentVersion) {
+ "FOREIGN KEY (" + PARENT_ID + ") REFERENCES "
+ MEDICATION_TABLE + "(" + MED_ID + ") ON DELETE CASCADE,"
+ "FOREIGN KEY (" + CHILD_ID + ") REFERENCES "
+ MEDICATION_TABLE + "(" + MED_ID + ") ON DELETE CASCADE"
+ ");"
+ MEDICATION_TABLE + "(" + MED_ID + ") ON DELETE CASCADE);"

+ "INSERT INTO " + MEDICATION_TABLE + "_1" + " SELECT * FROM " +
MEDICATION_TABLE +
";"
+ "INSERT INTO " + MEDICATION_TABLE + "_1" + " SELECT * FROM "
+ MEDICATION_TABLE + ";"
+ "DROP TABLE " + MEDICATION_TABLE + ";"
+ "ALTER TABLE " + MEDICATION_TABLE + "_1" + " RENAME TO '" +
MEDICATION_TABLE + "';"
Expand All @@ -270,8 +263,7 @@ void DatabaseController::upgrade(int currentVersion) {
+ OVERRIDE_DOSE_UNIT + " TEXT,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" +
MED_ID +
") ON DELETE CASCADE"
+ ");"
") ON DELETE CASCADE);"

+ "INSERT INTO " + MEDICATION_TRACKER_TABLE + "_1 "
+ "SELECT * FROM " + MEDICATION_TRACKER_TABLE + ";"
Expand All @@ -285,6 +277,17 @@ void DatabaseController::upgrade(int currentVersion) {
manager.execSql(sql);
}

if (currentVersion < 17) {
manager.execSql(
"ALTER TABLE " + SETTINGS_TABLE
+ " ADD COLUMN " + EXPORT_FREQUENCY + " INTEGER DEFAULT -1;"
+ "ALTER TABLE " + SETTINGS_TABLE
+ " ADD COLUMN " + EXPORT_START + " DATETIME;"
+ "ALTER TABLE " + SETTINGS_TABLE
+ " ADD COLUMN " + EXPORT_FILE_NAME + " TEXT;"
);
}

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

Expand Down Expand Up @@ -610,6 +613,10 @@ void DatabaseController::deleteNotification(long id) {
manager.deleteRecord(NOTIFICATIONS, {pair(DOSE_ID, to_string(id))});
}

void DatabaseController::deleteNotificationsByMedicationId(long medicationId) {
manager.deleteRecord(NOTIFICATIONS, {pair(MED_ID, to_string(medicationId))});
}

void DatabaseController::repairImportErrors() {
auto doses = manager.execSqlWithReturn("SELECT * FROM " + MEDICATION_TRACKER_TABLE);
auto meds = manager.execSqlWithReturn("SELECT * FROM " + MEDICATION_TABLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace DateFormats {

class DatabaseController {
private:
const int DB_VERSION = 16;
const int DB_VERSION = 17;
const string DATABASE_NAME = "Medications.db";
vector<string> tablesToIgnore;
DbManager manager;
Expand Down Expand Up @@ -81,6 +81,9 @@ class DatabaseController {
const string AGREED_TO_TERMS = "AgreedToTerms";
const string DATE_FORMAT = "DateFormat";
const string TIME_FORMAT = "TimeFormat";
const string EXPORT_FREQUENCY = "ExportFrequency";
const string EXPORT_START = "ExportStart";
const string EXPORT_FILE_NAME = "ExportFileName";

// Activity Change
const string CHANGE_EVENT_ID = "ChangeId";
Expand Down Expand Up @@ -250,6 +253,12 @@ class DatabaseController {
*/
void deleteNotification(long id);

/**
* Deleted all notifications with the given medication ID
* @param medicationId
*/
void deleteNotificationsByMedicationId(long medicationId);

/**
* Resolves issues in caused by imports where the last character of the last record was removed
*/
Expand Down
59 changes: 54 additions & 5 deletions app/src/main/cpp/medicationtracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std::map<std::string, std::string> getValues(jobjectArray arr, JNIEnv *env) {
return vals;
}

jobject doseToJavaConverter(const Dose& dose, JNIEnv *env, jobject &jMedication) {
jobject doseToJavaConverter(const Dose &dose, JNIEnv *env, jobject &jMedication) {
jfieldID medDoses = env->GetFieldID(env->GetObjectClass(jMedication), "doses",
"[Lprojects/medicationtracker/Models/Dose;");
auto jDoses = static_cast<jobjectArray>(env->GetObjectField(jMedication, medDoses));
Expand Down Expand Up @@ -280,8 +280,11 @@ Java_projects_medicationtracker_Helpers_NativeDbHelper_dbImporter(

extern "C"
JNIEXPORT void JNICALL
Java_projects_medicationtracker_Helpers_NativeDbHelper_dbCreate(JNIEnv *env, jobject thiz,
jstring db_path) {
Java_projects_medicationtracker_Helpers_NativeDbHelper_dbCreate(
JNIEnv *env,
jobject thiz,
jstring db_path
) {
std::string db = env->GetStringUTFChars(db_path, new jboolean(true));

try {
Expand All @@ -293,8 +296,12 @@ Java_projects_medicationtracker_Helpers_NativeDbHelper_dbCreate(JNIEnv *env, job

extern "C"
JNIEXPORT void JNICALL
Java_projects_medicationtracker_Helpers_NativeDbHelper_dbUpgrade(JNIEnv *env, jobject thiz,
jstring db_path, jint version) {
Java_projects_medicationtracker_Helpers_NativeDbHelper_dbUpgrade(
JNIEnv *env,
jobject thiz,
jstring db_path,
jint version
) {
std::string db = env->GetStringUTFChars(db_path, new jboolean(true));

try {
Expand Down Expand Up @@ -647,3 +654,45 @@ Java_projects_medicationtracker_Helpers_NativeDbHelper_addDose(

return rowId;
}

extern "C"
JNIEXPORT void JNICALL
Java_projects_medicationtracker_Helpers_NativeDbHelper_updateSettings(
JNIEnv *env,
jobject thiz,
jstring db_path,
jobjectArray settings
) {
std::string dbPath = env->GetStringUTFChars(db_path, new jboolean(true));
std::map<std::string, std::string> opts = getValues(settings, env);

DatabaseController controller(dbPath);

try {
controller.updateSettings(opts);
} catch (exception& e) {
auto err = "Unable to update settings";

__android_log_write(ANDROID_LOG_ERROR, "SETTINGS UPDATE", err);
}
}
extern "C"
JNIEXPORT void JNICALL
Java_projects_medicationtracker_Helpers_NativeDbHelper_deleteNotificationsByMedId(
JNIEnv *env,
jobject thiz,
jstring db_path,
jlong medicationid
) {
std::string dbPath = env->GetStringUTFChars(db_path, new jboolean(true));

DatabaseController controller(dbPath);

try {
controller.deleteNotificationsByMedicationId(medicationid);
} catch (exception& e) {
auto err = "Unable to delete notifications for medication: " + to_string(medicationid);

__android_log_write(ANDROID_LOG_ERROR, "SETTINGS UPDATE", err.c_str());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ private void saveMedication() {

db.updateMedication(parentMed);
db.addNote(changesNotes, childId);
nativeDb.deleteNotificationByMedicationId(parentMed.getId());

long[] ids = db.getMedicationTimeIds(parentMed);
NotificationManager manager = (NotificationManager) getSystemService(
Expand Down
Loading

0 comments on commit 2daee17

Please sign in to comment.