2016-11-25 05:59:16 +00:00
|
|
|
package telegra.ph
|
|
|
|
|
|
|
|
import android.content.Intent
|
2016-11-26 07:58:37 +00:00
|
|
|
import android.graphics.Bitmap
|
2016-11-25 05:59:16 +00:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.support.v7.app.AppCompatActivity
|
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
2016-12-21 10:07:08 +00:00
|
|
|
import android.view.View
|
2016-11-26 10:37:27 +00:00
|
|
|
import com.afollestad.materialdialogs.MaterialDialog
|
2016-11-26 07:58:37 +00:00
|
|
|
import im.delight.android.webview.AdvancedWebView
|
2016-11-25 05:59:16 +00:00
|
|
|
|
2016-11-26 07:58:37 +00:00
|
|
|
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
2016-12-21 14:52:07 +00:00
|
|
|
private val webView: AdvancedWebView? by lazy { findViewById(R.id.webView) as AdvancedWebView? }
|
|
|
|
private val editor: Editor? by lazy { findViewById(R.id.editor) as Editor? }
|
2016-11-25 05:59:16 +00:00
|
|
|
|
2016-12-21 19:57:36 +00:00
|
|
|
private var currentUrl = ""
|
|
|
|
private var currentPage: Page? = null
|
2016-12-21 14:52:07 +00:00
|
|
|
private var editorMode = true
|
2016-12-21 19:57:36 +00:00
|
|
|
private var canEdit = false
|
|
|
|
private var isEdit = false
|
2016-12-21 10:07:08 +00:00
|
|
|
|
2016-11-25 05:59:16 +00:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2016-12-21 19:57:36 +00:00
|
|
|
setContentView(R.layout.activity_main)
|
2016-11-26 07:58:37 +00:00
|
|
|
webView?.apply {
|
2016-12-21 10:07:08 +00:00
|
|
|
setListener(this@MainActivity, this@MainActivity)
|
2016-11-26 07:58:37 +00:00
|
|
|
setMixedContentAllowed(true)
|
|
|
|
setCookiesEnabled(true)
|
|
|
|
setThirdPartyCookiesEnabled(true)
|
|
|
|
addPermittedHostname("telegra.ph")
|
2016-12-21 10:07:08 +00:00
|
|
|
isHorizontalScrollBarEnabled = false
|
|
|
|
isVerticalScrollBarEnabled = false
|
|
|
|
overScrollMode = View.OVER_SCROLL_NEVER
|
2016-11-26 07:58:37 +00:00
|
|
|
}
|
2016-12-21 20:56:56 +00:00
|
|
|
if (accessToken().isNullOrBlank()) Api().createAccount { accessToken ->
|
|
|
|
if (accessToken != null) saveAccessToken(accessToken)
|
|
|
|
}
|
2016-12-21 19:57:36 +00:00
|
|
|
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
|
2016-12-21 20:56:56 +00:00
|
|
|
if (path != null) Api().getPage(path, accessToken()) { success, page ->
|
|
|
|
if (success) runOnUiThread {
|
2016-12-21 19:57:36 +00:00
|
|
|
isEdit = true
|
|
|
|
currentPage = page
|
|
|
|
editor?.setText(page?.content ?: "")
|
|
|
|
}
|
2016-12-21 20:56:56 +00:00
|
|
|
else showError()
|
2016-12-21 19:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadPage(path: String) {
|
|
|
|
runOnUiThread {
|
|
|
|
editorMode = false
|
|
|
|
canEdit = false
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
webView?.visibility = View.VISIBLE
|
|
|
|
editor?.visibility = View.GONE
|
|
|
|
currentPage = null
|
|
|
|
// Load
|
2016-12-21 20:56:56 +00:00
|
|
|
Api().getPage(path, accessToken()) { success, page ->
|
|
|
|
if (success) showPage(page)
|
|
|
|
else showError()
|
2016-12-21 19:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-12-21 10:07:08 +00:00
|
|
|
page?.let {
|
2016-12-21 20:56:56 +00:00
|
|
|
var html = getString(R.string.viewer_html_head)
|
2016-12-21 10:07:08 +00:00
|
|
|
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
|
2016-12-21 20:56:56 +00:00
|
|
|
html += getString(R.string.viewer_html_end)
|
2016-12-21 10:07:08 +00:00
|
|
|
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
|
2016-12-21 19:57:36 +00:00
|
|
|
currentUrl = it.url
|
2016-12-21 10:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 20:56:56 +00:00
|
|
|
private fun showError() {
|
|
|
|
runOnUiThread {
|
|
|
|
MaterialDialog.Builder(this)
|
|
|
|
.title(R.string.error)
|
|
|
|
.content(R.string.error_desc)
|
|
|
|
.positiveText(android.R.string.ok)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 07:58:37 +00:00
|
|
|
override fun onPageFinished(url: String?) {
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPageStarted(url: String?, favicon: Bitmap?) {
|
|
|
|
}
|
2016-11-25 05:59:16 +00:00
|
|
|
|
2016-11-26 07:58:37 +00:00
|
|
|
override fun onPageError(errorCode: Int, description: String?, failingUrl: String?) {
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDownloadRequested(url: String?, suggestedFilename: String?, mimeType: String?, contentLength: Long, contentDisposition: String?, userAgent: String?) {
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onExternalPageRequest(url: String?) {
|
|
|
|
AdvancedWebView.Browsers.openUrl(this, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
webView?.onResume()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
webView?.onPause()
|
|
|
|
super.onPause()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
webView?.onDestroy()
|
|
|
|
super.onDestroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
webView?.onActivityResult(requestCode, resultCode, data)
|
2016-11-25 05:59:16 +00:00
|
|
|
}
|
|
|
|
|
2016-11-26 09:15:37 +00:00
|
|
|
override fun onBackPressed() {
|
|
|
|
if (webView?.onBackPressed() == false) return
|
|
|
|
else super.onBackPressed()
|
|
|
|
}
|
|
|
|
|
2016-11-25 05:59:16 +00:00
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
|
super.onCreateOptionsMenu(menu)
|
|
|
|
menuInflater.inflate(R.menu.activity_main, menu)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-21 14:52:07 +00:00
|
|
|
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
|
|
|
|
super.onPrepareOptionsMenu(menu)
|
|
|
|
menu?.findItem(R.id.create)?.isVisible = !editorMode
|
2016-12-21 19:57:36 +00:00
|
|
|
menu?.findItem(R.id.share)?.isVisible = !editorMode
|
2016-12-21 20:56:56 +00:00
|
|
|
menu?.findItem(R.id.try_edit)?.isVisible = !editorMode && !canEdit
|
2016-12-21 14:52:07 +00:00
|
|
|
menu?.findItem(R.id.publish)?.isVisible = editorMode
|
2016-12-21 19:57:36 +00:00
|
|
|
menu?.findItem(R.id.edit)?.isVisible = canEdit
|
2016-12-21 14:52:07 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-25 05:59:16 +00:00
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
2016-11-26 07:58:37 +00:00
|
|
|
return when (item.itemId) {
|
2016-12-21 11:26:31 +00:00
|
|
|
R.id.create -> {
|
|
|
|
loadEditor()
|
|
|
|
true
|
|
|
|
}
|
2016-12-21 19:57:36 +00:00
|
|
|
R.id.publish -> {
|
|
|
|
editor?.getText { json ->
|
|
|
|
MaterialDialog.Builder(this)
|
|
|
|
.title(R.string.title_question)
|
|
|
|
.input(getString(R.string.title_hint), currentPage?.title ?: "", { dialog, title ->
|
2016-12-21 20:56:56 +00:00
|
|
|
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()
|
2016-12-21 19:57:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2016-12-21 20:56:56 +00:00
|
|
|
R.id.edit, R.id.try_edit -> {
|
2016-12-21 19:57:36 +00:00
|
|
|
loadEditor(currentPage?.path)
|
|
|
|
true
|
|
|
|
}
|
2016-12-21 11:26:31 +00:00
|
|
|
R.id.bookmarks -> {
|
2016-12-21 10:07:08 +00:00
|
|
|
MaterialDialog.Builder(this)
|
2016-12-21 11:26:31 +00:00
|
|
|
.title(R.string.bookmarks)
|
2016-12-22 07:55:38 +00:00
|
|
|
.positiveText(android.R.string.ok)
|
2016-12-21 11:26:31 +00:00
|
|
|
.items(bookmarks().reversed().map { it.split("xxx;xxx")[1] })
|
2016-12-21 10:07:08 +00:00
|
|
|
.itemsCallback { materialDialog, view, i, charSequence ->
|
2016-12-21 11:26:31 +00:00
|
|
|
loadPage(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
|
2016-12-21 10:07:08 +00:00
|
|
|
}
|
2016-12-21 11:26:31 +00:00
|
|
|
.itemsLongCallback { materialDialog, view, i, charSequence ->
|
|
|
|
MaterialDialog.Builder(this)
|
|
|
|
.title(R.string.delete)
|
|
|
|
.content(R.string.delete_question)
|
|
|
|
.positiveText(android.R.string.yes)
|
|
|
|
.negativeText(android.R.string.no)
|
|
|
|
.onPositive { materialDialog, dialogAction ->
|
|
|
|
deleteBookmark(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
true
|
|
|
|
}
|
2016-12-22 07:55:38 +00:00
|
|
|
R.id.published -> {
|
|
|
|
Api().getPageList(accessToken()) { success, result ->
|
|
|
|
if (!success || result == null || result.isEmpty()) showError()
|
|
|
|
else {
|
|
|
|
MaterialDialog.Builder(this)
|
|
|
|
.title(R.string.published)
|
|
|
|
.positiveText(android.R.string.ok)
|
|
|
|
.items(result.map(Page::title))
|
|
|
|
.itemsCallback { materialDialog, view, i, charSequence ->
|
|
|
|
loadPage(result.map(Page::path)[i])
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2016-12-21 11:26:31 +00:00
|
|
|
R.id.bookmark -> {
|
|
|
|
MaterialDialog.Builder(this)
|
|
|
|
.title(R.string.title_question)
|
|
|
|
.input(getString(R.string.title_hint), "", { dialog, input ->
|
2016-12-21 19:57:36 +00:00
|
|
|
addBookmark("${(if (webView?.url != "about:blank") webView?.url ?: currentUrl else currentUrl).split("/").last()}xxx;xxx$input")
|
2016-12-21 11:26:31 +00:00
|
|
|
})
|
2016-12-21 10:07:08 +00:00
|
|
|
.show()
|
|
|
|
true
|
|
|
|
}
|
2016-11-25 05:59:16 +00:00
|
|
|
R.id.share -> {
|
|
|
|
val shareIntent = Intent()
|
|
|
|
shareIntent.action = Intent.ACTION_SEND
|
|
|
|
shareIntent.type = "text/plain"
|
|
|
|
shareIntent.putExtra(Intent.EXTRA_TITLE, webView?.title)
|
2016-12-21 19:57:36 +00:00
|
|
|
shareIntent.putExtra(Intent.EXTRA_TEXT, currentUrl)
|
2016-11-25 05:59:16 +00:00
|
|
|
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)))
|
2016-11-26 07:58:37 +00:00
|
|
|
true
|
2016-11-25 05:59:16 +00:00
|
|
|
}
|
2016-11-26 10:37:27 +00:00
|
|
|
R.id.help -> {
|
|
|
|
MaterialDialog.Builder(this)
|
|
|
|
.title(R.string.help)
|
|
|
|
.content(R.string.help_text)
|
2016-12-21 21:21:58 +00:00
|
|
|
.positiveText(android.R.string.ok)
|
2016-11-26 10:37:27 +00:00
|
|
|
.show()
|
|
|
|
true
|
|
|
|
}
|
2016-11-26 07:58:37 +00:00
|
|
|
else -> super.onOptionsItemSelected(item)
|
2016-11-25 05:59:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|