From b0b4c739ba32f8af21594206b1ce3bf23cb6080c Mon Sep 17 00:00:00 2001 From: PKompis Date: Sat, 21 May 2016 17:04:06 +0300 Subject: [PATCH 1/6] issue 68 not yet completed --- .../isoron/uhabits/io/HabitsCSVExporter.java | 12 ++++ .../isoron/uhabits/models/CheckmarkList.java | 61 +++++++++++++++++++ build.gradle | 2 +- 3 files changed, 74 insertions(+), 1 deletion(-) 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' } } From d7778221fb0586182b8f84fe124404e282f6a884 Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Wed, 25 May 2016 21:26:46 +0300 Subject: [PATCH 2/6] Update CheckmarkList.java --- .../isoron/uhabits/models/CheckmarkList.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) 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 4fefddcfe..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. * @@ -19,7 +19,6 @@ package org.isoron.uhabits.models; -import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; @@ -29,7 +28,6 @@ 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; @@ -306,8 +304,6 @@ public class CheckmarkList 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. @@ -319,11 +315,8 @@ public class CheckmarkList 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); @@ -338,26 +331,34 @@ public class CheckmarkList { 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))); + 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 ="+date+" and habit ='"+name+"'"; - SQLiteDatabase db2 = Cache.openDatabase(); - Cursor cursor2 = db2.rawQuery(query, null); + 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(cursor2 != null && cursor2.moveToFirst()) { - habitValue = Integer.toString(cursor2.getInt(0)); - cursor2.close(); + if(cursor != null && cursor.moveToFirst()) { + habitValue = Integer.toString(cursor.getInt(0)); + cursor.close(); } return habitValue; - }//until here + } } + From 2721c22e6ac78ca9919872ed9eb3c7a4507beec4 Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Wed, 25 May 2016 21:27:11 +0300 Subject: [PATCH 3/6] Update RepetitionList.java --- app/src/main/java/org/isoron/uhabits/models/RepetitionList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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. * From d917029eaaf8583b113323c4690ff0900b658894 Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Wed, 25 May 2016 21:27:39 +0300 Subject: [PATCH 4/6] Update HabitsCSVExporter.java --- .../isoron/uhabits/io/HabitsCSVExporter.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) 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 f7eddfcdc..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,19 +60,21 @@ public class HabitsCSVExporter FileWriter out = new FileWriter(exportDirName + filename); generateFilenames.add(filename); 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); + filename = "AllCheckmarks.csv"; + new File(exportDirName).mkdirs(); + out = new FileWriter(exportDirName + filename); + generateFilenames.add(filename); CheckmarkList check = new CheckmarkList(); - check.writeCSVMultipleHabits (habits, out2); - out2.close(); - //until here - + 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) { From 8c37d88785d76d5d07d4794a97a5fe17a5a337f5 Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Wed, 25 May 2016 21:28:30 +0300 Subject: [PATCH 5/6] Update build.gradle --- build.gradle | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 5d5e307c8..554cf0159 100644 --- a/build.gradle +++ b/build.gradle @@ -3,14 +3,16 @@ buildscript { repositories { jcenter() } + dependencies { - classpath 'com.android.tools.build:gradle:2.0.0' + classpath 'com.android.tools.build:gradle:2.1.0' } } allprojects { repositories { jcenter() + maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } } } @@ -24,4 +26,4 @@ subprojects { project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs } } -} \ No newline at end of file +} From 0d523ac74ba972cfc1784ed7865a08bba70e36c4 Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Wed, 25 May 2016 21:31:02 +0300 Subject: [PATCH 6/6] Update ScoreList.java --- .../org/isoron/uhabits/models/ScoreList.java | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) 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; + } }