mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2025-01-31 13:48:20 +01:00
[Updates] Fix no update toast not visible.
This commit is contained in:
parent
8cc594d170
commit
111d040cf9
@ -5,6 +5,10 @@
|
|||||||
package pl.szczodrzynski.edziennik.data.firebase
|
package pl.szczodrzynski.edziennik.data.firebase
|
||||||
|
|
||||||
import com.google.gson.JsonParser
|
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.*
|
||||||
import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update
|
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
|
||||||
@ -15,8 +19,17 @@ import pl.szczodrzynski.edziennik.data.db.entity.Profile
|
|||||||
import pl.szczodrzynski.edziennik.sync.UpdateWorker
|
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
|
||||||
|
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 {
|
init {
|
||||||
run {
|
run {
|
||||||
val type = message.data.getString("type") ?: return@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("title") ?: "",
|
||||||
message.data.getString("message") ?: ""
|
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)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,10 +13,7 @@ import android.text.Html
|
|||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.work.*
|
import androidx.work.*
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
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
|
||||||
@ -75,23 +72,23 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker(
|
|||||||
WorkManager.getInstance(app).cancelAllWorkByTag(TAG)
|
WorkManager.getInstance(app).cancelAllWorkByTag(TAG)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun runNow(app: App, overrideUpdate: Update? = null) {
|
suspend fun runNow(app: App, overrideUpdate: Update? = null) {
|
||||||
try {
|
try {
|
||||||
val update = if (overrideUpdate == null) {
|
val update = overrideUpdate
|
||||||
val api = SzkolnyApi(app)
|
?: run {
|
||||||
val response = api.getUpdate("beta")
|
val response = withContext(Dispatchers.Default) { SzkolnyApi(app).getUpdate("beta") }
|
||||||
if (response?.success != true) {
|
if (response?.success != true) {
|
||||||
Toast.makeText(app, app.getString(R.string.notification_cant_check_update), Toast.LENGTH_SHORT).show()
|
Toast.makeText(app, app.getString(R.string.notification_cant_check_update), Toast.LENGTH_SHORT).show()
|
||||||
return
|
return@run null
|
||||||
}
|
}
|
||||||
val updates = response.data
|
val updates = response.data
|
||||||
if (updates?.isNotEmpty() != true) {
|
if (updates?.isNotEmpty() != true) {
|
||||||
|
app.config.update = null
|
||||||
Toast.makeText(app, app.getString(R.string.notification_no_update), Toast.LENGTH_SHORT).show()
|
Toast.makeText(app, app.getString(R.string.notification_no_update), Toast.LENGTH_SHORT).show()
|
||||||
return
|
return@run null
|
||||||
}
|
}
|
||||||
updates[0]
|
updates[0]
|
||||||
}
|
} ?: return
|
||||||
else overrideUpdate
|
|
||||||
|
|
||||||
app.config.update = update
|
app.config.update = update
|
||||||
|
|
||||||
@ -123,7 +120,7 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker(
|
|||||||
|
|
||||||
private val job = Job()
|
private val job = Job()
|
||||||
override val coroutineContext: CoroutineContext
|
override val coroutineContext: CoroutineContext
|
||||||
get() = job + Dispatchers.Default
|
get() = job + Dispatchers.Main
|
||||||
|
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
Utils.d(TAG, "Running worker ID ${params.id}")
|
Utils.d(TAG, "Running worker ID ${params.id}")
|
||||||
@ -139,4 +136,15 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker(
|
|||||||
rescheduleNext(this.context)
|
rescheduleNext(this.context)
|
||||||
return Result.success()
|
return Result.success()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class JavaWrapper(app: App) : CoroutineScope {
|
||||||
|
private val job = Job()
|
||||||
|
override val coroutineContext: CoroutineContext
|
||||||
|
get() = job + Dispatchers.Main
|
||||||
|
init {
|
||||||
|
launch {
|
||||||
|
runNow(app)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1211,7 +1211,7 @@ public class SettingsNewFragment extends MaterialAboutFragment {
|
|||||||
.show();
|
.show();
|
||||||
} else {
|
} else {
|
||||||
AsyncTask.execute(() -> {
|
AsyncTask.execute(() -> {
|
||||||
UpdateWorker.Companion.runNow(app, null);
|
new UpdateWorker.JavaWrapper(app);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user