Fix order by position

pull/312/head
Alinson S. Xavier 8 years ago
parent ea640a8a17
commit 7872983064

@ -343,6 +343,16 @@ public class Habit
return new HabitData(data); return new HabitData(data);
} }
public Integer getPosition()
{
return data.position;
}
public void setPosition(int newPosition)
{
data.position = newPosition;
}
public static class HabitData public static class HabitData
{ {
@NonNull @NonNull
@ -370,6 +380,8 @@ public class Habit
@Nullable @Nullable
public Reminder reminder; public Reminder reminder;
public int position;
public HabitData() public HabitData()
{ {
this.color = 8; this.color = 8;
@ -381,6 +393,7 @@ public class Habit
this.targetType = AT_LEAST; this.targetType = AT_LEAST;
this.targetValue = 100; this.targetValue = 100;
this.unit = ""; this.unit = "";
this.position = 0;
} }
public HabitData(@NonNull HabitData model) public HabitData(@NonNull HabitData model)
@ -395,6 +408,7 @@ public class Habit
this.type = model.type; this.type = model.type;
this.unit = model.unit; this.unit = model.unit;
this.reminder = model.reminder; this.reminder = model.reminder;
this.position = model.position;
} }
@Override @Override
@ -411,6 +425,7 @@ public class Habit
.append("type", type) .append("type", type)
.append("unit", unit) .append("unit", unit)
.append("reminder", reminder) .append("reminder", reminder)
.append("position", position)
.toString(); .toString();
} }
@ -434,6 +449,7 @@ public class Habit
.append(frequency, habitData.frequency) .append(frequency, habitData.frequency)
.append(unit, habitData.unit) .append(unit, habitData.unit)
.append(reminder, habitData.reminder) .append(reminder, habitData.reminder)
.append(position, habitData.position)
.isEquals(); .isEquals();
} }
@ -451,6 +467,7 @@ public class Habit
.append(type) .append(type)
.append(unit) .append(unit)
.append(reminder) .append(reminder)
.append(position)
.toHashCode(); .toHashCode();
} }
} }

@ -139,10 +139,18 @@ public class MemoryHabitList extends HabitList
Double s1 = h1.getScores().getTodayValue(); Double s1 = h1.getScores().getTodayValue();
Double s2 = h2.getScores().getTodayValue(); Double s2 = h2.getScores().getTodayValue();
if (s1.equals(s2)) return nameComparator.compare(h1, h2); if (s1.equals(s2)) return nameComparator.compare(h1, h2);
return s2.compareTo(s1); else return s2.compareTo(s1);
}; };
if (order == BY_POSITION) return null; Comparator<Habit> positionComparator = (h1, h2) ->
{
Integer p1 = h1.getPosition();
Integer p2 = h2.getPosition();
if (p1.equals(p2)) return nameComparator.compare(h1, h2);
else return p1.compareTo(p2);
};
if (order == BY_POSITION) return positionComparator;
if (order == BY_NAME) return nameComparator; if (order == BY_NAME) return nameComparator;
if (order == BY_COLOR) return colorComparator; if (order == BY_COLOR) return colorComparator;
if (order == BY_SCORE) return scoreComparator; if (order == BY_SCORE) return scoreComparator;

@ -85,11 +85,11 @@ public class SQLiteHabitList extends HabitList
public synchronized void add(@NonNull Habit habit) public synchronized void add(@NonNull Habit habit)
{ {
loadRecords(); loadRecords();
habit.setPosition(size());
list.add(habit); list.add(habit);
HabitRecord record = new HabitRecord(); HabitRecord record = new HabitRecord();
record.copyFrom(habit); record.copyFrom(habit);
record.position = size();
repository.save(record); repository.save(record);
rebuildOrder(); rebuildOrder();
@ -173,8 +173,8 @@ public class SQLiteHabitList extends HabitList
((SQLiteRepetitionList) habit.getRepetitions()).removeAll(); ((SQLiteRepetitionList) habit.getRepetitions()).removeAll();
repository.remove(record); repository.remove(record);
}); });
rebuildOrder();
rebuildOrder();
getObservable().notifyListeners(); getObservable().notifyListeners();
} }

