Skip to content

Commit

Permalink
Merge pull request #129 from AdamGuidarini/develop
Browse files Browse the repository at this point in the history
Develop - Adding decimal points to doses
  • Loading branch information
AdamGuidarini authored Oct 27, 2024
2 parents 2f2c483 + 1ac546c commit 0c6e842
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 126 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MediTrak is an Android application designed to make it easier for users to keep

## Requirements

- Android 10+
- Android 9+

## Languages
- English
Expand Down
6 changes: 3 additions & 3 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 29
minSdkVersion 28
targetSdkVersion 34
compileSdk 34

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

ndk {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void DatabaseController::create() {
+ MED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ MED_NAME + " TEXT,"
+ PATIENT_NAME + " Text,"
+ MED_DOSAGE + " DECIMAL(3,2),"
+ MED_DOSAGE + " REAL,"
+ MED_UNITS + " TEXT,"
+ START_DATE + " DATETIME,"
+ MED_FREQUENCY + " INTEGER,"
Expand All @@ -69,7 +69,7 @@ void DatabaseController::create() {
+ DOSE_TIME + " DATETIME,"
+ TAKEN + " BOOLEAN,"
+ TIME_TAKEN + " DATETIME,"
+ OVERRIDE_DOSE_AMOUNT + " INTEGER,"
+ OVERRIDE_DOSE_AMOUNT + " REAL,"
+ OVERRIDE_DOSE_UNIT + " TEXT,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" + MED_ID +
") ON DELETE CASCADE"
Expand Down Expand Up @@ -231,6 +231,60 @@ void DatabaseController::upgrade(int currentVersion) {
repairImportErrors();
}

if (currentVersion < 16) {
string sql =
"BEGIN TRANSACTION; PRAGMA foreign_keys = OFF; CREATE TABLE "
+ MEDICATION_TABLE + "_1("
+ MED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ MED_NAME + " TEXT,"
+ PATIENT_NAME + " Text,"
+ MED_DOSAGE + " REAL,"
+ MED_UNITS + " TEXT,"
+ START_DATE + " DATETIME,"
+ MED_FREQUENCY + " INTEGER,"
+ ALIAS + " TEXT,"
+ ACTIVE + " BOOLEAN DEFAULT 1,"
+ PARENT_ID + " INTEGER,"
+ CHILD_ID + " INTEGER,"
+ INSTRUCTIONS + " TEXT,"
+ "FOREIGN KEY (" + PARENT_ID + ") REFERENCES "
+ MEDICATION_TABLE + "(" + MED_ID + ") ON DELETE CASCADE,"
+ "FOREIGN KEY (" + CHILD_ID + ") REFERENCES "
+ MEDICATION_TABLE + "(" + MED_ID + ") ON DELETE CASCADE"
+ ");"

+ "INSERT INTO " + MEDICATION_TABLE + "_1" + " SELECT * FROM " +
MEDICATION_TABLE +
";"
+ "DROP TABLE " + MEDICATION_TABLE + ";"
+ "ALTER TABLE " + MEDICATION_TABLE + "_1" + " RENAME TO '" +
MEDICATION_TABLE + "';"

+ "CREATE TABLE " + MEDICATION_TRACKER_TABLE + "_1 ("
+ DOSE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ MED_ID + " INTEGER,"
+ DOSE_TIME + " DATETIME,"
+ TAKEN + " BOOLEAN,"
+ TIME_TAKEN + " DATETIME,"
+ OVERRIDE_DOSE_AMOUNT + " REAL,"
+ OVERRIDE_DOSE_UNIT + " TEXT,"
+ "FOREIGN KEY (" + MED_ID + ") REFERENCES " + MEDICATION_TABLE + "(" +
MED_ID +
") ON DELETE CASCADE"
+ ");"

+ "INSERT INTO " + MEDICATION_TRACKER_TABLE + "_1 "
+ "SELECT * FROM " + MEDICATION_TRACKER_TABLE + ";"
+ "DROP TABLE " + MEDICATION_TRACKER_TABLE + ";"
+ "ALTER TABLE " + MEDICATION_TRACKER_TABLE + "_1 RENAME TO '" +
MEDICATION_TRACKER_TABLE + "';"

+ " PRAGMA foreign_keys = ON;"
+ " COMMIT;";

manager.execSql(sql);
}

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

Expand Down Expand Up @@ -325,7 +379,7 @@ Medication DatabaseController::getMedication(long medicationId) {
{},
table->getItem(START_DATE),
stol(table->getItem(MED_ID)),
stoi(table->getItem(MED_DOSAGE)),
stof(table->getItem(MED_DOSAGE)),
stoi(table->getItem(MED_FREQUENCY)),
table->getItem(ACTIVE) == "1",
table->getItem(ALIAS)
Expand Down Expand Up @@ -365,11 +419,11 @@ vector<Dose> DatabaseController::getTakenDoses(long medicationId) {
Table *table = manager.execSqlWithReturn(query);

while (table->getCount() > 0 && !table->isAfterLast()) {
int overrideDose = -1;
float overrideDose = -1;
string overrideUnit;

if (!empty(table->getItem(OVERRIDE_DOSE_AMOUNT))) {
overrideDose = stoi(table->getItem(OVERRIDE_DOSE_AMOUNT));
overrideDose = stof(table->getItem(OVERRIDE_DOSE_AMOUNT));
}

if (!empty(table->getItem(OVERRIDE_DOSE_UNIT))) {
Expand Down Expand Up @@ -400,11 +454,11 @@ Dose *DatabaseController::setDose(Table *table) {
if (table->getCount() > 0) {
table->moveToFirst();

int overrideDose = -1;
float overrideDose = -1;
string overrideUnit;

if (!empty(table->getItem(OVERRIDE_DOSE_AMOUNT))) {
overrideDose = stoi(table->getItem(OVERRIDE_DOSE_AMOUNT));
overrideDose = stof(table->getItem(OVERRIDE_DOSE_AMOUNT));
}

if (!empty(table->getItem(OVERRIDE_DOSE_UNIT))) {
Expand Down Expand Up @@ -650,7 +704,7 @@ void DatabaseController::repairImportErrors() {
} else if (!match && timeEdited.length() == DateFormats::DB_DATE_FORMAT.length() - 1) {
manager.update(
NOTES_TABLE,
{pair(TIME_EDITED, timeEdited += "0")},
{pair(TIME_EDITED, timeEdited + "0")},
{pair(NOTE_ID, notes->getItem(NOTE_ID))}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ namespace DateFormats {

class DatabaseController {
private:
const int DB_VERSION = 15;
const int DB_VERSION = 16;
const string DATABASE_NAME = "Medications.db";
vector<string> tablesToIgnore;
DbManager manager;

const string ANDROID_METADATA = "android_metadata";

const string MEDICATION_TABLE = "Medication";
Expand Down Expand Up @@ -104,10 +104,6 @@ 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 Expand Up @@ -253,6 +249,11 @@ class DatabaseController {
* @param id ID of notification to delete
*/
void deleteNotification(long id);

/**
* Resolves issues in caused by imports where the last character of the last record was removed
*/
void repairImportErrors();
};

#endif //MEDICATIONTRACKER_DATABASECONTROLLER_H
10 changes: 9 additions & 1 deletion app/src/main/cpp/MediTrakCore/Dose/Dose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ Dose::Dose() {
doseTime = {};
}

Dose::Dose(long id, long medicationId, bool taken, std::string doseTime, std::string timeTaken, int overrideDoseAmount, std::string overrideDoseUnit) {
Dose::Dose(
long id,
long medicationId,
bool taken,
std::string doseTime,
std::string timeTaken,
float overrideDoseAmount,
std::string overrideDoseUnit
) {
this->id = id;
this->medicationId = medicationId;
this->taken = taken;
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/cpp/MediTrakCore/Dose/Dose.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ struct Dose {
bool taken;
std::string doseTime;
std::string timeTaken;
int overrideDoseAmount;
float overrideDoseAmount;
std::string overrideDoseUnit;

Dose();
Dose(long id, long medicationId, bool taken, std::string doseTime, std::string timeTaken, int overrideDose = -1, std::string overrideDoseUnit = "");

Dose(
long id,
long medicationId,
bool taken,
std::string doseTime,
std::string timeTaken,
float overrideDose = -1,
std::string overrideDoseUnit = ""
);
};


Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/MediTrakCore/Medication/Medication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Medication::Medication(
vector<string> times,
string startDate,
long id,
int dosage,
float dosage,
int frequency,
bool active,
string alias
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/cpp/MediTrakCore/Medication/Medication.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Medication {
vector<string> times;
vector<Dose> doses;
long id;
int dosage;
float dosage;
int frequency;
bool active;
shared_ptr<Medication> parent = nullptr;
Expand Down Expand Up @@ -52,7 +52,7 @@ struct Medication {
vector<string> times,
string startDate,
long id,
int dosage,
float dosage,
int frequency,
bool active,
string alias = ""
Expand Down
17 changes: 6 additions & 11 deletions app/src/main/cpp/medicationtracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobject doseToJavaConverter(const Dose& dose, JNIEnv *env, jobject &jMedication)
auto jDoses = static_cast<jobjectArray>(env->GetObjectField(jMedication, medDoses));
jclass jDose = env->GetObjectClass(env->GetObjectArrayElement(jDoses, 0));

jmethodID setOverrideDoseAmount = env->GetMethodID(jDose, "setOverrideDoseAmount", "(I)V");
jmethodID setOverrideDoseAmount = env->GetMethodID(jDose, "setOverrideDoseAmount", "(F)V");
jmethodID setOverrideDoseUnit = env->GetMethodID(jDose, "setOverrideDoseUnit",
"(Ljava/lang/String;)V");

Expand All @@ -45,12 +45,6 @@ jobject doseToJavaConverter(const Dose& dose, JNIEnv *env, jobject &jMedication)
jmethodID setTimeTaken = env->GetMethodID(jDose, "setTimeTaken", "(Ljava/lang/String;)V");
jmethodID setDoseTime = env->GetMethodID(jDose, "setDoseTime", "(Ljava/lang/String;)V");

jmethodID constructor = env->GetMethodID(
jDose,
"<init>",
"(JJZLjava/lang/String;Ljava/lang/String;)V"
);

jmethodID constructorDefault = env->GetMethodID(
jDose,
"<init>",
Expand Down Expand Up @@ -95,7 +89,7 @@ Dose javaDoseToNativeDoseConverter(jobject jDose, JNIEnv *env) {
"()Ljava/lang/String;");
jmethodID getDoseTimeText = env->GetMethodID(jDoseClass, "getDoseTimeText",
"()Ljava/lang/String;");
jmethodID getOverrideDoseAmount = env->GetMethodID(jDoseClass, "getOverrideDoseAmount", "()I");
jmethodID getOverrideDoseAmount = env->GetMethodID(jDoseClass, "getOverrideDoseAmount", "()F");
jmethodID getOverrideDoseUnit = env->GetMethodID(jDoseClass, "getOverrideDoseUnit",
"()Ljava/lang/String;");

Expand All @@ -107,7 +101,7 @@ Dose javaDoseToNativeDoseConverter(jobject jDose, JNIEnv *env) {
new jboolean(false)),
env->GetStringUTFChars((jstring) env->CallObjectMethod(jDose, getTimeTakenText),
new jboolean(false)),
env->CallIntMethod(jDose, getOverrideDoseAmount),
env->CallFloatMethod(jDose, getOverrideDoseAmount),
env->GetStringUTFChars((jstring) env->CallObjectMethod(jDose, getOverrideDoseUnit),
new jboolean(false))
);
Expand All @@ -119,7 +113,7 @@ jobject medicationToJavaConverter(Medication med, JNIEnv *env, jclass jMedicatio
jmethodID medConstructor = env->GetMethodID(
jMedication,
"<init>",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;JIILjava/lang/String;)V"
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;JIFLjava/lang/String;)V"
);

for (int i = 0; i < med.times.size(); i++) {
Expand All @@ -139,7 +133,7 @@ jobject medicationToJavaConverter(Medication med, JNIEnv *env, jclass jMedicatio
env->NewStringUTF(med.startDate.c_str()),
med.id,
jint(med.frequency),
jint(med.dosage),
med.dosage,
env->NewStringUTF(med.alias.c_str())
);

Expand Down Expand Up @@ -274,6 +268,7 @@ Java_projects_medicationtracker_Helpers_NativeDbHelper_dbImporter(

try {
controller.importJSONString(fileContents, ignoredTbls);
controller.repairImportErrors();
} catch (exception &e) {
__android_log_write(ANDROID_LOG_ERROR, nullptr, e.what());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package projects.medicationtracker.Adapters;

import static projects.medicationtracker.MediTrak.formatter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -11,9 +13,7 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Locale;
import java.util.stream.Stream;

import projects.medicationtracker.Models.Medication;
import projects.medicationtracker.R;
Expand Down Expand Up @@ -82,9 +82,9 @@ public void onBindViewHolder(@NonNull HistoryAdapter.ViewHolder holder, int posi
String doseLbl = "";

if (doses[position].getOverrideDoseAmount() != -1) {
doseLbl += doses[position].getOverrideDoseAmount() + " ";
doseLbl += formatter.format(doses[position].getOverrideDoseAmount()) + " ";
} else {
doseLbl += currentMed.getDosage() + " ";
doseLbl += formatter.format(currentMed.getDosage()) + " ";
}

if (!doses[position].getOverrideDoseUnit().isEmpty()) {
Expand Down
Loading

0 comments on commit 0c6e842

Please sign in to comment.