use the helper function for creating the database handler

This commit is contained in:
tibbi 2017-04-02 11:50:16 +02:00
parent 5d690b8cc5
commit 4e781cf8d7
18 changed files with 45 additions and 47 deletions

View File

@ -10,9 +10,9 @@ import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.MyDayPagerAdapter
import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.getNewEventTimestampFromCode
import com.simplemobiletools.calendar.helpers.DAY_CODE
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.NEW_EVENT_START_TS
import com.simplemobiletools.calendar.interfaces.NavigationListener
@ -42,7 +42,7 @@ class DayActivity : SimpleActivity(), NavigationListener, ViewPager.OnPageChange
day_fab.setOnClickListener { addNewEvent() }
updateTextColors(day_coordinator)
DBHelper.newInstance(applicationContext).getEventTypes {
dbHelper.getEventTypes {
eventTypeColors.clear()
it.map { eventTypeColors.put(it.id, it.color) }
invalidateOptionsMenu()

View File

@ -43,7 +43,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
mDialogTheme = getAppropriateTheme()
val eventId = intent.getIntExtra(EVENT_ID, 0)
val event = DBHelper.newInstance(applicationContext).getEvent(eventId)
val event = dbHelper.getEvent(eventId)
if (event != null) {
mEvent = event
mEventOccurrenceTS = intent.getIntExtra(EVENT_OCCURRENCE_TS, 0)
@ -231,7 +231,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
}
private fun updateEventType() {
val eventType = DBHelper.newInstance(applicationContext).getEventType(mEventTypeId)
val eventType = dbHelper.getEventType(mEventTypeId)
if (eventType != null) {
event_type.text = eventType.title
event_type_color.setBackgroundWithStroke(eventType.color, config.backgroundColor)
@ -260,11 +260,10 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
private fun deleteEvent() {
DeleteEventDialog(this, arrayListOf(mEvent.id)) {
val db = DBHelper.newInstance(applicationContext, null)
if (it) {
db.deleteEvents(arrayOf(mEvent.id.toString()))
dbHelper.deleteEvents(arrayOf(mEvent.id.toString()))
} else {
db.deleteEventOccurrence(mEvent.id, mEventOccurrenceTS)
dbHelper.deleteEventOccurrence(mEvent.id, mEventOccurrenceTS)
}
finish()
}

View File

@ -19,10 +19,7 @@ import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter
import com.simplemobiletools.calendar.dialogs.ChangeViewDialog
import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog
import com.simplemobiletools.calendar.dialogs.ImportEventsDialog
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.launchNewEventIntent
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.extensions.updateWidgets
import com.simplemobiletools.calendar.extensions.*
import com.simplemobiletools.calendar.fragments.EventListFragment
import com.simplemobiletools.calendar.fragments.WeekFragment
import com.simplemobiletools.calendar.helpers.*
@ -79,7 +76,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
if (mStoredTextColor != config.textColor || mStoredBackgroundColor != config.backgroundColor || mStoredPrimaryColor != config.primaryColor)
updateViewPager()
DBHelper.newInstance(applicationContext).getEventTypes {
dbHelper.getEventTypes {
eventTypeColors.clear()
it.map { eventTypeColors.put(it.id, it.color) }
mShouldFilterBeVisible = eventTypeColors.size() > 1 || config.displayEventTypes.isEmpty()

View File

@ -4,6 +4,7 @@ import android.os.Bundle
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.EventTypeAdapter
import com.simplemobiletools.calendar.dialogs.NewEventTypeDialog
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.interfaces.DeleteItemsListener
import com.simplemobiletools.calendar.models.EventType
@ -32,7 +33,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteItemsListener {
}
private fun getEventTypes() {
DBHelper.newInstance(applicationContext).getEventTypes {
dbHelper.getEventTypes {
runOnUiThread {
manage_event_types_list.adapter = EventTypeAdapter(this, it, this) {
showEventTypeDialog(it)
@ -46,7 +47,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteItemsListener {
toast(R.string.cannot_delete_default_type)
}
DBHelper.newInstance(applicationContext).deleteEventTypes(ids) {
dbHelper.deleteEventTypes(ids) {
if (it > 0) {
getEventTypes()
} else {

View File

@ -8,6 +8,7 @@ import android.widget.RemoteViewsService
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.R.id.event_item_holder
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.EVENT_ID
@ -18,7 +19,6 @@ import com.simplemobiletools.calendar.models.ListItem
import com.simplemobiletools.calendar.models.ListSection
import org.joda.time.DateTime
import java.util.*
import kotlin.comparisons.compareBy
class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsService.RemoteViewsFactory {
val ITEM_EVENT = 0
@ -98,7 +98,7 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV
override fun onDataSetChanged() {
val fromTS = DateTime().seconds()
val toTS = DateTime().plusYears(1).seconds()
DBHelper.newInstance(context).getEventsInBackground(fromTS, toTS, object : DBHelper.GetEventsListener {
context.dbHelper.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

@ -5,7 +5,7 @@ import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.ViewGroup
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_delete_event.view.*
@ -14,7 +14,7 @@ class DeleteEventDialog(val activity: Activity, eventIds: List<Int>, val callbac
val dialog: AlertDialog?
init {
val events = DBHelper.newInstance(activity).getEventsWithIds(eventIds)
val events = activity.dbHelper.getEventsWithIds(eventIds)
val hasRepeatableEvent = events.any { it.repeatInterval > 0 }
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_delete_event, null).apply {

View File

@ -6,7 +6,7 @@ import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.adapters.FilterEventTypeAdapter
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_filter_event_types.view.*
@ -18,7 +18,7 @@ class FilterEventTypesDialog(val activity: SimpleActivity, val callback: () -> U
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_filter_event_types, null)
init {
DBHelper.newInstance(activity).getEventTypes {
activity.dbHelper.getEventTypes {
val displayEventTypes = activity.config.displayEventTypes
eventTypes = it
view.filter_event_types_list.adapter = FilterEventTypeAdapter(activity, it, displayEventTypes)

View File

@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.IcsParser
import com.simplemobiletools.calendar.helpers.IcsParser.ImportResult.*
@ -47,7 +48,7 @@ class ImportEventsDialog(val activity: Activity, val path: String, val callback:
}
private fun updateEventType(view: ViewGroup) {
val eventType = DBHelper.newInstance(context).getEventType(currEventTypeId)
val eventType = context.dbHelper.getEventType(currEventTypeId)
view.import_event_type_title.text = eventType!!.title
view.import_event_type_color.setBackgroundWithStroke(eventType.color, activity.config.backgroundColor)
}

View File

@ -7,7 +7,7 @@ import android.view.WindowManager
import android.widget.ImageView
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.setBackgroundWithStroke
@ -34,7 +34,6 @@ class NewEventTypeDialog(val activity: Activity, var eventType: EventType? = nul
}
}
val db = DBHelper.newInstance(activity)
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
@ -43,7 +42,7 @@ class NewEventTypeDialog(val activity: Activity, var eventType: EventType? = nul
activity.setupDialogStuff(view, this, if (isNewEvent) R.string.add_new_type else R.string.edit_type)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val title = view.type_title.value
val eventIdWithTitle = db.getEventTypeIdWithTitle(title)
val eventIdWithTitle = activity.dbHelper.getEventTypeIdWithTitle(title)
var isEventTypeTitleTaken = isNewEvent && eventIdWithTitle != -1
if (!isEventTypeTitleTaken)
isEventTypeTitleTaken = !isNewEvent && eventType!!.id != eventIdWithTitle && eventIdWithTitle != -1
@ -60,9 +59,9 @@ class NewEventTypeDialog(val activity: Activity, var eventType: EventType? = nul
val eventTypeId: Int
if (isNewEvent) {
eventTypeId = db.insertEventType(eventType!!)
eventTypeId = activity.dbHelper.insertEventType(eventType!!)
} else {
eventTypeId = db.updateEventType(eventType!!)
eventTypeId = activity.dbHelper.updateEventType(eventType!!)
}
if (eventTypeId != -1) {

View File

@ -8,6 +8,7 @@ import android.widget.RadioButton
import android.widget.RadioGroup
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.extensions.hideKeyboard
@ -30,7 +31,7 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null) as ViewGroup
radioGroup = view.dialog_radio_group
DBHelper.newInstance(activity).getEventTypes {
activity.dbHelper.getEventTypes {
eventTypes = it
activity.runOnUiThread {
eventTypes.forEach {

View File

@ -17,6 +17,7 @@ import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.adapters.DayEventsAdapter
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.getAppropriateTheme
import com.simplemobiletools.calendar.extensions.getFilteredEvents
import com.simplemobiletools.calendar.helpers.*
@ -30,7 +31,6 @@ import kotlinx.android.synthetic.main.fragment_day.view.*
import kotlinx.android.synthetic.main.top_navigation.view.*
import org.joda.time.DateTime
import java.util.*
import kotlin.comparisons.compareBy
class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEventsListener, DeleteEventsListener {
private var mTextColor = 0
@ -142,9 +142,8 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
}
override fun deleteEventOccurrences(parentIds: ArrayList<Int>, timestamps: ArrayList<Int>) {
val db = DBHelper.newInstance(context)
parentIds.forEachIndexed { index, value ->
db.deleteEventOccurrence(parentIds[index], timestamps[index])
context.dbHelper.deleteEventOccurrence(parentIds[index], timestamps[index])
}
(activity as DayActivity).recheckEvents()
}

View File

@ -11,6 +11,7 @@ import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.adapters.EventListAdapter
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.getFilteredEvents
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.DBHelper
@ -27,7 +28,6 @@ import com.simplemobiletools.commons.extensions.beVisibleIf
import kotlinx.android.synthetic.main.fragment_event_list.view.*
import org.joda.time.DateTime
import java.util.*
import kotlin.comparisons.compareBy
class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.EventUpdateListener, DeleteEventsListener {
private var mEvents: List<Event> = ArrayList()
@ -49,7 +49,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event
private fun checkEvents() {
val fromTS = DateTime().seconds()
val toTS = DateTime().plusYears(1).seconds()
DBHelper.newInstance(context).getEvents(fromTS, toTS, this)
context.dbHelper.getEvents(fromTS, toTS, this)
}
override fun gotEvents(events: MutableList<Event>) {
@ -109,9 +109,8 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event
}
override fun deleteEventOccurrences(parentIds: ArrayList<Int>, timestamps: ArrayList<Int>) {
val db = DBHelper.newInstance(context)
parentIds.forEachIndexed { index, value ->
db.deleteEventOccurrence(parentIds[index], timestamps[index])
context.dbHelper.deleteEventOccurrence(parentIds[index], timestamps[index])
}
checkEvents()
}

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.helpers
import android.content.Context
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.IcsParser.ImportResult.*
import com.simplemobiletools.calendar.models.Event
@ -57,8 +58,7 @@ class IcsParser {
fun parseIcs(context: Context, path: String, defaultEventType: Int): ImportResult {
try {
val dbHelper = DBHelper.newInstance(context)
val importIDs = dbHelper.getImportIds()
val importIDs = context.dbHelper.getImportIds()
var prevLine = ""
File(path).inputStream().bufferedReader().use {
@ -109,7 +109,7 @@ class IcsParser {
importIDs.add(curImportId)
val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes, -1, -1, curRepeatInterval,
curImportId, curFlags, curRepeatLimit, curEventType)
dbHelper.insert(event) { }
context.dbHelper.insert(event) { }
eventsImported++
resetValues()
}
@ -161,11 +161,10 @@ class IcsParser {
categories
}
val dbHelper = DBHelper.newInstance(context)
val eventId = dbHelper.getEventTypeIdWithTitle(eventTypeTitle)
val eventId = context.dbHelper.getEventTypeIdWithTitle(eventTypeTitle)
if (eventId == -1) {
val eventType = EventType(0, eventTypeTitle, context.resources.getColor(R.color.color_primary))
curEventType = dbHelper.insertEventType(eventType)
curEventType = context.dbHelper.insertEventType(eventType)
} else {
curEventType = eventId
}

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.helpers
import android.content.Context
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.getFilteredEvents
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
@ -29,7 +30,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
mTargetDate = targetDate
val startTS = mTargetDate.minusMonths(1).seconds()
val endTS = mTargetDate.plusMonths(1).seconds()
DBHelper.newInstance(mContext).getEvents(startTS, endTS, this)
mContext.dbHelper.getEvents(startTS, endTS, this)
}
fun getPrevMonth() {

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.calendar.helpers
import android.content.Context
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.interfaces.WeeklyCalendar
import com.simplemobiletools.calendar.models.Event
import java.util.*
@ -15,7 +16,7 @@ class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) :
fun updateWeeklyCalendar(weekStartTS: Int) {
val startTS = weekStartTS
val endTS = startTS + WEEK_SECONDS
DBHelper.newInstance(mContext).getEvents(startTS, endTS, this)
mContext.dbHelper.getEvents(startTS, endTS, this)
}
override fun gotEvents(events: MutableList<Event>) {

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.helpers
import android.content.Context
import android.util.SparseArray
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.getFilteredEvents
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.interfaces.YearlyCalendar
@ -15,7 +16,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.newInstance(context).getEvents(startTS, endTS, this)
context.dbHelper.getEvents(startTS, endTS, this)
}
override fun gotEvents(events: MutableList<Event>) {

View File

@ -3,13 +3,13 @@ package com.simplemobiletools.calendar.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.scheduleNextEventReminder
import com.simplemobiletools.calendar.helpers.DBHelper
class BootCompletedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, arg1: Intent) {
val events = DBHelper.newInstance(context).getEventsAtReboot()
val events = context.dbHelper.getEventsAtReboot()
for (event in events) {
context.scheduleNextEventReminder(event)
}

View File

@ -10,9 +10,9 @@ import android.net.Uri
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.scheduleNextEventReminder
import com.simplemobiletools.calendar.extensions.updateListWidget
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.EVENT_ID
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.models.Event
@ -25,7 +25,7 @@ class NotificationReceiver : BroadcastReceiver() {
if (id == -1)
return
val event = DBHelper.newInstance(context).getEvent(id)
val event = context.dbHelper.getEvent(id)
if (event == null || event.getReminders().isEmpty())
return