Import Loop DB without app restart

pull/312/head
Alinson S. Xavier 8 years ago
parent 33f7acc9ca
commit 8ccada67d6

@ -25,7 +25,6 @@ import android.support.annotation.*;
import org.isoron.androidbase.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.utils.*;
import java.io.*;
@ -48,10 +47,4 @@ public class AndroidDatabaseOpener implements DatabaseOpener
SQLiteDatabase.openDatabase(file.getAbsolutePath(), null,
SQLiteDatabase.OPEN_READWRITE));
}
@Override
public File getProductionDatabaseFile()
{
return DatabaseUtils.getDatabaseFile(context);
}
}

@ -26,6 +26,4 @@ import java.io.*;
public interface DatabaseOpener
{
Database open(@NonNull File file);
File getProductionDatabaseFile();
}

@ -32,11 +32,11 @@ import java.util.*;
*/
public abstract class AbstractImporter
{
protected final HabitList habits;
protected final HabitList habitList;
public AbstractImporter(HabitList habits)
public AbstractImporter(HabitList habitList)
{
this.habits = habits;
this.habitList = habitList;
}
public abstract boolean canHandle(@NonNull File file) throws IOException;

@ -89,7 +89,7 @@ public class HabitBullCSVImporter extends AbstractImporter
h.setName(name);
h.setDescription(description);
h.setFrequency(Frequency.DAILY);
habits.add(h);
habitList.add(h);
map.put(name, h);
}

@ -21,11 +21,12 @@ package org.isoron.uhabits.core.io;
import android.support.annotation.*;
import org.apache.commons.io.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.sqlite.records.*;
import java.io.*;
import java.util.*;
import javax.inject.*;
@ -36,14 +37,19 @@ import static org.isoron.uhabits.core.Config.*;
*/
public class LoopDBImporter extends AbstractImporter
{
@NonNull
private final ModelFactory modelFactory;
@NonNull
private final DatabaseOpener opener;
@Inject
public LoopDBImporter(@NonNull HabitList habits,
public LoopDBImporter(@NonNull HabitList habitList,
@NonNull ModelFactory modelFactory,
@NonNull DatabaseOpener opener)
{
super(habits);
super(habitList);
this.modelFactory = modelFactory;
this.opener = opener;
}
@ -78,11 +84,32 @@ public class LoopDBImporter extends AbstractImporter
}
@Override
public void importHabitsFromFile(@NonNull File file) throws IOException
public synchronized void importHabitsFromFile(@NonNull File file)
throws IOException
{
// DatabaseUtils.dispose();
File originalDB = opener.getProductionDatabaseFile();
FileUtils.copyFile(file, originalDB);
// DatabaseUtils.initializeDatabase(context);
Database db = opener.open(file);
MigrationHelper helper = new MigrationHelper(db);
helper.migrateTo(DATABASE_VERSION);
Repository<HabitRecord> habitsRepository;
Repository<RepetitionRecord> repsRepository;
habitsRepository = new Repository<>(HabitRecord.class, db);
repsRepository = new Repository<>(RepetitionRecord.class, db);
for (HabitRecord habitRecord : habitsRepository.findAll(
"order by position"))
{
Habit h = modelFactory.buildHabit();
habitRecord.copyTo(h);
h.setId(null);
habitList.add(h);
List<RepetitionRecord> reps =
repsRepository.findAll("where habit = ?",
habitRecord.id.toString());
for (RepetitionRecord r : reps)
h.getRepetitions().toggle(r.timestamp, r.value);
}
}
}

@ -128,7 +128,7 @@ public class RewireDBImporter extends AbstractImporter
}
habit.setFrequency(new Frequency(numerator, denominator));
habits.add(habit);
habitList.add(habit);
createReminder(db, habit, id);
createCheckmarks(db, habit, id);
@ -204,7 +204,7 @@ public class RewireDBImporter extends AbstractImporter
Reminder reminder = new Reminder(hour, minute, days);
habit.setReminder(reminder);
habits.update(habit);
habitList.update(habit);
}
finally
{

@ -129,7 +129,7 @@ public class TickmateDBImporter extends AbstractImporter
habit.setName(name);
habit.setDescription(description);
habit.setFrequency(Frequency.DAILY);
habits.add(habit);
habitList.add(habit);
createCheckmarks(db, habit, id);

@ -213,9 +213,6 @@ public abstract class RepetitionList
public void toggle(long timestamp, int value)
{
if(!habit.isNumerical())
throw new IllegalStateException("habit must be numerical");
Repetition rep = getByTimestamp(timestamp);
if(rep != null) remove(rep);
add(new Repetition(timestamp, value));

@ -37,8 +37,6 @@ import java.io.*;
import java.sql.*;
import java.util.*;
import sun.reflect.generics.reflectiveObjects.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
@ -73,12 +71,6 @@ public class BaseUnitTest
throw new RuntimeException(e);
}
}
@Override
public File getProductionDatabaseFile()
{
throw new NotImplementedException();
}
};
@Before

@ -60,7 +60,6 @@ public class ImportTest extends BaseUnitTest
}
@Test
@Ignore
public void testLoopDB() throws IOException
{
importFromFile("loop.db");
@ -133,7 +132,7 @@ public class ImportTest extends BaseUnitTest
assertTrue(file.canRead());
GenericImporter importer = new GenericImporter(habitList,
new LoopDBImporter(habitList, databaseOpener),
new LoopDBImporter(habitList, modelFactory, databaseOpener),
new RewireDBImporter(habitList, modelFactory, databaseOpener),
new TickmateDBImporter(habitList, modelFactory, databaseOpener),
new HabitBullCSVImporter(habitList, modelFactory));

Loading…
Cancel
Save