[Firebase] Implement handling app updates.

This commit is contained in:
Kuba Szczodrzyński 2020-01-19 21:49:58 +01:00
parent 0fad12fea5
commit ca60ceb2a7
2 changed files with 16 additions and 9 deletions

View File

@ -6,11 +6,13 @@ package pl.szczodrzynski.edziennik.data.firebase
import com.google.gson.JsonParser import com.google.gson.JsonParser
import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update
import pl.szczodrzynski.edziennik.data.api.task.PostNotifications import pl.szczodrzynski.edziennik.data.api.task.PostNotifications
import pl.szczodrzynski.edziennik.data.db.entity.Event import pl.szczodrzynski.edziennik.data.db.entity.Event
import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.entity.Metadata
import pl.szczodrzynski.edziennik.data.db.entity.Notification import pl.szczodrzynski.edziennik.data.db.entity.Notification
import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.entity.Profile
import pl.szczodrzynski.edziennik.sync.UpdateWorker
import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Date
import pl.szczodrzynski.edziennik.utils.models.Time import pl.szczodrzynski.edziennik.utils.models.Time
@ -34,6 +36,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
message.data.getString("title") ?: "", message.data.getString("title") ?: "",
message.data.getString("message") ?: "" message.data.getString("message") ?: ""
) )
"appUpdate" -> UpdateWorker.runNow(app, app.gson.fromJson(message.data.get("update"), Update::class.java))
} }
} }
} }

View File

@ -19,6 +19,7 @@ import kotlinx.coroutines.launch
import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update
import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.Utils
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -73,16 +74,19 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker(
WorkManager.getInstance(app).cancelAllWorkByTag(TAG) WorkManager.getInstance(app).cancelAllWorkByTag(TAG)
} }
fun runNow(app: App) { fun runNow(app: App, overrideUpdate: Update? = null) {
try { try {
val api = SzkolnyApi(app) val update = if (overrideUpdate == null) {
val response = api.getUpdate("beta") val api = SzkolnyApi(app)
if (response?.success != true) val response = api.getUpdate("beta")
return if (response?.success != true)
val updates = response.data return
if (updates?.isNotEmpty() != true) val updates = response.data
return if (updates?.isNotEmpty() != true)
val update = updates[0] return
updates[0]
}
else overrideUpdate
app.config.update = update app.config.update = update