diff options
author | Allan Wang <me@allanwang.ca> | 2018-12-31 18:57:28 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-31 18:57:28 -0500 |
commit | 149c6be1bfd4bd84381757940fece1be7b9801aa (patch) | |
tree | 85fe10e3ee3ea34ad717f0d61975ca0119dd36d5 /app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt | |
parent | 7661bbfc9b8f34bf9d92dc08a9fcd7cc6ec7cbb3 (diff) | |
download | frost-149c6be1bfd4bd84381757940fece1be7b9801aa.tar.gz frost-149c6be1bfd4bd84381757940fece1be7b9801aa.tar.bz2 frost-149c6be1bfd4bd84381757940fece1be7b9801aa.zip |
Enhancement/coroutines (#1273)
* Convert rest of fbcookie to suspended methods
* Replace active checks with yield
* Apply spotless
* Switch cookie domain to exact url
* Optimize imports and enable travis tests again
* Update proguard rules
* Remove unnecessary yield
* Remove unused flyweight
* Remove unused disposable and method
* Use contexthelper instead of dispatcher main
* Convert login activity to coroutines
* Use kau helper methods for coroutines
* Enhancement/offline site (#1288)
* Begin conversion of offline site logic
* Fix offline tests and add validation tests
* Ignore cookie in jsoup if it is blank
* Force load and zip to be in io
* Use different zip files to fix tests
* Log all test output
* Do not log stdout
* Allow test skip for fb offline
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt index cca7ace0..3f92c41d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -70,6 +70,9 @@ import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.FbUrlFormatter.Companion.VIDEO_REDIRECT import com.pitchedapps.frost.facebook.USER_AGENT_BASIC import com.pitchedapps.frost.facebook.formattedFbUrl +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import org.apache.commons.text.StringEscapeUtils import org.jsoup.Jsoup import org.jsoup.nodes.Element @@ -88,6 +91,14 @@ const val ARG_IMAGE_URL = "arg_image_url" const val ARG_TEXT = "arg_text" const val ARG_COOKIE = "arg_cookie" +/** + * Most context items implement [CoroutineScope] by default. + * We will add a fallback just in case. + * It is expected that the scope returned always has the Android main dispatcher as part of the context. + */ +internal inline val Context.ctxCoroutine: CoroutineScope + get() = this as? CoroutineScope ?: GlobalScope + inline fun <reified T : Activity> Context.launchNewTask( cookieList: ArrayList<CookieModel> = arrayListOf(), clearStack: Boolean = false @@ -116,7 +127,9 @@ private inline fun <reified T : WebOverlayActivityBase> Context.launchWebOverlay L.v { "Launch received: $url\nLaunch web overlay: $argUrl" } if (argUrl.isFacebookUrl && argUrl.contains("/logout.php")) { L.d { "Logout php found" } - FbCookie.logout(this) + ctxCoroutine.launch { + FbCookie.logout(this@launchWebOverlayImpl) + } } else if (!(Prefs.linksInDefaultApp && resolveActivityForUri(Uri.parse(argUrl)))) startActivity<T>(false, intentBuilder = { putExtra(ARG_URL, argUrl) @@ -375,10 +388,8 @@ fun frostJsoup(url: String) = frostJsoup(FbCookie.webCookie, url) fun frostJsoup(cookie: String?, url: String) = Jsoup.connect(url).run { - if (cookie != null) cookie( - FACEBOOK_COM, - cookie - ) else this + if (cookie.isNullOrBlank()) this + else cookie(FACEBOOK_COM, cookie) }.userAgent(USER_AGENT_BASIC).get()!! fun Element.first(vararg select: String): Element? { |