avoid passing activities around at the CalDAVHelper

This commit is contained in:
tibbi 2018-12-04 20:43:29 +01:00
parent 06fa70dc16
commit 26511514d4
7 changed files with 25 additions and 22 deletions

View File

@ -605,7 +605,7 @@ class EventActivity : SimpleActivity() {
event_caldav_calendar_holder.beVisible()
event_caldav_calendar_divider.beVisible()
val calendars = calDAVHelper.getCalDAVCalendars(this).filter {
val calendars = calDAVHelper.getCalDAVCalendars("", true).filter {
config.getSyncedCalendarIdsAsList().contains(it.id)
}
updateCurrentCalendarInfo(if (mEventCalendarId == STORED_LOCALLY_ONLY) null else getCalendarWithId(calendars, getCalendarId()))
@ -738,7 +738,7 @@ class EventActivity : SimpleActivity() {
val newEventType = if (!config.caldavSync || config.lastUsedCaldavCalendarId == 0 || mEventCalendarId == STORED_LOCALLY_ONLY) {
mEventTypeId
} else {
calDAVHelper.getCalDAVCalendars(this).firstOrNull { it.id == mEventCalendarId }?.apply {
calDAVHelper.getCalDAVCalendars("", true).firstOrNull { it.id == mEventCalendarId }?.apply {
if (!canWrite()) {
runOnUiThread {
toast(R.string.insufficient_permissions)

View File

@ -364,8 +364,8 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
toast(R.string.refreshing)
}
syncCalDAVCalendars(this) {
calDAVHelper.refreshCalendars(this) {
syncCalDAVCalendars {
calDAVHelper.refreshCalendars(true) {
calDAVChanged()
}
}

View File

@ -218,8 +218,8 @@ class SettingsActivity : SimpleActivity() {
}
}
syncCalDAVCalendars(this) {
calDAVHelper.refreshCalendars(this) {
syncCalDAVCalendars {
calDAVHelper.refreshCalendars(true) {
if (settings_caldav_sync.isChecked) {
toast(R.string.synchronization_completed)
}

View File

@ -38,13 +38,13 @@ open class SimpleActivity : BaseSimpleActivity() {
override fun getAppLauncherName() = getString(R.string.app_launcher_name)
fun Context.syncCalDAVCalendars(activity: SimpleActivity?, callback: () -> Unit) {
fun Context.syncCalDAVCalendars(callback: () -> Unit) {
calDAVRefreshCallback = callback
Thread {
val uri = CalendarContract.Calendars.CONTENT_URI
contentResolver.unregisterContentObserver(calDAVSyncObserver)
contentResolver.registerContentObserver(uri, false, calDAVSyncObserver)
refreshCalDAVCalendars(activity, config.caldavSyncedCalendarIDs)
refreshCalDAVCalendars(config.caldavSyncedCalendarIDs, true)
}.start()
}

View File

@ -21,7 +21,7 @@ class SelectCalendarsDialog(val activity: SimpleActivity, val callback: () -> Un
init {
val ids = activity.config.getSyncedCalendarIdsAsList()
val calendars = activity.calDAVHelper.getCalDAVCalendars(activity)
val calendars = activity.calDAVHelper.getCalDAVCalendars("", true)
val sorted = calendars.sortedWith(compareBy({ it.accountName }, { it.displayName }))
sorted.forEach {
if (prevAccount != it.accountName) {

View File

@ -302,12 +302,12 @@ fun Context.getNewEventTimestampFromCode(dayCode: String): Long {
return newDateTime.withDate(dateTime.year, dateTime.monthOfYear, dateTime.dayOfMonth).seconds()
}
fun Context.getSyncedCalDAVCalendars() = calDAVHelper.getCalDAVCalendars(null, config.caldavSyncedCalendarIDs)
fun Context.getSyncedCalDAVCalendars() = calDAVHelper.getCalDAVCalendars(config.caldavSyncedCalendarIDs, false)
fun Context.recheckCalDAVCalendars(callback: () -> Unit) {
if (config.caldavSync) {
Thread {
calDAVHelper.refreshCalendars(null, callback)
calDAVHelper.refreshCalendars(false, callback)
updateWidgets()
}.start()
}
@ -427,10 +427,10 @@ fun Context.handleEventDeleting(eventIds: List<Long>, timestamps: List<Long>, ac
}
}
fun Context.refreshCalDAVCalendars(activity: SimpleActivity?, ids: String) {
fun Context.refreshCalDAVCalendars(ids: String, showErrorToasts: Boolean) {
val uri = CalendarContract.Calendars.CONTENT_URI
val accounts = HashSet<Account>()
val calendars = calDAVHelper.getCalDAVCalendars(activity, ids)
val calendars = calDAVHelper.getCalDAVCalendars(ids, showErrorToasts)
calendars.forEach {
accounts.add(Account(it.accountName, it.accountType))
}

View File

@ -8,7 +8,6 @@ import android.database.Cursor
import android.provider.CalendarContract
import android.provider.CalendarContract.Reminders
import android.util.SparseIntArray
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.models.CalDAVCalendar
import com.simplemobiletools.calendar.pro.models.Event
@ -23,14 +22,14 @@ import kotlin.collections.ArrayList
class CalDAVHelper(val context: Context) {
private val eventsHelper = context.eventsHelper
fun refreshCalendars(activity: SimpleActivity? = null, callback: () -> Unit) {
fun refreshCalendars(showErrorToasts: Boolean, callback: () -> Unit) {
if (isUpdatingCalDAV) {
return
}
isUpdatingCalDAV = true
try {
val calDAVCalendars = getCalDAVCalendars(activity, context.config.caldavSyncedCalendarIDs)
val calDAVCalendars = getCalDAVCalendars(context.config.caldavSyncedCalendarIDs, showErrorToasts)
for (calendar in calDAVCalendars) {
val localEventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendar.id) ?: continue
localEventType.apply {
@ -40,7 +39,7 @@ class CalDAVHelper(val context: Context) {
eventsHelper.insertOrUpdateEventTypeSync(this)
}
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, activity)
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showErrorToasts)
}
context.scheduleCalDAVSync(true)
callback()
@ -50,7 +49,7 @@ class CalDAVHelper(val context: Context) {
}
@SuppressLint("MissingPermission")
fun getCalDAVCalendars(activity: SimpleActivity? = null, ids: String = ""): List<CalDAVCalendar> {
fun getCalDAVCalendars(ids: String, showErrorToasts: Boolean): List<CalDAVCalendar> {
val calendars = ArrayList<CalDAVCalendar>()
if (!context.hasPermission(PERMISSION_WRITE_CALENDAR) || !context.hasPermission(PERMISSION_READ_CALENDAR)) {
return calendars
@ -84,7 +83,9 @@ class CalDAVHelper(val context: Context) {
} while (cursor.moveToNext())
}
} catch (e: Exception) {
activity?.showErrorToast(e)
if (showErrorToasts) {
context.showErrorToast(e)
}
} finally {
cursor?.close()
}
@ -161,7 +162,7 @@ class CalDAVHelper(val context: Context) {
}
@SuppressLint("MissingPermission")
private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Long, activity: SimpleActivity?) {
private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Long, showErrorToasts: Boolean) {
val importIdsMap = HashMap<String, Event>()
val fetchedEventIds = ArrayList<String>()
val existingEvents = context.eventsDB.getEventsFromCalDAVCalendar("$CALDAV-$calendarId")
@ -264,7 +265,9 @@ class CalDAVHelper(val context: Context) {
} while (cursor.moveToNext())
}
} catch (e: Exception) {
activity?.showErrorToast(e)
if (showErrorToasts) {
context.showErrorToast(e)
}
} finally {
cursor?.close()
}
@ -438,5 +441,5 @@ class CalDAVHelper(val context: Context) {
private fun getCalDAVEventImportId(calendarId: Int, eventId: Long) = "$CALDAV-$calendarId-$eventId"
private fun refreshCalDAVCalendar(event: Event) = context.refreshCalDAVCalendars(null, event.getCalDAVCalendarId().toString())
private fun refreshCalDAVCalendar(event: Event) = context.refreshCalDAVCalendars(event.getCalDAVCalendarId().toString(), false)
}