aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/src/main/java/com/wireguard/android/util/Extensions.kt
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-09-15 23:30:20 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2020-09-15 23:46:11 +0200
commit49ac61304e5a244c674028d279f70f9631351f9c (patch)
treee196a0fb2cddb9674f6f72e50801a57e05599140 /ui/src/main/java/com/wireguard/android/util/Extensions.kt
parentMonkeyedTextInputEditText: au revoir (diff)
downloadwireguard-android-49ac61304e5a244c674028d279f70f9631351f9c.tar.xz
wireguard-android-49ac61304e5a244c674028d279f70f9631351f9c.zip
coroutines: use lifecycleScope where appropriate
There's still a bit of GlobalScope lingering around, which might be removable. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/util/Extensions.kt')
-rw-r--r--ui/src/main/java/com/wireguard/android/util/Extensions.kt10
1 files changed, 10 insertions, 0 deletions
diff --git a/ui/src/main/java/com/wireguard/android/util/Extensions.kt b/ui/src/main/java/com/wireguard/android/util/Extensions.kt
index a705401..9106ec7 100644
--- a/ui/src/main/java/com/wireguard/android/util/Extensions.kt
+++ b/ui/src/main/java/com/wireguard/android/util/Extensions.kt
@@ -9,6 +9,10 @@ import android.content.Context
import android.util.TypedValue
import androidx.annotation.AttrRes
import androidx.fragment.app.Fragment
+import androidx.lifecycle.lifecycleScope
+import androidx.preference.Preference
+import com.wireguard.android.activity.SettingsActivity
+import kotlinx.coroutines.CoroutineScope
fun Context.resolveAttribute(@AttrRes attrRes: Int): Int {
val typedValue = TypedValue()
@@ -19,3 +23,9 @@ fun Context.resolveAttribute(@AttrRes attrRes: Int): Int {
fun Fragment.requireTargetFragment(): Fragment {
return requireNotNull(targetFragment) { "A target fragment should always be set for $this" }
}
+
+val Preference.activity: SettingsActivity
+ get() = if (this.context is SettingsActivity) this.context as SettingsActivity else throw IllegalStateException("Failed to resolve SettingsActivity")
+
+val Preference.lifecycleScope: CoroutineScope
+ get() = this.activity.lifecycleScope