diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/io/HabitsCSVExporter.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/io/HabitsCSVExporter.kt index 019ab9516..fa5af0522 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/io/HabitsCSVExporter.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/io/HabitsCSVExporter.kt @@ -22,6 +22,7 @@ import org.isoron.uhabits.core.models.Entry import org.isoron.uhabits.core.models.EntryList import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.HabitList +import org.isoron.uhabits.core.models.HabitType import org.isoron.uhabits.core.models.Score import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.utils.DateFormats @@ -95,7 +96,7 @@ class HabitsCSVExporter( File(exportDirName + habitDirName).mkdirs() generatedDirs.add(habitDirName) writeScores(habitDirName, h) - writeEntries(habitDirName, h.computedEntries) + writeEntries(habitDirName, h.computedEntries, h.type) } writeMultipleHabits() } @@ -117,18 +118,52 @@ class HabitsCSVExporter( out.close() } - private fun writeEntries(habitDirName: String, entries: EntryList) { + private fun getEntryTypeNameFromValue(value: Int) : String { + // I mean the code below follows pattern + // If there is something in Kotlin that could do: + // if value <= 3: Entry.Companion.get(value).toString(), that would be nice + return when(value){ + Entry.SKIP -> { Entry.SKIP.toString() } + Entry.YES_AUTO -> { Entry.YES_AUTO.toString() } + Entry.NO -> { Entry.NO.toString() } + Entry.UNKNOWN -> { Entry.UNKNOWN.toString() } + Entry.YES_MANUAL -> { Entry.YES_MANUAL.toString() } + else -> { "" } + } + } + + private fun writeEntries(habitDirName: String, entries: EntryList, habitType: HabitType) { val filename = habitDirName + "Checkmarks.csv" val out = FileWriter(exportDirName + filename) generatedFilenames.add(filename) val dateFormat = DateFormats.getCSVDateFormat() for ((timestamp, value) in entries.getKnown()) { val date = dateFormat.format(timestamp.toJavaDate()) - out.write(String.format(Locale.US, "%s,%.3f\n", date, value.toDouble() / 1000.0)) + out.write(String.format(Locale.US, "%s,%s\n", date, getEntryValueString(habitType, value))) } out.close() } + private fun getEntryValueString(habitType: HabitType, value : Int) : String{ + when(habitType){ + HabitType.NUMERICAL -> { + val outputtedValue = getEntryTypeNameFromValue(value) + if(outputtedValue.isEmpty()) // Not a stringable value + return String.format("%.3f", value.toDouble() / 1000.0) + + return outputtedValue + } + + HabitType.YES_NO -> { + val outputtedValue = getEntryTypeNameFromValue(value) + if (outputtedValue.isEmpty()) + throw IllegalStateException() // YES_NO habits should have named entries + + return outputtedValue + } + } + } + /** * Writes a scores file and a checkmarks file containing scores and checkmarks of every habit. * The first column corresponds to the date. Subsequent columns correspond to a habit. @@ -150,8 +185,9 @@ class HabitsCSVExporter( val timeframe = getTimeframe() val oldest = timeframe[0] val newest = DateUtils.getTodayWithOffset() - val checkmarks: MutableList> = ArrayList() - val scores: MutableList> = ArrayList() + val checkmarks: ArrayList> = ArrayList() + val scores: ArrayList> = ArrayList() + for (habit in selectedHabits) { checkmarks.add(ArrayList(habit.computedEntries.getByInterval(oldest, newest))) scores.add(ArrayList(habit.scores.getByInterval(oldest, newest))) @@ -167,9 +203,13 @@ class HabitsCSVExporter( checksWriter.write(sb.toString()) scoresWriter.write(sb.toString()) for (j in selectedHabits.indices) { - checksWriter.write(String.format("%.3f", checkmarks[j][i].value.toDouble() / 1000.0)) + val check = String.format("%s", + getEntryValueString(selectedHabits[j].type, checkmarks[j][i].value)) + checksWriter.write(check) checksWriter.write(delimiter) - val score = String.format(Locale.US, "%.4f", scores[j][i].value) + + val score = String.format(Locale.US, "%s", + getEntryValueString(selectedHabits[j].type, checkmarks[j][i].value)) scoresWriter.write(score) scoresWriter.write(delimiter) }