mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Fix order by position
This commit is contained in:
@@ -343,6 +343,16 @@ public class Habit
|
||||
return new HabitData(data);
|
||||
}
|
||||
|
||||
public Integer getPosition()
|
||||
{
|
||||
return data.position;
|
||||
}
|
||||
|
||||
public void setPosition(int newPosition)
|
||||
{
|
||||
data.position = newPosition;
|
||||
}
|
||||
|
||||
public static class HabitData
|
||||
{
|
||||
@NonNull
|
||||
@@ -370,6 +380,8 @@ public class Habit
|
||||
@Nullable
|
||||
public Reminder reminder;
|
||||
|
||||
public int position;
|
||||
|
||||
public HabitData()
|
||||
{
|
||||
this.color = 8;
|
||||
@@ -381,6 +393,7 @@ public class Habit
|
||||
this.targetType = AT_LEAST;
|
||||
this.targetValue = 100;
|
||||
this.unit = "";
|
||||
this.position = 0;
|
||||
}
|
||||
|
||||
public HabitData(@NonNull HabitData model)
|
||||
@@ -395,6 +408,7 @@ public class Habit
|
||||
this.type = model.type;
|
||||
this.unit = model.unit;
|
||||
this.reminder = model.reminder;
|
||||
this.position = model.position;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -411,6 +425,7 @@ public class Habit
|
||||
.append("type", type)
|
||||
.append("unit", unit)
|
||||
.append("reminder", reminder)
|
||||
.append("position", position)
|
||||
.toString();
|
||||
}
|
||||
|
||||
@@ -434,6 +449,7 @@ public class Habit
|
||||
.append(frequency, habitData.frequency)
|
||||
.append(unit, habitData.unit)
|
||||
.append(reminder, habitData.reminder)
|
||||
.append(position, habitData.position)
|
||||
.isEquals();
|
||||
}
|
||||
|
||||
@@ -451,6 +467,7 @@ public class Habit
|
||||
.append(type)
|
||||
.append(unit)
|
||||
.append(reminder)
|
||||
.append(position)
|
||||
.toHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,10 +139,18 @@ public class MemoryHabitList extends HabitList
|
||||
Double s1 = h1.getScores().getTodayValue();
|
||||
Double s2 = h2.getScores().getTodayValue();
|
||||
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_COLOR) return colorComparator;
|
||||
if (order == BY_SCORE) return scoreComparator;
|
||||
|
||||
@@ -85,11 +85,11 @@ public class SQLiteHabitList extends HabitList
|
||||
public synchronized void add(@NonNull Habit habit)
|
||||
{
|
||||
loadRecords();
|
||||
habit.setPosition(size());
|
||||
list.add(habit);
|
||||
|
||||
HabitRecord record = new HabitRecord();
|
||||
record.copyFrom(habit);
|
||||
record.position = size();
|
||||
repository.save(record);
|
||||
rebuildOrder();
|
||||
|
||||
@@ -173,8 +173,8 @@ public class SQLiteHabitList extends HabitList
|
||||
((SQLiteRepetitionList) habit.getRepetitions()).removeAll();
|
||||
repository.remove(record);
|
||||
});
|
||||
rebuildOrder();
|
||||
|
||||
rebuildOrder();
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ public class HabitRecord
|
||||
this.targetType = model.getTargetType();
|
||||
this.targetValue = model.getTargetValue();
|
||||
this.unit = model.getUnit();
|
||||
this.position = model.getPosition();
|
||||
|
||||
Frequency freq = model.getFrequency();
|
||||
this.freqNum = freq.getNumerator();
|
||||
@@ -120,6 +121,7 @@ public class HabitRecord
|
||||
habit.setTargetType(this.targetType);
|
||||
habit.setTargetValue(this.targetValue);
|
||||
habit.setUnit(this.unit);
|
||||
habit.setPosition(this.position);
|
||||
|
||||
if (reminderHour != null && reminderMin != null)
|
||||
{
|
||||
|
||||
@@ -118,18 +118,22 @@ public class HabitListTest extends BaseUnitTest
|
||||
Habit h1 = fixtures.createEmptyHabit();
|
||||
h1.setName("A Habit");
|
||||
h1.setColor(2);
|
||||
h1.setPosition(1);
|
||||
|
||||
Habit h2 = fixtures.createEmptyHabit();
|
||||
h2.setName("B Habit");
|
||||
h2.setColor(2);
|
||||
h2.setPosition(3);
|
||||
|
||||
Habit h3 = fixtures.createEmptyHabit();
|
||||
h3.setName("C Habit");
|
||||
h3.setColor(0);
|
||||
h3.setPosition(0);
|
||||
|
||||
Habit h4 = fixtures.createEmptyHabit();
|
||||
h4.setName("D Habit");
|
||||
h4.setColor(1);
|
||||
h4.setPosition(2);
|
||||
|
||||
list.add(h3);
|
||||
list.add(h1);
|
||||
@@ -157,6 +161,12 @@ public class HabitListTest extends BaseUnitTest
|
||||
assertThat(list.getByPosition(1), equalTo(h4));
|
||||
assertThat(list.getByPosition(2), equalTo(h1));
|
||||
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
|
||||
|
||||
@@ -30,47 +30,26 @@ import static org.hamcrest.core.IsEqual.*;
|
||||
|
||||
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
|
||||
public void testCopyFrom()
|
||||
{
|
||||
HabitRecord rec = new HabitRecord();
|
||||
rec.copyFrom(habit);
|
||||
Habit original = modelFactory.buildHabit();
|
||||
original.setName("Hello world");
|
||||
original.setDescription("Did you greet the world today?");
|
||||
original.setColor(1);
|
||||
original.setArchived(true);
|
||||
original.setFrequency(Frequency.THREE_TIMES_PER_WEEK);
|
||||
original.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
|
||||
original.setId(1000L);
|
||||
original.setPosition(20);
|
||||
|
||||
assertThat(rec.name, equalTo(habit.getName()));
|
||||
assertThat(rec.description, equalTo(habit.getDescription()));
|
||||
assertThat(rec.color, equalTo(habit.getColor()));
|
||||
assertThat(rec.archived, equalTo(1));
|
||||
assertThat(rec.freqDen, equalTo(7));
|
||||
assertThat(rec.freqNum, equalTo(3));
|
||||
HabitRecord record = new HabitRecord();
|
||||
record.copyFrom(original);
|
||||
|
||||
Reminder reminder = habit.getReminder();
|
||||
assertThat(rec.reminderDays, equalTo(reminder.getDays().toInteger()));
|
||||
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(rec.reminderMin, equalTo(null));
|
||||
assertThat(rec.reminderHour, equalTo(null));
|
||||
assertThat(rec.reminderDays, equalTo(0));
|
||||
assertThat(original.getData(), equalTo(duplicate.getData()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user