From 54d96232bc94d90a2d5794bad61fbe34470d325f Mon Sep 17 00:00:00 2001 From: Niko Diamadis Date: Wed, 28 Dec 2022 00:35:34 +0100 Subject: [PATCH] Implement general coroutine helper method --- app/src/main/java/com/github/gotify/Utils.kt | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/gotify/Utils.kt b/app/src/main/java/com/github/gotify/Utils.kt index 84e0f25..154bec8 100644 --- a/app/src/main/java/com/github/gotify/Utils.kt +++ b/app/src/main/java/com/github/gotify/Utils.kt @@ -7,14 +7,14 @@ import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable import android.text.format.DateUtils import android.view.View +import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope import com.github.gotify.client.JSON import com.github.gotify.log.Log import com.google.android.material.snackbar.Snackbar import com.google.gson.Gson import com.squareup.picasso.Picasso.LoadedFrom import com.squareup.picasso.Target -import okio.Buffer -import org.threeten.bp.OffsetDateTime import java.io.BufferedReader import java.io.IOException import java.io.InputStream @@ -23,6 +23,12 @@ import java.net.MalformedURLException import java.net.URI import java.net.URISyntaxException 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 { val JSON: Gson = JSON().gson @@ -95,6 +101,15 @@ internal object Utils { 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 loaded(drawable: Drawable?) }