Skip to content

Commit

Permalink
Automatically repair inconsistent databases before crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Oct 10, 2016
1 parent ab3b946 commit e273fe7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/org/isoron/uhabits/activities/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

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.*;

Expand Down Expand Up @@ -125,6 +127,14 @@ public void uncaughtException(@Nullable Thread thread,
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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.*;
Expand Down Expand Up @@ -160,19 +159,7 @@ public void onInvalidToggle()
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);
});
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/isoron/uhabits/models/HabitList.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ public void removeAll()
*/
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,11 @@ private String buildSelectQuery()
appendOrderBy(query);
return query.toString();
}

@Override
public void repair()
{
super.repair();
rebuildOrder();
}
}

0 comments on commit e273fe7

Please sign in to comment.