[Updates] Fix no update toast not visible.

This commit is contained in:
Kuba Szczodrzyński 2020-01-20 19:27:59 +01:00
parent 8cc594d170
commit 111d040cf9
3 changed files with 45 additions and 24 deletions

View File

@ -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<Profile>, 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<Profile>, 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<Profile>, 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)) }
}
}
}

View File

@ -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)
}
}
}
}

View File

@ -1211,7 +1211,7 @@ public class SettingsNewFragment extends MaterialAboutFragment {
.show();
} else {
AsyncTask.execute(() -> {
UpdateWorker.Companion.runNow(app, null);
new UpdateWorker.JavaWrapper(app);
});
}
})