From 929dbf6e6074284a418f2ca8b9944a7d908b4755 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 2 Sep 2017 00:20:02 +0200 Subject: [PATCH] make the error reporting at ics file importing more verbose --- .../calendar/activities/MainActivity.kt | 2 +- .../calendar/dialogs/ImportEventsDialog.kt | 6 +++--- .../calendar/helpers/IcsImporter.kt | 16 ++++++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index 179afbde5..42f1e2a6e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -264,7 +264,7 @@ class MainActivity : SimpleActivity(), NavigationListener { val eventType = EventType(0, holidays, config.primaryColor) eventTypeId = dbHelper.insertEventType(eventType) } - val result = IcsImporter().importEvents(applicationContext, it as String, eventTypeId) + val result = IcsImporter().importEvents(this, it as String, eventTypeId) handleParseResult(result) }).start() } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt index f6f3f01a3..e14d7b8bf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt @@ -1,9 +1,9 @@ package com.simplemobiletools.calendar.dialogs -import android.app.Activity import android.support.v7.app.AlertDialog import android.view.ViewGroup import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.helpers.DBHelper @@ -14,7 +14,7 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.toast import kotlinx.android.synthetic.main.dialog_import_events.view.* -class ImportEventsDialog(val activity: Activity, val path: String, val callback: (refreshView: Boolean) -> Unit) : AlertDialog.Builder(activity) { +class ImportEventsDialog(val activity: SimpleActivity, val path: String, val callback: (refreshView: Boolean) -> Unit) : AlertDialog.Builder(activity) { var currEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID init { @@ -36,7 +36,7 @@ class ImportEventsDialog(val activity: Activity, val path: String, val callback: getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ activity.toast(R.string.importing) Thread({ - val result = IcsImporter().importEvents(context, path, currEventTypeId) + val result = IcsImporter().importEvents(activity, path, currEventTypeId) handleParseResult(result) dismiss() }).start() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt index d9f130c4d..60a9e9692 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt @@ -1,11 +1,14 @@ package com.simplemobiletools.calendar.helpers import android.content.Context +import android.widget.Toast import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.helpers.IcsImporter.ImportResult.* import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.EventType +import com.simplemobiletools.commons.extensions.showErrorToast import java.io.File class IcsImporter { @@ -32,15 +35,15 @@ class IcsImporter { var eventsImported = 0 var eventsFailed = 0 - fun importEvents(context: Context, path: String, defaultEventType: Int): ImportResult { + fun importEvents(activity: SimpleActivity, path: String, defaultEventType: Int): ImportResult { try { - val importIDs = context.dbHelper.getImportIds() + val importIDs = activity.dbHelper.getImportIds() var prevLine = "" val inputStream = if (path.contains("/")) { File(path).inputStream() } else { - context.assets.open(path) + activity.assets.open(path) } inputStream.bufferedReader().use { @@ -84,7 +87,7 @@ class IcsImporter { curReminderMinutes.add(Parser().parseDurationSeconds(line.substring(TRIGGER.length)) / 60) } else if (line.startsWith(CATEGORIES)) { val categories = line.substring(CATEGORIES.length) - tryAddCategories(categories, context) + tryAddCategories(categories, activity) } else if (line.startsWith(LAST_MODIFIED)) { curLastModified = getTimestamp(line.substring(LAST_MODIFIED.length)) * 1000L } else if (line.startsWith(EXDATE)) { @@ -105,9 +108,9 @@ class IcsImporter { event.endTS -= DAY } - context.dbHelper.insert(event, true) { + activity.dbHelper.insert(event, true) { for (exceptionTS in curRepeatExceptions) { - context.dbHelper.addEventRepeatException(it, exceptionTS) + activity.dbHelper.addEventRepeatException(it, exceptionTS) } eventsImported++ resetValues() @@ -117,6 +120,7 @@ class IcsImporter { } } } catch (e: Exception) { + activity.showErrorToast(e.toString(), Toast.LENGTH_LONG) eventsFailed++ }