store the events in a database
This commit is contained in:
parent
6a014a99df
commit
6098b54712
|
@ -1,8 +1,5 @@
|
||||||
package com.simplemobiletools.calendar;
|
package com.simplemobiletools.calendar;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.util.SparseBooleanArray;
|
|
||||||
|
|
||||||
import com.simplemobiletools.calendar.models.Day;
|
import com.simplemobiletools.calendar.models.Day;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
@ -18,15 +15,11 @@ public class CalendarImpl {
|
||||||
|
|
||||||
private final Calendar mCallback;
|
private final Calendar mCallback;
|
||||||
private final String mToday;
|
private final String mToday;
|
||||||
private final Context mContext;
|
|
||||||
private DateTime mTargetDate;
|
private DateTime mTargetDate;
|
||||||
private SparseBooleanArray mEvents;
|
|
||||||
|
|
||||||
public CalendarImpl(Calendar callback, Context context) {
|
public CalendarImpl(Calendar callback) {
|
||||||
this.mCallback = callback;
|
this.mCallback = callback;
|
||||||
mToday = new DateTime().toString(Constants.DATE_PATTERN);
|
mToday = new DateTime().toString(Constants.DATE_PATTERN);
|
||||||
mContext = context;
|
|
||||||
mEvents = Config.newInstance(context).getEvents();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateCalendar(DateTime targetDate) {
|
public void updateCalendar(DateTime targetDate) {
|
||||||
|
@ -81,7 +74,7 @@ public class CalendarImpl {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasEvent(String dayCode) {
|
private boolean hasEvent(String dayCode) {
|
||||||
return mEvents.get(Integer.parseInt(dayCode));
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isToday(DateTime targetDate, int curDayInMonth) {
|
private boolean isToday(DateTime targetDate, int curDayInMonth) {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package com.simplemobiletools.calendar;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.util.SparseBooleanArray;
|
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
private SharedPreferences mPrefs;
|
private SharedPreferences mPrefs;
|
||||||
|
@ -22,13 +21,4 @@ public class Config {
|
||||||
public void setIsFirstRun(boolean firstRun) {
|
public void setIsFirstRun(boolean firstRun) {
|
||||||
mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply();
|
mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SparseBooleanArray getEvents() {
|
|
||||||
final String json = mPrefs.getString(Constants.EVENTS, "{}");
|
|
||||||
return Utils.deserializeJson(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEvents(SparseBooleanArray events) {
|
|
||||||
mPrefs.edit().putString(Constants.EVENTS, Utils.serializeArray(events)).apply();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
package com.simplemobiletools.calendar;
|
||||||
|
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
|
||||||
|
import com.simplemobiletools.calendar.models.Event;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class DBHelper extends SQLiteOpenHelper {
|
||||||
|
private static SQLiteDatabase mDb;
|
||||||
|
private static String[] mProjection;
|
||||||
|
|
||||||
|
private static final String DB_NAME = "events.db";
|
||||||
|
private static final int DB_VERSION = 1;
|
||||||
|
|
||||||
|
private static final String TABLE_NAME = "events";
|
||||||
|
private static final String COL_ID = "id";
|
||||||
|
private static final String COL_START_TS = "start_ts";
|
||||||
|
private static final String COL_END_TS = "end_ts";
|
||||||
|
private static final String COL_TITLE = "title";
|
||||||
|
private static final String COL_DESCRIPTION = "description";
|
||||||
|
|
||||||
|
public static DBHelper newInstance(Context context) {
|
||||||
|
return new DBHelper(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
public void onCreate(SQLiteDatabase db) {
|
||||||
|
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
|
||||||
|
COL_ID + " INTEGER PRIMARY KEY, " +
|
||||||
|
COL_START_TS + " INTEGER," +
|
||||||
|
COL_END_TS + " INTEGER," +
|
||||||
|
COL_TITLE + " TEXT," +
|
||||||
|
COL_DESCRIPTION + " TEXT" +
|
||||||
|
")");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(Event event) {
|
||||||
|
final ContentValues values = new ContentValues();
|
||||||
|
values.put(COL_START_TS, event.getStartTS());
|
||||||
|
values.put(COL_END_TS, event.getEndTS());
|
||||||
|
values.put(COL_TITLE, event.getTitle());
|
||||||
|
values.put(COL_DESCRIPTION, event.getDescription());
|
||||||
|
mDb.insert(TABLE_NAME, null, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Event> getEvents(int fromTS, int toTS) {
|
||||||
|
List<Event> events = new ArrayList<>();
|
||||||
|
final String selection = COL_START_TS + " >= ? AND " + COL_START_TS + " <= ?";
|
||||||
|
final String[] selectionArgs = {String.valueOf(fromTS), String.valueOf(toTS)};
|
||||||
|
final Cursor cursor = mDb.query(TABLE_NAME, mProjection, selection, selectionArgs, null, null, null);
|
||||||
|
if (cursor != null) {
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
final int id = cursor.getInt(cursor.getColumnIndex(COL_ID));
|
||||||
|
final int startTS = cursor.getInt(cursor.getColumnIndex(COL_START_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));
|
||||||
|
events.add(new Event(id, startTS, endTS, title, description));
|
||||||
|
} while (cursor.moveToNext());
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
}
|
|
@ -69,7 +69,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
|
||||||
final int bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK);
|
final int bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK);
|
||||||
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
|
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
|
||||||
|
|
||||||
mCalendar = new CalendarImpl(this, mContext);
|
mCalendar = new CalendarImpl(this);
|
||||||
mCalendar.updateCalendar(new DateTime());
|
mCalendar.updateCalendar(new DateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,8 @@
|
||||||
package com.simplemobiletools.calendar;
|
package com.simplemobiletools.calendar;
|
||||||
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.util.Log;
|
|
||||||
import android.util.SparseBooleanArray;
|
|
||||||
|
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
private static final String TAG = Utils.class.getSimpleName();
|
|
||||||
|
|
||||||
public static int adjustAlpha(int color, float factor) {
|
public static int adjustAlpha(int color, float factor) {
|
||||||
final int alpha = Math.round(Color.alpha(color) * factor);
|
final int alpha = Math.round(Color.alpha(color) * factor);
|
||||||
|
@ -19,33 +11,4 @@ public class Utils {
|
||||||
final int blue = Color.blue(color);
|
final int blue = Color.blue(color);
|
||||||
return Color.argb(alpha, red, green, blue);
|
return Color.argb(alpha, red, green, blue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String serializeArray(SparseBooleanArray arr) {
|
|
||||||
JSONObject json = new JSONObject();
|
|
||||||
try {
|
|
||||||
for (int i = 0; i < arr.size(); i++) {
|
|
||||||
int key = arr.keyAt(i);
|
|
||||||
json.put(String.valueOf(key), arr.get(key));
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
Log.e(TAG, "serializeArray " + e.getMessage());
|
|
||||||
}
|
|
||||||
return json.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SparseBooleanArray deserializeJson(String string) {
|
|
||||||
final SparseBooleanArray sparseBooleanArray = new SparseBooleanArray();
|
|
||||||
try {
|
|
||||||
final JSONObject json = new JSONObject(string);
|
|
||||||
final Iterator<String> iter = json.keys();
|
|
||||||
while (iter.hasNext()) {
|
|
||||||
String key = iter.next();
|
|
||||||
Boolean value = (Boolean) json.get(key);
|
|
||||||
sparseBooleanArray.put(Integer.parseInt(key), value);
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
Log.e(TAG, "deserializeJson " + e.getMessage());
|
|
||||||
}
|
|
||||||
return sparseBooleanArray;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,7 +77,7 @@ public class MainActivity extends AppCompatActivity implements Calendar {
|
||||||
final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mCalendarHolder.getLayoutParams();
|
final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mCalendarHolder.getLayoutParams();
|
||||||
params.setMargins(mActivityMargin, mActivityMargin, mActivityMargin, mActivityMargin);
|
params.setMargins(mActivityMargin, mActivityMargin, mActivityMargin, mActivityMargin);
|
||||||
|
|
||||||
mCalendar = new CalendarImpl(this, getApplicationContext());
|
mCalendar = new CalendarImpl(this);
|
||||||
mCalendar.updateCalendar(new DateTime());
|
mCalendar.updateCalendar(new DateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,10 +18,10 @@ import android.widget.TextView;
|
||||||
import com.simplemobiletools.calendar.Calendar;
|
import com.simplemobiletools.calendar.Calendar;
|
||||||
import com.simplemobiletools.calendar.CalendarImpl;
|
import com.simplemobiletools.calendar.CalendarImpl;
|
||||||
import com.simplemobiletools.calendar.Constants;
|
import com.simplemobiletools.calendar.Constants;
|
||||||
import com.simplemobiletools.calendar.models.Day;
|
|
||||||
import com.simplemobiletools.calendar.Utils;
|
|
||||||
import com.simplemobiletools.calendar.MyWidgetProvider;
|
import com.simplemobiletools.calendar.MyWidgetProvider;
|
||||||
import com.simplemobiletools.calendar.R;
|
import com.simplemobiletools.calendar.R;
|
||||||
|
import com.simplemobiletools.calendar.Utils;
|
||||||
|
import com.simplemobiletools.calendar.models.Day;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ public class WidgetConfigureActivity extends AppCompatActivity implements Calend
|
||||||
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
|
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
|
||||||
updateBgColor();
|
updateBgColor();
|
||||||
|
|
||||||
mCalendar = new CalendarImpl(this, getApplicationContext());
|
mCalendar = new CalendarImpl(this);
|
||||||
mCalendar.updateCalendar(new DateTime());
|
mCalendar.updateCalendar(new DateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,24 @@
|
||||||
package com.simplemobiletools.calendar.models;
|
package com.simplemobiletools.calendar.models;
|
||||||
|
|
||||||
public class Event {
|
public class Event {
|
||||||
|
private final int mId;
|
||||||
private final int mStartTS;
|
private final int mStartTS;
|
||||||
private final int mEndTS;
|
private final int mEndTS;
|
||||||
|
private final String mTitle;
|
||||||
private final String mDescription;
|
private final String mDescription;
|
||||||
|
|
||||||
public Event(int startTS, int endTS, String description) {
|
public Event(int id, int startTS, int endTS, String title, String description) {
|
||||||
|
mId = id;
|
||||||
mStartTS = startTS;
|
mStartTS = startTS;
|
||||||
mEndTS = endTS;
|
mEndTS = endTS;
|
||||||
|
mTitle = title;
|
||||||
mDescription = description;
|
mDescription = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return mId;
|
||||||
|
}
|
||||||
|
|
||||||
public int getStartTS() {
|
public int getStartTS() {
|
||||||
return mStartTS;
|
return mStartTS;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +27,22 @@ public class Event {
|
||||||
return mEndTS;
|
return mEndTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return mTitle;
|
||||||
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return mDescription;
|
return mDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Event {" +
|
||||||
|
"id=" + getId() +
|
||||||
|
", startTS=" + getStartTS() +
|
||||||
|
", endTS=" + getEndTS() +
|
||||||
|
", title=" + getTitle() +
|
||||||
|
", description=" + getDescription() +
|
||||||
|
"}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,10 +96,11 @@
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/event_description"
|
android:id="@+id/event_description"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_below="@+id/event_description_label"
|
android:layout_below="@+id/event_description_label"
|
||||||
android:gravity="top"
|
android:gravity="top"
|
||||||
|
android:minEms="20"
|
||||||
android:textSize="@dimen/day_text_size"/>
|
android:textSize="@dimen/day_text_size"/>
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
Loading…
Reference in New Issue