Skip to content

Commit

Permalink
refactor: addToPocket to coroutine, and support open article link aft…
Browse files Browse the repository at this point in the history
…er saved.
  • Loading branch information
plateaukao committed Apr 8, 2023
1 parent e0513a2 commit 88c7313
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar
import info.plateaukao.einkbro.R
import info.plateaukao.einkbro.browser.AlbumController
import info.plateaukao.einkbro.browser.BrowserContainer
Expand Down Expand Up @@ -268,9 +269,20 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
runOnUiThread { addNewTab(authUrl) }
}
} else {
pocketNetwork.addUrlToPocket(config.pocketAccessToken, url) { success ->
runOnUiThread {
NinjaToast.showShort(this, if (success) "Added" else "Failed")
lifecycleScope.launch {
val resolvedUrl = pocketNetwork.addUrlToPocket(config.pocketAccessToken, url)
if (resolvedUrl == null) {
NinjaToast.showShort(this@BrowserActivity, "Failed")
} else {
Snackbar.make(
binding.root,
"Added",
Snackbar.LENGTH_SHORT
).apply {
setAction("Go to Pocket article url") {
addNewTab(resolvedUrl)
}
}.show()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package info.plateaukao.einkbro.pocket

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.Call
import okhttp3.Callback
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okio.IOException
import org.json.JSONObject

class PocketNetwork {
private val client = OkHttpClient()
Expand Down Expand Up @@ -68,7 +71,13 @@ class PocketNetwork {
})
}

fun addUrlToPocket(accessToken: String, url: String, title: String? = null, tags: String? = null, callback: (Boolean) -> Unit) {
fun addUrlToPocket(
accessToken: String,
url: String,
title: String? = null,
tags: String? = null,
callback: (Boolean) -> Unit
) {
val requestBodyBuilder = FormBody.Builder()
.add("url", url)
.add("consumer_key", consumerKey)
Expand Down Expand Up @@ -100,4 +109,61 @@ class PocketNetwork {
}
})
}

@OptIn(ExperimentalCoroutinesApi::class)
suspend fun addUrlToPocket(
accessToken: String,
url: String,
title: String? = null,
tags: String? = null
): String? {
return suspendCancellableCoroutine { continuation ->
val requestBodyBuilder = FormBody.Builder()
.add("url", url)
.add("consumer_key", consumerKey)
.add("access_token", accessToken)

title?.let {
requestBodyBuilder.add("title", it)
}

tags?.let {
requestBodyBuilder.add("tags", it)
}

val requestBody = requestBodyBuilder.build()

val request = Request.Builder()
.url("https://getpocket.com/v3/add")
.post(requestBody)
.build()

client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
if (continuation.isActive) {
continuation.resume(null) {}
}
}

override fun onResponse(call: Call, response: Response) {
if (continuation.isActive) {
val body = response.body?.string()
if (body != null) {
val jsonResponse = JSONObject(body)
val item = jsonResponse.getJSONObject("item")
val articleUrl =
"https://getpocket.com/read/" + item.getString("item_id")
continuation.resume(articleUrl) {}
} else {
continuation.resume(null) {}
}
}
}
})

continuation.invokeOnCancellation {
client.dispatcher.executorService.shutdownNow()
}
}
}
}

0 comments on commit 88c7313

Please sign in to comment.