mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
LoopDBImporter: Use commands instead of directly modifying DB
This commit is contained in:
@@ -21,6 +21,7 @@ package org.isoron.uhabits.core.io;
|
|||||||
|
|
||||||
import androidx.annotation.*;
|
import androidx.annotation.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.core.commands.*;
|
||||||
import org.isoron.uhabits.core.database.*;
|
import org.isoron.uhabits.core.database.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
import org.isoron.uhabits.core.models.sqlite.records.*;
|
||||||
@@ -42,15 +43,19 @@ public class LoopDBImporter extends AbstractImporter
|
|||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final DatabaseOpener opener;
|
private final DatabaseOpener opener;
|
||||||
|
@NonNull
|
||||||
|
private final CommandRunner runner;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public LoopDBImporter(@NonNull HabitList habitList,
|
public LoopDBImporter(@NonNull HabitList habitList,
|
||||||
@NonNull ModelFactory modelFactory,
|
@NonNull ModelFactory modelFactory,
|
||||||
@NonNull DatabaseOpener opener)
|
@NonNull DatabaseOpener opener,
|
||||||
|
@NonNull CommandRunner runner)
|
||||||
{
|
{
|
||||||
super(habitList);
|
super(habitList);
|
||||||
this.modelFactory = modelFactory;
|
this.modelFactory = modelFactory;
|
||||||
this.opener = opener;
|
this.opener = opener;
|
||||||
|
this.runner = runner;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -99,17 +104,32 @@ public class LoopDBImporter extends AbstractImporter
|
|||||||
for (HabitRecord habitRecord : habitsRepository.findAll(
|
for (HabitRecord habitRecord : habitsRepository.findAll(
|
||||||
"order by position"))
|
"order by position"))
|
||||||
{
|
{
|
||||||
Habit h = modelFactory.buildHabit();
|
Habit habit = habitList.getById(habitRecord.id);
|
||||||
habitRecord.copyTo(h);
|
if (habit == null)
|
||||||
h.setId(null);
|
{
|
||||||
habitList.add(h);
|
habit = modelFactory.buildHabit();
|
||||||
|
habitRecord.copyTo(habit);
|
||||||
|
runner.execute(new CreateHabitCommand(modelFactory, habitList, habit), null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Habit modified = modelFactory.buildHabit();
|
||||||
|
habitRecord.copyTo(modified);
|
||||||
|
if (!modified.getData().equals(habit.getData()))
|
||||||
|
runner.execute(new EditHabitCommand(modelFactory, habitList, habit, modified), null);
|
||||||
|
}
|
||||||
|
|
||||||
List<RepetitionRecord> reps =
|
List<RepetitionRecord> reps =
|
||||||
repsRepository.findAll("where habit = ?",
|
repsRepository.findAll("where habit = ?",
|
||||||
habitRecord.id.toString());
|
habitRecord.id.toString());
|
||||||
|
|
||||||
for (RepetitionRecord r : reps)
|
for (RepetitionRecord r : reps)
|
||||||
h.getRepetitions().toggle(new Timestamp(r.timestamp), r.value);
|
{
|
||||||
|
Timestamp t = new Timestamp(r.timestamp);
|
||||||
|
Repetition rep = habit.getRepetitions().getByTimestamp(t);
|
||||||
|
if(rep == null || rep.getValue() != r.value)
|
||||||
|
runner.execute(new CreateRepetitionCommand(habitList, habit, t, r.value), habit.id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user