Much progress

This commit is contained in:
Jan-Lk Else 2016-12-21 21:56:56 +01:00
parent b4d82140fe
commit 0a3191c07e
4 changed files with 68 additions and 33 deletions

View File

@ -8,24 +8,36 @@ class Api {
private val ApiBase = "https://api.telegra.ph/"
fun getPage(id: String?, accessToken: String?, callback: (page: Page?) -> Unit) {
Bridge.get("${ApiBase}getPage/$id?return_content=true" + if (accessToken != null) "&access_token=$accessToken" else "").asJsonObject { response, jsonObject, bridgeException ->
if (jsonObject != null) callback(jsonObject.parsePage())
else callback(null)
fun getPage(path: String?, accessToken: String?, callback: (success: Boolean, page: Page?) -> Unit) {
Bridge.get("${ApiBase}getPage/$path?access_token=%s&return_content=true", accessToken).asString { response, s, bridgeException ->
if (!s.isNullOrBlank() && bridgeException == null) try {
callback(true, JSONObject(s).parsePage())
} catch (e: Exception) {
callback(false, null)
}
else callback(false, null)
}
}
fun createPage(accessToken: String?, content: String?, title: String?, callback: (Page?) -> Unit) {
Bridge.get("${ApiBase}createPage?access_token=$accessToken&title=%s&content=$content&return_content=true", title).asJsonObject { response, jsonObject, bridgeException ->
if (jsonObject != null) callback(jsonObject.parsePage())
else callback(null)
fun createPage(accessToken: String?, content: String?, title: String?, callback: (success: Boolean, Page?) -> Unit) {
Bridge.get("${ApiBase}createPage?access_token=%s&title=%s&content=%s&return_content=true", accessToken, title, content).asString { response, s, bridgeException ->
if (!s.isNullOrBlank() && bridgeException == null) try {
callback(true, JSONObject(s).parsePage())
} catch (e: Exception) {
callback(false, null)
}
else callback(false, null)
}
}
fun editPage(accessToken: String?, path: String?, content: String?, title: String?, callback: (Page?) -> Unit) {
Bridge.get("${ApiBase}editPage/$path?access_token=$accessToken&title=%s&content=$content&return_content=true", title).asJsonObject { response, jsonObject, bridgeException ->
if (jsonObject != null) callback(jsonObject.parsePage())
else callback(null)
fun editPage(accessToken: String?, path: String?, content: String?, title: String?, callback: (success: Boolean, Page?) -> Unit) {
Bridge.get("${ApiBase}editPage/$path?access_token=%s&title=%s&content=%s&return_content=true", accessToken, title, content).asString { response, s, bridgeException ->
if (!s.isNullOrBlank() && bridgeException == null) try {
callback(true, JSONObject(s).parsePage())
} catch (e: Exception) {
callback(false, null)
}
else callback(false, null)
}
}

View File

@ -11,10 +11,6 @@ import com.afollestad.materialdialogs.MaterialDialog
import im.delight.android.webview.AdvancedWebView
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
private val htmlHead = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\"><style> * { max-width: 100%; height: auto; word-break: break-all; word-break: break-word; }</style></head><body>"
private val htmlEnd = "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></body></html>"
private val webView: AdvancedWebView? by lazy { findViewById(R.id.webView) as AdvancedWebView? }
private val editor: Editor? by lazy { findViewById(R.id.editor) as Editor? }
@ -37,6 +33,9 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
isVerticalScrollBarEnabled = false
overScrollMode = View.OVER_SCROLL_NEVER
}
if (accessToken().isNullOrBlank()) Api().createAccount { accessToken ->
if (accessToken != null) saveAccessToken(accessToken)
}
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last())
else loadEditor()
}
@ -51,12 +50,13 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
webView?.visibility = View.GONE
currentPage = null
// Load
if (path != null) Api().getPage(path, accessToken()) { page ->
runOnUiThread {
if (path != null) Api().getPage(path, accessToken()) { success, page ->
if (success) runOnUiThread {
isEdit = true
currentPage = page
editor?.setText(page?.content ?: "")
}
else showError()
}
}
}
@ -70,8 +70,9 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
editor?.visibility = View.GONE
currentPage = null
// Load
Api().getPage(path, accessToken()) { page ->
showPage(page)
Api().getPage(path, accessToken()) { success, page ->
if (success) showPage(page)
else showError()
}
}
}
@ -86,19 +87,29 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
currentPage = page
// Show
page?.let {
var html = htmlHead
var html = getString(R.string.viewer_html_head)
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>"
if (it.views != 0) html += "${it.views} times viewed<br><br>"
if (it.content.isNullOrBlank()) html += it.description.replace("\n", "<br>") else html += it.content
html += htmlEnd
html += getString(R.string.viewer_html_end)
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
currentUrl = it.url
}
}
}
private fun showError() {
runOnUiThread {
MaterialDialog.Builder(this)
.title(R.string.error)
.content(R.string.error_desc)
.positiveText(android.R.string.ok)
.show()
}
}
override fun onPageFinished(url: String?) {
}
@ -150,6 +161,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
super.onPrepareOptionsMenu(menu)
menu?.findItem(R.id.create)?.isVisible = !editorMode
menu?.findItem(R.id.share)?.isVisible = !editorMode
menu?.findItem(R.id.try_edit)?.isVisible = !editorMode && !canEdit
menu?.findItem(R.id.publish)?.isVisible = editorMode
menu?.findItem(R.id.edit)?.isVisible = canEdit
return true
@ -166,22 +178,20 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
MaterialDialog.Builder(this)
.title(R.string.title_question)
.input(getString(R.string.title_hint), currentPage?.title ?: "", { dialog, title ->
if (accessToken().isNullOrBlank()) {
Api().createAccount { accessToken ->
if (accessToken != null) saveAccessToken(accessToken)
if (isEdit) Api().editPage(accessToken(), currentPage?.path, json, title.toString()) { page -> showPage(page) }
else Api().createPage(accessToken(), json, title.toString()) { page -> showPage(page) }
}
} else {
if (isEdit) Api().editPage(accessToken(), currentPage?.path, json, title.toString()) { page -> showPage(page) }
else Api().createPage(accessToken(), json, title.toString()) { page -> showPage(page) }
if (isEdit) Api().editPage(accessToken(), currentPage?.path, json, title.toString()) { success, page ->
if (success) showPage(page)
else showError()
}
else Api().createPage(accessToken(), json, title.toString()) { success, page ->
if (success) showPage(page)
else showError()
}
})
.show()
}
true
}
R.id.edit -> {
R.id.edit, R.id.try_edit -> {
loadEditor(currentPage?.path)
true
}

View File

@ -13,6 +13,10 @@
android:id="@+id/edit"
android:title="@string/edit"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/try_edit"
android:title="@string/try_edit"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/bookmarks"
android:title="@string/bookmarks"

View File

@ -13,5 +13,14 @@
<string name="delete">Delete</string>
<string name="delete_question">Do you really want to delete this?</string>
<string name="publish">Publish</string>
<string name="edit">Edit</string>
<string name="edit">Edit (safe)</string>
<string name="try_edit">Edit (unsafe)</string>
<string name="error">Error</string>
<string name="error_desc">Something unexpected happened!</string>
<string name="viewer_html_head">
<![CDATA[<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\"><style> * { max-width: 100%; height: auto; word-break: break-all; word-break: break-word; }</style></head><body>]]>
</string>
<string name="viewer_html_end">
<![CDATA[<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js\"></script><script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></body></html>]]>
</string>
</resources>