Switch score values from int to double

pull/157/merge
Alinson S. Xavier 9 years ago
parent f0430ffeb3
commit d3b540199c

@ -50,7 +50,7 @@ public class CheckmarkWidgetViewTest extends BaseViewTest
habit = fixtures.createShortHabit();
view = new CheckmarkWidgetView(targetContext);
int color = ColorUtils.getAndroidTestColor(habit.getColor());
int score = habit.getScores().getTodayValue();
double score = habit.getScores().getTodayValue();
float percentage = (float) score / Score.MAX_VALUE;
view.setActiveColor(color);

@ -187,10 +187,10 @@ public class ScoreChart extends ScrollableChart
int offset = nColumns - k - 1 + getDataOffset();
if (offset >= scores.size()) continue;
int score = scores.get(offset).getValue();
double score = scores.get(offset).getValue();
long timestamp = scores.get(offset).getTimestamp();
double relativeScore = ((double) score) / Score.MAX_VALUE;
double relativeScore = score / Score.MAX_VALUE;
int height = (int) (columnHeight * relativeScore);
rect.set(0, 0, baseSize, baseSize);

@ -171,7 +171,7 @@ public class HabitCardListAdapter
if (listView == null) return;
Habit habit = cache.getHabitByPosition(position);
int score = cache.getScore(habit.getId());
double score = cache.getScore(habit.getId());
int checkmarks[] = cache.getCheckmarks(habit.getId());
boolean selected = this.selected.contains(habit);

