From 2fc0bb1a03f624e297d2afdeb95231cf906afc21 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 14 Sep 2020 14:27:55 +0200 Subject: coroutines: convert low-hanging fruits Signed-off-by: Jason A. Donenfeld --- .../com/wireguard/android/preference/VersionPreference.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt') diff --git a/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt b/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt index 0734df45..f944233b 100644 --- a/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt @@ -16,6 +16,10 @@ import com.wireguard.android.R import com.wireguard.android.backend.Backend import com.wireguard.android.backend.GoBackend import com.wireguard.android.backend.WgQuickBackend +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.util.Locale class VersionPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) { @@ -45,11 +49,12 @@ class VersionPreference(context: Context, attrs: AttributeSet?) : Preference(con init { Application.getBackendAsync().thenAccept { backend -> versionSummary = getContext().getString(R.string.version_summary_checking, getBackendPrettyName(context, backend).toLowerCase(Locale.ENGLISH)) - Application.getAsyncWorker().supplyAsync(backend::getVersion).whenComplete { version, exception -> - versionSummary = if (exception == null) - getContext().getString(R.string.version_summary, getBackendPrettyName(context, backend), version) - else + CoroutineScope(Dispatchers.Main).launch { + versionSummary = try { + getContext().getString(R.string.version_summary, getBackendPrettyName(context, backend), withContext(Dispatchers.IO) { backend.version }) + } catch (_: Exception) { getContext().getString(R.string.version_summary_unknown, getBackendPrettyName(context, backend).toLowerCase(Locale.ENGLISH)) + } notifyChanged() } } -- cgit v1.2.3-59-g8ed1b