"
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 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)
setContentView(R.layout.activity_main)
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
}
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 += "
${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