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..f7eddfcdc 100644 --- a/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java +++ b/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java @@ -62,6 +62,18 @@ public class HabitsCSVExporter Habit.writeCSV(habits, out); out.close(); + //my contribution + String filename2 = "AllCheckmarks.csv"; + new File(exportDirName).mkdirs(); + FileWriter out2 = new FileWriter(exportDirName + filename2); + generateFilenames.add(filename2); + CheckmarkList check = new CheckmarkList(); + check.writeCSVMultipleHabits (habits, out2); + out2.close(); + //until here + + + for(Habit h : habits) { String habitDirName = String.format("%03d %s/", h.position + 1, h.name); 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..4fefddcfe 100644 --- a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java +++ b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java @@ -19,6 +19,7 @@ package org.isoron.uhabits.models; +import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; @@ -28,6 +29,7 @@ import android.support.annotation.Nullable; import com.activeandroid.Cache; import com.activeandroid.query.Delete; import com.activeandroid.query.Select; +import com.opencsv.CSVWriter; import org.isoron.uhabits.helpers.DateHelper; import org.isoron.uhabits.helpers.UIHelper; @@ -42,6 +44,10 @@ public class CheckmarkList { private Habit habit; + public CheckmarkList() + { + } + public CheckmarkList(Habit habit) { this.habit = habit; @@ -299,4 +305,59 @@ public class CheckmarkList cursor.close(); out.close(); } + + //my contribution + + /** + * 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.name, timestamp))); + } + } while(cursor.moveToNext()); + cursor.close(); + out.close(); + } + + public String getHabitValue(String name, String date) + { + String query = "select value from checkmarks where timestamp ="+date+" and habit ='"+name+"'"; + SQLiteDatabase db2 = Cache.openDatabase(); + Cursor cursor2 = db2.rawQuery(query, null); + String habitValue = " "; + if(cursor2 != null && cursor2.moveToFirst()) { + habitValue = Integer.toString(cursor2.getInt(0)); + cursor2.close(); + } + return habitValue; + }//until here } diff --git a/build.gradle b/build.gradle index d71fd8517..5d5e307c8 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0-alpha3' + classpath 'com.android.tools.build:gradle:2.0.0' } }