@ -91,6 +91,7 @@ public class HabitRecord
this.targetType = model.getTargetType(); this.targetType = model.getTargetType();
this.targetValue = model.getTargetValue(); this.targetValue = model.getTargetValue();
this.unit = model.getUnit(); this.unit = model.getUnit();
this.position = model.getPosition();
Frequency freq = model.getFrequency(); Frequency freq = model.getFrequency();
this.freqNum = freq.getNumerator(); this.freqNum = freq.getNumerator();
@ -120,6 +121,7 @@ public class HabitRecord
habit.setTargetType(this.targetType); habit.setTargetType(this.targetType);
habit.setTargetValue(this.targetValue); habit.setTargetValue(this.targetValue);
habit.setUnit(this.unit); habit.setUnit(this.unit);
habit.setPosition(this.position);
if (reminderHour != null && reminderMin != null) if (reminderHour != null && reminderMin != null)
{ {

@ -118,18 +118,22 @@ public class HabitListTest extends BaseUnitTest
Habit h1 = fixtures.createEmptyHabit(); Habit h1 = fixtures.createEmptyHabit();
h1.setName("A Habit"); h1.setName("A Habit");
h1.setColor(2); h1.setColor(2);
h1.setPosition(1);
Habit h2 = fixtures.createEmptyHabit(); Habit h2 = fixtures.createEmptyHabit();
h2.setName("B Habit"); h2.setName("B Habit");
h2.setColor(2); h2.setColor(2);
h2.setPosition(3);
Habit h3 = fixtures.createEmptyHabit(); Habit h3 = fixtures.createEmptyHabit();
h3.setName("C Habit"); h3.setName("C Habit");
h3.setColor(0); h3.setColor(0);
h3.setPosition(0);
Habit h4 = fixtures.createEmptyHabit(); Habit h4 = fixtures.createEmptyHabit();
h4.setName("D Habit"); h4.setName("D Habit");
h4.setColor(1); h4.setColor(1);
h4.setPosition(2);
list.add(h3); list.add(h3);
list.add(h1); list.add(h1);
@ -157,6 +161,12 @@ public class HabitListTest extends BaseUnitTest
assertThat(list.getByPosition(1), equalTo(h4)); assertThat(list.getByPosition(1), equalTo(h4));
assertThat(list.getByPosition(2), equalTo(h1)); assertThat(list.getByPosition(2), equalTo(h1));
assertThat(list.getByPosition(3), equalTo(h2)); assertThat(list.getByPosition(3), equalTo(h2));
list.setOrder(BY_POSITION);
assertThat(list.getByPosition(0), equalTo(h3));
assertThat(list.getByPosition(1), equalTo(h1));
assertThat(list.getByPosition(2), equalTo(h4));
assertThat(list.getByPosition(3), equalTo(h2));
} }
@Test @Test

@ -30,47 +30,26 @@ import static org.hamcrest.core.IsEqual.*;
public class HabitRecordTest extends BaseUnitTest public class HabitRecordTest extends BaseUnitTest
{ {
private Habit habit;
@Before
@Override
public void setUp() throws Exception
{
super.setUp();
habit = modelFactory.buildHabit();
habit.setName("Hello world");
habit.setDescription("Did you greet the world today?");
habit.setColor(1);
habit.setArchived(true);
habit.setFrequency(Frequency.THREE_TIMES_PER_WEEK);
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
habit.setId(1000L);
}
@Test @Test
public void testCopyFrom() public void testCopyFrom()
{ {
HabitRecord rec = new HabitRecord(); Habit original = modelFactory.buildHabit();
rec.copyFrom(habit); original.setName("Hello world");
original.setDescription("Did you greet the world today?");
assertThat(rec.name, equalTo(habit.getName())); original.setColor(1);
assertThat(rec.description, equalTo(habit.getDescription())); original.setArchived(true);
assertThat(rec.color, equalTo(habit.getColor())); original.setFrequency(Frequency.THREE_TIMES_PER_WEEK);
assertThat(rec.archived, equalTo(1)); original.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
assertThat(rec.freqDen, equalTo(7)); original.setId(1000L);
assertThat(rec.freqNum, equalTo(3)); original.setPosition(20);
Reminder reminder = habit.getReminder(); HabitRecord record = new HabitRecord();
assertThat(rec.reminderDays, equalTo(reminder.getDays().toInteger())); record.copyFrom(original);
assertThat(rec.reminderHour, equalTo(reminder.getHour()));
assertThat(rec.reminderMin, equalTo(reminder.getMinute())); Habit duplicate = modelFactory.buildHabit();
record.copyTo(duplicate);
habit.setReminder(null);
rec.copyFrom(habit); assertThat(original.getData(), equalTo(duplicate.getData()));
assertThat(rec.reminderMin, equalTo(null));
assertThat(rec.reminderHour, equalTo(null));
assertThat(rec.reminderDays, equalTo(0));
} }
} }

Loading…
Cancel
Save