diff --git a/app/src/main/assets/editor.html b/app/src/main/assets/editor.html index e19a9b3..1a3091f 100644 --- a/app/src/main/assets/editor.html +++ b/app/src/main/assets/editor.html @@ -1,13 +1,13 @@
- - - - - + + + + + @@ -15,27 +15,59 @@ + \ No newline at end of file diff --git a/app/src/main/java/telegra/ph/Api.kt b/app/src/main/java/telegra/ph/Api.kt index fd1fec3..d4ab1e8 100644 --- a/app/src/main/java/telegra/ph/Api.kt +++ b/app/src/main/java/telegra/ph/Api.kt @@ -8,13 +8,34 @@ class Api { private val ApiBase = "https://api.telegra.ph/" - fun getPage(id: String?, callback: (page: Page?) -> Unit) { - Bridge.get("${ApiBase}getPage/$id?return_content=true").asJsonObject { response, jsonObject, bridgeException -> + 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 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 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 createAccount(callback: (accessToken: String?) -> Unit) { + Bridge.get("${ApiBase}createAccount?short_name=teleposter").asJsonObject { response, jsonObject, bridgeException -> + if (jsonObject != null) callback(jsonObject.optJSONObject("result")?.optString("access_token")) + else callback(null) + } + } + private fun JSONObject.parsePage(): Page? { val result: Page = Page() if (optBoolean("ok", false)) { diff --git a/app/src/main/java/telegra/ph/Editor.kt b/app/src/main/java/telegra/ph/Editor.kt index fdf523f..23173b3 100644 --- a/app/src/main/java/telegra/ph/Editor.kt +++ b/app/src/main/java/telegra/ph/Editor.kt @@ -9,21 +9,17 @@ import android.webkit.WebView import android.webkit.WebViewClient class Editor : WebView { - private var text = "" - internal var context: Context + private var getCallback: (json: String?) -> Unit? = {} constructor(context: Context) : super(context) { - this.context = context init() } constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { - this.context = context init() } constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { - this.context = context init() } @@ -38,8 +34,8 @@ class Editor : WebView { private inner class MyJavaScriptInterface { @JavascriptInterface - fun getText(html: String) { - text = html + fun getText(json: String) { + getCallback(json) } } @@ -53,19 +49,9 @@ class Editor : WebView { this.loadUrl("javascript:$('#summernote').summernote('code', '" + html.replace("'", "\\'") + "');") } - fun getText(): String { - text = "P/%TE5XpkAijBc%LjA;_-pZcbiU25E6feX5y/n6qxCTmhprLrqC3H%^hU!%q2,k'm`SHheoW^'mQ~zW93,C?~GtYk!wi/&'3KxW8" - this.loadUrl("javascript:window.android.getText" + "(document.getElementsByClassName('note-editable')[0].innerHTML);") - var i = 0 - try { - while (text == "P/%TE5XpkAijBc%LjA;_-pZcbiU25E6feX5y/n6qxCTmhprLrqC3H%^hU!%q2,k'm`SHheoW^'mQ~zW93,C?~GtYk!wi/&'3KxW8" && i < 100) { - Thread.sleep(50) - i++ - } - } catch (e: Exception) { - text = "" - } - return text + fun getText(callback: (json: String?) -> Unit) { + getCallback = callback + this.loadUrl("javascript:getNodeJson();") } } \ No newline at end of file diff --git a/app/src/main/java/telegra/ph/MainActivity.kt b/app/src/main/java/telegra/ph/MainActivity.kt index a293396..b935310 100644 --- a/app/src/main/java/telegra/ph/MainActivity.kt +++ b/app/src/main/java/telegra/ph/MainActivity.kt @@ -12,34 +12,21 @@ import im.delight.android.webview.AdvancedWebView class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { - private val TELEGRAPH = "http://telegra.ph/" private val htmlHead = "" private val htmlEnd = "" 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 var url = "" + private var currentUrl = "" + private var currentPage: Page? = null private var editorMode = true + private var canEdit = false + private var isEdit = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last()) - else loadEditor() - } - - private fun loadEditor() { - editorMode = true - invalidateOptionsMenu() - // Init - setContentView(R.layout.main_write) - } - - private fun loadPage(path: String) { - editorMode = false - invalidateOptionsMenu() - // Init - setContentView(R.layout.main_read) + setContentView(R.layout.activity_main) webView?.apply { setListener(this@MainActivity, this@MainActivity) setMixedContentAllowed(true) @@ -50,8 +37,54 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { isVerticalScrollBarEnabled = false overScrollMode = View.OVER_SCROLL_NEVER } - // Load - Api().getPage(path) { page -> + if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last()) + else loadEditor() + } + + private fun loadEditor(path: String? = null) { + runOnUiThread { + editorMode = true + canEdit = false + isEdit = false + invalidateOptionsMenu() + editor?.visibility = View.VISIBLE + webView?.visibility = View.GONE + currentPage = null + // Load + if (path != null) Api().getPage(path, accessToken()) { page -> + runOnUiThread { + isEdit = true + currentPage = page + editor?.setText(page?.content ?: "") + } + } + } + } + + private fun loadPage(path: String) { + runOnUiThread { + editorMode = false + canEdit = false + invalidateOptionsMenu() + webView?.visibility = View.VISIBLE + editor?.visibility = View.GONE + currentPage = null + // Load + Api().getPage(path, accessToken()) { page -> + showPage(page) + } + } + } + + private fun showPage(page: Page?) { + runOnUiThread { + editorMode = false + canEdit = page?.can_edit ?: false + invalidateOptionsMenu() + webView?.visibility = View.VISIBLE + editor?.visibility = View.GONE + currentPage = page + // Show page?.let { var html = htmlHead html += "