mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-04-01 12:10:14 +02:00
avoid passing activities around at the CalDAVHelper
This commit is contained in:
parent
06fa70dc16
commit
26511514d4
@ -605,7 +605,7 @@ class EventActivity : SimpleActivity() {
|
|||||||
event_caldav_calendar_holder.beVisible()
|
event_caldav_calendar_holder.beVisible()
|
||||||
event_caldav_calendar_divider.beVisible()
|
event_caldav_calendar_divider.beVisible()
|
||||||
|
|
||||||
val calendars = calDAVHelper.getCalDAVCalendars(this).filter {
|
val calendars = calDAVHelper.getCalDAVCalendars("", true).filter {
|
||||||
config.getSyncedCalendarIdsAsList().contains(it.id)
|
config.getSyncedCalendarIdsAsList().contains(it.id)
|
||||||
}
|
}
|
||||||
updateCurrentCalendarInfo(if (mEventCalendarId == STORED_LOCALLY_ONLY) null else getCalendarWithId(calendars, getCalendarId()))
|
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) {
|
val newEventType = if (!config.caldavSync || config.lastUsedCaldavCalendarId == 0 || mEventCalendarId == STORED_LOCALLY_ONLY) {
|
||||||
mEventTypeId
|
mEventTypeId
|
||||||
} else {
|
} else {
|
||||||
calDAVHelper.getCalDAVCalendars(this).firstOrNull { it.id == mEventCalendarId }?.apply {
|
calDAVHelper.getCalDAVCalendars("", true).firstOrNull { it.id == mEventCalendarId }?.apply {
|
||||||
if (!canWrite()) {
|
if (!canWrite()) {
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
toast(R.string.insufficient_permissions)
|
toast(R.string.insufficient_permissions)
|
||||||
|
@ -364,8 +364,8 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
|||||||
toast(R.string.refreshing)
|
toast(R.string.refreshing)
|
||||||
}
|
}
|
||||||
|
|
||||||
syncCalDAVCalendars(this) {
|
syncCalDAVCalendars {
|
||||||
calDAVHelper.refreshCalendars(this) {
|
calDAVHelper.refreshCalendars(true) {
|
||||||
calDAVChanged()
|
calDAVChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -218,8 +218,8 @@ class SettingsActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syncCalDAVCalendars(this) {
|
syncCalDAVCalendars {
|
||||||
calDAVHelper.refreshCalendars(this) {
|
calDAVHelper.refreshCalendars(true) {
|
||||||
if (settings_caldav_sync.isChecked) {
|
if (settings_caldav_sync.isChecked) {
|
||||||
toast(R.string.synchronization_completed)
|
toast(R.string.synchronization_completed)
|
||||||
}
|
}
|
||||||
|
@ -38,13 +38,13 @@ open class SimpleActivity : BaseSimpleActivity() {
|
|||||||
|
|
||||||
override fun getAppLauncherName() = getString(R.string.app_launcher_name)
|
override fun getAppLauncherName() = getString(R.string.app_launcher_name)
|
||||||
|
|
||||||
fun Context.syncCalDAVCalendars(activity: SimpleActivity?, callback: () -> Unit) {
|
fun Context.syncCalDAVCalendars(callback: () -> Unit) {
|
||||||
calDAVRefreshCallback = callback
|
calDAVRefreshCallback = callback
|
||||||
Thread {
|
Thread {
|
||||||
val uri = CalendarContract.Calendars.CONTENT_URI
|
val uri = CalendarContract.Calendars.CONTENT_URI
|
||||||
contentResolver.unregisterContentObserver(calDAVSyncObserver)
|
contentResolver.unregisterContentObserver(calDAVSyncObserver)
|
||||||
contentResolver.registerContentObserver(uri, false, calDAVSyncObserver)
|
contentResolver.registerContentObserver(uri, false, calDAVSyncObserver)
|
||||||
refreshCalDAVCalendars(activity, config.caldavSyncedCalendarIDs)
|
refreshCalDAVCalendars(config.caldavSyncedCalendarIDs, true)
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ class SelectCalendarsDialog(val activity: SimpleActivity, val callback: () -> Un
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
val ids = activity.config.getSyncedCalendarIdsAsList()
|
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 }))
|
val sorted = calendars.sortedWith(compareBy({ it.accountName }, { it.displayName }))
|
||||||
sorted.forEach {
|
sorted.forEach {
|
||||||
if (prevAccount != it.accountName) {
|
if (prevAccount != it.accountName) {
|
||||||
|
@ -302,12 +302,12 @@ fun Context.getNewEventTimestampFromCode(dayCode: String): Long {
|
|||||||
return newDateTime.withDate(dateTime.year, dateTime.monthOfYear, dateTime.dayOfMonth).seconds()
|
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) {
|
fun Context.recheckCalDAVCalendars(callback: () -> Unit) {
|
||||||
if (config.caldavSync) {
|
if (config.caldavSync) {
|
||||||
Thread {
|
Thread {
|
||||||
calDAVHelper.refreshCalendars(null, callback)
|
calDAVHelper.refreshCalendars(false, callback)
|
||||||
updateWidgets()
|
updateWidgets()
|
||||||
}.start()
|
}.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 uri = CalendarContract.Calendars.CONTENT_URI
|
||||||
val accounts = HashSet<Account>()
|
val accounts = HashSet<Account>()
|
||||||
val calendars = calDAVHelper.getCalDAVCalendars(activity, ids)
|
val calendars = calDAVHelper.getCalDAVCalendars(ids, showErrorToasts)
|
||||||
calendars.forEach {
|
calendars.forEach {
|
||||||
accounts.add(Account(it.accountName, it.accountType))
|
accounts.add(Account(it.accountName, it.accountType))
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import android.database.Cursor
|
|||||||
import android.provider.CalendarContract
|
import android.provider.CalendarContract
|
||||||
import android.provider.CalendarContract.Reminders
|
import android.provider.CalendarContract.Reminders
|
||||||
import android.util.SparseIntArray
|
import android.util.SparseIntArray
|
||||||
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.CalDAVCalendar
|
import com.simplemobiletools.calendar.pro.models.CalDAVCalendar
|
||||||
import com.simplemobiletools.calendar.pro.models.Event
|
import com.simplemobiletools.calendar.pro.models.Event
|
||||||
@ -23,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(activity: SimpleActivity? = null, callback: () -> Unit) {
|
fun refreshCalendars(showErrorToasts: Boolean, callback: () -> Unit) {
|
||||||
if (isUpdatingCalDAV) {
|
if (isUpdatingCalDAV) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
isUpdatingCalDAV = true
|
isUpdatingCalDAV = true
|
||||||
try {
|
try {
|
||||||
val calDAVCalendars = getCalDAVCalendars(activity, context.config.caldavSyncedCalendarIDs)
|
val calDAVCalendars = getCalDAVCalendars(context.config.caldavSyncedCalendarIDs, showErrorToasts)
|
||||||
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 {
|
||||||
@ -40,7 +39,7 @@ class CalDAVHelper(val context: Context) {
|
|||||||
eventsHelper.insertOrUpdateEventTypeSync(this)
|
eventsHelper.insertOrUpdateEventTypeSync(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, activity)
|
fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, showErrorToasts)
|
||||||
}
|
}
|
||||||
context.scheduleCalDAVSync(true)
|
context.scheduleCalDAVSync(true)
|
||||||
callback()
|
callback()
|
||||||
@ -50,7 +49,7 @@ class CalDAVHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
fun getCalDAVCalendars(activity: SimpleActivity? = null, ids: String = ""): List<CalDAVCalendar> {
|
fun getCalDAVCalendars(ids: String, showErrorToasts: 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
|
||||||
@ -84,7 +83,9 @@ class CalDAVHelper(val context: Context) {
|
|||||||
} while (cursor.moveToNext())
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
activity?.showErrorToast(e)
|
if (showErrorToasts) {
|
||||||
|
context.showErrorToast(e)
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
}
|
}
|
||||||
@ -161,7 +162,7 @@ class CalDAVHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@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 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")
|
||||||
@ -264,7 +265,9 @@ class CalDAVHelper(val context: Context) {
|
|||||||
} while (cursor.moveToNext())
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
activity?.showErrorToast(e)
|
if (showErrorToasts) {
|
||||||
|
context.showErrorToast(e)
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
}
|
}
|
||||||
@ -438,5 +441,5 @@ class CalDAVHelper(val context: Context) {
|
|||||||
|
|
||||||
private fun getCalDAVEventImportId(calendarId: Int, eventId: Long) = "$CALDAV-$calendarId-$eventId"
|
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)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user