diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/activities/DebugActivity.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/activities/DebugActivity.kt | 59 |
1 files changed, 28 insertions, 31 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/DebugActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/DebugActivity.kt index 6257e6f1..a1b41830 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/DebugActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/DebugActivity.kt @@ -22,6 +22,7 @@ import android.content.Intent import android.content.res.ColorStateList import android.os.Bundle import ca.allanwang.kau.internal.KauBaseActivity +import ca.allanwang.kau.utils.launchMain import ca.allanwang.kau.utils.setIcon import ca.allanwang.kau.utils.visible import com.mikepenz.google_material_typeface_library.GoogleMaterial @@ -32,12 +33,12 @@ import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.createFreshDir import com.pitchedapps.frost.utils.setFrostColors -import io.reactivex.Single -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.schedulers.Schedulers import kotlinx.android.synthetic.main.activity_debug.* import kotlinx.android.synthetic.main.view_main_fab.* +import kotlinx.coroutines.CoroutineExceptionHandler import java.io.File +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine /** * Created by Allan Wang on 05/01/18. @@ -74,36 +75,32 @@ class DebugActivity : KauBaseActivity() { fab.setOnClickListener { _ -> fab.hide() - val parent = baseDir(this) - parent.createFreshDir() - val rxScreenshot = Single.fromCallable { - debug_webview.getScreenshot(File(parent, "screenshot.png")) - }.subscribeOn(Schedulers.io()) - val rxBody = Single.create<String> { emitter -> - debug_webview.evaluateJavascript(JsActions.RETURN_BODY.function) { - emitter.onSuccess(it) - } - }.subscribeOn(AndroidSchedulers.mainThread()) - Single.zip(listOf(rxScreenshot, rxBody)) { - val screenshot = it[0] == true - val body = it[1] as? String - screenshot to body - }.observeOn(AndroidSchedulers.mainThread()) - .subscribe { (screenshot, body), err -> - if (err != null) { - L.e { "DebugActivity error ${err.message}" } - setResult(Activity.RESULT_CANCELED) - finish() - return@subscribe + val errorHandler = CoroutineExceptionHandler { _, throwable -> + L.e { "DebugActivity error ${throwable.message}" } + setResult(Activity.RESULT_CANCELED) + finish() + } + + launchMain(errorHandler) { + val parent = baseDir(this@DebugActivity) + parent.createFreshDir() + + val body: String? = suspendCoroutine { cont -> + debug_webview.evaluateJavascript(JsActions.RETURN_BODY.function) { + cont.resume(it) } - val intent = Intent() - intent.putExtra(RESULT_URL, debug_webview.url) - intent.putExtra(RESULT_SCREENSHOT, screenshot) - if (body != null) - intent.putExtra(RESULT_BODY, body) - setResult(Activity.RESULT_OK, intent) - finish() } + + val hasScreenshot: Boolean = debug_webview.getScreenshot(File(parent, "screenshot.png")) + + val intent = Intent() + intent.putExtra(RESULT_URL, debug_webview.url) + intent.putExtra(RESULT_SCREENSHOT, hasScreenshot) + if (body != null) + intent.putExtra(RESULT_BODY, body) + setResult(Activity.RESULT_OK, intent) + finish() + } } } |