Skip to content

Commit

Permalink
Handling special values for entries of habits
Browse files Browse the repository at this point in the history
  • Loading branch information
foralost committed Feb 3, 2024
1 parent bc972e8 commit a02de47
Showing 1 changed file with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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.
Expand All @@ -150,8 +185,9 @@ class HabitsCSVExporter(
val timeframe = getTimeframe()
val oldest = timeframe[0]
val newest = DateUtils.getTodayWithOffset()
val checkmarks: MutableList<ArrayList<Entry>> = ArrayList()
val scores: MutableList<ArrayList<Score>> = ArrayList()
val checkmarks: ArrayList<ArrayList<Entry>> = ArrayList()
val scores: ArrayList<ArrayList<Score>> = ArrayList()

for (habit in selectedHabits) {
checkmarks.add(ArrayList(habit.computedEntries.getByInterval(oldest, newest)))
scores.add(ArrayList(habit.scores.getByInterval(oldest, newest)))
Expand All @@ -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)
}
Expand Down

0 comments on commit a02de47

Please sign in to comment.