insert the parsed events in db

This commit is contained in:
tibbi 2017-01-26 21:41:30 +01:00
parent 83ebb68f54
commit a7739dbbe2
16 changed files with 85 additions and 45 deletions

View File

@ -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)

View File

@ -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

View File

@ -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() {

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">תזכורת לאירוע</string>
<string name="events_imported_successfully">האירועים יובאו בהצלחה</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->

View File

@ -13,6 +13,6 @@
<dimen name="min_widget_height">250dp</dimen>
<dimen name="meta_text_size">14sp</dimen>
<dimen name="day_text_size">20sp</dimen>
<dimen name="day_text_size">17sp</dimen>
<dimen name="month_text_size">22sp</dimen>
</resources>

View File

@ -47,6 +47,7 @@
<string name="import_events_from_ics">Import events from an .ics file</string>
<string name="event_reminder">Event reminder</string>
<string name="events_imported_successfully">Events imported successfully</string>
<string name="importing_some_events_failed">Importing some events failed</string>
<string name="invalid_file_format">Invalid file format</string>
<!-- Day details -->