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

307 lines
9.2 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
2018-02-14 17:51:20 +00:00
import android.net.Uri
2016-11-25 05:59:16 +00:00
import android.os.Bundle
2018-09-22 06:18:57 +00:00
import androidx.appcompat.app.AppCompatActivity
2016-11-25 05:59:16 +00:00
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 com.afollestad.materialdialogs.input.input
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import im.delight.android.webview.AdvancedWebView
2018-02-14 17:13:55 +00:00
import java.net.URI
2016-11-25 05:59:16 +00:00
2018-02-12 16:59:30 +00:00
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
2018-02-12 18:36:19 +00:00
private val viewer: Viewer? by lazy {
findViewById<Viewer?>(R.id.viewer)?.apply {
2018-02-12 16:59:30 +00:00
setListener(this@MainActivity, this@MainActivity)
}
}
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
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-14 17:13:55 +00:00
if (accessToken.isBlank()) TelegraphApi.createAccount(shortName = "teleposter") { success, account, error ->
2018-02-04 21:49:32 +00:00
if (success && account != null && account.accessToken != null) {
2018-02-14 17:13:55 +00:00
accessToken = account.accessToken
2018-02-04 21:49:32 +00:00
} else {
showError(error)
}
2016-12-21 20:56:56 +00:00
}
2018-02-14 17:13:55 +00:00
if (intent.action == Intent.ACTION_VIEW) {
val uri = URI.create(intent.dataString)
when (uri.host) {
"telegra.ph", "graph.org" -> loadPage(uri.path)
"edit.telegra.ph", "edit.graph.org" -> login(uri.toString())
}
}
2016-12-21 19:57:36 +00:00
}
private fun loadEditor(path: String? = null) {
runOnUiThread {
editorMode = true
canEdit = false
isEdit = false
invalidateOptionsMenu()
editor?.visibility = View.VISIBLE
2018-02-12 18:36:19 +00:00
viewer?.visibility = View.GONE
2016-12-21 19:57:36 +00:00
currentPage = null
// Load
2018-02-14 17:13:55 +00:00
if (path != null) TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
2018-02-04 21:49:32 +00:00
if (success && page != null) {
2016-12-21 19:57:36 +00:00
isEdit = true
currentPage = page
2018-02-12 18:36:19 +00:00
editor?.setContent(page.content)
2018-02-04 21:49:32 +00:00
} else {
showError(error)
2016-12-21 19:57:36 +00:00
}
2018-02-12 17:18:16 +00:00
} else {
// Reset
editor?.reset()
2016-12-21 19:57:36 +00:00
}
}
}
2018-02-14 17:13:55 +00:00
private fun login(authUrl: String) {
TelegraphApi.login(authUrl) { success, accessToken, account ->
if (success && accessToken != null) {
this.accessToken = accessToken
this.authorName = account?.authorName
showMessage(getString(R.string.success), getString(R.string.login_success))
} else showError(getString(R.string.login_failed))
}
}
2016-12-21 19:57:36 +00:00
private fun loadPage(path: String) {
runOnUiThread {
editorMode = false
canEdit = false
invalidateOptionsMenu()
2018-02-12 18:36:19 +00:00
viewer?.visibility = View.VISIBLE
2016-12-21 19:57:36 +00:00
editor?.visibility = View.GONE
currentPage = null
// Load
2018-02-14 17:13:55 +00:00
TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
2018-02-04 21:49:32 +00:00
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()
2018-02-12 18:36:19 +00:00
viewer?.visibility = View.VISIBLE
2016-12-21 19:57:36 +00:00
editor?.visibility = View.GONE
currentPage = page
2018-02-12 18:36:19 +00:00
viewer?.clearHistory()
2016-12-21 19:57:36 +00:00
// Show
2016-12-21 10:07:08 +00:00
page?.let {
2018-02-12 18:36:19 +00:00
viewer?.setArticleTitle(it.title)
viewer?.setAuthor(it.authorName, it.authorUrl)
viewer?.setViews(it.views)
if (it.content == null) viewer?.setDescription(it.description)
else viewer?.setContent(it.content)
2016-12-21 10:07:08 +00:00
}
}
}
2018-02-14 17:13:55 +00:00
private fun showError(message: String? = null) = showMessage(getString(R.string.error), message
?: getString(R.string.error_desc))
private fun showMessage(title: String? = null, message: String? = null) {
2016-12-21 20:56:56 +00:00
runOnUiThread {
MaterialDialog(this)
.title(text = title ?: "")
.message(text = message ?: "")
.positiveButton(android.R.string.ok)
2016-12-21 20:56:56 +00:00
.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 onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.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() {
2018-02-12 18:36:19 +00:00
if (viewer?.onBackPressed() == false) return
2016-11-26 09:15:37 +00:00
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(this)
2016-12-21 19:57:36 +00:00
.title(R.string.title_question)
.input(hintRes = R.string.title_hint, prefill = currentPage?.title
?: "") { _, title ->
MaterialDialog(this)
2016-12-22 08:13:59 +00:00
.title(R.string.name_question)
.input(hintRes = R.string.name_hint, prefill = if (isEdit) currentPage?.authorName
2018-02-14 17:13:55 +00:00
?: authorName ?: "" else authorName
?: "") { _, name ->
2018-02-14 17:13:55 +00:00
if (!isEdit) authorName = name.toString()
if (isEdit) TelegraphApi.editPage(accessToken, currentPage?.path
2018-02-04 21:49:32 +00:00
?: "", authorName = name.toString(), title = title.toString(), content = json
?: "", returnContent = true) { success, page, error ->
if (success && page != null) showPage(page)
else showError(error)
2018-02-14 17:13:55 +00:00
} else TelegraphApi.createPage(accessToken, content = json
2018-02-04 21:49:32 +00:00
?: "", 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
}
}
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 -> {
MaterialDialog(this)
2016-12-21 11:26:31 +00:00
.title(R.string.bookmarks)
.positiveButton(R.string.open)
.negativeButton(android.R.string.cancel)
.listItemsSingleChoice(items = bookmarks().reversed().map { it.second }) { _, index, _ ->
loadPage(bookmarks().reversed().map { it.first }[index])
2016-12-21 10:07:08 +00:00
}
.show()
true
}
R.id.delete_bookmark -> {
MaterialDialog(this)
.title(R.string.delete_bookmark)
.positiveButton(R.string.delete)
.negativeButton(android.R.string.cancel)
.listItemsSingleChoice(items = bookmarks().reversed().map { it.second }) { _, index, _ ->
MaterialDialog(this)
2016-12-21 11:26:31 +00:00
.title(R.string.delete)
.message(R.string.delete_question)
.positiveButton(android.R.string.yes)
.negativeButton(android.R.string.no)
.positiveButton { _ ->
deleteBookmark(bookmarks().reversed().map { it.first }[index])
2016-12-21 11:26:31 +00:00
}
.show()
}
.show()
true
}
2016-12-22 07:55:38 +00:00
R.id.published -> {
2018-02-14 17:13:55 +00:00
TelegraphApi.getPageList(accessToken) { success, pageList, error ->
2018-02-04 21:49:32 +00:00
if (success && pageList != null && pageList.pages != null) {
MaterialDialog(this)
2016-12-22 07:55:38 +00:00
.title(R.string.published)
.positiveButton(R.string.open)
.negativeButton(android.R.string.cancel)
.listItemsSingleChoice(items = pageList.pages.map { it.title }) { _, 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(this)
2016-12-21 11:26:31 +00:00
.title(R.string.title_question)
.input(hintRes = R.string.title_hint, prefill = currentPage?.title
?: "") { _, input ->
2018-02-12 18:36:19 +00:00
val curPage = currentPage
if (curPage?.url != null) addBookmark(curPage.url.split("/").last(), input.toString())
}
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"
2018-02-12 18:36:19 +00:00
shareIntent.putExtra(Intent.EXTRA_TITLE, currentPage?.title)
shareIntent.putExtra(Intent.EXTRA_TEXT, currentPage?.url)
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(this)
2016-11-26 10:37:27 +00:00
.title(R.string.help)
.message(R.string.help_text)
.positiveButton(android.R.string.ok)
2016-11-26 10:37:27 +00:00
.show()
true
}
2018-02-14 17:51:20 +00:00
R.id.login -> {
MaterialDialog(this)
2018-02-14 17:51:20 +00:00
.title(R.string.login)
.message(R.string.login_desc)
.positiveButton(android.R.string.ok)
.positiveButton { _ ->
2018-02-14 17:51:20 +00:00
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/telegraph")))
}
.show()
true
}
else -> super.onOptionsItemSelected(item)
2016-11-25 05:59:16 +00:00
}
}
}