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 94cbb4b60..1bb4e911d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -160,13 +160,8 @@ class MainActivity : SimpleActivity(), EventListFragment.DeleteListener { FilePickerDialog(this) { if (it.toLowerCase().endsWith(".ics")) { ImportEventsDialog(this, it) { - runOnUiThread { - if (it) { - toast(R.string.events_imported_successfully) - } else { - toast(R.string.unknown_error_occurred) - } - } + if (it) + updateViewPager() } } else { toast(R.string.invalid_file_format) 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 b84b4b83b..d078ecb85 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt @@ -9,13 +9,11 @@ import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.extensions.getDefaultReminderTypeIndex import com.simplemobiletools.calendar.extensions.setupReminderPeriod import com.simplemobiletools.calendar.helpers.* -import com.simplemobiletools.commons.extensions.humanizePath -import com.simplemobiletools.commons.extensions.setupDialogStuff -import com.simplemobiletools.commons.extensions.showKeyboard -import com.simplemobiletools.commons.extensions.value +import com.simplemobiletools.calendar.helpers.IcsParser.ImportResult.* +import com.simplemobiletools.commons.extensions.* import kotlinx.android.synthetic.main.dialog_import_events.view.* -class ImportEventsDialog(val activity: Activity, val path: String, val callback: (success: Boolean) -> Unit) : AlertDialog.Builder(activity) { +class ImportEventsDialog(val activity: Activity, val path: String, val callback: (refreshView: Boolean) -> Unit) : AlertDialog.Builder(activity) { init { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_import_events, null).apply { import_events_filename.text = activity.humanizePath(path) @@ -50,18 +48,26 @@ class ImportEventsDialog(val activity: Activity, val path: String, val callback: else -> getReminderMinutes(view) } - try { - Thread({ - IcsParser.parseIcs(context, minutes, path) - callback.invoke(true) - }).start() - } catch (e: Exception) { - callback.invoke(false) - } + Thread({ + val result = IcsParser().parseIcs(context, minutes, path) + handleParseResult(result) + dismiss() + }).start() }) } } + private fun handleParseResult(result: IcsParser.ImportResult) { + activity.runOnUiThread { + activity.toast(when (result) { + IMPORT_OK -> R.string.events_imported_successfully + IMPORT_PARTIAL -> R.string.importing_some_events_failed + else -> R.string.importing_some_events_failed + }) + callback.invoke(result != IMPORT_FAIL) + } + } + private fun getReminderMinutes(view: View): Int { val multiplier = when (view.import_events_custom_reminder_other_period.selectedItemPosition) { 1 -> HOUR_MINS diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt index 88f307d37..cb96c1511 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt @@ -2,12 +2,17 @@ package com.simplemobiletools.calendar.helpers import android.content.Context import com.simplemobiletools.calendar.extensions.seconds +import com.simplemobiletools.calendar.helpers.IcsParser.ImportResult.* import com.simplemobiletools.calendar.models.Event import org.joda.time.DateTimeZone import org.joda.time.format.DateTimeFormat import java.io.File -object IcsParser { +class IcsParser { + enum class ImportResult { + IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL + } + private val BEGIN_EVENT = "BEGIN:VEVENT" private val BEGIN_ALARM = "BEGIN:VALARM" private val END = "END:VEVENT" @@ -21,37 +26,59 @@ object IcsParser { var curTitle = "" var curDescription = "" - fun parseIcs(context: Context, reminderMinutes: Int, path: String) { - val inputStream = File(path).inputStream() + var eventsImported = 0 + var eventsFailed = 0 - inputStream.bufferedReader().use { - while (true) { - val line = it.readLine()?.trim() ?: break - if (line == BEGIN_EVENT) { - resetValues() - } else if (line.startsWith(DTSTART)) { - curStart = getTimestamp(line.substring(DTSTART.length)) - } else if (line.startsWith(DTEND)) { - curEnd = getTimestamp(line.substring(DTEND.length)) - } else if (line.startsWith(SUMMARY)) { - curTitle = line.substring(SUMMARY.length) - } else if (line.startsWith(DESCRIPTION)) { - curDescription = line.substring(DESCRIPTION.length) - } else if (line == END || line == BEGIN_ALARM) { - if (curTitle.isEmpty() || curStart == -1 || curEnd == -1) - continue + fun parseIcs(context: Context, reminderMinutes: Int, path: String): ImportResult { + try { + val dbHelper = DBHelper(context) - val event = Event(0, curStart, curEnd, curTitle, curDescription, reminderMinutes) - resetValues() + File(path).inputStream().bufferedReader().use { + while (true) { + val line = it.readLine()?.trim() ?: break + if (line == BEGIN_EVENT) { + resetValues() + } else if (line.startsWith(DTSTART)) { + curStart = getTimestamp(line.substring(DTSTART.length)) + } else if (line.startsWith(DTEND)) { + curEnd = getTimestamp(line.substring(DTEND.length)) + } else if (line.startsWith(SUMMARY)) { + curTitle = line.substring(SUMMARY.length) + } else if (line.startsWith(DESCRIPTION)) { + curDescription = line.substring(DESCRIPTION.length) + } else if (line == END || line == BEGIN_ALARM) { + if (curTitle.isEmpty() || curStart == -1 || curEnd == -1) + continue + + val event = Event(0, curStart, curEnd, curTitle, curDescription, reminderMinutes) + dbHelper.insert(event) + eventsImported++ + resetValues() + } } } + } catch (e: Exception) { + eventsFailed++ + } + + return if (eventsImported == 0) { + IMPORT_FAIL + } else if (eventsFailed > 0) { + IMPORT_PARTIAL + } else { + IMPORT_OK } } private fun getTimestamp(fullString: String): Int { - val digitString = fullString.replace("T", "").replace("Z", "") - val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMddHHmmss") - return dateTimeFormat.parseDateTime(digitString).withZoneRetainFields(DateTimeZone.UTC).seconds() + try { + val digitString = fullString.replace("T", "").replace("Z", "") + val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMddHHmmss") + return dateTimeFormat.parseDateTime(digitString).withZoneRetainFields(DateTimeZone.UTC).seconds() + } catch (e: Exception) { + eventsFailed++ + return -1 + } } private fun resetValues() { diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 196dbccd7..2e315304d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-es-rES/strings.xml b/app/src/main/res/values-es-rES/strings.xml index cce19337e..50f0d249b 100644 --- a/app/src/main/res/values-es-rES/strings.xml +++ b/app/src/main/res/values-es-rES/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index e6dc333bb..e1aa0cf54 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-hi-rIN/strings.xml b/app/src/main/res/values-hi-rIN/strings.xml index d8d0ae9e0..e5ad8c8b8 100644 --- a/app/src/main/res/values-hi-rIN/strings.xml +++ b/app/src/main/res/values-hi-rIN/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index 98e195244..b6fe6e35c 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index f820d4f27..11a2a99f1 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-iw/strings.xml b/app/src/main/res/values-iw/strings.xml index b4ad8eac9..ea2b5780b 100644 --- a/app/src/main/res/values-iw/strings.xml +++ b/app/src/main/res/values-iw/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file תזכורת לאירוע האירועים יובאו בהצלחה + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 0a9364eee..ffa69670a 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index 45dc63d6c..9253853a1 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index ac41cf5cf..6fa4b44d2 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index cedef3a69..59b15e239 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 06775e63e..038f9baf8 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -13,6 +13,6 @@ 250dp 14sp - 20sp + 17sp 22sp diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d775b65e3..963caf238 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,6 +47,7 @@ Import events from an .ics file Event reminder Events imported successfully + Importing some events failed Invalid file format