diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt index 937a2301..aa069d0f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt @@ -5,6 +5,10 @@ package pl.szczodrzynski.edziennik.data.firebase import com.google.gson.JsonParser +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update import pl.szczodrzynski.edziennik.data.api.task.PostNotifications @@ -15,8 +19,17 @@ 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.Time +import kotlin.coroutines.CoroutineContext + +class SzkolnyAppFirebase(val app: App, val profiles: List, val message: FirebaseService.Message) : CoroutineScope { + companion object { + private const val TAG = "SzkolnyAppFirebase" + } + + private val job = Job() + override val coroutineContext: CoroutineContext + get() = job + Dispatchers.Main -class SzkolnyAppFirebase(val app: App, val profiles: List, val message: FirebaseService.Message) { init { run { val type = message.data.getString("type") ?: return@run @@ -36,7 +49,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, val message: message.data.getString("title") ?: "", message.data.getString("message") ?: "" ) - "appUpdate" -> UpdateWorker.runNow(app, app.gson.fromJson(message.data.get("update"), Update::class.java)) + "appUpdate" -> launch { UpdateWorker.runNow(app, app.gson.fromJson(message.data.get("update"), Update::class.java)) } } } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/sync/UpdateWorker.kt b/app/src/main/java/pl/szczodrzynski/edziennik/sync/UpdateWorker.kt index d9994cc9..f527f071 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/sync/UpdateWorker.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/sync/UpdateWorker.kt @@ -13,10 +13,7 @@ import android.text.Html import android.widget.Toast import androidx.core.app.NotificationCompat import androidx.work.* -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlinx.coroutines.launch +import kotlinx.coroutines.* import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi @@ -75,23 +72,23 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker( WorkManager.getInstance(app).cancelAllWorkByTag(TAG) } - fun runNow(app: App, overrideUpdate: Update? = null) { + suspend fun runNow(app: App, overrideUpdate: Update? = null) { try { - val update = if (overrideUpdate == null) { - val api = SzkolnyApi(app) - val response = api.getUpdate("beta") - if (response?.success != true) { - Toast.makeText(app, app.getString(R.string.notification_cant_check_update), Toast.LENGTH_SHORT).show() - return - } - val updates = response.data - if (updates?.isNotEmpty() != true) { - Toast.makeText(app, app.getString(R.string.notification_no_update), Toast.LENGTH_SHORT).show() - return - } - updates[0] - } - else overrideUpdate + val update = overrideUpdate + ?: run { + val response = withContext(Dispatchers.Default) { SzkolnyApi(app).getUpdate("beta") } + if (response?.success != true) { + Toast.makeText(app, app.getString(R.string.notification_cant_check_update), Toast.LENGTH_SHORT).show() + return@run null + } + val updates = response.data + if (updates?.isNotEmpty() != true) { + app.config.update = null + Toast.makeText(app, app.getString(R.string.notification_no_update), Toast.LENGTH_SHORT).show() + return@run null + } + updates[0] + } ?: return app.config.update = update @@ -123,7 +120,7 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker( private val job = Job() override val coroutineContext: CoroutineContext - get() = job + Dispatchers.Default + get() = job + Dispatchers.Main override fun doWork(): Result { Utils.d(TAG, "Running worker ID ${params.id}") @@ -139,4 +136,15 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker( rescheduleNext(this.context) return Result.success() } + + class JavaWrapper(app: App) : CoroutineScope { + private val job = Job() + override val coroutineContext: CoroutineContext + get() = job + Dispatchers.Main + init { + launch { + runNow(app) + } + } + } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java index d88aec98..3c40a831 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java @@ -1211,7 +1211,7 @@ public class SettingsNewFragment extends MaterialAboutFragment { .show(); } else { AsyncTask.execute(() -> { - UpdateWorker.Companion.runNow(app, null); + new UpdateWorker.JavaWrapper(app); }); } })