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/" private val ApiBase = "https://api.telegra.ph/"
fun getPage(id: String?, accessToken: String?, callback: (page: Page?) -> Unit) { fun getPage(path: String?, accessToken: String?, callback: (success: Boolean, page: Page?) -> Unit) {
Bridge.get("${ApiBase}getPage/$id?return_content=true" + if (accessToken != null) "&access_token=$accessToken" else "").asJsonObject { response, jsonObject, bridgeException -> Bridge.get("${ApiBase}getPage/$path?access_token=%s&return_content=true", accessToken).asString { response, s, bridgeException ->
if (jsonObject != null) callback(jsonObject.parsePage()) if (!s.isNullOrBlank() && bridgeException == null) try {
else callback(null) 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) { fun createPage(accessToken: String?, content: String?, title: String?, callback: (success: Boolean, Page?) -> Unit) {
Bridge.get("${ApiBase}createPage?access_token=$accessToken&title=%s&content=$content&return_content=true", title).asJsonObject { response, jsonObject, bridgeException -> Bridge.get("${ApiBase}createPage?access_token=%s&title=%s&content=%s&return_content=true", accessToken, title, content).asString { response, s, bridgeException ->
if (jsonObject != null) callback(jsonObject.parsePage()) if (!s.isNullOrBlank() && bridgeException == null) try {
else callback(null) 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) { fun editPage(accessToken: String?, path: String?, content: String?, title: String?, callback: (success: Boolean, Page?) -> Unit) {
Bridge.get("${ApiBase}editPage/$path?access_token=$accessToken&title=%s&content=$content&return_content=true", title).asJsonObject { response, jsonObject, bridgeException -> Bridge.get("${ApiBase}editPage/$path?access_token=%s&title=%s&content=%s&return_content=true", accessToken, title, content).asString { response, s, bridgeException ->
if (jsonObject != null) callback(jsonObject.parsePage()) if (!s.isNullOrBlank() && bridgeException == null) try {
else callback(null) 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 import im.delight.android.webview.AdvancedWebView
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { 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 webView: AdvancedWebView? by lazy { findViewById(R.id.webView) as AdvancedWebView? }
private val editor: Editor? by lazy { findViewById(R.id.editor) as Editor? } private val editor: Editor? by lazy { findViewById(R.id.editor) as Editor? }
@ -37,6 +33,9 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
isVerticalScrollBarEnabled = false isVerticalScrollBarEnabled = false
overScrollMode = View.OVER_SCROLL_NEVER 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()) if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last())
else loadEditor() else loadEditor()
} }
@ -51,12 +50,13 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
webView?.visibility = View.GONE webView?.visibility = View.GONE
currentPage = null currentPage = null
// Load // Load
if (path != null) Api().getPage(path, accessToken()) { page -> if (path != null) Api().getPage(path, accessToken()) { success, page ->
runOnUiThread { if (success) runOnUiThread {
isEdit = true isEdit = true
currentPage = page currentPage = page
editor?.setText(page?.content ?: "") editor?.setText(page?.content ?: "")
} }
else showError()
} }
} }
} }
@ -70,8 +70,9 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
editor?.visibility = View.GONE editor?.visibility = View.GONE
currentPage = null currentPage = null
// Load // Load
Api().getPage(path, accessToken()) { page -> Api().getPage(path, accessToken()) { success, page ->
showPage(page) if (success) showPage(page)
else showError()
} }
} }
} }
@ -86,19 +87,29 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
currentPage = page currentPage = page
// Show // Show
page?.let { page?.let {
var html = htmlHead var html = getString(R.string.viewer_html_head)
html += "<h1>${it.title}</h1>" 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>" 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>" else if (!it.author_name.isNullOrEmpty()) html += "${it.author_name}<br>"
if (it.views != 0) html += "${it.views} times viewed<br><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 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) webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
currentUrl = it.url 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?) { override fun onPageFinished(url: String?) {
} }
@ -150,6 +161,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
super.onPrepareOptionsMenu(menu) super.onPrepareOptionsMenu(menu)
menu?.findItem(R.id.create)?.isVisible = !editorMode menu?.findItem(R.id.create)?.isVisible = !editorMode
menu?.findItem(R.id.share)?.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.publish)?.isVisible = editorMode
menu?.findItem(R.id.edit)?.isVisible = canEdit menu?.findItem(R.id.edit)?.isVisible = canEdit
return true return true
@ -166,22 +178,20 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
MaterialDialog.Builder(this) MaterialDialog.Builder(this)
.title(R.string.title_question) .title(R.string.title_question)
.input(getString(R.string.title_hint), currentPage?.title ?: "", { dialog, title -> .input(getString(R.string.title_hint), currentPage?.title ?: "", { dialog, title ->
if (accessToken().isNullOrBlank()) { if (isEdit) Api().editPage(accessToken(), currentPage?.path, json, title.toString()) { success, page ->
Api().createAccount { accessToken -> if (success) showPage(page)
if (accessToken != null) saveAccessToken(accessToken) else showError()
if (isEdit) Api().editPage(accessToken(), currentPage?.path, json, title.toString()) { page -> showPage(page) } }
else Api().createPage(accessToken(), json, title.toString()) { page -> showPage(page) } else Api().createPage(accessToken(), json, title.toString()) { success, page ->
} if (success) showPage(page)
} else { else showError()
if (isEdit) Api().editPage(accessToken(), currentPage?.path, json, title.toString()) { page -> showPage(page) }
else Api().createPage(accessToken(), json, title.toString()) { page -> showPage(page) }
} }
}) })
.show() .show()
} }
true true
} }
R.id.edit -> { R.id.edit, R.id.try_edit -> {
loadEditor(currentPage?.path) loadEditor(currentPage?.path)
true true
} }

View File

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

View File

@ -13,5 +13,14 @@
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="delete_question">Do you really want to delete this?</string> <string name="delete_question">Do you really want to delete this?</string>
<string name="publish">Publish</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> </resources>