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.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
}

View File

@ -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<Void, Void, List<Event>>() {
class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: GoogleSyncListener? = null) : AsyncTask<Void, Void, String>() {
private val CONFIRMED = "confirmed"
private val ITEMS = "items"
private val OVERRIDES = "overrides"
@ -32,15 +33,16 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
private var eventColors = SparseIntArray()
private var service = activity.getGoogleSyncService()
override fun doInBackground(vararg params: Void): List<Event>? {
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<Void, Void, List
}
}
private fun getDataFromApi(): List<Event> {
val parsedEvents = ArrayList<Event>()
private fun getDataFromApi() {
var currToken = ""
while (true) {
val events = service.events().list(PRIMARY)
@ -60,12 +61,10 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
for ((key, value) in events) {
if (key == ITEMS) {
dbHelper.getEventTypes {
eventTypes = it
eventTypes = dbHelper.fetchEventTypes()
parseEvents(value.toString())
}
}
}
if (events.containsKey(NEXT_PAGE_TOKEN)) {
currToken = events[NEXT_PAGE_TOKEN] as String
@ -73,15 +72,12 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
break
}
}
return parsedEvents
}
private fun parseEvents(json: String): List<Event> {
private fun parseEvents(json: String) {
var updateEvent = false
var eventId = 0
val importIDs = activity.dbHelper.getImportIds()
val events = ArrayList<Event>()
val token = object : TypeToken<List<GoogleEvent>>() {}.type
val googleEvents = Gson().fromJson<ArrayList<GoogleEvent>>(json, token) ?: ArrayList<GoogleEvent>(8)
for (googleEvent in googleEvents) {
@ -136,7 +132,6 @@ class FetchGoogleEventsTask(val activity: Activity) : AsyncTask<Void, Void, List
dbHelper.insert(event) {}
}
}
return events
}
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) {
Thread({
fetchEventTypes(callback)
callback(fetchEventTypes())
}).start()
}
fun fetchEventTypes(callback: (types: ArrayList<EventType>) -> Unit) {
fun fetchEventTypes(): ArrayList<EventType> {
val eventTypes = ArrayList<EventType>(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<Int>): Boolean {

View File

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