[API/Szkolny] Restrict AppSync to run only every 24 hours (if no WebPush needed).

This commit is contained in:
Kuba Szczodrzyński 2020-02-16 14:30:13 +01:00
parent 54363ee919
commit fc21d757c3
2 changed files with 25 additions and 7 deletions

View File

@ -100,6 +100,11 @@ class Config(val db: AppDb) : CoroutineScope, AbstractConfig {
get() { mWidgetConfigs = mWidgetConfigs ?: values.get("widgetConfigs", JsonObject()); return mWidgetConfigs ?: JsonObject() } get() { mWidgetConfigs = mWidgetConfigs ?: values.get("widgetConfigs", JsonObject()); return mWidgetConfigs ?: JsonObject() }
set(value) { set("widgetConfigs", value); mWidgetConfigs = value } set(value) { set("widgetConfigs", value); mWidgetConfigs = value }
private var mLastAppSync: Long? = null
var lastAppSync: Long
get() { mLastAppSync = mLastAppSync ?: values.get("lastAppSync", 0L); return mLastAppSync ?: 0L }
set(value) { set("lastAppSync", value); mLastAppSync = value }
private var rawEntries: List<ConfigEntry> = db.configDao().getAllNow() private var rawEntries: List<ConfigEntry> = db.configDao().getAllNow()
private val profileConfigs: HashMap<Int, ProfileConfig> = hashMapOf() private val profileConfigs: HashMap<Int, ProfileConfig> = hashMapOf()
init { init {

View File

@ -5,6 +5,7 @@
package pl.szczodrzynski.edziennik.data.api.task package pl.szczodrzynski.edziennik.data.api.task
import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.HOUR
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
@ -25,18 +26,30 @@ class SzkolnyTask(val app: App, val syncingProfiles: List<Profile>) : IApiTask(-
internal fun run(taskCallback: EdziennikCallback) { internal fun run(taskCallback: EdziennikCallback) {
val startTime = System.currentTimeMillis() val startTime = System.currentTimeMillis()
val notifications = Notifications(app, notificationList, profiles)
// create all e-register data notifications // create all e-register data notifications
val notifications = Notifications(app, notificationList, profiles)
notifications.run() notifications.run()
// send notifications to web push, get shared events
AppSync(app, notificationList, profiles, api).run() val shouldAppSync = notificationList.isNotEmpty() || (System.currentTimeMillis() - app.config.lastAppSync > 24*HOUR*1000)
// create notifications for shared events (not present before app sync) // do an AppSync every 24 hours, or if WebPush has a notification
notifications.sharedEventNotifications() if (shouldAppSync) {
// send notifications to web push, get shared events
val addedEvents = AppSync(app, notificationList, profiles, api).run()
if (addedEvents > 0) {
// create notifications for shared events (not present before app sync)
notifications.sharedEventNotifications()
}
app.config.lastAppSync = System.currentTimeMillis()
}
d(TAG, "Created ${notificationList.count()} notifications.") d(TAG, "Created ${notificationList.count()} notifications.")
// update the database // update the database
app.db.metadataDao().setAllNotified(true) app.db.metadataDao().setAllNotified(true)
app.db.notificationDao().addAll(notificationList) if (notificationList.isNotEmpty())
app.db.notificationDao().addAll(notificationList)
app.db.profileDao().setAllNotEmpty() app.db.profileDao().setAllNotEmpty()
// post all notifications // post all notifications
PostNotifications(app, notificationList) PostNotifications(app, notificationList)
d(TAG, "SzkolnyTask: finished in ${System.currentTimeMillis()-startTime} ms.") d(TAG, "SzkolnyTask: finished in ${System.currentTimeMillis()-startTime} ms.")