mirror of
https://github.com/bspeice/UNCCGameDay
synced 2024-11-05 07:38:13 -05:00
Further Alert cleanup >AGK
This commit is contained in:
parent
33afb534bd
commit
06f7c7933f
@ -1,7 +1,10 @@
|
|||||||
package com.uncc.gameday.alerts;
|
package com.uncc.gameday.alerts;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
@ -16,230 +19,264 @@ import android.database.sqlite.SQLiteOpenHelper;
|
|||||||
* The Class AlertDB.
|
* The Class AlertDB.
|
||||||
*/
|
*/
|
||||||
public class AlertDB extends SQLiteOpenHelper {
|
public class AlertDB extends SQLiteOpenHelper {
|
||||||
|
|
||||||
/** The Constant DATABASE_VERSION. */
|
/** The Constant DATABASE_VERSION. */
|
||||||
private static final int DATABASE_VERSION = 1;
|
private static final int DATABASE_VERSION = 1;
|
||||||
|
|
||||||
/** The Constant DATABASE_NAME. */
|
/** The Constant DATABASE_NAME. */
|
||||||
private static final String DATABASE_NAME = "AlertDataBase";
|
private static final String DATABASE_NAME = "AlertDataBase";
|
||||||
|
|
||||||
/** The Constant TABLE_ALERTS. */
|
/** The Constant TABLE_ALERTS. */
|
||||||
private static final String TABLE_ALERTS = "alerts";
|
private static final String TABLE_ALERTS = "alerts";
|
||||||
|
|
||||||
/** The Constant KEY_ALARM_DATE. */
|
/** The Constant KEY_ALARM_DATE. */
|
||||||
private static final String KEY_ALARM_DATE = "alarm_date";
|
private static final String KEY_ALARM_DATE = "alarm_date";
|
||||||
|
|
||||||
/** The Constant KEY_MESSAGE. */
|
/** The Constant KEY_MESSAGE. */
|
||||||
private static final String KEY_MESSAGE = "message";
|
private static final String KEY_MESSAGE = "message";
|
||||||
|
|
||||||
/** The Constant KEY_SHOWN. */
|
/** The Constant KEY_SHOWN. */
|
||||||
private static final String KEY_SHOWN = "shown";
|
private static final String KEY_SHOWN = "shown";
|
||||||
|
|
||||||
/** The Constant KEY_TYPE. */
|
/** The Constant KEY_TYPE. */
|
||||||
private static final String KEY_TYPE = "type";
|
private static final String KEY_TYPE = "type";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates our wrapper around the SQLiteOpenHelper
|
* Instantiates our wrapper around the SQLiteOpenHelper
|
||||||
*
|
*
|
||||||
* @param context - The context to open the database in
|
* @param context - The context to open the database in
|
||||||
*/
|
*/
|
||||||
public AlertDB(Context context){
|
public AlertDB(Context context){
|
||||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the initial Database
|
* Create the initial Database
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(SQLiteDatabase db) {
|
public void onCreate(SQLiteDatabase db) {
|
||||||
String CREATE_ALERTS_TABLE = "CREATE TABLE " + TABLE_ALERTS + "("
|
String CREATE_ALERTS_TABLE = "CREATE TABLE " + TABLE_ALERTS + "("
|
||||||
+ KEY_MESSAGE + " STRING PRIMARY KEY," + KEY_ALARM_DATE + " LONG,"
|
+ KEY_MESSAGE + " STRING PRIMARY KEY," + KEY_ALARM_DATE + " LONG,"
|
||||||
+ KEY_SHOWN + " INT," + KEY_TYPE + " STRING" + ")";
|
+ KEY_SHOWN + " INT," + KEY_TYPE + " STRING" + ")";
|
||||||
db.execSQL(CREATE_ALERTS_TABLE);
|
db.execSQL(CREATE_ALERTS_TABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Upgrade the database on application update
|
* Upgrade the database on application update
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
//drop older table if it exists
|
//drop older table if it exists
|
||||||
db.execSQL("DROP OLDER TABLE " + TABLE_ALERTS);
|
db.execSQL("DROP OLDER TABLE " + TABLE_ALERTS);
|
||||||
|
|
||||||
//recreate tables
|
//recreate tables
|
||||||
onCreate(db);
|
onCreate(db);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persist an alert into the database
|
* Persist an alert into the database
|
||||||
*
|
*
|
||||||
* @param a - The Alert to persist
|
* @param a - The Alert to persist
|
||||||
*/
|
*/
|
||||||
public void persist(Alert a) {
|
public void persist(Alert a) {
|
||||||
|
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
|
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(KEY_MESSAGE, a.getMessage());
|
values.put(KEY_MESSAGE, a.getMessage());
|
||||||
|
|
||||||
long dateValue = a.getAlarmDate().getTime();
|
|
||||||
values.put(KEY_ALARM_DATE, dateValue);
|
values.put(KEY_ALARM_DATE, a.getAlarmDate());
|
||||||
values.put(KEY_SHOWN, a.isShown());
|
values.put(KEY_SHOWN, a.isShown());
|
||||||
values.put(KEY_TYPE, a.getType());
|
values.put(KEY_TYPE, a.getType());
|
||||||
|
|
||||||
db.insert(TABLE_ALERTS, null, values);
|
db.insert(TABLE_ALERTS, null, values);
|
||||||
db.close();
|
db.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persist a list of alerts into the database -
|
* Persist a list of alerts into the database -
|
||||||
* helper method for easy iteration
|
* helper method for easy iteration
|
||||||
*
|
*
|
||||||
* @param alerts - The alerts to store
|
* @param alerts - The alerts to store
|
||||||
*/
|
*/
|
||||||
public void persistMultiple(List<Alert> alerts) {
|
public void persistMultiple(List<Alert> alerts) {
|
||||||
|
|
||||||
for(int i = 0; i < alerts.size(); i++)
|
for(int i = 0; i < alerts.size(); i++)
|
||||||
{
|
{
|
||||||
persist(alerts.get(i));
|
persist(alerts.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch an alert by date
|
/**
|
||||||
* TODO: Unused. Remove?
|
* Fetch an alert by date
|
||||||
*
|
* TODO: Unused. Remove?
|
||||||
* @param d - The date to fetch an alert from
|
*
|
||||||
* @return the Alert
|
* @param d - The date to fetch an alert from
|
||||||
*/
|
* @return the Alert
|
||||||
public Alert fetch(Date d) {
|
*/
|
||||||
|
public Alert fetch(Date d) {
|
||||||
return null;
|
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Fetch multiple alerts from the Database
|
/**
|
||||||
* TODO: Unused. Remove?
|
* Fetch multiple alerts from the Database
|
||||||
*
|
* TODO: Unused. Remove?
|
||||||
* @param dates the dates to fetch alerts from
|
*
|
||||||
* @return the list
|
* @param dates the dates to fetch alerts from
|
||||||
*/
|
* @return the list
|
||||||
public List<Alert> fetchMultiple(List<Date> dates) {
|
*/
|
||||||
|
public List<Alert> fetchMultiple(List<Date> dates) {
|
||||||
return null;
|
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Delete an alert from the Database whenever the Alert message matches
|
/**
|
||||||
*
|
* Delete an alert from the Database whenever the Alert message matches
|
||||||
* @param alert the alert
|
*
|
||||||
*/
|
* @param alert the alert
|
||||||
public void deleteAlert(Alert alert) {
|
*/
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
public void deleteAlert(Alert alert) {
|
||||||
db.delete(TABLE_ALERTS, KEY_MESSAGE + " = ?",
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
new String[] { String.valueOf(alert.getMessage()) });
|
db.delete(TABLE_ALERTS, KEY_MESSAGE + " = ?",
|
||||||
db.close();
|
new String[] { String.valueOf(alert.getMessage()) });
|
||||||
|
db.close();
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all alerts from the Database
|
||||||
/**
|
*
|
||||||
* Fetch all alerts from the Database, and mark as read
|
* @return the list
|
||||||
*
|
*/
|
||||||
* @return the list
|
public List<Alert> fetchAll() {
|
||||||
*/
|
|
||||||
public List<Alert> fetchAll() {
|
|
||||||
|
|
||||||
List<Alert> alertList = new ArrayList<Alert>();
|
List<Alert> alertList = new ArrayList<Alert>();
|
||||||
|
|
||||||
String selectQuery = "SELECT * FROM " + TABLE_ALERTS;
|
String selectQuery = "SELECT * FROM " + TABLE_ALERTS;
|
||||||
|
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
Cursor cursor = db.rawQuery(selectQuery, null);
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
||||||
|
|
||||||
//for each alert in database
|
//for each alert in database
|
||||||
//add it to alert list to be shown
|
//add it to alert list to be shown
|
||||||
//and modify it in DB to be classified as shown
|
//and modify it in DB to be classified as shown
|
||||||
if (cursor.moveToFirst())
|
int i = 0;
|
||||||
{
|
if (cursor.moveToFirst())
|
||||||
do
|
{
|
||||||
{
|
|
||||||
Alert alert = new Alert();
|
GregorianCalendar todayDate = new GregorianCalendar();
|
||||||
alert.setMessage(cursor.getString(0));
|
long currentDate = todayDate.getTimeInMillis();
|
||||||
alert.setAlarmDate(new Date(cursor.getLong(1)));
|
|
||||||
alert.setShown(cursor.getInt(2));
|
do
|
||||||
updateAlert(alert);
|
{
|
||||||
|
if(currentDate >= cursor.getLong(1))
|
||||||
|
{
|
||||||
|
Alert alert = new Alert();
|
||||||
|
alert.setMessage(cursor.getString(0));
|
||||||
|
alert.setAlarmDate(cursor.getLong(1));
|
||||||
|
alert.setShown(cursor.getInt(2));
|
||||||
|
|
||||||
alertList.add(alert);
|
alertList.add(alert);
|
||||||
} while (cursor.moveToNext());
|
i++;
|
||||||
|
}
|
||||||
|
} while (cursor.moveToNext() && i < 10);
|
||||||
|
|
||||||
}
|
}
|
||||||
db.close();
|
db.close();
|
||||||
return alertList;
|
|
||||||
|
//sorts alerts by Date
|
||||||
|
Collections.sort(alertList, new Comparator<Alert>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Alert a1, Alert a2) {
|
||||||
|
|
||||||
|
String compareDate = Long.toString(a1.getAlarmDate());
|
||||||
|
String thisDate = Long.toString(a2.getAlarmDate());
|
||||||
|
return thisDate.compareTo(compareDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
return alertList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch unread alerts from the database, and mark them as read
|
* Fetch unread alerts from the database, and mark them as read
|
||||||
*
|
*
|
||||||
* @return the list
|
* @return the list
|
||||||
*/
|
*/
|
||||||
public List<Alert> fetchUnread() {
|
public List<Alert> fetchUnread() {
|
||||||
|
|
||||||
List<Alert> alertList = new ArrayList<Alert>();
|
List<Alert> alertList = new ArrayList<Alert>();
|
||||||
String selectQuery = "SELECT * FROM " + TABLE_ALERTS;
|
String selectQuery = "SELECT * FROM " + TABLE_ALERTS;
|
||||||
|
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
Cursor cursor = db.rawQuery(selectQuery, null);
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
||||||
|
|
||||||
//for each alert in database
|
//for each alert in database
|
||||||
//given it is classified as unread
|
//given it is classified as unread
|
||||||
//add it to alert list to be shown
|
//add it to alert list to be shown
|
||||||
//and modify it in DB to be classified as shown
|
//and modify it in DB to be classified as shown
|
||||||
if (cursor.moveToFirst())
|
int i = 0;
|
||||||
{
|
if (cursor.moveToFirst())
|
||||||
do
|
{
|
||||||
{
|
|
||||||
Alert alert = new Alert();
|
GregorianCalendar todayDate = new GregorianCalendar();
|
||||||
if(cursor.getInt(2) == 0)
|
long currentDate = todayDate.getTimeInMillis();
|
||||||
{
|
do
|
||||||
alert.setMessage(cursor.getString(0));
|
{
|
||||||
alert.setAlarmDate(new Date(cursor.getLong(1)));
|
if(cursor.getInt(2) == 0 && currentDate >= cursor.getLong(1))
|
||||||
alert.setShown(cursor.getInt(2));
|
{
|
||||||
|
Alert alert = new Alert();
|
||||||
|
alert.setMessage(cursor.getString(0));
|
||||||
|
alert.setAlarmDate(cursor.getLong(1));
|
||||||
|
alert.setShown(cursor.getInt(2));
|
||||||
|
|
||||||
updateAlert(alert);
|
updateAlert(alert);
|
||||||
alertList.add(alert);
|
alertList.add(alert);
|
||||||
}
|
i++;
|
||||||
} while (cursor.moveToNext());
|
}
|
||||||
|
} while (cursor.moveToNext() && i < 1);
|
||||||
|
}
|
||||||
|
db.close();
|
||||||
|
|
||||||
|
//sorts alerts by Date
|
||||||
|
Collections.sort(alertList, new Comparator<Alert>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Alert a1, Alert a2) {
|
||||||
|
|
||||||
|
String compareDate = Long.toString(a1.getAlarmDate());
|
||||||
|
String thisDate = Long.toString(a2.getAlarmDate());
|
||||||
|
return thisDate.compareTo(compareDate);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
});
|
||||||
db.close();
|
return alertList;
|
||||||
return alertList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update an alert to be marked as shown
|
* Update an alert to be marked as shown
|
||||||
*
|
*
|
||||||
* @param alert the alert
|
* @param alert the alert
|
||||||
* @return the int
|
* @return the int
|
||||||
*/
|
*/
|
||||||
private int updateAlert(Alert alert) {
|
private int updateAlert(Alert alert) {
|
||||||
|
|
||||||
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
|
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
ContentValues values = new ContentValues();
|
||||||
|
|
||||||
ContentValues values = new ContentValues();
|
values.put(KEY_ALARM_DATE, alert.getAlarmDate());
|
||||||
|
values.put(KEY_SHOWN, 1);
|
||||||
|
|
||||||
|
return db.update(TABLE_ALERTS, values, KEY_MESSAGE + " = ?",
|
||||||
|
new String[] { String.valueOf(alert.getMessage()) });
|
||||||
|
|
||||||
long dateValue = alert.getAlarmDate().getTime();
|
}
|
||||||
values.put(KEY_ALARM_DATE, dateValue);
|
|
||||||
values.put(KEY_SHOWN, 1);
|
|
||||||
|
|
||||||
return db.update(TABLE_ALERTS, values, KEY_MESSAGE + " = ?",
|
|
||||||
new String[] { String.valueOf(alert.getMessage()) });
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user