properly handle updating event repeat interval

This commit is contained in:
tibbi 2016-09-11 11:45:15 +02:00
parent 7ea9e214b1
commit f11dd4dd2e
1 changed files with 18 additions and 12 deletions

View File

@ -54,10 +54,9 @@ public class DBHelper extends SQLiteOpenHelper {
COL_TITLE + " TEXT," + COL_TITLE + " TEXT," +
COL_DESCRIPTION + " TEXT," + COL_DESCRIPTION + " TEXT," +
COL_REMINDER_MINUTES + " INTEGER" + COL_REMINDER_MINUTES + " INTEGER" +
")" ")");
);
createMeta(db); createMetaTable(db);
} }
@Override @Override
@ -67,18 +66,17 @@ public class DBHelper extends SQLiteOpenHelper {
} }
if (newVersion == 3) { if (newVersion == 3) {
createMeta(db); createMetaTable(db);
} }
} }
private void createMeta(SQLiteDatabase db) { private void createMetaTable(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + META_TABLE_NAME + " (" + db.execSQL("CREATE TABLE " + META_TABLE_NAME + " (" +
COL_ID + " INTEGER PRIMARY KEY, " + COL_ID + " INTEGER PRIMARY KEY, " +
COL_EVENT_ID + " INTEGER, " + COL_EVENT_ID + " INTEGER UNIQUE, " +
COL_REPEAT_START + " INTEGER, " + COL_REPEAT_START + " INTEGER, " +
COL_REPEAT_INTERVAL + " INTEGER " + COL_REPEAT_INTERVAL + " INTEGER " +
")" ")");
);
} }
public void insert(Event event) { public void insert(Event event) {
@ -95,11 +93,19 @@ public class DBHelper extends SQLiteOpenHelper {
} }
public void update(Event event) { public void update(Event event) {
final String[] selectionArgs = {String.valueOf(event.getId())};
final ContentValues values = fillContentValues(event); final ContentValues values = fillContentValues(event);
final String selection = COL_ID + " = ?"; final String selection = COL_ID + " = ?";
final String[] selectionArgs = {String.valueOf(event.getId())};
mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs); mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs);
if (event.getRepeatInterval() == 0) {
final String metaSelection = COL_EVENT_ID + " = ?";
mDb.delete(META_TABLE_NAME, metaSelection, selectionArgs);
} else {
final ContentValues metaValues = fillMetaValues(event);
mDb.insertWithOnConflict(META_TABLE_NAME, null, metaValues, SQLiteDatabase.CONFLICT_REPLACE);
}
if (mCallback != null) if (mCallback != null)
mCallback.eventUpdated(event); mCallback.eventUpdated(event);
} }
@ -117,7 +123,7 @@ public class DBHelper extends SQLiteOpenHelper {
private ContentValues fillMetaValues(Event event) { private ContentValues fillMetaValues(Event event) {
final ContentValues values = new ContentValues(); final ContentValues values = new ContentValues();
values.put(COL_EVENT_ID, event.getId()); values.put(COL_EVENT_ID, event.getId());
values.put(COL_REPEAT_START, event.getRepeatInterval()); values.put(COL_REPEAT_START, event.getStartTS());
values.put(COL_REPEAT_INTERVAL, event.getRepeatInterval()); values.put(COL_REPEAT_INTERVAL, event.getRepeatInterval());
return values; return values;
} }
@ -181,8 +187,8 @@ public class DBHelper extends SQLiteOpenHelper {
private Cursor getEventsCursor(String selection, String[] selectionArgs) { private Cursor getEventsCursor(String selection, String[] selectionArgs) {
final SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); final SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(MAIN_TABLE_NAME + " LEFT OUTER JOIN " + META_TABLE_NAME + " ON " + COL_EVENT_ID + " = " + MAIN_TABLE_NAME + "." builder.setTables(
+ COL_ID); MAIN_TABLE_NAME + " LEFT OUTER JOIN " + META_TABLE_NAME + " ON " + COL_EVENT_ID + " = " + MAIN_TABLE_NAME + "." + COL_ID);
final String[] projection = getAllColumns(); final String[] projection = getAllColumns();
return builder.query(mDb, projection, selection, selectionArgs, MAIN_TABLE_NAME + "." + COL_ID, null, COL_START_TS); return builder.query(mDb, projection, selection, selectionArgs, MAIN_TABLE_NAME + "." + COL_ID, null, COL_START_TS);
} }