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 { dependencies {
implementation 'com.simplemobiletools:commons:5.5.4' implementation 'com.simplemobiletools:commons:5.5.5'
implementation 'joda-time:joda-time:2.10.1' implementation 'joda-time:joda-time:2.10.1'
implementation 'androidx.multidex:multidex:2.0.0' implementation 'androidx.multidex:multidex:2.0.0'

View File

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

View File

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

View File

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

View File

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

View File

@ -3,7 +3,6 @@ package com.simplemobiletools.calendar.pro.helpers
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import androidx.collection.LongSparseArray import androidx.collection.LongSparseArray
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.* import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventType import com.simplemobiletools.calendar.pro.models.EventType
@ -70,7 +69,7 @@ class EventsHelper(val context: Context) {
eventTypesDB.deleteEventTypes(typesToDelete) 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) { if (event.startTS > event.endTS) {
callback?.invoke(0) callback?.invoke(0)
return return
@ -79,7 +78,7 @@ class EventsHelper(val context: Context) {
event.id = eventsDB.insertOrUpdate(event) event.id = eventsDB.insertOrUpdate(event)
context.updateWidgets() context.updateWidgets()
context.scheduleNextEventReminder(event, activity) context.scheduleNextEventReminder(event, showToasts)
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && config.caldavSync) { if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && config.caldavSync) {
context.calDAVHelper.insertCalDAVEvent(event) context.calDAVHelper.insertCalDAVEvent(event)
@ -97,7 +96,7 @@ class EventsHelper(val context: Context) {
event.id = eventsDB.insertOrUpdate(event) 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) { if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && event.source != SOURCE_IMPORTED_ICS && config.caldavSync) {
context.calDAVHelper.insertCalDAVEvent(event) 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) eventsDB.insertOrUpdate(event)
context.updateWidgets() context.updateWidgets()
context.scheduleNextEventReminder(event, activity) context.scheduleNextEventReminder(event, showToasts)
if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && config.caldavSync) { if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && config.caldavSync) {
context.calDAVHelper.updateCalDAVEvent(event) context.calDAVHelper.updateCalDAVEvent(event)
} }
@ -201,7 +200,7 @@ class EventsHelper(val context: Context) {
repetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS)) repetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS))
repetitionExceptions = repetitionExceptions.distinct().toMutableList() as ArrayList<String> repetitionExceptions = repetitionExceptions.distinct().toMutableList() as ArrayList<String>
eventsDB.updateEventRepetitionExceptions(repetitionExceptions, parentEventId) eventsDB.updateEventRepetitionExceptions(repetitionExceptions, parentEventId)
context.scheduleNextEventReminder(parentEvent) context.scheduleNextEventReminder(parentEvent, false)
if (addToCalDAV && config.caldavSync) { if (addToCalDAV && config.caldavSync) {
context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS) 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 an event belongs to a sequence insert it immediately, to avoid some glitches with linked events
if (isSequence) { if (isSequence) {
if (curRecurrenceDayCode.isEmpty()) { if (curRecurrenceDayCode.isEmpty()) {
eventsHelper.insertEvent(null, event, true) eventsHelper.insertEvent(event, true, false)
} else { } else {
// if an event contains the RECURRENCE-ID field, it is an exception to a recurring event, so update its parent too // 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) val parentEvent = activity.eventsDB.getEventWithImportId(event.importId)
if (parentEvent != null && !parentEvent.repetitionExceptions.contains(curRecurrenceDayCode)) { if (parentEvent != null && !parentEvent.repetitionExceptions.contains(curRecurrenceDayCode)) {
parentEvent.addRepetitionException(curRecurrenceDayCode) parentEvent.addRepetitionException(curRecurrenceDayCode)
eventsHelper.insertEvent(null, parentEvent, true) eventsHelper.insertEvent(parentEvent, true, false)
event.parentId = parentEvent.id!! event.parentId = parentEvent.id!!
eventsToInsert.add(event) eventsToInsert.add(event)
@ -185,7 +185,7 @@ class IcsImporter(val activity: SimpleActivity) {
} }
} else { } else {
event.id = eventToUpdate.id event.id = eventToUpdate.id
eventsHelper.updateEvent(null, event, true) eventsHelper.updateEvent(event, true, false)
} }
eventsImported++ eventsImported++
resetValues() resetValues()

View File

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