From 0a3191c07e13a7472ba5fbc9c2a08599116db8d9 Mon Sep 17 00:00:00 2001 From: Jan-Lk Else Date: Wed, 21 Dec 2016 21:56:56 +0100 Subject: [PATCH] Much progress --- app/src/main/java/telegra/ph/Api.kt | 36 +++++++++----- app/src/main/java/telegra/ph/MainActivity.kt | 50 ++++++++++++-------- app/src/main/res/menu/activity_main.xml | 4 ++ app/src/main/res/values/strings.xml | 11 ++++- 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/telegra/ph/Api.kt b/app/src/main/java/telegra/ph/Api.kt index d4ab1e8..59cfde2 100644 --- a/app/src/main/java/telegra/ph/Api.kt +++ b/app/src/main/java/telegra/ph/Api.kt @@ -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) } } diff --git a/app/src/main/java/telegra/ph/MainActivity.kt b/app/src/main/java/telegra/ph/MainActivity.kt index b935310..c6ab0b6 100644 --- a/app/src/main/java/telegra/ph/MainActivity.kt +++ b/app/src/main/java/telegra/ph/MainActivity.kt @@ -11,10 +11,6 @@ import com.afollestad.materialdialogs.MaterialDialog import im.delight.android.webview.AdvancedWebView class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { - - 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? } @@ -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 += "

${it.title}

" if (!it.author_name.isNullOrEmpty() && !it.author_url.isNullOrBlank()) html += "${it.author_name}
" else if (!it.author_name.isNullOrEmpty()) html += "${it.author_name}
" if (it.views != 0) html += "${it.views} times viewed

" if (it.content.isNullOrBlank()) html += it.description.replace("\n", "
") 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 } diff --git a/app/src/main/res/menu/activity_main.xml b/app/src/main/res/menu/activity_main.xml index 0095a58..b414f14 100644 --- a/app/src/main/res/menu/activity_main.xml +++ b/app/src/main/res/menu/activity_main.xml @@ -13,6 +13,10 @@ android:id="@+id/edit" android:title="@string/edit" app:showAsAction="ifRoom"/> + Delete Do you really want to delete this? Publish - Edit + Edit (safe) + Edit (unsafe) + Error + Something unexpected happened! + + ]]> + + + ]]> +