change the dbhelper constructor

This commit is contained in:
tibbi 2017-02-11 11:38:18 +01:00
parent 7e4429df6b
commit 1c80ffee7b
11 changed files with 26 additions and 23 deletions

View File

@ -43,7 +43,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
mDialogTheme = getAppropriateTheme()
val eventId = intent.getIntExtra(EVENT_ID, 0)
val event = DBHelper(applicationContext).getEvent(eventId)
val event = DBHelper.newInstance(applicationContext).getEvent(eventId)
if (event != null) {
mEvent = event
setupEditEvent()
@ -253,7 +253,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
private fun deleteEvent() {
ConfirmationDialog(this) {
DBHelper(applicationContext, this).deleteEvents(arrayOf(mEvent.id.toString()))
DBHelper.newInstance(applicationContext, this).deleteEvents(arrayOf(mEvent.id.toString()))
finish()
}
}
@ -275,7 +275,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
}
val reminders = sortedSetOf(mReminder1Minutes, mReminder2Minutes, mReminder3Minutes).filter { it != REMINDER_OFF }
val dbHelper = DBHelper(applicationContext, this)
val dbHelper = DBHelper.newInstance(applicationContext, this)
val newDescription = event_description.value
mEvent.apply {
startTS = newStartTS

View File

@ -107,7 +107,7 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV
override fun onDataSetChanged() {
val fromTS = DateTime().seconds()
val toTS = DateTime().plusYears(1).seconds()
DBHelper(context).getEventsInBackground(fromTS, toTS, object : DBHelper.GetEventsListener {
DBHelper.newInstance(context).getEventsInBackground(fromTS, toTS, object : DBHelper.GetEventsListener {
override fun gotEvents(events: MutableList<Event>) {
val listItems = ArrayList<ListItem>(events.size)
val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description }))

View File

@ -114,7 +114,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
fun checkEvents() {
val startTS = Formatter.getDayStartTS(mDayCode)
val endTS = Formatter.getDayEndTS(mDayCode)
DBHelper(context, this).getEvents(startTS, endTS, this)
DBHelper.newInstance(context, this).getEvents(startTS, endTS, this)
}
private fun updateEvents(events: MutableList<Event>) {
@ -139,7 +139,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
override fun deleteEvents(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
DBHelper(activity.applicationContext, this).deleteEvents(eventIDs)
DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs)
}
override fun eventInserted(event: Event) {

View File

@ -47,7 +47,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event
private fun checkEvents() {
val fromTS = DateTime().seconds()
val toTS = DateTime().plusYears(1).seconds()
DBHelper(context).getEvents(fromTS, toTS, this)
DBHelper.newInstance(context).getEvents(fromTS, toTS, this)
}
override fun gotEvents(events: MutableList<Event>) {
@ -101,7 +101,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event
override fun deleteEvents(ids: ArrayList<Int>) {
val eventIDs = Array(ids.size, { i -> (ids[i].toString()) })
DBHelper(activity.applicationContext, this).deleteEvents(eventIDs)
DBHelper.newInstance(activity.applicationContext, this).deleteEvents(eventIDs)
}
override fun eventInserted(event: Event) {

View File

@ -16,7 +16,7 @@ import com.simplemobiletools.commons.extensions.getStringValue
import org.joda.time.DateTime
import java.util.*
class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
private val MAIN_TABLE_NAME = "events"
private val COL_ID = "id"
private val COL_START_TS = "start_ts"
@ -43,18 +43,16 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
private val COL_TYPE_TITLE = "event_type_title"
private val COL_TYPE_COLOR = "event_type_color"
private var mEventsListener: EventUpdateListener? = null
private var context: Context? = null
companion object {
private val DB_NAME = "events.db"
private val DB_VERSION = 7
lateinit private var mDb: SQLiteDatabase
}
private var mEventsListener: EventUpdateListener? = null
constructor(context: Context, callback: EventUpdateListener?) : this(context) {
mEventsListener = callback
this.context = context
fun newInstance(context: Context, callback: EventUpdateListener? = null): DBHelper {
mEventsListener = callback
return DBHelper(context)
}
}
init {
@ -105,7 +103,12 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
}
private fun createTypesTable(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $TYPES_TABLE_NAME ($COL_TYPE_ID INTEGER PRIMARY KEY, $COL_TYPE_TITLE TEXT, $COL_TYPE_COLOR INTEGER)" )
db.execSQL("CREATE TABLE $TYPES_TABLE_NAME ($COL_TYPE_ID INTEGER PRIMARY KEY, $COL_TYPE_TITLE TEXT, $COL_TYPE_COLOR INTEGER)")
addRegularEventType(db)
}
private fun addRegularEventType(db: SQLiteDatabase) {
}
fun insert(event: Event, insertListener: (event: Event) -> Unit) {

View File

@ -34,7 +34,7 @@ class IcsParser {
fun parseIcs(context: Context, reminderMinutes: Int, path: String): ImportResult {
try {
val dbHelper = DBHelper(context)
val dbHelper = DBHelper.newInstance(context)
val importIDs = dbHelper.getImportIds()
File(path).inputStream().bufferedReader().use {

View File

@ -26,7 +26,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
mTargetDate = targetDate
val startTS = mTargetDate.minusMonths(1).seconds()
val endTS = mTargetDate.plusMonths(1).seconds()
DBHelper(mContext).getEvents(startTS, endTS, this)
DBHelper.newInstance(mContext).getEvents(startTS, endTS, this)
}
fun getPrevMonth() {

View File

@ -15,7 +15,7 @@ class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) :
fun updateWeeklyCalendar(weekStartTS: Int) {
val startTS = weekStartTS
val endTS = startTS + WEEK_SECONDS
DBHelper(mContext).getEvents(startTS, endTS, this)
DBHelper.newInstance(mContext).getEvents(startTS, endTS, this)
}
override fun gotEvents(events: MutableList<Event>) {

View File

@ -14,7 +14,7 @@ class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val
val startDateTime = DateTime().withTime(0, 0, 0, 0).withDate(year, 1, 1)
val startTS = startDateTime.seconds()
val endTS = startDateTime.plusYears(1).minusSeconds(1).seconds()
DBHelper(context).getEvents(startTS, endTS, this)
DBHelper.newInstance(context).getEvents(startTS, endTS, this)
}
override fun gotEvents(events: MutableList<Event>) {

View File

@ -9,7 +9,7 @@ import com.simplemobiletools.calendar.helpers.DBHelper
class BootCompletedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, arg1: Intent) {
val events = DBHelper(context).getEventsAtReboot()
val events = DBHelper.newInstance(context).getEventsAtReboot()
for (event in events) {
context.scheduleNextEventReminder(event)
}

View File

@ -25,7 +25,7 @@ class NotificationReceiver : BroadcastReceiver() {
if (id == -1)
return
val event = DBHelper(context).getEvent(id)
val event = DBHelper.newInstance(context).getEvent(id)
if (event == null || event.getReminders().isEmpty())
return