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) {
|
fun getPage(id: String?, callback: (page: Page?) -> Unit) {
|
||||||
Bridge.get("${ApiBase}getPage/$id?return_content=true").asJsonObject { response, jsonObject, bridgeException ->
|
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? {
|
private fun JSONObject.parsePage(): Page? {
|
||||||
val result: Page = Page()
|
val result: Page = Page()
|
||||||
if (optBoolean("ok", false)) {
|
if (optBoolean("ok", false)) {
|
||||||
optJSONObject("result").let {
|
optJSONObject("result")?.let {
|
||||||
result.path = it.optString("path", "")
|
result.path = it.optString("path", "")
|
||||||
result.url = it.optString("url", "")
|
result.url = it.optString("url", "")
|
||||||
result.title = it.optString("title", "")
|
result.title = it.optString("title", "")
|
||||||
|
@ -34,22 +35,30 @@ class Api {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun JSONArray.parseContent(result: Page) {
|
private fun JSONArray.parseContent(result: Page) {
|
||||||
for (child in this) {
|
try {
|
||||||
if (child is String) result.content += child
|
for (child in this) {
|
||||||
if (child is JSONObject) {
|
try {
|
||||||
result.content += "<${child.optString("tag", "")}"
|
if (child is String) result.content += child
|
||||||
child.optJSONObject("attrs")?.let {
|
else if (child is JSONObject) {
|
||||||
for (key in it.keys()) {
|
result.content += "<${child.optString("tag", "")}"
|
||||||
result.content += " $key=\"${it.optString(key, "")}\""
|
child.optJSONObject("attrs")?.let {
|
||||||
}
|
for (key in it.keys()) {
|
||||||
for (i in 0 until length()) {
|
result.content += " $key=\"${it.optString(key, "")}\""
|
||||||
result.content += "${it.names()}"
|
}
|
||||||
|
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
|
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")) {
|
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) {
|
||||||
Api().getPage(intent.dataString.split("/").last()) { page ->
|
Api().getPage(intent.dataString.split("/").last()) { page ->
|
||||||
page?.let {
|
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)
|
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue