require Context at EventsHelper constructor

This commit is contained in:
tibbi 2018-11-14 19:08:23 +01:00
parent 8cbbc2effe
commit ec40b5954c
17 changed files with 60 additions and 60 deletions

View File

@ -648,7 +648,7 @@ class EventActivity : SimpleActivity() {
event_caldav_calendar_email.text = currentCalendar.accountName
Thread {
val calendarColor = EventsHelper().getEventTypeWithCalDAVCalendarId(applicationContext, currentCalendar.id)?.color
val calendarColor = EventsHelper(applicationContext).getEventTypeWithCalDAVCalendarId(currentCalendar.id)?.color
?: currentCalendar.color
runOnUiThread {
@ -732,7 +732,7 @@ class EventActivity : SimpleActivity() {
val newEventType = if (!config.caldavSync || config.lastUsedCaldavCalendarId == 0 || mEventCalendarId == STORED_LOCALLY_ONLY) {
mEventTypeId
} else {
EventsHelper().getEventTypeWithCalDAVCalendarId(applicationContext, mEventCalendarId)?.id ?: config.lastUsedLocalEventTypeId
EventsHelper(applicationContext).getEventTypeWithCalDAVCalendarId(mEventCalendarId)?.id ?: config.lastUsedLocalEventTypeId
}
val newSource = if (!config.caldavSync || mEventCalendarId == STORED_LOCALLY_ONLY) {
@ -785,7 +785,7 @@ class EventActivity : SimpleActivity() {
private fun storeEvent(wasRepeatable: Boolean) {
if (mEvent.id == 0L || mEvent.id == null) {
EventsHelper().insertEvent(applicationContext, this, mEvent, true) {
EventsHelper(applicationContext).insertEvent(this, mEvent, true) {
if (DateTime.now().isAfter(mEventStartDateTime.millis)) {
if (mEvent.repeatInterval == 0 && mEvent.getReminders().isNotEmpty()) {
notifyEvent(mEvent)
@ -800,7 +800,7 @@ class EventActivity : SimpleActivity() {
showEditRepeatingEventDialog()
}
} else {
EventsHelper().updateEvent(applicationContext, this, mEvent, true) {
EventsHelper(applicationContext).updateEvent(this, mEvent, true) {
finish()
}
}
@ -811,7 +811,7 @@ class EventActivity : SimpleActivity() {
EditRepeatingEventDialog(this) {
if (it) {
Thread {
EventsHelper().updateEvent(applicationContext, this, mEvent, true) {
EventsHelper(applicationContext).updateEvent(this, mEvent, true) {
finish()
}
}.start()
@ -826,7 +826,7 @@ class EventActivity : SimpleActivity() {
repeatLimit = 0
}
EventsHelper().insertEvent(applicationContext, this, mEvent, true) {
EventsHelper(applicationContext).insertEvent(this, mEvent, true) {
finish()
}
}.start()

View File

@ -112,7 +112,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
updateViewPager()
}
EventsHelper().getEventTypes(this) {
EventsHelper(applicationContext).getEventTypes(this) {
val newShouldFilterBeVisible = it.size > 1 || config.displayEventTypes.isEmpty()
if (newShouldFilterBeVisible != mShouldFilterBeVisible) {
mShouldFilterBeVisible = newShouldFilterBeVisible
@ -402,10 +402,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
toast(R.string.importing)
Thread {
val holidays = getString(R.string.holidays)
var eventTypeId = EventsHelper().getEventTypeIdWithTitle(applicationContext, holidays)
var eventTypeId = EventsHelper(applicationContext).getEventTypeIdWithTitle(holidays)
if (eventTypeId == -1L) {
val eventType = EventType(null, holidays, resources.getColor(R.color.default_holidays_color))
eventTypeId = EventsHelper().insertOrUpdateEventTypeSync(applicationContext, eventType)
eventTypeId = EventsHelper(applicationContext).insertOrUpdateEventTypeSync(eventType)
}
val result = IcsImporter(this).importEvents(it as String, eventTypeId, 0, false)
@ -505,7 +505,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
eventType = eventTypeId, source = source, lastUpdated = lastUpdated)
if (!importIDs.contains(contactId)) {
EventsHelper().insertEvent(applicationContext, null, event, false) {
EventsHelper(applicationContext).insertEvent(null, event, false) {
eventsAdded++
}
}
@ -528,20 +528,20 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private fun getBirthdaysEventTypeId(): Long {
val birthdays = getString(R.string.birthdays)
var eventTypeId = EventsHelper().getEventTypeIdWithTitle(applicationContext, birthdays)
var eventTypeId = EventsHelper(applicationContext).getEventTypeIdWithTitle(birthdays)
if (eventTypeId == -1L) {
val eventType = EventType(null, birthdays, resources.getColor(R.color.default_birthdays_color))
eventTypeId = EventsHelper().insertOrUpdateEventTypeSync(applicationContext, eventType)
eventTypeId = EventsHelper(applicationContext).insertOrUpdateEventTypeSync(eventType)
}
return eventTypeId
}
private fun getAnniversariesEventTypeId(): Long {
val anniversaries = getString(R.string.anniversaries)
var eventTypeId = EventsHelper().getEventTypeIdWithTitle(applicationContext, anniversaries)
var eventTypeId = EventsHelper(applicationContext).getEventTypeIdWithTitle(anniversaries)
if (eventTypeId == -1L) {
val eventType = EventType(null, anniversaries, resources.getColor(R.color.default_anniversaries_color))
eventTypeId = EventsHelper().insertOrUpdateEventTypeSync(applicationContext, eventType)
eventTypeId = EventsHelper(applicationContext).insertOrUpdateEventTypeSync(eventType)
}
return eventTypeId
}

View File

@ -30,7 +30,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener {
}
private fun getEventTypes() {
EventsHelper().getEventTypes(this) {
EventsHelper(applicationContext).getEventTypes(this) {
val adapter = ManageEventTypesAdapter(this, it, this, manage_event_types_list) {
showEventTypeDialog(it as EventType)
}
@ -60,7 +60,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener {
}
Thread {
EventsHelper().deleteEventTypes(applicationContext, eventTypes, deleteEvents)
EventsHelper(applicationContext).deleteEventTypes(eventTypes, deleteEvents)
}.start()
return true
}

View File

@ -89,11 +89,11 @@ class SettingsActivity : SimpleActivity() {
private fun checkPrimaryColor() {
if (config.primaryColor != mStoredPrimaryColor) {
Thread {
val eventTypes = EventsHelper().getEventTypesSync(this)
val eventTypes = EventsHelper(applicationContext).getEventTypesSync()
if (eventTypes.filter { it.caldavCalendarId == 0 }.size == 1) {
val eventType = eventTypes.first { it.caldavCalendarId == 0 }
eventType.color = config.primaryColor
EventsHelper().insertOrUpdateEventTypeSync(applicationContext, eventType)
EventsHelper(applicationContext).insertOrUpdateEventTypeSync(eventType)
}
}.start()
}
@ -208,13 +208,13 @@ class SettingsActivity : SimpleActivity() {
Thread {
if (newCalendarIds.isNotEmpty()) {
val existingEventTypeNames = EventsHelper().getEventTypesSync(applicationContext).map { it.getDisplayTitle().toLowerCase() } as ArrayList<String>
val existingEventTypeNames = EventsHelper(applicationContext).getEventTypesSync().map { it.getDisplayTitle().toLowerCase() } as ArrayList<String>
getSyncedCalDAVCalendars().forEach {
val calendarTitle = it.getFullTitle()
if (!existingEventTypeNames.contains(calendarTitle.toLowerCase())) {
val eventType = EventType(null, it.displayName, it.color, it.id, it.displayName, it.accountName)
existingEventTypeNames.add(calendarTitle.toLowerCase())
EventsHelper().insertOrUpdateEventType(this, eventType)
EventsHelper(applicationContext).insertOrUpdateEventType(this, eventType)
}
}
CalDAVHandler(applicationContext).refreshCalendars(this) {}
@ -223,8 +223,8 @@ class SettingsActivity : SimpleActivity() {
val removedCalendarIds = oldCalendarIds.filter { !newCalendarIds.contains(it) }
removedCalendarIds.forEach {
CalDAVHandler(applicationContext).deleteCalDAVCalendarEvents(it.toLong())
EventsHelper().getEventTypeWithCalDAVCalendarId(applicationContext, it)?.apply {
EventsHelper().deleteEventTypes(applicationContext, arrayListOf(this), true)
EventsHelper(applicationContext).getEventTypeWithCalDAVCalendarId(it)?.apply {
EventsHelper(applicationContext).deleteEventTypes(arrayListOf(this), true)
}
}

View File

@ -58,7 +58,7 @@ class EditEventTypeDialog(val activity: Activity, var eventType: EventType? = nu
}
private fun eventTypeConfirmed(title: String, dialog: AlertDialog) {
val eventIdWithTitle = EventsHelper().getEventTypeIdWithTitle(activity, title)
val eventIdWithTitle = EventsHelper(activity).getEventTypeIdWithTitle(title)
var isEventTypeTitleTaken = isNewEvent && eventIdWithTitle != -1L
if (!isEventTypeTitleTaken) {
isEventTypeTitleTaken = !isNewEvent && eventType!!.id != eventIdWithTitle && eventIdWithTitle != -1L
@ -77,7 +77,7 @@ class EditEventTypeDialog(val activity: Activity, var eventType: EventType? = nu
eventType!!.caldavDisplayName = title
}
eventType!!.id = EventsHelper().insertOrUpdateEventTypeSync(activity, eventType!!)
eventType!!.id = EventsHelper(activity).insertOrUpdateEventTypeSync(eventType!!)
if (eventType!!.id != -1L) {
activity.runOnUiThread {

View File

@ -19,7 +19,7 @@ class ExportEventsDialog(val activity: SimpleActivity, val path: String, val cal
export_events_folder.text = activity.humanizePath(path)
export_events_filename.setText("${activity.getString(R.string.events)}_${activity.getCurrentFormattedDateTime()}")
EventsHelper().getEventTypes(activity) {
EventsHelper(activity).getEventTypes(activity) {
val eventTypes = HashSet<String>()
it.mapTo(eventTypes) { it.id.toString() }

View File

@ -14,7 +14,7 @@ class FilterEventTypesDialog(val activity: SimpleActivity, val callback: () -> U
private val view = activity.layoutInflater.inflate(R.layout.dialog_filter_event_types, null)
init {
EventsHelper().getEventTypes(activity) {
EventsHelper(activity).getEventTypes(activity) {
val displayEventTypes = activity.config.displayEventTypes
view.filter_event_types_list.adapter = FilterEventTypeAdapter(activity, it, displayEventTypes)

View File

@ -34,7 +34,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal
private fun initDialog() {
val isLastCaldavCalendarOK = config.caldavSync && config.getSyncedCalendarIdsAsList().contains(config.lastUsedCaldavCalendarId)
currEventTypeId = if (isLastCaldavCalendarOK) {
val lastUsedCalDAVCalendar = EventsHelper().getEventTypeWithCalDAVCalendarId(activity, config.lastUsedCaldavCalendarId)
val lastUsedCalDAVCalendar = EventsHelper(activity).getEventTypeWithCalDAVCalendarId(config.lastUsedCaldavCalendarId)
if (lastUsedCalDAVCalendar != null) {
currEventTypeCalDAVCalendarId = config.lastUsedCaldavCalendarId
lastUsedCalDAVCalendar.id!!

View File

@ -28,7 +28,7 @@ class SelectEventCalendarDialog(val activity: Activity, val calendars: List<CalD
Thread {
calendars.forEach {
val localEventType = EventsHelper().getEventTypeWithCalDAVCalendarId(activity, it.id)
val localEventType = EventsHelper(activity).getEventTypeWithCalDAVCalendarId(it.id)
if (localEventType != null) {
it.color = localEventType.color
}

View File

@ -31,7 +31,7 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Long, val
val view = activity.layoutInflater.inflate(R.layout.dialog_select_radio_group, null) as ViewGroup
radioGroup = view.dialog_radio_group
EventsHelper().getEventTypes(activity) {
EventsHelper(activity).getEventTypes(activity) {
eventTypes = it
activity.runOnUiThread {
eventTypes.filter { showCalDAVCalendars || it.caldavCalendarId == 0 }.forEach {

View File

@ -66,7 +66,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
EventsHelper().getEventTypes(activity!!) {
EventsHelper(activity!!).getEventTypes(activity!!) {
it.map { eventTypeColors.put(it.id!!, it.color) }
}

View File

@ -26,12 +26,12 @@ class CalDAVHandler(val context: Context) {
fun refreshCalendars(activity: SimpleActivity? = null, callback: () -> Unit) {
val calDAVCalendars = getCalDAVCalendars(activity, context.config.caldavSyncedCalendarIDs)
for (calendar in calDAVCalendars) {
val localEventType = EventsHelper().getEventTypeWithCalDAVCalendarId(context, calendar.id) ?: continue
val localEventType = EventsHelper(context).getEventTypeWithCalDAVCalendarId(calendar.id) ?: continue
localEventType.apply {
title = calendar.displayName
caldavDisplayName = calendar.displayName
caldavEmail = calendar.accountName
EventsHelper().insertOrUpdateEventTypeSync(context, this)
EventsHelper(context).insertOrUpdateEventTypeSync(this)
}
CalDAVHandler(context).fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, activity)
@ -226,7 +226,7 @@ class CalDAVHandler(val context: Context) {
if (existingEvent.hashCode() != event.hashCode() && title.isNotEmpty()) {
event.id = originalEventId
EventsHelper().updateEvent(context, null, event, false)
EventsHelper(context).updateEvent(null, event, false)
}
} else {
// if the event is an exception from another events repeat rule, find the original parent event
@ -241,7 +241,7 @@ class CalDAVHandler(val context: Context) {
if (title.isNotEmpty()) {
importIdsMap[event.importId] = event
EventsHelper().insertEvent(context, null, event, false)
EventsHelper(context).insertEvent(null, event, false)
}
}
} while (cursor.moveToNext())

View File

@ -72,7 +72,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
}
EventsHelper().insertEvent(context, null, childEvent, false) {
EventsHelper(context).insertEvent(null, childEvent, false) {
val childEventId = it
val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
callback(eventRepetitionException)
@ -302,7 +302,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
events = events
.asSequence()
.distinct()
.filterNot { EventsHelper().getEventRepetitionIgnoredOccurrences(context, it).contains(Formatter.getDayCodeFromTS(it.startTS)) }
.filterNot { EventsHelper(context).getEventRepetitionIgnoredOccurrences(it).contains(Formatter.getDayCodeFromTS(it.startTS)) }
.toMutableList() as ArrayList<Event>
callback(events)
}

View File

@ -8,7 +8,7 @@ import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventType
import java.util.*
class EventsHelper {
class EventsHelper(val context: Context) {
fun getEventTypes(activity: Activity, callback: (notes: ArrayList<EventType>) -> Unit) {
Thread {
val eventTypes = activity.eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
@ -18,18 +18,18 @@ class EventsHelper {
}.start()
}
fun getEventTypesSync(context: Context) = context.eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
fun getEventTypesSync() = context.eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
fun insertOrUpdateEventType(activity: Activity, eventType: EventType, callback: ((newEventTypeId: Long) -> Unit)? = null) {
Thread {
val eventTypeId = insertOrUpdateEventTypeSync(activity, eventType)
val eventTypeId = insertOrUpdateEventTypeSync(eventType)
activity.runOnUiThread {
callback?.invoke(eventTypeId)
}
}.start()
}
fun insertOrUpdateEventTypeSync(context: Context, eventType: EventType): Long {
fun insertOrUpdateEventTypeSync(eventType: EventType): Long {
if (eventType.id != null && eventType.id!! > 0 && eventType.caldavCalendarId != 0) {
CalDAVHandler(context).updateCalDAVCalendar(eventType)
}
@ -39,11 +39,11 @@ class EventsHelper {
return newId
}
fun getEventTypeIdWithTitle(context: Context, title: String) = context.eventTypesDB.getEventTypeIdWithTitle(title) ?: -1L
fun getEventTypeIdWithTitle(title: String) = context.eventTypesDB.getEventTypeIdWithTitle(title) ?: -1L
fun getEventTypeWithCalDAVCalendarId(context: Context, calendarId: Int) = context.eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId)
fun getEventTypeWithCalDAVCalendarId(calendarId: Int) = context.eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId)
fun deleteEventTypes(context: Context, eventTypes: ArrayList<EventType>, deleteEvents: Boolean) {
fun deleteEventTypes(eventTypes: ArrayList<EventType>, deleteEvents: Boolean) {
val typesToDelete = eventTypes.asSequence().filter { it.caldavCalendarId == 0 && it.id != DBHelper.REGULAR_EVENT_TYPE_ID }.toMutableList()
val deleteIds = typesToDelete.map { it.id }.toMutableList()
val deletedSet = deleteIds.map { it.toString() }.toHashSet()
@ -64,7 +64,7 @@ class EventsHelper {
context.eventTypesDB.deleteEventTypes(typesToDelete)
}
fun getEventRepetitionIgnoredOccurrences(context: Context, event: Event): ArrayList<String> {
fun getEventRepetitionIgnoredOccurrences(event: Event): ArrayList<String> {
return if (event.id == null || event.repeatInterval == 0) {
ArrayList()
} else {
@ -72,7 +72,7 @@ class EventsHelper {
}
}
fun insertEvent(context: Context, activity: SimpleActivity? = null, event: Event, addToCalDAV: Boolean, callback: ((id: Long) -> Unit)? = null) {
fun insertEvent(activity: SimpleActivity? = null, event: Event, addToCalDAV: Boolean, callback: ((id: Long) -> Unit)? = null) {
if (event.startTS > event.endTS) {
callback?.invoke(0)
return
@ -95,31 +95,31 @@ class EventsHelper {
callback?.invoke(event.id!!)
}
fun insertEvents(activity: Activity, events: ArrayList<Event>, addToCalDAV: Boolean) {
fun insertEvents(events: ArrayList<Event>, addToCalDAV: Boolean) {
try {
for (event in events) {
if (event.startTS > event.endTS) {
continue
}
val id = activity.eventsDB.insertOrUpdate(event)
val id = context.eventsDB.insertOrUpdate(event)
event.id = id
if (event.repeatInterval != 0 && event.parentId == 0L) {
activity.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
}
//context.scheduleNextEventReminder(event, this)
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && event.source != SOURCE_IMPORTED_ICS && activity.config.caldavSync) {
CalDAVHandler(activity).insertCalDAVEvent(event)
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && event.source != SOURCE_IMPORTED_ICS && context.config.caldavSync) {
CalDAVHandler(context).insertCalDAVEvent(event)
}
}
} finally {
activity.updateWidgets()
context.updateWidgets()
}
}
fun updateEvent(context: Context, activity: Activity? = null, event: Event, updateAtCalDAV: Boolean, callback: (() -> Unit)? = null) {
fun updateEvent(activity: Activity? = null, event: Event, updateAtCalDAV: Boolean, callback: (() -> Unit)? = null) {
context.eventsDB.insertOrUpdate(event)
if (event.repeatInterval == 0) {

View File

@ -92,7 +92,7 @@ class IcsExporter {
}
private fun fillIgnoredOccurrences(activity: BaseSimpleActivity, event: Event, out: BufferedWriter) {
EventsHelper().getEventRepetitionIgnoredOccurrences(activity, event).forEach {
EventsHelper(activity).getEventRepetitionIgnoredOccurrences(event).forEach {
out.writeLn("$EXDATE:$it")
}
}

View File

@ -41,7 +41,7 @@ class IcsImporter(val activity: SimpleActivity) {
fun importEvents(path: String, defaultEventTypeId: Long, calDAVCalendarId: Int, overrideFileEventTypes: Boolean): ImportResult {
try {
val eventTypes = EventsHelper().getEventTypesSync(activity)
val eventTypes = EventsHelper(activity).getEventTypesSync()
val existingEvents = activity.dbHelper.getEventsWithImportIds()
val eventsToInsert = ArrayList<Event>()
var prevLine = ""
@ -158,7 +158,7 @@ class IcsImporter(val activity: SimpleActivity) {
if (curRepeatExceptions.isEmpty()) {
eventsToInsert.add(event)
} else {
EventsHelper().insertEvent(activity, activity, event, true) {
EventsHelper(activity).insertEvent(activity, event, true) {
for (exceptionTS in curRepeatExceptions) {
activity.dbHelper.addEventRepeatException(it, exceptionTS, true)
}
@ -167,7 +167,7 @@ class IcsImporter(val activity: SimpleActivity) {
}
} else {
event.id = eventToUpdate.id
EventsHelper().updateEvent(activity, null, event, true)
EventsHelper(activity).updateEvent(null, event, true)
}
eventsImported++
resetValues()
@ -176,7 +176,7 @@ class IcsImporter(val activity: SimpleActivity) {
}
}
EventsHelper().insertEvents(activity, eventsToInsert, true)
EventsHelper(activity).insertEvents(eventsToInsert, true)
} catch (e: Exception) {
activity.showErrorToast(e, Toast.LENGTH_LONG)
eventsFailed++
@ -223,11 +223,11 @@ class IcsImporter(val activity: SimpleActivity) {
categories
}
val eventId = EventsHelper().getEventTypeIdWithTitle(activity, eventTypeTitle)
val eventId = EventsHelper(activity).getEventTypeIdWithTitle(eventTypeTitle)
curEventTypeId = if (eventId == -1L) {
val newTypeColor = if (curCategoryColor == -2) activity.resources.getColor(R.color.color_primary) else curCategoryColor
val eventType = EventType(null, eventTypeTitle, newTypeColor)
EventsHelper().insertOrUpdateEventTypeSync(activity, eventType)
EventsHelper(activity).insertOrUpdateEventTypeSync(eventType)
} else {
eventId
}

View File

@ -35,7 +35,7 @@ class NotificationReceiver : BroadcastReceiver() {
return
}
if (!EventsHelper().getEventRepetitionIgnoredOccurrences(context, event).contains(Formatter.getDayCodeFromTS(event.startTS))) {
if (!EventsHelper(context).getEventRepetitionIgnoredOccurrences(event).contains(Formatter.getDayCodeFromTS(event.startTS))) {
context.notifyEvent(event)
}
context.scheduleNextEventReminder(event, context.dbHelper)