mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Import Loop DB without app restart
This commit is contained in:
@@ -25,7 +25,6 @@ import android.support.annotation.*;
|
|||||||
|
|
||||||
import org.isoron.androidbase.*;
|
import org.isoron.androidbase.*;
|
||||||
import org.isoron.uhabits.core.database.*;
|
import org.isoron.uhabits.core.database.*;
|
||||||
import org.isoron.uhabits.utils.*;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
@@ -48,10 +47,4 @@ public class AndroidDatabaseOpener implements DatabaseOpener
|
|||||||
SQLiteDatabase.openDatabase(file.getAbsolutePath(), null,
|
SQLiteDatabase.openDatabase(file.getAbsolutePath(), null,
|
||||||
SQLiteDatabase.OPEN_READWRITE));
|
SQLiteDatabase.OPEN_READWRITE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public File getProductionDatabaseFile()
|
|
||||||
{
|
|
||||||
return DatabaseUtils.getDatabaseFile(context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,4 @@ import java.io.*;
|
|||||||
public interface DatabaseOpener
|
public interface DatabaseOpener
|
||||||
{
|
{
|
||||||
Database open(@NonNull File file);
|
Database open(@NonNull File file);
|
||||||
|
|
||||||
File getProductionDatabaseFile();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,11 +32,11 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public abstract class AbstractImporter
|
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;
|
public abstract boolean canHandle(@NonNull File file) throws IOException;
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ public class HabitBullCSVImporter extends AbstractImporter
|
|||||||
h.setName(name);
|
h.setName(name);
|
||||||
h.setDescription(description);
|
h.setDescription(description);
|
||||||
h.setFrequency(Frequency.DAILY);
|
h.setFrequency(Frequency.DAILY);
|
||||||
habits.add(h);
|
habitList.add(h);
|
||||||
map.put(name, h);
|
map.put(name, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,11 +21,12 @@ package org.isoron.uhabits.core.io;
|
|||||||
|
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
|
|
||||||
import org.apache.commons.io.*;
|
|
||||||
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 java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
@@ -36,14 +37,19 @@ import static org.isoron.uhabits.core.Config.*;
|
|||||||
*/
|
*/
|
||||||
public class LoopDBImporter extends AbstractImporter
|
public class LoopDBImporter extends AbstractImporter
|
||||||
{
|
{
|
||||||
|
@NonNull
|
||||||
|
private final ModelFactory modelFactory;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final DatabaseOpener opener;
|
private final DatabaseOpener opener;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public LoopDBImporter(@NonNull HabitList habits,
|
public LoopDBImporter(@NonNull HabitList habitList,
|
||||||
|
@NonNull ModelFactory modelFactory,
|
||||||
@NonNull DatabaseOpener opener)
|
@NonNull DatabaseOpener opener)
|
||||||
{
|
{
|
||||||
super(habits);
|
super(habitList);
|
||||||
|
this.modelFactory = modelFactory;
|
||||||
this.opener = opener;
|
this.opener = opener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,11 +84,32 @@ public class LoopDBImporter extends AbstractImporter
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void importHabitsFromFile(@NonNull File file) throws IOException
|
public synchronized void importHabitsFromFile(@NonNull File file)
|
||||||
|
throws IOException
|
||||||
{
|
{
|
||||||
// DatabaseUtils.dispose();
|
Database db = opener.open(file);
|
||||||
File originalDB = opener.getProductionDatabaseFile();
|
MigrationHelper helper = new MigrationHelper(db);
|
||||||
FileUtils.copyFile(file, originalDB);
|
helper.migrateTo(DATABASE_VERSION);
|
||||||
// DatabaseUtils.initializeDatabase(context);
|
|
||||||
|
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));
|
habit.setFrequency(new Frequency(numerator, denominator));
|
||||||
habits.add(habit);
|
habitList.add(habit);
|
||||||
|
|
||||||
createReminder(db, habit, id);
|
createReminder(db, habit, id);
|
||||||
createCheckmarks(db, habit, id);
|
createCheckmarks(db, habit, id);
|
||||||
@@ -204,7 +204,7 @@ public class RewireDBImporter extends AbstractImporter
|
|||||||
|
|
||||||
Reminder reminder = new Reminder(hour, minute, days);
|
Reminder reminder = new Reminder(hour, minute, days);
|
||||||
habit.setReminder(reminder);
|
habit.setReminder(reminder);
|
||||||
habits.update(habit);
|
habitList.update(habit);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ public class TickmateDBImporter extends AbstractImporter
|
|||||||
habit.setName(name);
|
habit.setName(name);
|
||||||
habit.setDescription(description);
|
habit.setDescription(description);
|
||||||
habit.setFrequency(Frequency.DAILY);
|
habit.setFrequency(Frequency.DAILY);
|
||||||
habits.add(habit);
|
habitList.add(habit);
|
||||||
|
|
||||||
createCheckmarks(db, habit, id);
|
createCheckmarks(db, habit, id);
|
||||||
|
|
||||||
|
|||||||
@@ -213,9 +213,6 @@ public abstract class RepetitionList
|
|||||||
|
|
||||||
public void toggle(long timestamp, int value)
|
public void toggle(long timestamp, int value)
|
||||||
{
|
{
|
||||||
if(!habit.isNumerical())
|
|
||||||
throw new IllegalStateException("habit must be numerical");
|
|
||||||
|
|
||||||
Repetition rep = getByTimestamp(timestamp);
|
Repetition rep = getByTimestamp(timestamp);
|
||||||
if(rep != null) remove(rep);
|
if(rep != null) remove(rep);
|
||||||
add(new Repetition(timestamp, value));
|
add(new Repetition(timestamp, value));
|
||||||
|
|||||||
@@ -37,8 +37,6 @@ import java.io.*;
|
|||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import sun.reflect.generics.reflectiveObjects.*;
|
|
||||||
|
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
@@ -73,12 +71,6 @@ public class BaseUnitTest
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public File getProductionDatabaseFile()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
|||||||
@@ -60,7 +60,6 @@ public class ImportTest extends BaseUnitTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void testLoopDB() throws IOException
|
public void testLoopDB() throws IOException
|
||||||
{
|
{
|
||||||
importFromFile("loop.db");
|
importFromFile("loop.db");
|
||||||
@@ -133,7 +132,7 @@ public class ImportTest extends BaseUnitTest
|
|||||||
assertTrue(file.canRead());
|
assertTrue(file.canRead());
|
||||||
|
|
||||||
GenericImporter importer = new GenericImporter(habitList,
|
GenericImporter importer = new GenericImporter(habitList,
|
||||||
new LoopDBImporter(habitList, databaseOpener),
|
new LoopDBImporter(habitList, modelFactory, databaseOpener),
|
||||||
new RewireDBImporter(habitList, modelFactory, databaseOpener),
|
new RewireDBImporter(habitList, modelFactory, databaseOpener),
|
||||||
new TickmateDBImporter(habitList, modelFactory, databaseOpener),
|
new TickmateDBImporter(habitList, modelFactory, databaseOpener),
|
||||||
new HabitBullCSVImporter(habitList, modelFactory));
|
new HabitBullCSVImporter(habitList, modelFactory));
|
||||||
|
|||||||
Reference in New Issue
Block a user