teleposter/app/src/main/java/telegra/ph/MainActivity.kt

290 lines
8.7 KiB
Kotlin
Raw Normal View History

2016-11-25 05:59:16 +00:00
package telegra.ph
import android.content.Intent
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
import im.delight.android.webview.AdvancedWebView
2016-11-25 05:59:16 +00:00
2018-02-12 16:59:30 +00:00
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
private val webView: AdvancedWebView? by lazy {
findViewById<AdvancedWebView?>(R.id.webView)?.apply {
setListener(this@MainActivity, this@MainActivity)
setMixedContentAllowed(true)
setCookiesEnabled(true)
setThirdPartyCookiesEnabled(true)
addPermittedHostname("telegra.ph")
isHorizontalScrollBarEnabled = false
isVerticalScrollBarEnabled = false
overScrollMode = View.OVER_SCROLL_NEVER
}
}
private val editor: Editor? by lazy {
findViewById<Editor?>(R.id.editor)?.apply {
setListener(this@MainActivity, this@MainActivity)
}
}
2016-11-25 05:59:16 +00:00
2016-12-21 19:57:36 +00:00
private var currentUrl = ""
2018-02-04 21:49:32 +00:00
private var currentPage: TelegraphApi.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)
2018-02-04 21:49:32 +00:00
if (accessToken().isBlank()) TelegraphApi.createAccount(shortName = "teleposter") { success, account, error ->
if (success && account != null && account.accessToken != null) {
saveAccessToken(account.accessToken)
} else {
showError(error)
}
2016-12-21 20:56:56 +00:00
}
2016-12-24 21:15:50 +00:00
if (intent.action == Intent.ACTION_VIEW && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last())
2016-12-21 19:57:36 +00:00
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
2018-02-04 21:49:32 +00:00
if (path != null) TelegraphApi.getPage(accessToken(), path, true) { success, page, error ->
if (success && page != null) {
2016-12-21 19:57:36 +00:00
isEdit = true
currentPage = page
2018-02-04 21:49:32 +00:00
editor?.setText(page.content ?: "")
} else {
showError(error)
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
2018-02-04 21:49:32 +00:00
TelegraphApi.getPage(accessToken(), path, true) { success, page, error ->
if (success && page != null) showPage(page)
else showError(error)
2016-12-21 19:57:36 +00:00
}
}
}
2018-02-04 21:49:32 +00:00
private fun showPage(page: TelegraphApi.Page?) {
2016-12-21 19:57:36 +00:00
runOnUiThread {
editorMode = false
2018-02-04 21:49:32 +00:00
canEdit = page?.canEdit ?: false
2016-12-21 19:57:36 +00:00
invalidateOptionsMenu()
webView?.visibility = View.VISIBLE
editor?.visibility = View.GONE
currentPage = page
2016-12-22 08:13:59 +00:00
webView?.clearHistory()
2016-12-21 19:57:36 +00:00
// 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>"
2018-02-04 21:49:32 +00:00
if (!it.authorName.isNullOrBlank() && !it.authorUrl.isNullOrBlank()) html += "<a href=\"${it.authorUrl}\">${it.authorName}</a><br>"
else if (!it.authorName.isNullOrBlank()) html += "${it.authorName}<br>"
2016-12-21 10:07:08 +00:00
if (it.views != 0) html += "${it.views} times viewed<br><br>"
2018-02-04 21:49:32 +00:00
html += if (it.content.isNullOrBlank()) it.description.replace("\n", "<br>") else 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
}
}
}
2018-02-04 21:49:32 +00:00
private fun showError(message: String? = null) {
2016-12-21 20:56:56 +00:00
runOnUiThread {
MaterialDialog.Builder(this)
.title(R.string.error)
2018-02-04 21:49:32 +00:00
.content(message ?: getString(R.string.error_desc))
2016-12-21 20:56:56 +00:00
.positiveText(android.R.string.ok)
.show()
}
}
override fun onPageFinished(url: String?) {
}
override fun onPageStarted(url: String?, favicon: Bitmap?) {
}
2016-11-25 05:59:16 +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)
2018-02-12 16:59:30 +00:00
editor?.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
menu?.findItem(R.id.image)?.isVisible = editorMode
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 {
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)
2018-02-04 21:49:32 +00:00
.input(getString(R.string.title_hint), currentPage?.title
?: "", { _, title ->
2016-12-22 08:13:59 +00:00
MaterialDialog.Builder(this)
.title(R.string.name_question)
2018-02-04 21:49:32 +00:00
.input(getString(R.string.name_hint), if (isEdit) currentPage?.authorName
?: authorName() ?: "" else authorName()
?: "", { _, name ->
2016-12-24 21:15:50 +00:00
if (!isEdit) saveAuthorName(name.toString())
2018-02-04 21:49:32 +00:00
if (isEdit) TelegraphApi.editPage(accessToken(), currentPage?.path
?: "", authorName = name.toString(), title = title.toString(), content = json
?: "", returnContent = true) { success, page, error ->
if (success && page != null) showPage(page)
else showError(error)
} else TelegraphApi.createPage(accessToken(), content = json
?: "", title = title.toString(), authorName = name.toString(), returnContent = true) { success, page, error ->
if (success && page != null) showPage(page)
else showError(error)
2016-12-22 08:13:59 +00:00
}
})
.show()
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)
.items(bookmarks().reversed().map { it.second })
.itemsCallback { _, _, i, _ ->
loadPage(bookmarks().reversed().map { it.first }[i])
2016-12-21 10:07:08 +00:00
}
.itemsLongCallback { _, _, i, _ ->
2016-12-21 11:26:31 +00:00
MaterialDialog.Builder(this)
.title(R.string.delete)
.content(R.string.delete_question)
.positiveText(android.R.string.yes)
.negativeText(android.R.string.no)
.onPositive { _, _ ->
deleteBookmark(bookmarks().reversed().map { it.first }[i])
2016-12-21 11:26:31 +00:00
}
.show()
true
}
.show()
true
}
2016-12-22 07:55:38 +00:00
R.id.published -> {
2018-02-04 21:49:32 +00:00
TelegraphApi.getPageList(accessToken()) { success, pageList, error ->
if (success && pageList != null && pageList.pages != null) {
2016-12-22 07:55:38 +00:00
MaterialDialog.Builder(this)
.title(R.string.published)
.positiveText(android.R.string.ok)
2018-02-04 21:49:32 +00:00
.items(pageList.pages.map { it.title })
.itemsCallback { _, _, i, _ ->
2018-02-04 21:49:32 +00:00
loadPage(pageList.pages[i].path)
2016-12-22 07:55:38 +00:00
}
.show()
2018-02-04 21:49:32 +00:00
} else showError(error)
2016-12-22 07:55:38 +00:00
}
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), "", { _, input ->
addBookmark(currentUrl.split("/").last(), input.toString())
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)))
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)
2017-07-14 19:14:05 +00:00
.content(R.string.help_text, true)
2016-12-21 21:21:58 +00:00
.positiveText(android.R.string.ok)
2016-11-26 10:37:27 +00:00
.show()
true
}
else -> super.onOptionsItemSelected(item)
2016-11-25 05:59:16 +00:00
}
}
}