Automatically repair inconsistent databases before crashing

pull/201/head
Alinson S. Xavier 9 years ago
parent ab3b946c65
commit e273fe7375

@ -27,6 +27,8 @@ import android.view.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.habits.list.*; import org.isoron.uhabits.activities.habits.list.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.sqlite.*;
import static android.R.anim.*; import static android.R.anim.*;
@ -125,6 +127,14 @@ abstract public class BaseActivity extends AppCompatActivity
// ignored // ignored
} }
if (ex.getCause() instanceof InconsistentDatabaseException)
{
HabitsApplication app = (HabitsApplication) getApplication();
HabitList habits = app.getComponent().getHabitList();
habits.repair();
System.exit(0);
}
if (androidExceptionHandler != null) if (androidExceptionHandler != null)
androidExceptionHandler.uncaughtException(thread, ex); androidExceptionHandler.uncaughtException(thread, ex);
else System.exit(1); else System.exit(1);

@ -27,7 +27,6 @@ import org.isoron.uhabits.activities.habits.list.controllers.*;
import org.isoron.uhabits.activities.habits.list.model.*; import org.isoron.uhabits.activities.habits.list.model.*;
import org.isoron.uhabits.commands.*; import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.sqlite.*;
import org.isoron.uhabits.preferences.*; import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.tasks.*; import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
@ -160,19 +159,7 @@ public class ListHabitsController
public void onRepairDB() public void onRepairDB()
{ {
taskRunner.execute(() -> { taskRunner.execute(() -> {
for(Habit h : habitList) habitList.repair();
{
h.getCheckmarks().invalidateNewerThan(0);
h.getStreaks().invalidateNewerThan(0);
h.getScores().invalidateNewerThan(0);
}
if(habitList instanceof SQLiteHabitList)
{
SQLiteHabitList sqlList = (SQLiteHabitList) habitList;
sqlList.rebuildOrder();
}
screen.showMessage(R.string.database_repaired); screen.showMessage(R.string.database_repaired);
}); });
} }

@ -147,6 +147,16 @@ public abstract class HabitList implements Iterable<Habit>
*/ */
public abstract void reorder(Habit from, Habit to); public abstract void reorder(Habit from, Habit to);
public void repair()
{
for(Habit h : this)
{
h.getCheckmarks().invalidateNewerThan(0);
h.getStreaks().invalidateNewerThan(0);
h.getScores().invalidateNewerThan(0);
}
}
/** /**
* Returns the number of habits in this list. * Returns the number of habits in this list.
* *

@ -282,4 +282,11 @@ public class SQLiteHabitList extends HabitList
appendOrderBy(query); appendOrderBy(query);
return query.toString(); return query.toString();
} }
@Override
public void repair()
{
super.repair();
rebuildOrder();
}
} }

Loading…
Cancel
Save