EntryList: remove getValues

pull/707/head
Alinson S. Xavier 5 years ago
parent a74a4b390b
commit 51b9517897

@ -22,6 +22,7 @@ package org.isoron.uhabits.activities.common.views;
import androidx.test.ext.junit.runners.*;
import androidx.test.filters.*;
import org.apache.commons.lang3.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.ui.callbacks.*;
@ -56,9 +57,16 @@ public class HistoryChartTest extends BaseViewTest
habit = fixtures.createLongHabit();
today = new Timestamp(DateUtils.getStartOfToday());
Integer[] entries = habit
.getComputedEntries()
.getByInterval(today.minus(300), today)
.stream()
.map(Entry::getValue)
.toArray(Integer[]::new);
chart = new HistoryChart(targetContext);
chart.setSkipEnabled(true);
chart.setEntries(habit.getComputedEntries().getAllValues());
chart.setEntries(ArrayUtils.toPrimitive(entries));
chart.setColor(PaletteUtilsKt.toFixedAndroidColor(habit.getColor()));
measureView(chart, dpToPixels(400), dpToPixels(200));

@ -25,6 +25,7 @@ import org.isoron.uhabits.BaseViewTest
import org.isoron.uhabits.R
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.utils.DateUtils
import org.junit.Test
import org.junit.runner.RunWith
@ -37,6 +38,7 @@ class HabitCardViewTest : BaseViewTest() {
private lateinit var view: HabitCardView
private lateinit var habit1: Habit
private lateinit var habit2: Habit
private lateinit var today: Timestamp
override fun setUp() {
super.setUp()
@ -44,11 +46,16 @@ class HabitCardViewTest : BaseViewTest() {
habit1 = fixtures.createLongHabit()
habit2 = fixtures.createLongNumericalHabit()
val today = DateUtils.getTodayWithOffset()
today = DateUtils.getTodayWithOffset()
val entries = habit1
.computedEntries
.getByInterval(today.minus(300), today)
.map { it.value }.toIntArray()
view = component.getHabitCardViewFactory().create().apply {
habit = habit1
values = habit1.computedEntries.getAllValues()
values = entries
score = habit1.scores.get(today).value
isSelected = false
buttonCount = 5
@ -73,9 +80,14 @@ class HabitCardViewTest : BaseViewTest() {
@Test
fun testRender_numerical() {
val entries = habit2
.computedEntries
.getByInterval(today.minus(300), today)
.map { it.value }.toIntArray()
view.apply {
habit = habit2
values = habit2.computedEntries.getAllValues()
values = entries
}
assertRenders(view, "$PATH/render_numerical.png")
}

@ -31,6 +31,7 @@ import android.util.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.common.views.*;
import org.isoron.uhabits.activities.habits.show.views.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.preferences.*;
@ -178,7 +179,12 @@ public class HistoryEditorDialog extends AppCompatDialogFragment
@Override
public void doInBackground()
{
checkmarks = habit.getComputedEntries().getAllValues();
HistoryCardViewModel model = new HistoryCardPresenter(
habit,
prefs.getFirstWeekday(),
prefs.isSkipEnabled()
).present();
checkmarks = model.getEntries();
}
@Override

@ -24,6 +24,7 @@ import android.view.LayoutInflater
import android.widget.LinearLayout
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.utils.DateUtils
import org.isoron.uhabits.databinding.ShowHabitHistoryBinding
import org.isoron.uhabits.utils.toThemedAndroidColor
@ -63,11 +64,17 @@ class HistoryCardPresenter(
val firstWeekday: Int,
val isSkipEnabled: Boolean,
) {
fun present() = HistoryCardViewModel(
entries = habit.computedEntries.getAllValues(),
color = habit.color,
firstWeekday = firstWeekday,
isNumerical = habit.isNumerical,
isSkipEnabled = isSkipEnabled,
)
fun present(): HistoryCardViewModel {
val today = DateUtils.getTodayWithOffset()
val oldest = habit.computedEntries.getKnown().lastOrNull()?.timestamp ?: today
val entries = habit.computedEntries.getByInterval(oldest, today).map { it.value }.toIntArray()
return HistoryCardViewModel(
entries = entries,
color = habit.color,
firstWeekday = firstWeekday,
isNumerical = habit.isNumerical,
isSkipEnabled = isSkipEnabled,
)
}
}

@ -23,6 +23,7 @@ import android.app.PendingIntent
import android.content.Context
import android.view.View
import org.isoron.uhabits.activities.common.views.HistoryChart
import org.isoron.uhabits.activities.habits.show.views.HistoryCardPresenter
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.utils.toThemedAndroidColor
import org.isoron.uhabits.widgets.views.GraphWidgetView
@ -42,12 +43,17 @@ class HistoryWidget(
val widgetView = view as GraphWidgetView
widgetView.setBackgroundAlpha(preferedBackgroundAlpha)
if (preferedBackgroundAlpha >= 255) widgetView.setShadowAlpha(0x4f)
val model = HistoryCardPresenter(
habit = habit,
isSkipEnabled = prefs.isSkipEnabled,
firstWeekday = prefs.firstWeekday,
).present()
(widgetView.dataView as HistoryChart).apply {
setFirstWeekday(firstWeekday)
setSkipEnabled(prefs.isSkipEnabled)
setColor(habit.color.toThemedAndroidColor(context))
setEntries(habit.computedEntries.getAllValues())
setNumerical(habit.isNumerical)
setFirstWeekday(model.firstWeekday)
setSkipEnabled(model.isSkipEnabled)
setColor(model.color.toThemedAndroidColor(context))
setEntries(model.entries)
setNumerical(model.isNumerical)
}
}

@ -18,6 +18,7 @@
*/
package org.isoron.uhabits.core.io
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
@ -148,10 +149,10 @@ class HabitsCSVExporter(
val timeframe = getTimeframe()
val oldest = timeframe[0]
val newest = DateUtils.getToday()
val checkmarks: MutableList<IntArray> = ArrayList()
val checkmarks: MutableList<ArrayList<Entry>> = ArrayList()
val scores: MutableList<ArrayList<Score>> = ArrayList()
for (habit in selectedHabits) {
checkmarks.add(habit.computedEntries.getValues(oldest, newest))
checkmarks.add(ArrayList(habit.computedEntries.getByInterval(oldest, newest)))
scores.add(ArrayList(habit.scores.getByInterval(oldest, newest)))
}

@ -189,39 +189,6 @@ open class EntryList {
return map
}
/**
* Returns the values of the entries that fall inside a certain interval of time. The values
* are returned in an array containing one integer value for each day of the interval. The
* first entry corresponds to the most recent day in the interval. Each subsequent entry
* corresponds to one day older than the previous entry. The boundaries of the time interval
* are included.
*/
@Deprecated("")
@Synchronized
fun getValues(from: Timestamp, to: Timestamp): IntArray {
if (from.isNewerThan(to)) throw IllegalArgumentException()
val nDays = from.daysUntil(to) + 1
val result = IntArray(nDays) { UNKNOWN }
getKnown().filter { entry ->
!entry.timestamp.isNewerThan(to) && !entry.timestamp.isOlderThan(from)
}.forEach { entry ->
val offset = entry.timestamp.daysUntil(to)
result[offset] = entry.value
}
return result
}
@Deprecated("")
@Synchronized
fun getAllValues(): IntArray {
val entries = getKnown()
if (entries.isEmpty()) return IntArray(0)
var (fromTimestamp, _) = entries.last()
val toTimestamp = DateUtils.getTodayWithOffset()
if (fromTimestamp.isNewerThan(toTimestamp)) fromTimestamp = toTimestamp
return getValues(fromTimestamp, toTimestamp)
}
@Deprecated("")
@Synchronized
open fun getThisWeekValue(firstWeekday: Int, isNumerical: Boolean): Int {

@ -21,6 +21,7 @@ package org.isoron.uhabits.core.ui.screens.habits.list;
import androidx.annotation.*;
import org.apache.commons.lang3.*;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*;
@ -363,9 +364,12 @@ public class HabitCardListCache implements CommandRunner.Listener
if (targetId != null && !targetId.equals(id)) continue;
newData.scores.put(id, habit.getScores().get(today).getValue());
newData.checkmarks.put(
id,
habit.getComputedEntries().getValues(dateFrom, today));
Integer[] entries = habit.getComputedEntries()
.getByInterval(dateFrom, today)
.stream()
.map(Entry::getValue)
.toArray(Integer[]::new);
newData.checkmarks.put(id, ArrayUtils.toPrimitive(entries));
runner.publishProgress(this, position);
}

@ -81,13 +81,13 @@ public class WidgetBehavior
}
public void onIncrement(@NotNull Habit habit, @NotNull Timestamp timestamp, int amount) {
int currentValue = habit.getComputedEntries().getValues(timestamp, timestamp)[0];
int currentValue = habit.getComputedEntries().get(timestamp).getValue();
setValue(habit, timestamp, currentValue + amount);
notificationTray.cancel(habit);
}
public void onDecrement(@NotNull Habit habit, @NotNull Timestamp timestamp, int amount) {
int currentValue = habit.getComputedEntries().getValues(timestamp, timestamp)[0];
int currentValue = habit.getComputedEntries().get(timestamp).getValue();
setValue(habit, timestamp, currentValue - amount);
notificationTray.cancel(habit);
}

@ -65,27 +65,6 @@ class EntryListTest {
assertEquals(Entry(today.minus(5), 20), actual[5])
}
@Test
fun testGetValues() {
val entries = EntryList()
val today = DateUtils.getToday()
entries.add(Entry(today.minus(3), YES_MANUAL))
entries.add(Entry(today.minus(5), YES_MANUAL))
entries.add(Entry(today.minus(6), YES_MANUAL))
val expected = intArrayOf(
UNKNOWN, // 1
UNKNOWN, // 2
YES_MANUAL, // 3
UNKNOWN, // 4
YES_MANUAL, // 5
YES_MANUAL, // 6
UNKNOWN, // 7
)
assertThat(entries.getValues(today.minus(7), today.minus(1)), equalTo(expected))
}
@Test
fun testComputeBoolean() {
val today = DateUtils.getToday()

@ -19,6 +19,7 @@
package org.isoron.uhabits.core.ui.screens.habits.list;
import org.apache.commons.lang3.*;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*;
@ -104,7 +105,11 @@ public class HabitCardListCacheTest extends BaseUnitTest
assertThat(cache.getScore(h.getId()), equalTo(score));
int[] actualCheckmarks = cache.getCheckmarks(h.getId());
int[] expectedCheckmarks = h.getComputedEntries().getValues(today.minus(9), today);
int[] expectedCheckmarks = ArrayUtils.toPrimitive(h.getComputedEntries()
.getByInterval(today.minus(9), today)
.stream()
.map(Entry::getValue)
.toArray(Integer[]::new));
assertThat(actualCheckmarks, equalTo(expectedCheckmarks));
}

Loading…
Cancel
Save