Fixes and improvements

This commit is contained in:
Jan-Lukas Else 2019-02-05 22:18:51 +01:00
parent c93a7ae575
commit 2957a2f250
4 changed files with 135 additions and 132 deletions

View File

@ -105,12 +105,7 @@
});
function setContent(content) {
reset();
if (content) $('#summernote').summernote('code', content);
}
function reset() {
$('#summernote').summernote('reset');
$('#summernote').summernote('code', content);
}
</script>
</body>

View File

@ -6,23 +6,17 @@ import android.util.AttributeSet
import android.webkit.JavascriptInterface
import im.delight.android.webview.AdvancedWebView
class Editor : AdvancedWebView {
class Editor @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AdvancedWebView(context, attrs, defStyleAttr) {
private var getCallback: (json: String?) -> Unit? = {}
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
init()
init {
prepare()
}
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
private fun init() {
fun prepare() {
this.settings.javaScriptEnabled = true
this.addJavascriptInterface(MyJavaScriptInterface(), "android")
this.settings.loadWithOverviewMode = true
@ -33,15 +27,12 @@ class Editor : AdvancedWebView {
private inner class MyJavaScriptInterface {
@JavascriptInterface
@SuppressWarnings("unused")
fun getText(json: String) {
getCallback(json)
}
}
fun reset() {
this.loadUrl("javascript:reset();")
}
fun setContent(content: String?) {
this.loadUrl("javascript:setContent('${content?.replace("'", "\\'")}');")
}

View File

@ -15,17 +15,55 @@ import com.afollestad.materialdialogs.list.listItemsSingleChoice
import im.delight.android.webview.AdvancedWebView
import java.net.URI
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
class MainActivity : AppCompatActivity() {
private val viewer: Viewer? by lazy {
findViewById<Viewer?>(R.id.viewer)?.apply {
setListener(this@MainActivity, this@MainActivity)
setListener(this@MainActivity, object : AdvancedWebView.Listener {
override fun onPageFinished(url: String?) {
viewerPendingPage?.let { viewer?.showPage(it) }
viewerPendingPage = null
}
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@MainActivity, url)
}
override fun onPageStarted(url: String?, favicon: Bitmap?) {
}
})
}
}
private var viewerPendingPage: TelegraphApi.Page? = null
private val editor: Editor? by lazy {
findViewById<Editor?>(R.id.editor)?.apply {
setListener(this@MainActivity, this@MainActivity)
setListener(this@MainActivity, object : AdvancedWebView.Listener {
override fun onPageFinished(url: String?) {
editorPendingPage?.let { editor?.setContent(it.content) }
editorPendingPage = null
}
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@MainActivity, url)
}
override fun onPageStarted(url: String?, favicon: Bitmap?) {
}
})
}
}
private var editorPendingPage: TelegraphApi.Page? = null
private var currentPage: TelegraphApi.Page? = null
private var editorMode = true
@ -51,34 +89,43 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
}
}
private fun loadEditor(path: String? = null) {
runOnUiThread {
editorMode = true
canEdit = false
isEdit = false
invalidateOptionsMenu()
editor?.visibility = View.VISIBLE
viewer?.visibility = View.GONE
currentPage = null
// Load
if (path != null) TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
if (success && page != null) {
isEdit = true
currentPage = page
editor?.setContent(page.content)
} else {
showError(error)
}
} else {
// Reset
editor?.reset()
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
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())
}
}
}
private fun loadEditor(path: String? = null) {
editorMode = true
canEdit = false
isEdit = false
invalidateOptionsMenu()
editor?.visibility = View.VISIBLE
viewer?.visibility = View.GONE
currentPage = null
// Load
if (path != null) TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
if (success && page != null) {
isEdit = true
currentPage = page
editorPendingPage = page
editor?.prepare()
} else {
showError(error)
}
} else {
editor?.prepare()
}
}
private fun login(authUrl: String) {
TelegraphApi.login(authUrl) { success, accessToken, account ->
if (success && accessToken != null) {
if (success && !accessToken.isNullOrEmpty()) {
this.accessToken = accessToken
this.authorName = account?.authorName
showMessage(getString(R.string.success), getString(R.string.login_success))
@ -87,38 +134,22 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
}
private fun loadPage(path: String) {
runOnUiThread {
editorMode = false
canEdit = false
invalidateOptionsMenu()
viewer?.visibility = View.VISIBLE
editor?.visibility = View.GONE
currentPage = null
// Load
TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
if (success && page != null) showPage(page)
else showError(error)
}
}
}
private fun showPage(page: TelegraphApi.Page?) {
runOnUiThread {
editorMode = false
canEdit = page?.canEdit ?: false
invalidateOptionsMenu()
viewer?.visibility = View.VISIBLE
editor?.visibility = View.GONE
currentPage = page
viewer?.clearHistory()
// Show
page?.let {
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)
editorMode = false
canEdit = false
invalidateOptionsMenu()
viewer?.visibility = View.VISIBLE
editor?.visibility = View.GONE
currentPage = null
// Load
TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
if (success && page != null) {
canEdit = page.canEdit ?: false
invalidateOptionsMenu()
currentPage = page
viewerPendingPage = page
viewer?.prepare()
}
else showError(error)
}
}
@ -126,29 +157,11 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
?: getString(R.string.error_desc))
private fun showMessage(title: String? = null, message: String? = null) {
runOnUiThread {
MaterialDialog(this)
.title(text = title ?: "")
.message(text = message ?: "")
.positiveButton(android.R.string.ok)
.show()
}
}
override fun onPageFinished(url: String?) {
}
override fun onPageStarted(url: String?, favicon: Bitmap?) {
}
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)
MaterialDialog(this@MainActivity)
.title(text = title ?: "")
.message(text = message ?: "")
.positiveButton(android.R.string.ok)
.show()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@ -186,11 +199,11 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
}
R.id.publish -> {
editor?.getText { json ->
MaterialDialog(this)
MaterialDialog(this@MainActivity)
.title(R.string.title_question)
.input(hintRes = R.string.title_hint, prefill = currentPage?.title
?: "", allowEmpty = false) { _, title ->
MaterialDialog(this)
MaterialDialog(this@MainActivity)
.title(R.string.name_question)
.input(hintRes = R.string.name_hint, prefill = if (isEdit) currentPage?.authorName
?: authorName ?: "" else authorName
@ -199,11 +212,11 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
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)
if (success && page != null) loadPage(page.path)
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)
if (success && page != null) loadPage(page.path)
else showError(error)
}
}
@ -218,7 +231,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
true
}
R.id.bookmarks -> {
MaterialDialog(this)
MaterialDialog(this@MainActivity)
.title(R.string.bookmarks)
.positiveButton(R.string.open)
.negativeButton(android.R.string.cancel)
@ -229,12 +242,12 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
true
}
R.id.delete_bookmark -> {
MaterialDialog(this)
MaterialDialog(this@MainActivity)
.title(R.string.delete_bookmark)
.positiveButton(R.string.delete)
.negativeButton(android.R.string.cancel)
.listItemsMultiChoice(items = bookmarks().reversed().map { it.second }) { _, indices, _ ->
MaterialDialog(this)
MaterialDialog(this@MainActivity)
.title(R.string.delete)
.message(R.string.delete_question)
.positiveButton(android.R.string.yes)
@ -251,7 +264,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
R.id.published -> {
TelegraphApi.getPageList(accessToken) { success, pageList, error ->
if (success && pageList != null && pageList.pages != null) {
MaterialDialog(this)
MaterialDialog(this@MainActivity)
.title(R.string.published)
.positiveButton(R.string.open)
.negativeButton(android.R.string.cancel)
@ -264,7 +277,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
true
}
R.id.bookmark -> {
MaterialDialog(this)
MaterialDialog(this@MainActivity)
.title(R.string.title_question)
.input(hintRes = R.string.title_hint, prefill = currentPage?.title
?: "", allowEmpty = false) { _, input ->
@ -289,7 +302,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
true
}
R.id.login -> {
MaterialDialog(this)
MaterialDialog(this@MainActivity)
.title(R.string.login)
.message(R.string.login_desc)
.positiveButton(android.R.string.ok)

View File

@ -4,49 +4,53 @@ import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.webkit.JavascriptInterface
import im.delight.android.webview.AdvancedWebView
class Viewer : AdvancedWebView {
class Viewer @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AdvancedWebView(context, attrs, defStyleAttr) {
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
init()
init {
prepare()
}
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
private fun init() {
this.settings.javaScriptEnabled = true
this.settings.loadWithOverviewMode = true
this.settings.useWideViewPort = true
fun prepare() {
settings.javaScriptEnabled = true
settings.loadWithOverviewMode = true
settings.useWideViewPort = true
overScrollMode = View.OVER_SCROLL_NEVER
setMixedContentAllowed(true)
this.loadDataWithBaseURL("https://telegra.ph", context.assets.open("viewer.html").bufferedReader().readText(), "text/html", "utf-8", null)
loadDataWithBaseURL("https://telegra.ph", context.assets.open("viewer.html").bufferedReader().readText(), "text/html", "utf-8", null)
}
fun setArticleTitle(title: String) {
fun showPage(page: TelegraphApi.Page) {
clearHistory()
setArticleTitle(page.title)
setAuthor(page.authorName, page.authorUrl)
setViews(page.views)
if (page.content == null) setDescription(page.description)
else setContent(page.content)
}
private fun setArticleTitle(title: String) {
this.loadUrl("javascript:setTitle('$title');")
}
fun setAuthor(author: String?, url: String?) {
private fun setAuthor(author: String?, url: String?) {
this.loadUrl("javascript:setAuthor('$author','$url');")
}
fun setViews(views: Int) {
private fun setViews(views: Int) {
this.loadUrl("javascript:setViews('$views');")
}
fun setDescription(description: String) {
private fun setDescription(description: String) {
this.loadUrl("javascript:setDescription('${description.replace("\n", "<br>")}');")
}
fun setContent(content: String?) {
private fun setContent(content: String?) {
this.loadUrl("javascript:setContent('${content?.replace("'", "\\'")}');")
}