diff --git a/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java b/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java index f84ad666c..ba40c1cf3 100644 --- a/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java +++ b/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Álinson Santos Xavier + * Copyright (C) 2016 Álinson Santos Xavier * * This file is part of Loop Habit Tracker. * @@ -60,6 +60,20 @@ public class HabitsCSVExporter FileWriter out = new FileWriter(exportDirName + filename); generateFilenames.add(filename); Habit.writeCSV(habits, out); + + filename = "AllCheckmarks.csv"; + new File(exportDirName).mkdirs(); + out = new FileWriter(exportDirName + filename); + generateFilenames.add(filename); + CheckmarkList check = new CheckmarkList(); + check.writeCSVMultipleHabits (habits, out); + + filename = "AllScores.csv"; + new File(exportDirName).mkdirs(); + out = new FileWriter(exportDirName + filename); + generateFilenames.add(filename); + ScoreList score = new ScoreList(); + score.writeCSVMultipleHabits (habits, out); out.close(); for(Habit h : habits) diff --git a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java index 80b565c9c..b1ac60a0c 100644 --- a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java +++ b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Álinson Santos Xavier + * Copyright (C) 2016 Álinson Santos Xavier * * This file is part of Loop Habit Tracker. * @@ -42,6 +42,10 @@ public class CheckmarkList { private Habit habit; + public CheckmarkList() + { + } + public CheckmarkList(Habit habit) { this.habit = habit; @@ -299,4 +303,62 @@ public class CheckmarkList cursor.close(); out.close(); } + + /** + * Writes the checkmark of all habits for each date to the given writer, in CSV format. There is one + * line for each date. Each line contains two fields: timestamp and value. + * + * @param habits the list of habits to write + * @param out the writer where the CSV will be output + * @throws IOException in case write operations fail + */ + + public void writeCSVMultipleHabits(List habits, Writer out) throws IOException + { + SimpleDateFormat dateFormat = DateHelper.getCSVDateFormat(); + String query = "select DISTINCT timestamp from checkmarks order by timestamp"; + SQLiteDatabase db = Cache.openDatabase(); + Cursor cursor = db.rawQuery(query, null); + + if(!cursor.moveToFirst()) return; + + out.write(String.format("%s", "Date")); + for(Habit habit : habits) + { + out.write(String.format(",%s", habit.name)); + } + do + { + String timestamp = dateFormat.format(new Date(cursor.getLong(0))); + out.write(String.format("\n%s", timestamp)); + for(Habit habit : habits) + { + out.write(String.format(",%s", getHabitValue(habit.getId().toString(), Long.toString(cursor.getLong(0))))); + } + } while(cursor.moveToNext()); + cursor.close(); + out.close(); + } + + /** + * Returns the habit value for a given timestamp and habit. + * + * @param name habit for the query + * @param out the writer where the CSV will be output + * + * @return habit value for a specific timestamp and habit + */ + public String getHabitValue(String name, String date) + { + String query = "select value from checkmarks where timestamp = ? and habit = ? "; + SQLiteDatabase db = Cache.openDatabase(); + Cursor cursor = db.rawQuery(query, new String[] {date, name}); + String habitValue = " "; + if(cursor != null && cursor.moveToFirst()) { + habitValue = Integer.toString(cursor.getInt(0)); + cursor.close(); + } + return habitValue; + } } + diff --git a/app/src/main/java/org/isoron/uhabits/models/RepetitionList.java b/app/src/main/java/org/isoron/uhabits/models/RepetitionList.java index 5bfe22fba..7d429c3a7 100644 --- a/app/src/main/java/org/isoron/uhabits/models/RepetitionList.java +++ b/app/src/main/java/org/isoron/uhabits/models/RepetitionList.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Álinson Santos Xavier + * Copyright (C) 2016 Álinson Santos Xavier * * This file is part of Loop Habit Tracker. * diff --git a/app/src/main/java/org/isoron/uhabits/models/ScoreList.java b/app/src/main/java/org/isoron/uhabits/models/ScoreList.java index eca89b9e0..a593128c0 100644 --- a/app/src/main/java/org/isoron/uhabits/models/ScoreList.java +++ b/app/src/main/java/org/isoron/uhabits/models/ScoreList.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Álinson Santos Xavier + * Copyright (C) 2016 Álinson Santos Xavier * * This file is part of Loop Habit Tracker. * @@ -39,6 +39,7 @@ import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; public class ScoreList { @@ -55,6 +56,10 @@ public class ScoreList this.habit = habit; } + public ScoreList() + { + } + protected From select() { return new Select() @@ -342,4 +347,60 @@ public class ScoreList cursor.close(); out.close(); } + + /** + * Writes the checkmark of all habits for each date to the given writer, in CSV format. There is one + * line for each date. Each line contains two fields: timestamp and value. + * + * @param habits the list of habits to write + * @param out the writer where the CSV will be output + * @throws IOException in case write operations fail + */ + + public void writeCSVMultipleHabits(List habits, Writer out) throws IOException + { + SimpleDateFormat dateFormat = DateHelper.getCSVDateFormat(); + String query = "select DISTINCT timestamp from checkmarks order by timestamp"; + SQLiteDatabase db = Cache.openDatabase(); + Cursor cursor = db.rawQuery(query, null); + + if(!cursor.moveToFirst()) return; + + out.write(String.format("%s", "Date")); + for(Habit habit : habits) + { + out.write(String.format(",%s", habit.name)); + } + do + { + String timestamp = dateFormat.format(new Date(cursor.getLong(0))); + out.write(String.format("\n%s", timestamp)); + for(Habit habit : habits) + { + out.write(String.format(",%s", getHabitScore(habit.getId().toString(), Long.toString(cursor.getLong(0))))); + } + } while(cursor.moveToNext()); + cursor.close(); + out.close(); + } + + /** + * Returns the habit score for a given timestamp and habit. + * + * @param name habit for the query + * @param out the writer where the CSV will be output + * + * @return habit value for a specific timestamp and habit + */ + public String getHabitScore(String name, String date){ + String query = "select score from Score where timestamp = ? and habit = ?"; + SQLiteDatabase db = Cache.openDatabase(); + Cursor cursor = db.rawQuery(query, new String[] {date, name}); + String habitScore = " "; + if(cursor != null && cursor.moveToFirst()) { + habitScore = String.format("%.4f", ((float) cursor.getInt(0)) / Score.MAX_VALUE); + cursor.close(); + } + return habitScore; + } } diff --git a/build.gradle b/build.gradle index eb0a985ee..554cf0159 100644 --- a/build.gradle +++ b/build.gradle @@ -26,4 +26,4 @@ subprojects { project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs } } -} \ No newline at end of file +}