mark repeating events on main monthly calendar

This commit is contained in:
tibbi 2016-09-11 14:44:55 +02:00
parent f11dd4dd2e
commit a70c14b389
3 changed files with 28 additions and 14 deletions

View File

@ -7,6 +7,10 @@ public class Constants {
public static final String DAY_CODE = "day_code"; public static final String DAY_CODE = "day_code";
public static final String EVENT = "event"; public static final String EVENT = "event";
public static final int DAY = 86400;
public static final int WEEK = 604800;
public static final int YEAR = 31536000;
// Shared Preferences // Shared Preferences
public static final String PREFS_KEY = "Calendar"; public static final String PREFS_KEY = "Calendar";
public static final String IS_FIRST_RUN = "is_first_run"; public static final String IS_FIRST_RUN = "is_first_run";

View File

@ -161,12 +161,26 @@ public class DBHelper extends SQLiteOpenHelper {
public void getEvents(int fromTS, int toTS) { public void getEvents(int fromTS, int toTS) {
List<Event> events = new ArrayList<>(); List<Event> events = new ArrayList<>();
for (int ts = fromTS; ts < toTS; ts += Constants.DAY) {
final int dayExclusive = Constants.DAY - 1;
final String selection = "(? - " + COL_REPEAT_START + ") % " + COL_REPEAT_INTERVAL + " BETWEEN 0 AND " + dayExclusive;
final String[] selectionArgs = {String.valueOf(ts)};
final Cursor cursor = getEventsCursor(selection, selectionArgs);
if (cursor != null) {
final List<Event> newEvents = fillEvents(cursor);
for (Event e : newEvents) {
final int periods = (ts - e.getStartTS()) / e.getRepeatInterval();
e.setStartTS(e.getStartTS() + periods * e.getRepeatInterval());
}
events.addAll(newEvents);
}
}
final String selection = COL_START_TS + " <= ? AND " + COL_END_TS + " >= ?"; final String selection = COL_START_TS + " <= ? AND " + COL_END_TS + " >= ?";
final String[] selectionArgs = {String.valueOf(toTS), String.valueOf(fromTS)}; final String[] selectionArgs = {String.valueOf(toTS), String.valueOf(fromTS)};
final Cursor cursor = getEventsCursor(selection, selectionArgs); final Cursor cursor = getEventsCursor(selection, selectionArgs);
if (cursor != null) { if (cursor != null) {
events = fillEvents(cursor); events.addAll(fillEvents(cursor));
} }
if (mCallback != null) if (mCallback != null)
@ -205,10 +219,10 @@ public class DBHelper extends SQLiteOpenHelper {
final int id = cursor.getInt(cursor.getColumnIndex(COL_ID)); final int id = cursor.getInt(cursor.getColumnIndex(COL_ID));
final int startTS = cursor.getInt(cursor.getColumnIndex(COL_START_TS)); final int startTS = cursor.getInt(cursor.getColumnIndex(COL_START_TS));
final int endTS = cursor.getInt(cursor.getColumnIndex(COL_END_TS)); final int endTS = cursor.getInt(cursor.getColumnIndex(COL_END_TS));
final String title = cursor.getString(cursor.getColumnIndex(COL_TITLE));
final String description = cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION));
final int reminderMinutes = cursor.getInt(cursor.getColumnIndex(COL_REMINDER_MINUTES)); final int reminderMinutes = cursor.getInt(cursor.getColumnIndex(COL_REMINDER_MINUTES));
final int repeatInterval = cursor.getInt(cursor.getColumnIndex(COL_REPEAT_INTERVAL)); final int repeatInterval = cursor.getInt(cursor.getColumnIndex(COL_REPEAT_INTERVAL));
final String title = cursor.getString(cursor.getColumnIndex(COL_TITLE));
final String description = cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION));
events.add(new Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval)); events.add(new Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }

View File

@ -34,10 +34,6 @@ import butterknife.OnClick;
import butterknife.OnItemSelected; import butterknife.OnItemSelected;
public class EventActivity extends SimpleActivity implements DBHelper.DBOperationsListener { public class EventActivity extends SimpleActivity implements DBHelper.DBOperationsListener {
private static final int DAILY = 86400;
private static final int WEEKLY = 604800;
private static final int YEARLY = 31536000;
@BindView(R.id.event_start_date) TextView mStartDate; @BindView(R.id.event_start_date) TextView mStartDate;
@BindView(R.id.event_start_time) TextView mStartTime; @BindView(R.id.event_start_time) TextView mStartTime;
@BindView(R.id.event_end_date) TextView mEndDate; @BindView(R.id.event_end_date) TextView mEndDate;
@ -132,13 +128,13 @@ public class EventActivity extends SimpleActivity implements DBHelper.DBOperatio
private void setupRepetition() { private void setupRepetition() {
switch (mEvent.getRepeatInterval()) { switch (mEvent.getRepeatInterval()) {
case DAILY: case Constants.DAY:
mRepetition.setSelection(1); mRepetition.setSelection(1);
break; break;
case WEEKLY: case Constants.WEEK:
mRepetition.setSelection(2); mRepetition.setSelection(2);
break; break;
case YEARLY: case Constants.YEAR:
mRepetition.setSelection(3); mRepetition.setSelection(3);
break; break;
default: default:
@ -244,11 +240,11 @@ public class EventActivity extends SimpleActivity implements DBHelper.DBOperatio
private int getRepeatInterval() { private int getRepeatInterval() {
switch (mRepetition.getSelectedItemPosition()) { switch (mRepetition.getSelectedItemPosition()) {
case 1: case 1:
return DAILY; return Constants.DAY;
case 2: case 2:
return WEEKLY; return Constants.WEEK;
case 3: case 3:
return YEARLY; return Constants.YEAR;
default: default:
return 0; return 0;
} }