mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Speed up CreateHabitCommand and CreateRepetitionCommand
This commit is contained in:
@@ -60,6 +60,7 @@ public class CreateRepetitionCommand extends Command
|
||||
RepetitionList reps = habit.getRepetitions();
|
||||
previousValue = reps.getValue(timestamp);
|
||||
reps.setValue(timestamp, value);
|
||||
habitList.resort();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
||||
@@ -260,7 +260,7 @@ public abstract class HabitList implements Iterable<Habit>
|
||||
csv.close();
|
||||
}
|
||||
|
||||
|
||||
public abstract void resort();
|
||||
|
||||
public enum Order
|
||||
{
|
||||
|
||||
@@ -79,8 +79,6 @@ public class MemoryHabitList extends HabitList
|
||||
if (id == null) habit.setId((long) list.size());
|
||||
list.addLast(habit);
|
||||
resort();
|
||||
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -133,7 +131,6 @@ public class MemoryHabitList extends HabitList
|
||||
this.primaryOrder = order;
|
||||
this.comparator = getComposedComparatorByOrder(this.primaryOrder, this.secondaryOrder);
|
||||
resort();
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -142,7 +139,6 @@ public class MemoryHabitList extends HabitList
|
||||
this.secondaryOrder = order;
|
||||
this.comparator = getComposedComparatorByOrder(this.primaryOrder, this.secondaryOrder);
|
||||
resort();
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
private Comparator<Habit> getComposedComparatorByOrder(Order firstOrder, Order secondOrder)
|
||||
@@ -265,7 +261,6 @@ public class MemoryHabitList extends HabitList
|
||||
public synchronized void update(List<Habit> habits)
|
||||
{
|
||||
resort();
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
private void throwIfHasParent()
|
||||
@@ -284,8 +279,9 @@ public class MemoryHabitList extends HabitList
|
||||
resort();
|
||||
}
|
||||
|
||||
private synchronized void resort()
|
||||
public synchronized void resort()
|
||||
{
|
||||
if (comparator != null) Collections.sort(list, comparator);
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,12 +66,20 @@ public class SQLiteHabitList extends HabitList
|
||||
|
||||
list.removeAll();
|
||||
List<HabitRecord> records = repository.findAll("order by position");
|
||||
|
||||
int expectedPosition = 0;
|
||||
boolean shouldRebuildOrder = false;
|
||||
for (HabitRecord rec : records)
|
||||
{
|
||||
if (rec.position != expectedPosition) shouldRebuildOrder = true;
|
||||
expectedPosition++;
|
||||
|
||||
Habit h = modelFactory.buildHabit();
|
||||
rec.copyTo(h);
|
||||
list.add(h);
|
||||
}
|
||||
|
||||
if(shouldRebuildOrder) rebuildOrder();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,7 +92,6 @@ public class SQLiteHabitList extends HabitList
|
||||
record.copyFrom(habit);
|
||||
repository.save(record);
|
||||
habit.id = record.id;
|
||||
rebuildOrder();
|
||||
|
||||
list.add(habit);
|
||||
getObservable().notifyListeners();
|
||||
@@ -166,11 +173,17 @@ public class SQLiteHabitList extends HabitList
|
||||
private synchronized void rebuildOrder()
|
||||
{
|
||||
List<HabitRecord> records = repository.findAll("order by position");
|
||||
repository.executeAsTransaction(() -> {
|
||||
repository.executeAsTransaction(() ->
|
||||
{
|
||||
int pos = 0;
|
||||
for (HabitRecord r : records) {
|
||||
r.position = pos++;
|
||||
repository.save(r);
|
||||
for (HabitRecord r : records)
|
||||
{
|
||||
if (r.position != pos)
|
||||
{
|
||||
r.position = pos;
|
||||
repository.save(r);
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -179,6 +192,9 @@ public class SQLiteHabitList extends HabitList
|
||||
public synchronized void remove(@NonNull Habit habit)
|
||||
{
|
||||
loadRecords();
|
||||
|
||||
reorder(habit, list.getByPosition(size() - 1));
|
||||
|
||||
list.remove(habit);
|
||||
|
||||
HabitRecord record = repository.find(habit.getId());
|
||||
@@ -189,7 +205,6 @@ public class SQLiteHabitList extends HabitList
|
||||
repository.remove(record);
|
||||
});
|
||||
|
||||
rebuildOrder();
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
@@ -267,6 +282,13 @@ public class SQLiteHabitList extends HabitList
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resort()
|
||||
{
|
||||
list.resort();
|
||||
getObservable().notifyListeners();
|
||||
}
|
||||
|
||||
public synchronized void reload()
|
||||
{
|
||||
loaded = false;
|
||||
|
||||
Reference in New Issue
Block a user