some refactoring at getting the events

This commit is contained in:
tibbi 2016-07-04 16:55:19 +02:00
parent e6433a7cee
commit 5ecd7a11ee
4 changed files with 18 additions and 13 deletions

View file

@ -13,7 +13,6 @@ import java.util.List;
public class DBHelper extends SQLiteOpenHelper {
private static SQLiteDatabase mDb;
private static String[] mProjection;
private static DBOperationsListener mCallback;
private static final String DB_NAME = "events.db";
@ -34,7 +33,6 @@ public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
mDb = getWritableDatabase();
mProjection = new String[]{COL_ID, COL_START_TS, COL_END_TS, COL_TITLE, COL_DESCRIPTION};
}
@Override
@ -64,10 +62,11 @@ public class DBHelper extends SQLiteOpenHelper {
}
public void getEvents(int fromTS, int toTS) {
final String[] projection = {COL_ID, COL_START_TS, COL_END_TS, COL_TITLE, COL_DESCRIPTION};
List<Event> events = new ArrayList<>();
final String selection = COL_START_TS + " <= ? AND " + COL_END_TS + " >= ?";
final String[] selectionArgs = {String.valueOf(toTS), String.valueOf(fromTS)};
final Cursor cursor = mDb.query(TABLE_NAME, mProjection, selection, selectionArgs, null, null, COL_START_TS);
final Cursor cursor = mDb.query(TABLE_NAME, projection, selection, selectionArgs, null, null, COL_START_TS);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {

View file

@ -10,7 +10,7 @@ public class Formatter {
private static final String EVENT_TIME_PATTERN = "HH:mm";
public static String getEventDate(String dayCode) {
return getDateTime(dayCode).toString(EVENT_DATE_PATTERN);
return getDateTimeFromCode(dayCode).toString(EVENT_DATE_PATTERN);
}
public static String getEventDate(DateTime dateTime) {
@ -21,7 +21,7 @@ public class Formatter {
return dateTime.toString(EVENT_TIME_PATTERN);
}
public static DateTime getDateTime(String dayCode) {
public static DateTime getDateTimeFromCode(String dayCode) {
final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(Constants.DATE_PATTERN).withZone(DateTimeZone.UTC);
return dateTimeFormatter.parseDateTime(dayCode);
}
@ -30,4 +30,14 @@ public class Formatter {
final DateTime dateTime = new DateTime(ts * 1000L, DateTimeZone.UTC);
return getEventTime(dateTime);
}
public static int getDayStartTS(String dayCode) {
final DateTime dateTime = getDateTimeFromCode(dayCode);
return (int) (dateTime.getMillis() / 1000);
}
public static int getDayEndTS(String dayCode) {
final DateTime dateTime = getDateTimeFromCode(dayCode);
return (int) (dateTime.plusDays(1).minusMinutes(1).getMillis() / 1000);
}
}

View file

@ -14,9 +14,6 @@ import com.simplemobiletools.calendar.Formatter;
import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.models.Event;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.List;
import butterknife.BindView;
@ -61,9 +58,8 @@ public class DetailsActivity extends AppCompatActivity implements DBHelper.DBOpe
}
private void checkEvents() {
final DateTime dateTime = Formatter.getDateTime(dayCode).withZone(DateTimeZone.UTC);
final int startTS = (int) (dateTime.getMillis() / 1000);
final int endTS = (int) (dateTime.plusDays(1).minusMinutes(1).getMillis() / 1000);
final int startTS = Formatter.getDayStartTS(dayCode);
final int endTS = Formatter.getDayEndTS(dayCode);
DBHelper.newInstance(getApplicationContext(), this).getEvents(startTS, endTS);
}

View file

@ -53,11 +53,11 @@ public class EventActivity extends AppCompatActivity implements DBHelper.DBOpera
if (dayCode == null || dayCode.isEmpty())
return;
mEventStartDateTime = Formatter.getDateTime(dayCode);
mEventStartDateTime = Formatter.getDateTimeFromCode(dayCode);
updateStartDate();
updateStartTime();
mEventEndDateTime = Formatter.getDateTime(dayCode);
mEventEndDateTime = Formatter.getDateTimeFromCode(dayCode);
updateEndDate();
updateEndTime();
}