show some error toast if something goes wrong during caldav sync

This commit is contained in:
tibbi 2017-09-10 12:10:57 +02:00
parent d7c106243f
commit 97df40eee7
4 changed files with 17 additions and 13 deletions

View File

@ -153,7 +153,7 @@ class SettingsActivity : SimpleActivity() {
dbHelper.insertEventType(eventType) dbHelper.insertEventType(eventType)
} }
} }
CalDAVHandler(applicationContext).refreshCalendars {} CalDAVHandler(applicationContext).refreshCalendars(this) {}
} }
val removedCalendarIds = oldCalendarIds.filter { !newCalendarIds.contains(it) } val removedCalendarIds = oldCalendarIds.filter { !newCalendarIds.contains(it) }

View File

@ -238,11 +238,11 @@ fun Context.getNewEventTimestampFromCode(dayCode: String): Int {
fun Context.getCurrentOffset() = SimpleDateFormat("Z", Locale.getDefault()).format(Date()) fun Context.getCurrentOffset() = SimpleDateFormat("Z", Locale.getDefault()).format(Date())
fun Context.getSyncedCalDAVCalendars() = CalDAVHandler(this).getCalDAVCalendars(config.caldavSyncedCalendarIDs) fun Context.getSyncedCalDAVCalendars() = CalDAVHandler(this).getCalDAVCalendars(null, config.caldavSyncedCalendarIDs)
fun Context.recheckCalDAVCalendars(callback: () -> Unit) { fun Context.recheckCalDAVCalendars(callback: () -> Unit) {
if (config.caldavSync) { if (config.caldavSync) {
CalDAVHandler(this).refreshCalendars(callback) CalDAVHandler(this).refreshCalendars(null, callback)
} }
} }

View File

@ -7,6 +7,7 @@ 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.activities.SimpleActivity
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.hasCalendarPermission import com.simplemobiletools.calendar.extensions.hasCalendarPermission
@ -17,13 +18,14 @@ import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.showErrorToast
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class CalDAVHandler(val context: Context) { class CalDAVHandler(val context: Context) {
fun refreshCalendars(callback: () -> Unit) { fun refreshCalendars(activity: SimpleActivity? = null, callback: () -> Unit) {
val dbHelper = context.dbHelper val dbHelper = context.dbHelper
getCalDAVCalendars(context.config.caldavSyncedCalendarIDs).forEach { getCalDAVCalendars(activity, context.config.caldavSyncedCalendarIDs).forEach {
val localEventType = dbHelper.getEventTypeWithCalDAVCalendarId(it.id) val localEventType = dbHelper.getEventTypeWithCalDAVCalendarId(it.id)
localEventType?.apply { localEventType?.apply {
title = it.displayName title = it.displayName
@ -33,13 +35,13 @@ class CalDAVHandler(val context: Context) {
dbHelper.updateLocalEventType(this) dbHelper.updateLocalEventType(this)
} }
CalDAVHandler(context).fetchCalDAVCalendarEvents(it.id, localEventType!!.id) CalDAVHandler(context).fetchCalDAVCalendarEvents(it.id, localEventType!!.id, activity)
} }
context.scheduleCalDAVSync(true) context.scheduleCalDAVSync(true)
callback() callback()
} }
fun getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> { fun getCalDAVCalendars(activity: SimpleActivity? = null, ids: String = ""): List<CalDAVCalendar> {
val calendars = ArrayList<CalDAVCalendar>() val calendars = ArrayList<CalDAVCalendar>()
if (!context.hasCalendarPermission()) { if (!context.hasCalendarPermission()) {
return calendars return calendars
@ -70,6 +72,8 @@ class CalDAVHandler(val context: Context) {
calendars.add(calendar) calendars.add(calendar)
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} catch (e: Exception) {
activity?.showErrorToast(e)
} finally { } finally {
cursor?.close() cursor?.close()
} }
@ -183,14 +187,12 @@ class CalDAVHandler(val context: Context) {
} }
val sortedColors = ArrayList<Int>(colors.size()) val sortedColors = ArrayList<Int>(colors.size())
for (i in 0 until colors.size()) { (0 until colors.size()).mapTo(sortedColors) { colors[it] }
sortedColors.add(colors[i])
}
return sortedColors return sortedColors
} }
private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Int) { private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Int, activity: SimpleActivity?) {
val importIdsMap = HashMap<String, Event>() val importIdsMap = HashMap<String, Event>()
val fetchedEventIds = ArrayList<String>() val fetchedEventIds = ArrayList<String>()
val existingEvents = context.dbHelper.getEventsFromCalDAVCalendar(calendarId) val existingEvents = context.dbHelper.getEventsFromCalDAVCalendar(calendarId)
@ -256,6 +258,8 @@ class CalDAVHandler(val context: Context) {
} }
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} catch (e: Exception) {
activity?.showErrorToast(e)
} finally { } finally {
cursor?.close() cursor?.close()
} }
@ -396,5 +400,5 @@ class CalDAVHandler(val context: Context) {
return reminders return reminders
} }
fun getCalDAVEventImportId(calendarId: Int, eventId: Long) = "$CALDAV-$calendarId-$eventId" private fun getCalDAVEventImportId(calendarId: Int, eventId: Long) = "$CALDAV-$calendarId-$eventId"
} }

View File

@ -48,7 +48,7 @@ class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val
val day = dateTime.dayOfMonth val day = dateTime.dayOfMonth
if (arr[month] == null) if (arr[month] == null)
arr.put(month, ArrayList<Int>()) arr.put(month, ArrayList())
if (dateTime.year == year) if (dateTime.year == year)
arr.get(month).add(day) arr.get(month).add(day)