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,12 +160,14 @@ class MainActivity : SimpleActivity(), EventListFragment.DeleteListener {
FilePickerDialog(this) { FilePickerDialog(this) {
if (it.toLowerCase().endsWith(".ics")) { if (it.toLowerCase().endsWith(".ics")) {
ImportEventsDialog(this, it) { ImportEventsDialog(this, it) {
runOnUiThread {
if (it) { if (it) {
toast(R.string.events_imported_successfully) toast(R.string.events_imported_successfully)
} else { } else {
toast(R.string.unknown_error_occurred) toast(R.string.unknown_error_occurred)
} }
} }
}
} else { } else {
toast(R.string.invalid_file_format) toast(R.string.invalid_file_format)
} }

View File

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

View File

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