refresh the calendar after fetching google events

This commit is contained in:
tibbi 2017-07-23 20:34:59 +02:00
parent 3767883d19
commit 84d710dd6d
4 changed files with 30 additions and 18 deletions

View File

@ -18,6 +18,7 @@ import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.MyMonthPagerAdapter import com.simplemobiletools.calendar.adapters.MyMonthPagerAdapter
import com.simplemobiletools.calendar.adapters.MyWeekPagerAdapter import com.simplemobiletools.calendar.adapters.MyWeekPagerAdapter
import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter
import com.simplemobiletools.calendar.asynctasks.FetchGoogleEventsTask
import com.simplemobiletools.calendar.dialogs.ExportEventsDialog import com.simplemobiletools.calendar.dialogs.ExportEventsDialog
import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog
import com.simplemobiletools.calendar.dialogs.ImportEventsDialog 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.fragments.WeekFragment
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.interfaces.GoogleSyncListener
import com.simplemobiletools.calendar.interfaces.NavigationListener import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.calendar.models.EventType
@ -87,6 +89,10 @@ class MainActivity : SimpleActivity(), NavigationListener {
if (!hasGetAccountsPermission()) { if (!hasGetAccountsPermission()) {
config.googleSync = false config.googleSync = false
} }
if (config.syncAccountName.isNotEmpty()) {
FetchGoogleEventsTask(this, googleSyncListener).execute()
}
} }
override fun onResume() { override fun onResume() {
@ -527,6 +533,12 @@ class MainActivity : SimpleActivity(), NavigationListener {
supportFragmentManager.beginTransaction().replace(R.id.calendar_event_list_holder, EventListFragment(), "").commit() supportFragmentManager.beginTransaction().replace(R.id.calendar_event_list_holder, EventListFragment(), "").commit()
} }
val googleSyncListener = object : GoogleSyncListener {
override fun syncCompleted() {
refreshViewPager()
}
}
override fun goLeft() { override fun goLeft() {
main_view_pager.currentItem = main_view_pager.currentItem - 1 main_view_pager.currentItem = main_view_pager.currentItem - 1
} }

View File

@ -14,12 +14,13 @@ import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.getGoogleSyncService import com.simplemobiletools.calendar.extensions.getGoogleSyncService
import com.simplemobiletools.calendar.extensions.seconds import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.interfaces.GoogleSyncListener
import com.simplemobiletools.calendar.models.* import com.simplemobiletools.calendar.models.*
import org.joda.time.DateTime import org.joda.time.DateTime
import java.util.* import java.util.*
// more info about event fields at https://developers.google.com/google-apps/calendar/v3/reference/events/insert // more info about event fields at https://developers.google.com/google-apps/calendar/v3/reference/events/insert
class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List<Event>>() { class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: GoogleSyncListener? = null) : AsyncTask<Void, Void, String>() {
private val CONFIRMED = "confirmed" private val CONFIRMED = "confirmed"
private val ITEMS = "items" private val ITEMS = "items"
private val OVERRIDES = "overrides" private val OVERRIDES = "overrides"
@ -32,15 +33,16 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
private var eventColors = SparseIntArray() private var eventColors = SparseIntArray()
private var service = activity.getGoogleSyncService() private var service = activity.getGoogleSyncService()
override fun doInBackground(vararg params: Void): List<Event>? { override fun doInBackground(vararg params: Void): String {
return try { try {
getColors() getColors()
getDataFromApi() getDataFromApi()
} catch (e: Exception) { } catch (e: Exception) {
lastError = e lastError = e
cancel(true) cancel(true)
null
} }
googleSyncListener?.syncCompleted()
return ""
} }
private fun getColors() { private fun getColors() {
@ -50,8 +52,7 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
} }
} }
private fun getDataFromApi(): List<Event> { private fun getDataFromApi() {
val parsedEvents = ArrayList<Event>()
var currToken = "" var currToken = ""
while (true) { while (true) {
val events = service.events().list(PRIMARY) val events = service.events().list(PRIMARY)
@ -60,10 +61,8 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
for ((key, value) in events) { for ((key, value) in events) {
if (key == ITEMS) { if (key == ITEMS) {
dbHelper.getEventTypes { eventTypes = dbHelper.fetchEventTypes()
eventTypes = it parseEvents(value.toString())
parseEvents(value.toString())
}
} }
} }
@ -73,15 +72,12 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
break break
} }
} }
return parsedEvents
} }
private fun parseEvents(json: String): List<Event> { private fun parseEvents(json: String) {
var updateEvent = false var updateEvent = false
var eventId = 0 var eventId = 0
val importIDs = activity.dbHelper.getImportIds() val importIDs = activity.dbHelper.getImportIds()
val events = ArrayList<Event>()
val token = object : TypeToken<List<GoogleEvent>>() {}.type val token = object : TypeToken<List<GoogleEvent>>() {}.type
val googleEvents = Gson().fromJson<ArrayList<GoogleEvent>>(json, token) ?: ArrayList<GoogleEvent>(8) val googleEvents = Gson().fromJson<ArrayList<GoogleEvent>>(json, token) ?: ArrayList<GoogleEvent>(8)
for (googleEvent in googleEvents) { for (googleEvent in googleEvents) {
@ -136,7 +132,6 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
dbHelper.insert(event) {} dbHelper.insert(event) {}
} }
} }
return events
} }
private fun getEventTypeId(colorId: Int): Int { private fun getEventTypeId(colorId: Int): Int {

View File

@ -736,11 +736,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun getEventTypes(callback: (types: ArrayList<EventType>) -> Unit) { fun getEventTypes(callback: (types: ArrayList<EventType>) -> Unit) {
Thread({ Thread({
fetchEventTypes(callback) callback(fetchEventTypes())
}).start() }).start()
} }
fun fetchEventTypes(callback: (types: ArrayList<EventType>) -> Unit) { fun fetchEventTypes(): ArrayList<EventType> {
val eventTypes = ArrayList<EventType>(3) val eventTypes = ArrayList<EventType>(3)
val cols = arrayOf(COL_TYPE_ID, COL_TYPE_TITLE, COL_TYPE_COLOR) val cols = arrayOf(COL_TYPE_ID, COL_TYPE_TITLE, COL_TYPE_COLOR)
var cursor: Cursor? = null var cursor: Cursor? = null
@ -758,7 +758,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} finally { } finally {
cursor?.close() cursor?.close()
} }
callback.invoke(eventTypes) return eventTypes
} }
fun doEventTypesContainEvent(types: ArrayList<Int>): Boolean { fun doEventTypesContainEvent(types: ArrayList<Int>): Boolean {

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.calendar.interfaces
interface GoogleSyncListener {
fun syncCompleted()
}