Implement general coroutine helper method

This commit is contained in:
Niko Diamadis
2022-12-28 00:35:34 +01:00
parent f36c93ef9a
commit 54d96232bc

View File

@@ -7,14 +7,14 @@ import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.text.format.DateUtils import android.text.format.DateUtils
import android.view.View import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.github.gotify.client.JSON import com.github.gotify.client.JSON
import com.github.gotify.log.Log import com.github.gotify.log.Log
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.google.gson.Gson import com.google.gson.Gson
import com.squareup.picasso.Picasso.LoadedFrom import com.squareup.picasso.Picasso.LoadedFrom
import com.squareup.picasso.Target import com.squareup.picasso.Target
import okio.Buffer
import org.threeten.bp.OffsetDateTime
import java.io.BufferedReader import java.io.BufferedReader
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
@@ -23,6 +23,12 @@ import java.net.MalformedURLException
import java.net.URI import java.net.URI
import java.net.URISyntaxException import java.net.URISyntaxException
import java.net.URL import java.net.URL
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okio.Buffer
import org.threeten.bp.OffsetDateTime
internal object Utils { internal object Utils {
val JSON: Gson = JSON().gson val JSON: Gson = JSON().gson
@@ -95,6 +101,15 @@ internal object Utils {
return if (str == null) null else Buffer().writeUtf8(str).inputStream() return if (str == null) null else Buffer().writeUtf8(str).inputStream()
} }
fun AppCompatActivity.launchCoroutine(
dispatcher: CoroutineDispatcher = Dispatchers.IO,
action: suspend (coroutineScope: CoroutineScope) -> Unit
) {
this.lifecycleScope.launch(dispatcher) {
action(this)
}
}
fun interface DrawableReceiver { fun interface DrawableReceiver {
fun loaded(drawable: Drawable?) fun loaded(drawable: Drawable?)
} }