Merge pull request #1278 from hiqua/fix_habit_bull_numerical

Handle numerical habits from HabitBull
pull/1305/head
Alinson S. Xavier 4 years ago committed by GitHub
commit 1d3bd48535
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,11 @@
HabitName,HabitDescription,HabitCategory,CalendarDate,Value,CommentText
Pushups,,Fitness,2021-09-01,30,
Pushups,,Fitness,2022-01-08,100,
Pushups,,Fitness,2022-01-09,100,
Pushups,,Fitness,2022-01-10,100,
Pushups,,Fitness,2022-01-11,100,
Pushups,,Fitness,2022-01-12,100,
Pushups,,Fitness,2022-01-13,100,
run,,Fitness,2022-01-03,1,
run,,Fitness,2022-01-18,1,
run,,Fitness,2022-01-19,1,
1 HabitName HabitDescription HabitCategory CalendarDate Value CommentText
2 Pushups Fitness 2021-09-01 30
3 Pushups Fitness 2022-01-08 100
4 Pushups Fitness 2022-01-09 100
5 Pushups Fitness 2022-01-10 100
6 Pushups Fitness 2022-01-11 100
7 Pushups Fitness 2022-01-12 100
8 Pushups Fitness 2022-01-13 100
9 run Fitness 2022-01-03 1
10 run Fitness 2022-01-18 1
11 run Fitness 2022-01-19 1

@ -23,6 +23,7 @@ import org.isoron.uhabits.core.models.Entry
import org.isoron.uhabits.core.models.Frequency
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.ModelFactory
import org.isoron.uhabits.core.models.Timestamp
import java.io.BufferedReader
@ -50,7 +51,7 @@ class HabitBullCSVImporter
logging: Logging,
) : AbstractImporter() {
val logger = logging.getLogger("HabitBullCSVImporter")
private val logger = logging.getLogger("HabitBullCSVImporter")
override fun canHandle(file: File): Boolean {
val reader = BufferedReader(FileReader(file))
@ -77,10 +78,16 @@ class HabitBullCSVImporter
logger.info("Creating habit: $name")
}
val notes = cols[5] ?: ""
if (parseInt(cols[4]) == 1) {
h.originalEntries.add(Entry(timestamp, Entry.YES_MANUAL, notes))
} else {
h.originalEntries.add(Entry(timestamp, Entry.NO, notes))
when (val value = parseInt(cols[4])) {
0 -> h.originalEntries.add(Entry(timestamp, Entry.NO, notes))
1 -> h.originalEntries.add(Entry(timestamp, Entry.YES_MANUAL, notes))
else -> {
if (value > 1 && h.type != HabitType.NUMERICAL) {
logger.info("Found a value of $value, considering this habit as numerical.")
h.type = HabitType.NUMERICAL
}
h.originalEntries.add(Entry(timestamp, value, notes))
}
}
}
}

@ -26,6 +26,7 @@ import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.Entry
import org.isoron.uhabits.core.models.Frequency
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitType
import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar
import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocalTime
@ -73,6 +74,30 @@ class ImportTest : BaseUnitTest() {
assertTrue(isNotesEqual(habit, 2019, 6, 14, "Habit 3 notes"))
}
@Test
@Throws(IOException::class)
fun testHabitBullCSV3() {
importFromFile("habitbull3.csv")
assertThat(habitList.size(), equalTo(2))
val habit = habitList.getByPosition(0)
assertThat(habit.name, equalTo("Pushups"))
assertThat(habit.type, equalTo(HabitType.NUMERICAL))
assertThat(habit.description, equalTo(""))
assertThat(habit.frequency, equalTo(Frequency.DAILY))
assertThat(getValue(habit, 2021, 9, 1), equalTo(30))
assertThat(getValue(habit, 2022, 1, 8), equalTo(100))
val habit2 = habitList.getByPosition(1)
assertThat(habit2.name, equalTo("run"))
assertThat(habit2.type, equalTo(HabitType.YES_NO))
assertThat(habit2.description, equalTo(""))
assertThat(habit2.frequency, equalTo(Frequency.DAILY))
assertTrue(isChecked(habit2, 2022, 1, 3))
assertTrue(isChecked(habit2, 2022, 1, 18))
assertTrue(isChecked(habit2, 2022, 1, 19))
}
@Test
@Throws(IOException::class)
fun testLoopDB() {
@ -124,10 +149,14 @@ class ImportTest : BaseUnitTest() {
}
private fun isChecked(h: Habit, year: Int, month: Int, day: Int): Boolean {
return getValue(h, year, month, day) == Entry.YES_MANUAL
}
private fun getValue(h: Habit, year: Int, month: Int, day: Int): Int {
val date = getStartOfTodayCalendar()
date.set(year, month - 1, day)
val timestamp = Timestamp(date)
return h.originalEntries.get(timestamp).value == Entry.YES_MANUAL
return h.originalEntries.get(timestamp).value
}
private fun isNotesEqual(h: Habit, year: Int, month: Int, day: Int, notes: String): Boolean {

Loading…
Cancel
Save