mirror of https://github.com/jlelse/teleposter
Some more fixes
This commit is contained in:
parent
843ccc020c
commit
006e1bef2b
|
@ -10,14 +10,15 @@ class Api {
|
|||
|
||||
fun getPage(id: String?, callback: (page: Page?) -> Unit) {
|
||||
Bridge.get("${ApiBase}getPage/$id?return_content=true").asJsonObject { response, jsonObject, bridgeException ->
|
||||
callback(jsonObject?.parsePage())
|
||||
if (jsonObject != null) callback(jsonObject.parsePage())
|
||||
else callback(null)
|
||||
}
|
||||
}
|
||||
|
||||
private fun JSONObject.parsePage(): Page? {
|
||||
val result: Page = Page()
|
||||
if (optBoolean("ok", false)) {
|
||||
optJSONObject("result").let {
|
||||
optJSONObject("result")?.let {
|
||||
result.path = it.optString("path", "")
|
||||
result.url = it.optString("url", "")
|
||||
result.title = it.optString("title", "")
|
||||
|
@ -34,22 +35,30 @@ class Api {
|
|||
}
|
||||
|
||||
private fun JSONArray.parseContent(result: Page) {
|
||||
for (child in this) {
|
||||
if (child is String) result.content += child
|
||||
if (child is JSONObject) {
|
||||
result.content += "<${child.optString("tag", "")}"
|
||||
child.optJSONObject("attrs")?.let {
|
||||
for (key in it.keys()) {
|
||||
result.content += " $key=\"${it.optString(key, "")}\""
|
||||
}
|
||||
for (i in 0 until length()) {
|
||||
result.content += "${it.names()}"
|
||||
try {
|
||||
for (child in this) {
|
||||
try {
|
||||
if (child is String) result.content += child
|
||||
else if (child is JSONObject) {
|
||||
result.content += "<${child.optString("tag", "")}"
|
||||
child.optJSONObject("attrs")?.let {
|
||||
for (key in it.keys()) {
|
||||
result.content += " $key=\"${it.optString(key, "")}\""
|
||||
}
|
||||
for (i in 0 until length()) {
|
||||
result.content += "${it.names()}"
|
||||
}
|
||||
}
|
||||
result.content += ">"
|
||||
child.optJSONArray("children").parseContent(result)
|
||||
result.content += "</${child.optString("tag", "")}>"
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
result.content += ">"
|
||||
child.optJSONArray("children").parseContent(result)
|
||||
result.content += "</${child.optString("tag", "")}>"
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,4 +2,4 @@ package telegra.ph
|
|||
|
||||
import org.json.JSONArray
|
||||
|
||||
operator fun JSONArray.iterator(): Iterator<Any> = (0 until length()).asSequence().map { opt(it) }.iterator()
|
||||
operator fun JSONArray.iterator(): Iterator<Any> = (0..length() - 1).map { opt(it) }.iterator()
|
|
@ -30,7 +30,10 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) {
|
||||
Api().getPage(intent.dataString.split("/").last()) { page ->
|
||||
page?.let {
|
||||
val html = "<h1>${it.title}</h1>${it.content}"
|
||||
var html = "<h1>${it.title}</h1>"
|
||||
if (!it.author_name.isNullOrEmpty() && !it.author_url.isNullOrBlank()) html += "<a href=\"${it.author_url}\">${it.author_name}</a><br>"
|
||||
else if (!it.author_name.isNullOrEmpty()) html += "${it.author_name}<br>"
|
||||
html += it.content
|
||||
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue