From 97df40eee7737f2c0bbba36e6f2b9c3ed68796d2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 10 Sep 2017 12:10:57 +0200 Subject: [PATCH] show some error toast if something goes wrong during caldav sync --- .../calendar/activities/SettingsActivity.kt | 2 +- .../calendar/extensions/Context.kt | 4 ++-- .../calendar/helpers/CalDAVHandler.kt | 22 +++++++++++-------- .../calendar/helpers/YearlyCalendarImpl.kt | 2 +- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt index e46711fde..a7190063b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -153,7 +153,7 @@ class SettingsActivity : SimpleActivity() { dbHelper.insertEventType(eventType) } } - CalDAVHandler(applicationContext).refreshCalendars {} + CalDAVHandler(applicationContext).refreshCalendars(this) {} } val removedCalendarIds = oldCalendarIds.filter { !newCalendarIds.contains(it) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index f4089af3b..fb4a6701a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -238,11 +238,11 @@ fun Context.getNewEventTimestampFromCode(dayCode: String): Int { 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) { if (config.caldavSync) { - CalDAVHandler(this).refreshCalendars(callback) + CalDAVHandler(this).refreshCalendars(null, callback) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt index 483f5e28d..0bc712555 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt @@ -7,6 +7,7 @@ import android.database.Cursor import android.provider.CalendarContract import android.provider.CalendarContract.Reminders import android.util.SparseIntArray +import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.dbHelper 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.getLongValue import com.simplemobiletools.commons.extensions.getStringValue +import com.simplemobiletools.commons.extensions.showErrorToast import java.util.* import kotlin.collections.ArrayList class CalDAVHandler(val context: Context) { - fun refreshCalendars(callback: () -> Unit) { + fun refreshCalendars(activity: SimpleActivity? = null, callback: () -> Unit) { val dbHelper = context.dbHelper - getCalDAVCalendars(context.config.caldavSyncedCalendarIDs).forEach { + getCalDAVCalendars(activity, context.config.caldavSyncedCalendarIDs).forEach { val localEventType = dbHelper.getEventTypeWithCalDAVCalendarId(it.id) localEventType?.apply { title = it.displayName @@ -33,13 +35,13 @@ class CalDAVHandler(val context: Context) { dbHelper.updateLocalEventType(this) } - CalDAVHandler(context).fetchCalDAVCalendarEvents(it.id, localEventType!!.id) + CalDAVHandler(context).fetchCalDAVCalendarEvents(it.id, localEventType!!.id, activity) } context.scheduleCalDAVSync(true) callback() } - fun getCalDAVCalendars(ids: String = ""): List { + fun getCalDAVCalendars(activity: SimpleActivity? = null, ids: String = ""): List { val calendars = ArrayList() if (!context.hasCalendarPermission()) { return calendars @@ -70,6 +72,8 @@ class CalDAVHandler(val context: Context) { calendars.add(calendar) } while (cursor.moveToNext()) } + } catch (e: Exception) { + activity?.showErrorToast(e) } finally { cursor?.close() } @@ -183,14 +187,12 @@ class CalDAVHandler(val context: Context) { } val sortedColors = ArrayList(colors.size()) - for (i in 0 until colors.size()) { - sortedColors.add(colors[i]) - } + (0 until colors.size()).mapTo(sortedColors) { colors[it] } return sortedColors } - private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Int) { + private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Int, activity: SimpleActivity?) { val importIdsMap = HashMap() val fetchedEventIds = ArrayList() val existingEvents = context.dbHelper.getEventsFromCalDAVCalendar(calendarId) @@ -256,6 +258,8 @@ class CalDAVHandler(val context: Context) { } } while (cursor.moveToNext()) } + } catch (e: Exception) { + activity?.showErrorToast(e) } finally { cursor?.close() } @@ -396,5 +400,5 @@ class CalDAVHandler(val context: Context) { return reminders } - fun getCalDAVEventImportId(calendarId: Int, eventId: Long) = "$CALDAV-$calendarId-$eventId" + private fun getCalDAVEventImportId(calendarId: Int, eventId: Long) = "$CALDAV-$calendarId-$eventId" } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt index 4d09d12fb..51e94aac8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt @@ -48,7 +48,7 @@ class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val val day = dateTime.dayOfMonth if (arr[month] == null) - arr.put(month, ArrayList()) + arr.put(month, ArrayList()) if (dateTime.year == year) arr.get(month).add(day)