mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Update habit.position after reordering list
This commit is contained in:
@@ -25,10 +25,7 @@ import org.isoron.uhabits.core.models.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.isoron.uhabits.core.models.HabitList.Order.BY_COLOR;
|
||||
import static org.isoron.uhabits.core.models.HabitList.Order.BY_NAME;
|
||||
import static org.isoron.uhabits.core.models.HabitList.Order.BY_POSITION;
|
||||
import static org.isoron.uhabits.core.models.HabitList.Order.BY_SCORE;
|
||||
import static org.isoron.uhabits.core.models.HabitList.Order.*;
|
||||
|
||||
/**
|
||||
* In-memory implementation of {@link HabitList}.
|
||||
@@ -182,17 +179,23 @@ public class MemoryHabitList extends HabitList
|
||||
public synchronized void reorder(@NonNull Habit from, @NonNull Habit to)
|
||||
{
|
||||
throwIfHasParent();
|
||||
if (indexOf(from) < 0)
|
||||
throw new IllegalArgumentException(
|
||||
"list does not contain (from) habit");
|
||||
if (order != BY_POSITION) throw new IllegalStateException(
|
||||
"cannot reorder automatically sorted list");
|
||||
|
||||
if (indexOf(from) < 0) throw new IllegalArgumentException(
|
||||
"list does not contain (from) habit");
|
||||
|
||||
int toPos = indexOf(to);
|
||||
if (toPos < 0)
|
||||
throw new IllegalArgumentException(
|
||||
"list does not contain (to) habit");
|
||||
if (toPos < 0) throw new IllegalArgumentException(
|
||||
"list does not contain (to) habit");
|
||||
|
||||
list.remove(from);
|
||||
list.add(toPos, from);
|
||||
|
||||
int position = 0;
|
||||
for(Habit h : list)
|
||||
h.setPosition(position++);
|
||||
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,9 +31,12 @@ public class HabitFixtures
|
||||
|
||||
private final ModelFactory modelFactory;
|
||||
|
||||
public HabitFixtures(ModelFactory modelFactory)
|
||||
private HabitList habitList;
|
||||
|
||||
public HabitFixtures(ModelFactory modelFactory, HabitList habitList)
|
||||
{
|
||||
this.modelFactory = modelFactory;
|
||||
this.habitList = habitList;
|
||||
}
|
||||
|
||||
public Habit createEmptyHabit()
|
||||
@@ -113,6 +116,6 @@ public class HabitFixtures
|
||||
private void saveIfSQLite(Habit habit)
|
||||
{
|
||||
if (!(habit.getRepetitions() instanceof SQLiteRepetitionList)) return;
|
||||
new SQLiteHabitList(modelFactory).add(habit);
|
||||
habitList.add(habit);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user