From 495df6c84bb7a0daf8cea789f03396b6dabcdf2d Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 5 Jan 2019 00:10:25 -0500 Subject: Remove reactivex from debugger --- .../pitchedapps/frost/activities/DebugActivity.kt | 59 ++++++++++------------ .../pitchedapps/frost/activities/LoginActivity.kt | 23 +++++++-- .../com/pitchedapps/frost/dbflow/CookiesDb.kt | 24 --------- .../frost/facebook/requests/FbRequest.kt | 16 ------ .../com/pitchedapps/frost/web/DebugWebView.kt | 13 +++-- 5 files changed, 55 insertions(+), 80 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 { 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() + } } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt index 27a488e3..f3eb8fe6 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt @@ -33,9 +33,10 @@ import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.target.Target import com.pitchedapps.frost.R import com.pitchedapps.frost.dbflow.CookieModel -import com.pitchedapps.frost.dbflow.fetchUsername import com.pitchedapps.frost.dbflow.loadFbCookiesSuspend +import com.pitchedapps.frost.dbflow.saveFbCookie import com.pitchedapps.frost.facebook.FbCookie +import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.profilePictureUrl import com.pitchedapps.frost.glide.FrostGlide import com.pitchedapps.frost.glide.GlideApp @@ -43,6 +44,7 @@ import com.pitchedapps.frost.glide.transform import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Showcase import com.pitchedapps.frost.utils.frostEvent +import com.pitchedapps.frost.utils.frostJsoup import com.pitchedapps.frost.utils.launchNewTask import com.pitchedapps.frost.utils.logFrostEvent import com.pitchedapps.frost.utils.setFrostColors @@ -55,6 +57,8 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout +import java.net.UnknownHostException import kotlin.coroutines.resume /** @@ -168,11 +172,22 @@ class LoginActivity : BaseActivity() { } private suspend fun loadUsername(cookie: CookieModel): String = withContext(Dispatchers.IO) { - suspendCancellableCoroutine { cont -> - cookie.fetchUsername { - cont.resume(it) + val result: String = try { + withTimeout(5000) { + frostJsoup(cookie.cookie, FbItem.PROFILE.url).title() } + } catch (e: Exception) { + if (e !is UnknownHostException) + e.logFrostEvent("Fetch username failed") + "" } + + if (cookie.name?.isNotBlank() == false && result != cookie.name) { + cookie.name = result + saveFbCookie(cookie) + } + + cookie.name ?: "" } override fun backConsumer(): Boolean { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt index d7dd71ed..10746b75 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt @@ -17,11 +17,7 @@ package com.pitchedapps.frost.dbflow import android.os.Parcelable -import com.pitchedapps.frost.dbflow.CookieModel_Table.cookie -import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.utils.L -import com.pitchedapps.frost.utils.frostJsoup -import com.pitchedapps.frost.utils.logFrostEvent import com.raizlabs.android.dbflow.annotation.ConflictAction import com.raizlabs.android.dbflow.annotation.Database import com.raizlabs.android.dbflow.annotation.PrimaryKey @@ -37,8 +33,6 @@ import com.raizlabs.android.dbflow.structure.BaseModel import kotlinx.android.parcel.Parcelize import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -import kotlinx.coroutines.withTimeoutOrNull -import java.net.UnknownHostException /** * Created by Allan Wang on 2017-05-30. @@ -95,22 +89,4 @@ fun removeCookie(id: Long) { L.d { "Fb cookie deleted" } L._d { id } } -} - -suspend fun CookieModel.fetchUsername(): String? = withContext(Dispatchers.IO) { - withTimeoutOrNull(5000) { - var result: String? = null - try { - result = frostJsoup(cookie, FbItem.PROFILE.url).title() - L.d { "Fetch username found" } - } catch (e: Exception) { - if (e !is UnknownHostException) - e.logFrostEvent("Fetch username failed") - } - if (name?.isNotBlank() == false && result != null && result != name) { - name = result - saveFbCookie(this@fetchUsername) - } - result - } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt index 67a03ad4..b49fd970 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt @@ -26,8 +26,6 @@ import com.pitchedapps.frost.facebook.USER_AGENT_BASIC import com.pitchedapps.frost.facebook.get import com.pitchedapps.frost.kotlin.Flyweight import com.pitchedapps.frost.utils.L -import io.reactivex.Single -import io.reactivex.schedulers.Schedulers import kotlinx.coroutines.GlobalScope import okhttp3.Call import okhttp3.FormBody @@ -35,7 +33,6 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.logging.HttpLoggingInterceptor import org.apache.commons.text.StringEscapeUtils -import java.lang.Exception /** * Created by Allan Wang on 21/12/17. @@ -142,19 +139,6 @@ fun String.getAuth(): RequestAuth { return auth } -inline fun Array.zip( - crossinline mapper: (List) -> O, - crossinline caller: (T) -> R -): Single { - if (isEmpty()) - return Single.just(mapper(emptyList())) - val singles = map { Single.fromCallable { caller(it) }.subscribeOn(Schedulers.io()) } - return Single.zip(singles) { - val results = it.mapNotNull { it as? R } - mapper(results) - } -} - /** * Execute the call and attempt to check validity * Valid = not blank & no "error" instance diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/DebugWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/DebugWebView.kt index 22668309..d2b53ab5 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/DebugWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/DebugWebView.kt @@ -23,7 +23,6 @@ import android.graphics.Color import android.util.AttributeSet import android.view.View import android.webkit.WebView -import androidx.annotation.WorkerThread import ca.allanwang.kau.utils.withAlpha import com.pitchedapps.frost.facebook.USER_AGENT_BASIC import com.pitchedapps.frost.injectors.CssAssets @@ -33,6 +32,8 @@ import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.createFreshFile import com.pitchedapps.frost.utils.isFacebookUrl +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.io.File /** @@ -61,14 +62,16 @@ class DebugWebView @JvmOverloads constructor( isDrawingCacheEnabled = true } - @WorkerThread - fun getScreenshot(output: File): Boolean { + /** + * Fetches a screenshot of the current webview, returning true if successful, false otherwise. + */ + suspend fun getScreenshot(output: File): Boolean = withContext(Dispatchers.IO) { if (!output.createFreshFile()) { L.e { "Failed to create ${output.absolutePath} for debug screenshot" } - return false + return@withContext false } - return try { + try { output.outputStream().use { drawingCache.compress(Bitmap.CompressFormat.PNG, 100, it) } -- cgit v1.2.3