avoid passing around activities on a few more places

This commit is contained in:
tibbi 2018-12-04 20:59:35 +01:00
parent 26511514d4
commit 008508041d
8 changed files with 45 additions and 41 deletions

View File

@ -52,7 +52,7 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:5.5.4'
implementation 'com.simplemobiletools:commons:5.5.5'
implementation 'joda-time:joda-time:2.10.1'
implementation 'androidx.multidex:multidex:2.0.0'

View File

@ -800,7 +800,7 @@ class EventActivity : SimpleActivity() {
private fun storeEvent(wasRepeatable: Boolean) {
if (mEvent.id == null || mEvent.id == null) {
eventsHelper.insertEvent(this, mEvent, true) {
eventsHelper.insertEvent(mEvent, true, true) {
if (DateTime.now().isAfter(mEventStartDateTime.millis)) {
if (mEvent.repeatInterval == 0 && mEvent.getReminders().isNotEmpty()) {
notifyEvent(mEvent)
@ -815,7 +815,7 @@ class EventActivity : SimpleActivity() {
showEditRepeatingEventDialog()
}
} else {
eventsHelper.updateEvent(this, mEvent, true) {
eventsHelper.updateEvent(mEvent, true, true) {
finish()
}
}
@ -826,7 +826,7 @@ class EventActivity : SimpleActivity() {
EditRepeatingEventDialog(this) {
if (it) {
Thread {
eventsHelper.updateEvent(this, mEvent, true) {
eventsHelper.updateEvent(mEvent, true, true) {
finish()
}
}.start()
@ -841,7 +841,7 @@ class EventActivity : SimpleActivity() {
repeatLimit = 0
}
eventsHelper.insertEvent(this, mEvent, true) {
eventsHelper.insertEvent(mEvent, true, true) {
finish()
}
}.start()

View File

@ -490,7 +490,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
eventType = eventTypeId, source = source, lastUpdated = lastUpdated)
if (!importIDs.contains(contactId)) {
eventsHelper.insertEvent(null, event, false) {
eventsHelper.insertEvent(event, false, false) {
eventsAdded++
}
}

View File

@ -22,7 +22,6 @@ import androidx.core.app.AlarmManagerCompat
import androidx.core.app.NotificationCompat
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.EventActivity
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.activities.SnoozeReminderActivity
import com.simplemobiletools.calendar.pro.databases.EventsDatabase
import com.simplemobiletools.calendar.pro.helpers.*
@ -76,13 +75,15 @@ fun Context.updateListWidget() {
fun Context.scheduleAllEvents() {
val events = eventsDB.getEventsAtReboot(getNowSeconds())
events.forEach {
scheduleNextEventReminder(it)
scheduleNextEventReminder(it, false)
}
}
fun Context.scheduleNextEventReminder(event: Event, activity: SimpleActivity? = null) {
fun Context.scheduleNextEventReminder(event: Event, showToasts: Boolean) {
if (event.getReminders().isEmpty()) {
activity?.toast(R.string.saving)
if (showToasts) {
toast(R.string.saving)
}
return
}
@ -93,28 +94,32 @@ fun Context.scheduleNextEventReminder(event: Event, activity: SimpleActivity? =
for (curEvent in it) {
for (curReminder in reminderSeconds) {
if (curEvent.getEventStartTS() - curReminder > now) {
scheduleEventIn((curEvent.getEventStartTS() - curReminder) * 1000L, curEvent, activity)
scheduleEventIn((curEvent.getEventStartTS() - curReminder) * 1000L, curEvent, showToasts)
return@getEvents
}
}
}
}
activity?.toast(R.string.saving)
if (showToasts) {
toast(R.string.saving)
}
}
}
fun Context.scheduleEventIn(notifTS: Long, event: Event, activity: SimpleActivity? = null) {
fun Context.scheduleEventIn(notifTS: Long, event: Event, showToasts: Boolean) {
if (notifTS < System.currentTimeMillis()) {
activity?.toast(R.string.saving)
if (showToasts) {
toast(R.string.saving)
}
return
}
val newNotifTS = notifTS + 1000
if (activity != null) {
if (showToasts) {
val secondsTillNotification = (newNotifTS - System.currentTimeMillis()) / 1000
val msg = String.format(getString(R.string.reminder_triggers_in), formatSecondsToTimeString(secondsTillNotification.toInt()))
activity.toast(msg)
toast(msg)
}
val pendingIntent = getNotificationIntent(applicationContext, event)
@ -281,7 +286,7 @@ private fun getSnoozePendingIntent(context: Context, event: Event): PendingInten
fun Context.rescheduleReminder(event: Event?, minutes: Int) {
if (event != null) {
applicationContext.scheduleEventIn(System.currentTimeMillis() + minutes * 60000, event)
applicationContext.scheduleEventIn(System.currentTimeMillis() + minutes * 60000, event, false)
cancelNotification(event.id!!)
}
}
@ -427,10 +432,10 @@ fun Context.handleEventDeleting(eventIds: List<Long>, timestamps: List<Long>, ac
}
}
fun Context.refreshCalDAVCalendars(ids: String, showErrorToasts: Boolean) {
fun Context.refreshCalDAVCalendars(ids: String, showToasts: Boolean) {
val uri = CalendarContract.Calendars.CONTENT_URI
val accounts = HashSet<Account>()
val calendars = calDAVHelper.getCalDAVCalendars(ids, showErrorToasts)
val calendars = calDAVHelper.getCalDAVCalendars(ids, showToasts)
calendars.forEach {
accounts.add(Account(it.accountName, it.accountType))
}

View File

@ -22,14 +22,14 @@ import kotlin.collections.ArrayList
class CalDAVHelper(val context: Context) {
private val eventsHelper = context.eventsHelper
fun refreshCalendars(showErrorToasts: Boolean, callback: () -> Unit) {
fun refreshCalendars(showToasts: Boolean, callback: () -> Unit) {
if (isUpdatingCalDAV) {
return
}
isUpdatingCalDAV = true
try {
val calDAVCalendars = getCalDAVCalendars(context.config.caldavSyncedCalendarIDs, showErrorToasts)
val calDAVCalendars = getCalDAVCalendars(context.config.caldavSyncedCalendarIDs, showToasts)
for (calendar in calDAVCalendars) {
val localEventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendar.id) ?: continue
localEventType.apply {
@ -39,7 +39,7 @@ class CalDAVHelper(val context: Context) {
eventsHelper.insertOrUpdateEventTypeSync(this)
}
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showErrorToasts)
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showToasts)
}
context.scheduleCalDAVSync(true)
callback()
@ -49,7 +49,7 @@ class CalDAVHelper(val context: Context) {
}
@SuppressLint("MissingPermission")
fun getCalDAVCalendars(ids: String, showErrorToasts: Boolean): List<CalDAVCalendar> {
fun getCalDAVCalendars(ids: String, showToasts: Boolean): List<CalDAVCalendar> {
val calendars = ArrayList<CalDAVCalendar>()
if (!context.hasPermission(PERMISSION_WRITE_CALENDAR) || !context.hasPermission(PERMISSION_READ_CALENDAR)) {
return calendars
@ -83,7 +83,7 @@ class CalDAVHelper(val context: Context) {
} while (cursor.moveToNext())
}
} catch (e: Exception) {
if (showErrorToasts) {
if (showToasts) {
context.showErrorToast(e)
}
} finally {
@ -162,7 +162,7 @@ class CalDAVHelper(val context: Context) {
}
@SuppressLint("MissingPermission")
private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Long, showErrorToasts: Boolean) {
private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Long, showToasts: Boolean) {
val importIdsMap = HashMap<String, Event>()
val fetchedEventIds = ArrayList<String>()
val existingEvents = context.eventsDB.getEventsFromCalDAVCalendar("$CALDAV-$calendarId")
@ -233,10 +233,10 @@ class CalDAVHelper(val context: Context) {
if (parentEvent != null && !parentEvent.repetitionExceptions.contains(originalDayCode)) {
event.parentId = parentEvent.id!!
parentEvent.addRepetitionException(originalDayCode)
eventsHelper.insertEvent(null, parentEvent, false)
eventsHelper.insertEvent(parentEvent, false, false)
event.parentId = parentEvent.id!!
eventsHelper.insertEvent(null, event, false)
eventsHelper.insertEvent(event, false, false)
continue
}
}
@ -254,18 +254,18 @@ class CalDAVHelper(val context: Context) {
if (existingEvent.hashCode() != event.hashCode() && title.isNotEmpty()) {
event.id = originalEventId
eventsHelper.updateEvent(null, event, false)
eventsHelper.updateEvent(event, false, false)
}
} else {
if (title.isNotEmpty()) {
importIdsMap[event.importId] = event
eventsHelper.insertEvent(null, event, false)
eventsHelper.insertEvent(event, false, false)
}
}
} while (cursor.moveToNext())
}
} catch (e: Exception) {
if (showErrorToasts) {
if (showToasts) {
context.showErrorToast(e)
}
} finally {

View File

@ -3,7 +3,6 @@ package com.simplemobiletools.calendar.pro.helpers
import android.app.Activity
import android.content.Context
import androidx.collection.LongSparseArray
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventType
@ -70,7 +69,7 @@ class EventsHelper(val context: Context) {
eventTypesDB.deleteEventTypes(typesToDelete)
}
fun insertEvent(activity: SimpleActivity? = null, event: Event, addToCalDAV: Boolean, callback: ((id: Long) -> Unit)? = null) {
fun insertEvent(event: Event, addToCalDAV: Boolean, showToasts: Boolean, callback: ((id: Long) -> Unit)? = null) {
if (event.startTS > event.endTS) {
callback?.invoke(0)
return
@ -79,7 +78,7 @@ class EventsHelper(val context: Context) {
event.id = eventsDB.insertOrUpdate(event)
context.updateWidgets()
context.scheduleNextEventReminder(event, activity)
context.scheduleNextEventReminder(event, showToasts)
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && config.caldavSync) {
context.calDAVHelper.insertCalDAVEvent(event)
@ -97,7 +96,7 @@ class EventsHelper(val context: Context) {
event.id = eventsDB.insertOrUpdate(event)
context.scheduleNextEventReminder(event)
context.scheduleNextEventReminder(event, false)
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && event.source != SOURCE_IMPORTED_ICS && config.caldavSync) {
context.calDAVHelper.insertCalDAVEvent(event)
}
@ -107,11 +106,11 @@ class EventsHelper(val context: Context) {
}
}
fun updateEvent(activity: SimpleActivity? = null, event: Event, updateAtCalDAV: Boolean, callback: (() -> Unit)? = null) {
fun updateEvent(event: Event, updateAtCalDAV: Boolean, showToasts: Boolean, callback: (() -> Unit)? = null) {
eventsDB.insertOrUpdate(event)
context.updateWidgets()
context.scheduleNextEventReminder(event, activity)
context.scheduleNextEventReminder(event, showToasts)
if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && config.caldavSync) {
context.calDAVHelper.updateCalDAVEvent(event)
}
@ -201,7 +200,7 @@ class EventsHelper(val context: Context) {
repetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS))
repetitionExceptions = repetitionExceptions.distinct().toMutableList() as ArrayList<String>
eventsDB.updateEventRepetitionExceptions(repetitionExceptions, parentEventId)
context.scheduleNextEventReminder(parentEvent)
context.scheduleNextEventReminder(parentEvent, false)
if (addToCalDAV && config.caldavSync) {
context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS)

View File

@ -168,13 +168,13 @@ class IcsImporter(val activity: SimpleActivity) {
// if an event belongs to a sequence insert it immediately, to avoid some glitches with linked events
if (isSequence) {
if (curRecurrenceDayCode.isEmpty()) {
eventsHelper.insertEvent(null, event, true)
eventsHelper.insertEvent(event, true, false)
} else {
// if an event contains the RECURRENCE-ID field, it is an exception to a recurring event, so update its parent too
val parentEvent = activity.eventsDB.getEventWithImportId(event.importId)
if (parentEvent != null && !parentEvent.repetitionExceptions.contains(curRecurrenceDayCode)) {
parentEvent.addRepetitionException(curRecurrenceDayCode)
eventsHelper.insertEvent(null, parentEvent, true)
eventsHelper.insertEvent(parentEvent, true, false)
event.parentId = parentEvent.id!!
eventsToInsert.add(event)
@ -185,7 +185,7 @@ class IcsImporter(val activity: SimpleActivity) {
}
} else {
event.id = eventToUpdate.id
eventsHelper.updateEvent(null, event, true)
eventsHelper.updateEvent(event, true, false)
}
eventsImported++
resetValues()

View File

@ -37,6 +37,6 @@ class NotificationReceiver : BroadcastReceiver() {
if (!event.repetitionExceptions.contains(Formatter.getDayCodeFromTS(event.startTS))) {
context.notifyEvent(event)
}
context.scheduleNextEventReminder(event)
context.scheduleNextEventReminder(event, false)
}
}