do the file parsing on a background thread

This commit is contained in:
tibbi 2017-01-26 20:31:30 +01:00
parent dd8386d5f3
commit 83ebb68f54
3 changed files with 12 additions and 8 deletions

View File

@ -160,10 +160,12 @@ class MainActivity : SimpleActivity(), EventListFragment.DeleteListener {
FilePickerDialog(this) {
if (it.toLowerCase().endsWith(".ics")) {
ImportEventsDialog(this, it) {
if (it) {
toast(R.string.events_imported_successfully)
} else {
toast(R.string.unknown_error_occurred)
runOnUiThread {
if (it) {
toast(R.string.events_imported_successfully)
} else {
toast(R.string.unknown_error_occurred)
}
}
}
} else {

View File

@ -51,8 +51,10 @@ class ImportEventsDialog(val activity: Activity, val path: String, val callback:
}
try {
IcsParser.parseIcs(context, minutes, path)
callback.invoke(true)
Thread({
IcsParser.parseIcs(context, minutes, path)
callback.invoke(true)
}).start()
} catch (e: Exception) {
callback.invoke(false)
}

View File

@ -1,11 +1,11 @@
package com.simplemobiletools.calendar.helpers
import android.content.Context
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.models.Event
import org.joda.time.DateTimeZone
import org.joda.time.format.DateTimeFormat
import java.io.File
object IcsParser {
private val BEGIN_EVENT = "BEGIN:VEVENT"
@ -22,7 +22,7 @@ object IcsParser {
var curDescription = ""
fun parseIcs(context: Context, reminderMinutes: Int, path: String) {
val inputStream = context.resources.openRawResource(R.raw.sample)
val inputStream = File(path).inputStream()
inputStream.bufferedReader().use {
while (true) {