Commit a8e5756b authored by Joey's avatar Joey

core, data, export, glucose: remove usage of GlobalScope

Signed-off-by: Joey's avatarJoey <bevilacquajoey@gmail.com>
Change-Id: I36033444c4a1c9c71cc270a9b9b9346e76c7bbd7
parent 7c7840c6
......@@ -11,15 +11,16 @@ package it.diab.core.util.extensions
import android.widget.TextView
import androidx.core.text.PrecomputedTextCompat
import androidx.core.widget.TextViewCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
fun TextView.setPrecomputedText(text: CharSequence) {
val params = TextViewCompat.getTextMetricsParams(this)
GlobalScope.launch(Dispatchers.Default) {
val scope = CoroutineScope(Dispatchers.Default)
scope.launch {
val textDef = async { PrecomputedTextCompat.getTextFuture(text, params, null).get() }
launch(Dispatchers.Main) { this@setPrecomputedText.text = textDef.await() }
}
......
......@@ -21,10 +21,10 @@ import it.diab.data.entities.TimeFrame
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.io.BufferedReader
import java.io.ByteArrayInputStream
......@@ -40,7 +40,7 @@ import java.util.zip.ZipInputStream
class PluginManager(context: Context) {
private val job = Job()
private val scope = CoroutineScope(IO + job)
private val scope = CoroutineScope(Dispatchers.Default + job)
private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
......@@ -52,7 +52,7 @@ class PluginManager(context: Context) {
fun isInstalled() = pluginDir.exists() && pluginDir.list().isNotEmpty()
fun install(iStream: InputStream) {
scope.launch {
scope.launch(IO) {
val pattern = Pattern.compile("^estimator_[0-6].json")
var wasValid = false
......@@ -82,7 +82,7 @@ class PluginManager(context: Context) {
}
fun uninstall() {
scope.launch {
scope.launch(IO) {
pluginDir.deleteRecursively()
preferences[LAST_UPDATE] = 0L
}
......@@ -99,11 +99,11 @@ class PluginManager(context: Context) {
val value = glucose.value / 10 * 10
if (value <= LOWEST_SUGGESTION) {
GlobalScope.launch(Dispatchers.Main) { onExecuted(TOO_LOW) }
withContext(Dispatchers.Main) { onExecuted(TOO_LOW) }
return
}
if (value >= HIGHEST_SUGGESTION) {
GlobalScope.launch(Dispatchers.Main) { onExecuted(TOO_HIGH) }
withContext(Dispatchers.Main) { onExecuted(TOO_HIGH) }
return
}
......@@ -112,7 +112,7 @@ class PluginManager(context: Context) {
val result = map[value] ?: PARSE_ERROR
GlobalScope.launch(Dispatchers.Main) {
withContext(Dispatchers.Main) {
onExecuted(if (result == PARSE_ERROR) PARSE_ERROR else result + glucose.eatLevel - 1)
}
}
......
......@@ -28,6 +28,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class ExportService : Service() {
private lateinit var glucoseRepository: GlucoseRepository
......@@ -132,7 +133,7 @@ class ExportService : Service() {
serviceScope.launch {
val writer = MlWriter(descriptor, glucoseRepository, lowThreshold..highThreshold)
val result = writer.export()
GlobalScope.launch(Dispatchers.Main) { onTaskCompleted(result) }
withContext(Dispatchers.Main) { onTaskCompleted(result) }
}
}
......
......@@ -19,7 +19,6 @@ import it.diab.data.repositories.InsulinRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
......@@ -68,14 +67,14 @@ class EditorViewModel internal constructor(
if (pluginManager.isInstalled()) {
viewModelScope.launch { pluginManager.fetchSuggestion(glucose, block) }
} else {
GlobalScope.launch(Dispatchers.Main) { block(PluginManager.NO_MODEL) }
block(PluginManager.NO_MODEL)
}
}
fun applyInsulinSuggestion(value: Float, insulin: Insulin, block: () -> Unit) {
viewModelScope.launch {
runApplySuggestion(value, insulin)
GlobalScope.launch(Dispatchers.Main) { block() }
withContext(Dispatchers.Main) { block() }
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment