mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Automatically repair inconsistent databases before crashing
This commit is contained in:
@@ -27,6 +27,8 @@ import android.view.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.habits.list.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.models.sqlite.*;
|
||||
|
||||
import static android.R.anim.*;
|
||||
|
||||
@@ -125,6 +127,14 @@ abstract public class BaseActivity extends AppCompatActivity
|
||||
// ignored
|
||||
}
|
||||
|
||||
if (ex.getCause() instanceof InconsistentDatabaseException)
|
||||
{
|
||||
HabitsApplication app = (HabitsApplication) getApplication();
|
||||
HabitList habits = app.getComponent().getHabitList();
|
||||
habits.repair();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
if (androidExceptionHandler != null)
|
||||
androidExceptionHandler.uncaughtException(thread, ex);
|
||||
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.commands.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.models.sqlite.*;
|
||||
import org.isoron.uhabits.preferences.*;
|
||||
import org.isoron.uhabits.tasks.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
@@ -160,19 +159,7 @@ public class ListHabitsController
|
||||
public void onRepairDB()
|
||||
{
|
||||
taskRunner.execute(() -> {
|
||||
for(Habit h : habitList)
|
||||
{
|
||||
h.getCheckmarks().invalidateNewerThan(0);
|
||||
h.getStreaks().invalidateNewerThan(0);
|
||||
h.getScores().invalidateNewerThan(0);
|
||||
}
|
||||
|
||||
if(habitList instanceof SQLiteHabitList)
|
||||
{
|
||||
SQLiteHabitList sqlList = (SQLiteHabitList) habitList;
|
||||
sqlList.rebuildOrder();
|
||||
}
|
||||
|
||||
habitList.repair();
|
||||
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 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.
|
||||
*
|
||||
|
||||
@@ -282,4 +282,11 @@ public class SQLiteHabitList extends HabitList
|
||||
appendOrderBy(query);
|
||||
return query.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void repair()
|
||||
{
|
||||
super.repair();
|
||||
rebuildOrder();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user