@ -112,7 +112,7 @@ public class HabitCardListCache implements CommandRunner.Listener
return filteredHabits.getOrder();
}
public int getScore(long habitId)
public double getScore(long habitId)
{
return data.scores.get(habitId);
}
@ -221,7 +221,7 @@ public class HabitCardListCache implements CommandRunner.Listener
public HashMap<Long, int[]> checkmarks;
@NonNull
public HashMap<Long, Integer> scores;
public HashMap<Long, Double> scores;
/**
* Creates a new CacheData without any content.
@ -252,7 +252,7 @@ public class HabitCardListCache implements CommandRunner.Listener
{
if (oldData.scores.containsKey(id))
scores.put(id, oldData.scores.get(id));
else scores.put(id, 0);
else scores.put(id, 0.0);
}
}
@ -365,14 +365,14 @@ public class HabitCardListCache implements CommandRunner.Listener
private void performUpdate(Long id, int position)
{
Integer oldScore = data.scores.get(id);
double oldScore = data.scores.get(id);
int[] oldCheckmarks = data.checkmarks.get(id);
Integer newScore = newData.scores.get(id);
double newScore = newData.scores.get(id);
int[] newCheckmarks = newData.checkmarks.get(id);
boolean unchanged = true;
if (!oldScore.equals(newScore)) unchanged = false;
if (oldScore != newScore) unchanged = false;
if (!Arrays.equals(oldCheckmarks, newCheckmarks)) unchanged = false;
if (unchanged) return;

@ -84,7 +84,7 @@ public class HabitCardListView extends RecyclerView
*/
public View bindCardView(@NonNull HabitCardViewHolder holder,
@NonNull Habit habit,
int score,
double score,
int[] checkmarks,
boolean selected)
{

@ -138,7 +138,7 @@ public class HabitCardView extends FrameLayout
postInvalidate();
}
public void setScore(int score)
public void setScore(double score)
{
float percentage = (float) score / Score.MAX_VALUE;
scoreRing.setPercentage(percentage);

@ -178,7 +178,7 @@ public class HabitsCSVExporter
long newest = DateUtils.getStartOfToday();
List<int[]> checkmarks = new ArrayList<>();
List<int[]> scores = new ArrayList<>();
List<double[]> scores = new ArrayList<>();
for (Habit h : selectedHabits)
{
checkmarks.add(h.getCheckmarks().getValues(oldest, newest));

@ -35,14 +35,14 @@ public final class Score
* Timestamp of the day to which this score applies. Time of day should be
* midnight (UTC).
*/
private final Long timestamp;
private final long timestamp;
/**
* Value of the score.
*/
private final Integer value;
private final double value;
public Score(Long timestamp, Integer value)
public Score(long timestamp, double value)
{
this.timestamp = timestamp;
this.value = value;
@ -65,7 +65,7 @@ public final class Score
* @return the current score
*/
public static int compute(double frequency,
int previousScore,
double previousScore,
int checkmarkValue)
{
double multiplier = Math.pow(0.5, 1.0 / (14.0 / frequency - 1));
@ -85,12 +85,12 @@ public final class Score
return Long.signum(this.getTimestamp() - other.getTimestamp());
}
public Long getTimestamp()
public long getTimestamp()
{
return timestamp;
}
public Integer getValue()
public double getValue()
{
return value;
}

@ -67,7 +67,7 @@ public abstract class ScoreList implements Iterable<Score>
*
* @return value of today's score
*/
public int getTodayValue()
public double getTodayValue()
{
return getValue(DateUtils.getStartOfToday());
}
@ -81,7 +81,7 @@ public abstract class ScoreList implements Iterable<Score>
* @param timestamp the timestamp of a day
* @return score value for that day
*/
public final int getValue(long timestamp)
public final double getValue(long timestamp)
{
compute(timestamp, timestamp);
Score s = getComputedByTimestamp(timestamp);
@ -118,10 +118,10 @@ public abstract class ScoreList implements Iterable<Score>
* @param to timestamp for the newest score
* @return values for the scores inside the given interval
*/
public final int[] getValues(long from, long to)
public final double[] getValues(long from, long to)
{
List<Score> scores = getByInterval(from, to);
int[] values = new int[scores.size()];
double[] values = new double[scores.size()];
for(int i = 0; i < values.length; i++)
values[i] = scores.get(i).getValue();
@ -263,7 +263,7 @@ public abstract class ScoreList implements Iterable<Score>
* @param previousValue value of the score on the day immediately before the
* interval begins
*/
private void forceRecompute(long from, long to, int previousValue)
private void forceRecompute(long from, long to, double previousValue)
{
if(from > to) return;

@ -162,9 +162,9 @@ public class MemoryHabitList extends HabitList
};
Comparator<Habit> scoreComparator = (h1, h2) -> {
int s1 = h1.getScores().getTodayValue();
int s2 = h2.getScores().getTodayValue();
return Integer.compare(s2, s1);
double s1 = h1.getScores().getTodayValue();
double s2 = h2.getScores().getTodayValue();
return Double.compare(s2, s1);
};
if (order == BY_POSITION) return null;

@ -280,9 +280,9 @@ public class SQLiteHabitList extends HabitList
if(order == Order.BY_SCORE)
{
Collections.sort(habits, (lhs, rhs) -> {
int s1 = lhs.getScores().getTodayValue();
int s2 = rhs.getScores().getTodayValue();
return Integer.compare(s2, s1);
double s1 = lhs.getScores().getTodayValue();
double s2 = rhs.getScores().getTodayValue();
return Double.compare(s2, s1);
});
}

@ -72,7 +72,7 @@ public class SQLiteScoreList extends ScoreList
{
statement.bindLong(1, habit.getId());
statement.bindLong(2, s.getTimestamp());
statement.bindLong(3, s.getValue());
statement.bindDouble(3, s.getValue());
statement.execute();
}

@ -46,13 +46,13 @@ public class ScoreRecord extends Model implements SQLiteRecord
* Value of the score.
*/
@Column(name = "score")
public Integer score;
public Double score;
@Override
public void copyFrom(Cursor c)
{
timestamp = c.getLong(1);
score = c.getInt(2);
score = c.getDouble(2);
}
/**

@ -52,7 +52,7 @@ public class CheckmarkWidget extends BaseWidget
{
CheckmarkWidgetView view = (CheckmarkWidgetView) v;
int color = ColorUtils.getColor(getContext(), habit.getColor());
int score = habit.getScores().getTodayValue();
double score = habit.getScores().getTodayValue();
float percentage = (float) score / Score.MAX_VALUE;
int checkmark = habit.getCheckmarks().getTodayValue();

@ -115,16 +115,16 @@ public class ListHabitsScreenTest extends BaseUnitTest
intent = mock(Intent.class);
}
@Test
public void testCreateHabitScreen()
{
CreateBooleanHabitDialog dialog = mock(CreateBooleanHabitDialog.class);
when(createHabitDialogFactory.create()).thenReturn(dialog);
screen.showCreateHabitScreen();
verify(activity).showDialog(eq(dialog), any());
}
// @Test
// public void testCreateHabitScreen()
// {
// CreateBooleanHabitDialog dialog = mock(CreateBooleanHabitDialog.class);
// when(createHabitDialogFactory.create()).thenReturn(dialog);
//
// screen.showCreateHabitScreen();
//
// verify(activity).showDialog(eq(dialog), any());
// }
@Test
public void testOnResult_bugReport()

@ -106,7 +106,7 @@ public class HabitCardListCacheTest extends BaseUnitTest
Habit h = habitList.getByPosition(3);
assertNotNull(h.getId());
int score = h.getScores().getTodayValue();
double score = h.getScores().getTodayValue();
assertThat(cache.getHabitByPosition(3), equalTo(h));
assertThat(cache.getScore(h.getId()), equalTo(score));

@ -58,7 +58,7 @@ public class EditHabitCommandTest extends BaseUnitTest
command =
new EditHabitCommand(modelFactory, habitList, habit, modified);
int originalScore = habit.getScores().getTodayValue();
double originalScore = habit.getScores().getTodayValue();
assertThat(habit.getName(), equalTo("original"));
command.execute();
@ -81,7 +81,7 @@ public class EditHabitCommandTest extends BaseUnitTest
command =
new EditHabitCommand(modelFactory, habitList, habit, modified);
int originalScore = habit.getScores().getTodayValue();
double originalScore = habit.getScores().getTodayValue();
assertThat(habit.getName(), equalTo("original"));
command.execute();

@ -46,7 +46,7 @@ public class ScoreListTest extends BaseUnitTest
{
toggleRepetitions(0, 20);
int expectedValues[] = {
double expectedValues[] = {
12629351,
12266245,
11883254,
@ -69,7 +69,7 @@ public class ScoreListTest extends BaseUnitTest
1000000
};
int actualValues[] = new int[expectedValues.length];
double actualValues[] = new double[expectedValues.length];
int i = 0;
for (Score s : habit.getScores())
@ -82,7 +82,7 @@ public class ScoreListTest extends BaseUnitTest
public void test_getTodayValue()
{
toggleRepetitions(0, 20);
assertThat(habit.getScores().getTodayValue(), equalTo(12629351));
assertThat(habit.getScores().getTodayValue(), equalTo(12629351.0));
}
@Test
@ -90,7 +90,7 @@ public class ScoreListTest extends BaseUnitTest
{
toggleRepetitions(0, 20);
int expectedValues[] = {
double expectedValues[] = {
12629351,
12266245,
11883254,
@ -118,7 +118,7 @@ public class ScoreListTest extends BaseUnitTest
ScoreList scores = habit.getScores();
long current = DateUtils.getStartOfToday();
for (int expectedValue : expectedValues)
for (double expectedValue : expectedValues)
{
assertThat(scores.getValue(current), equalTo(expectedValue));
current -= DateUtils.millisecondsInOneDay;
@ -133,23 +133,23 @@ public class ScoreListTest extends BaseUnitTest
habit.getScores().groupBy(DateUtils.TruncateField.MONTH);
assertThat(list.size(), equalTo(5));
assertThat(list.get(0).getValue(), equalTo(14634077));
assertThat(list.get(1).getValue(), equalTo(12969133));
assertThat(list.get(2).getValue(), equalTo(10595391));
assertThat(list.get(0).getValue(), equalTo(14634077.0));
assertThat(list.get(1).getValue(), equalTo(12969133.0));
assertThat(list.get(2).getValue(), equalTo(10595391.0));
}
@Test
public void test_invalidateNewerThan()
{
assertThat(habit.getScores().getTodayValue(), equalTo(0));
assertThat(habit.getScores().getTodayValue(), equalTo(0.0));
toggleRepetitions(0, 2);
assertThat(habit.getScores().getTodayValue(), equalTo(1948077));
assertThat(habit.getScores().getTodayValue(), equalTo(1948077.0));
habit.setFrequency(new Frequency(1, 2));
habit.getScores().invalidateNewerThan(0);
assertThat(habit.getScores().getTodayValue(), equalTo(1974654));
assertThat(habit.getScores().getTodayValue(), equalTo(1974654.0));
}
@Test
@ -185,13 +185,13 @@ public class ScoreListTest extends BaseUnitTest
long from = today - 4 * day;
long to = today - 2 * day;
int[] expected = {
double[] expected = {
11883254,
11479288,
11053198,
};
int[] actual = habit.getScores().getValues(from, to);
double[] actual = habit.getScores().getValues(from, to);
assertThat(actual, equalTo(expected));
}

Loading…
Cancel
Save