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 a875887ec..8d0aad7aa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -18,6 +18,7 @@ import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.adapters.MyMonthPagerAdapter import com.simplemobiletools.calendar.adapters.MyWeekPagerAdapter import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter +import com.simplemobiletools.calendar.asynctasks.FetchGoogleEventsTask import com.simplemobiletools.calendar.dialogs.ExportEventsDialog import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog import com.simplemobiletools.calendar.dialogs.ImportEventsDialog @@ -26,6 +27,7 @@ import com.simplemobiletools.calendar.fragments.EventListFragment import com.simplemobiletools.calendar.fragments.WeekFragment import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.Formatter +import com.simplemobiletools.calendar.interfaces.GoogleSyncListener import com.simplemobiletools.calendar.interfaces.NavigationListener import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.EventType @@ -87,6 +89,10 @@ class MainActivity : SimpleActivity(), NavigationListener { if (!hasGetAccountsPermission()) { config.googleSync = false } + + if (config.syncAccountName.isNotEmpty()) { + FetchGoogleEventsTask(this, googleSyncListener).execute() + } } override fun onResume() { @@ -527,6 +533,12 @@ class MainActivity : SimpleActivity(), NavigationListener { supportFragmentManager.beginTransaction().replace(R.id.calendar_event_list_holder, EventListFragment(), "").commit() } + val googleSyncListener = object : GoogleSyncListener { + override fun syncCompleted() { + refreshViewPager() + } + } + override fun goLeft() { main_view_pager.currentItem = main_view_pager.currentItem - 1 } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt index 1c60733b3..f1ecf5ed0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt @@ -14,12 +14,13 @@ import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.extensions.getGoogleSyncService import com.simplemobiletools.calendar.extensions.seconds import com.simplemobiletools.calendar.helpers.* +import com.simplemobiletools.calendar.interfaces.GoogleSyncListener import com.simplemobiletools.calendar.models.* import org.joda.time.DateTime import java.util.* // more info about event fields at https://developers.google.com/google-apps/calendar/v3/reference/events/insert -class FetchGoogleEventsTask(val activity: Activity) : AsyncTask>() { +class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: GoogleSyncListener? = null) : AsyncTask() { private val CONFIRMED = "confirmed" private val ITEMS = "items" private val OVERRIDES = "overrides" @@ -32,15 +33,16 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask? { - return try { + override fun doInBackground(vararg params: Void): String { + try { getColors() getDataFromApi() } catch (e: Exception) { lastError = e cancel(true) - null } + googleSyncListener?.syncCompleted() + return "" } private fun getColors() { @@ -50,8 +52,7 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask { - val parsedEvents = ArrayList() + private fun getDataFromApi() { var currToken = "" while (true) { val events = service.events().list(PRIMARY) @@ -60,10 +61,8 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask { + private fun parseEvents(json: String) { var updateEvent = false var eventId = 0 val importIDs = activity.dbHelper.getImportIds() - val events = ArrayList() val token = object : TypeToken>() {}.type val googleEvents = Gson().fromJson>(json, token) ?: ArrayList(8) for (googleEvent in googleEvents) { @@ -136,7 +132,6 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask) -> Unit) { Thread({ - fetchEventTypes(callback) + callback(fetchEventTypes()) }).start() } - fun fetchEventTypes(callback: (types: ArrayList) -> Unit) { + fun fetchEventTypes(): ArrayList { val eventTypes = ArrayList(3) val cols = arrayOf(COL_TYPE_ID, COL_TYPE_TITLE, COL_TYPE_COLOR) var cursor: Cursor? = null @@ -758,7 +758,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } finally { cursor?.close() } - callback.invoke(eventTypes) + return eventTypes } fun doEventTypesContainEvent(types: ArrayList): Boolean { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/GoogleSyncListener.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/GoogleSyncListener.kt new file mode 100644 index 000000000..73bdfe1cd --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/GoogleSyncListener.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.calendar.interfaces + +interface GoogleSyncListener { + fun syncCompleted() +}