mirror of https://github.com/jlelse/teleposter
Viewer class
This commit is contained in:
parent
459a1b98f8
commit
f8bc76157a
|
@ -98,6 +98,15 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function setContent(content) {
|
||||||
|
reset();
|
||||||
|
if (content) $('#summernote').summernote('code', content);
|
||||||
|
}
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
$('#summernote').summernote('reset');
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<link rel="stylesheet"
|
||||||
|
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
|
||||||
|
<style>
|
||||||
|
* {
|
||||||
|
max-width: 100% !important;
|
||||||
|
height: auto;
|
||||||
|
word-break: break-all;
|
||||||
|
word-break: break-word;
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main role="main" class="container">
|
||||||
|
<div id="viewerTitle" class="mt-3"></div>
|
||||||
|
<div id="viewerAuthor"></div>
|
||||||
|
<div id="viewerViews"></div>
|
||||||
|
<div id="viewerContent"></div>
|
||||||
|
</main>
|
||||||
|
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
function setTitle(title) {
|
||||||
|
$('#viewerTitle').html('<h1>' + title + '</h1>');
|
||||||
|
}
|
||||||
|
|
||||||
|
function setAuthor(author, url) {
|
||||||
|
if (author && url) $('#viewerAuthor').html('<a href="' + url + '">' + author + '</a><br>');
|
||||||
|
else $('#viewerAuthor').html('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function setViews(views) {
|
||||||
|
$('#viewerViews').html(views + ' times viewed<br><br>');
|
||||||
|
}
|
||||||
|
|
||||||
|
function setDescription(description) {
|
||||||
|
$('#viewerContent').html(description);
|
||||||
|
}
|
||||||
|
|
||||||
|
function setContent(content) {
|
||||||
|
$('#viewerContent').html(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
|
@ -4,9 +4,6 @@ import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.webkit.JavascriptInterface
|
import android.webkit.JavascriptInterface
|
||||||
import android.webkit.WebSettings
|
|
||||||
import android.webkit.WebView
|
|
||||||
import android.webkit.WebViewClient
|
|
||||||
import im.delight.android.webview.AdvancedWebView
|
import im.delight.android.webview.AdvancedWebView
|
||||||
|
|
||||||
class Editor : AdvancedWebView {
|
class Editor : AdvancedWebView {
|
||||||
|
@ -27,7 +24,6 @@ class Editor : AdvancedWebView {
|
||||||
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
|
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
|
||||||
private fun init() {
|
private fun init() {
|
||||||
this.settings.javaScriptEnabled = true
|
this.settings.javaScriptEnabled = true
|
||||||
this.settings.cacheMode = WebSettings.LOAD_NO_CACHE
|
|
||||||
this.addJavascriptInterface(MyJavaScriptInterface(), "android")
|
this.addJavascriptInterface(MyJavaScriptInterface(), "android")
|
||||||
this.settings.loadWithOverviewMode = true
|
this.settings.loadWithOverviewMode = true
|
||||||
this.settings.useWideViewPort = true
|
this.settings.useWideViewPort = true
|
||||||
|
@ -42,21 +38,11 @@ class Editor : AdvancedWebView {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reset() {
|
fun reset() {
|
||||||
this.loadUrl("javascript:$('#summernote').summernote('reset');")
|
this.loadUrl("javascript:reset();")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setText(html: String) {
|
fun setContent(content: String?) {
|
||||||
webViewClient = object : WebViewClient() {
|
this.loadUrl("javascript:setContent('${content?.replace("'", "\\'")}');")
|
||||||
override fun onPageFinished(view: WebView, url: String) {
|
|
||||||
setText(html)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reset()
|
|
||||||
this.loadUrl("javascript:$('#summernote').summernote('code', '" + html.replace("'", "\\'") + "');")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addImage(url: String) {
|
|
||||||
this.loadUrl("javascript:$('#summernote').summernote('insertImage', '$url');")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getText(callback: (json: String?) -> Unit) {
|
fun getText(callback: (json: String?) -> Unit) {
|
||||||
|
|
|
@ -11,16 +11,9 @@ import com.afollestad.materialdialogs.MaterialDialog
|
||||||
import im.delight.android.webview.AdvancedWebView
|
import im.delight.android.webview.AdvancedWebView
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
private val webView: AdvancedWebView? by lazy {
|
private val viewer: Viewer? by lazy {
|
||||||
findViewById<AdvancedWebView?>(R.id.webView)?.apply {
|
findViewById<Viewer?>(R.id.viewer)?.apply {
|
||||||
setListener(this@MainActivity, this@MainActivity)
|
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 {
|
private val editor: Editor? by lazy {
|
||||||
|
@ -29,7 +22,6 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var currentUrl = ""
|
|
||||||
private var currentPage: TelegraphApi.Page? = null
|
private var currentPage: TelegraphApi.Page? = null
|
||||||
private var editorMode = true
|
private var editorMode = true
|
||||||
private var canEdit = false
|
private var canEdit = false
|
||||||
|
@ -56,14 +48,14 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
isEdit = false
|
isEdit = false
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
editor?.visibility = View.VISIBLE
|
editor?.visibility = View.VISIBLE
|
||||||
webView?.visibility = View.GONE
|
viewer?.visibility = View.GONE
|
||||||
currentPage = null
|
currentPage = null
|
||||||
// Load
|
// Load
|
||||||
if (path != null) TelegraphApi.getPage(accessToken(), path, true) { success, page, error ->
|
if (path != null) TelegraphApi.getPage(accessToken(), path, true) { success, page, error ->
|
||||||
if (success && page != null) {
|
if (success && page != null) {
|
||||||
isEdit = true
|
isEdit = true
|
||||||
currentPage = page
|
currentPage = page
|
||||||
editor?.setText(page.content ?: "")
|
editor?.setContent(page.content)
|
||||||
} else {
|
} else {
|
||||||
showError(error)
|
showError(error)
|
||||||
}
|
}
|
||||||
|
@ -79,7 +71,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
editorMode = false
|
editorMode = false
|
||||||
canEdit = false
|
canEdit = false
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
webView?.visibility = View.VISIBLE
|
viewer?.visibility = View.VISIBLE
|
||||||
editor?.visibility = View.GONE
|
editor?.visibility = View.GONE
|
||||||
currentPage = null
|
currentPage = null
|
||||||
// Load
|
// Load
|
||||||
|
@ -95,21 +87,17 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
editorMode = false
|
editorMode = false
|
||||||
canEdit = page?.canEdit ?: false
|
canEdit = page?.canEdit ?: false
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
webView?.visibility = View.VISIBLE
|
viewer?.visibility = View.VISIBLE
|
||||||
editor?.visibility = View.GONE
|
editor?.visibility = View.GONE
|
||||||
currentPage = page
|
currentPage = page
|
||||||
webView?.clearHistory()
|
viewer?.clearHistory()
|
||||||
// Show
|
// Show
|
||||||
page?.let {
|
page?.let {
|
||||||
var html = getString(R.string.viewer_html_head)
|
viewer?.setArticleTitle(it.title)
|
||||||
html += "<h1>${it.title}</h1>"
|
viewer?.setAuthor(it.authorName, it.authorUrl)
|
||||||
if (!it.authorName.isNullOrBlank() && !it.authorUrl.isNullOrBlank()) html += "<a href=\"${it.authorUrl}\">${it.authorName}</a><br>"
|
viewer?.setViews(it.views)
|
||||||
else if (!it.authorName.isNullOrBlank()) html += "${it.authorName}<br>"
|
if (it.content == null) viewer?.setDescription(it.description)
|
||||||
if (it.views != 0) html += "${it.views} times viewed<br><br>"
|
else viewer?.setContent(it.content)
|
||||||
html += if (it.content.isNullOrBlank()) it.description.replace("\n", "<br>") else it.content
|
|
||||||
html += getString(R.string.viewer_html_end)
|
|
||||||
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
|
|
||||||
currentUrl = it.url
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,29 +128,13 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
AdvancedWebView.Browsers.openUrl(this, url)
|
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?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
super.onActivityResult(requestCode, resultCode, data)
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
webView?.onActivityResult(requestCode, resultCode, data)
|
|
||||||
editor?.onActivityResult(requestCode, resultCode, data)
|
editor?.onActivityResult(requestCode, resultCode, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBackPressed() {
|
override fun onBackPressed() {
|
||||||
if (webView?.onBackPressed() == false) return
|
if (viewer?.onBackPressed() == false) return
|
||||||
else super.onBackPressed()
|
else super.onBackPressed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,7 +236,8 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
.title(R.string.title_question)
|
.title(R.string.title_question)
|
||||||
.input(getString(R.string.title_hint), "", { _, input ->
|
.input(getString(R.string.title_hint), "", { _, input ->
|
||||||
addBookmark(currentUrl.split("/").last(), input.toString())
|
val curPage = currentPage
|
||||||
|
if (curPage?.url != null) addBookmark(curPage.url.split("/").last(), input.toString())
|
||||||
})
|
})
|
||||||
.show()
|
.show()
|
||||||
true
|
true
|
||||||
|
@ -273,8 +246,8 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
val shareIntent = Intent()
|
val shareIntent = Intent()
|
||||||
shareIntent.action = Intent.ACTION_SEND
|
shareIntent.action = Intent.ACTION_SEND
|
||||||
shareIntent.type = "text/plain"
|
shareIntent.type = "text/plain"
|
||||||
shareIntent.putExtra(Intent.EXTRA_TITLE, webView?.title)
|
shareIntent.putExtra(Intent.EXTRA_TITLE, currentPage?.title)
|
||||||
shareIntent.putExtra(Intent.EXTRA_TEXT, currentUrl)
|
shareIntent.putExtra(Intent.EXTRA_TEXT, currentPage?.url)
|
||||||
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)))
|
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)))
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
package telegra.ph
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.AttributeSet
|
||||||
|
import android.view.View
|
||||||
|
import im.delight.android.webview.AdvancedWebView
|
||||||
|
|
||||||
|
class Viewer : AdvancedWebView {
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
|
||||||
|
private fun init() {
|
||||||
|
this.settings.javaScriptEnabled = true
|
||||||
|
this.settings.loadWithOverviewMode = true
|
||||||
|
this.settings.useWideViewPort = true
|
||||||
|
overScrollMode = View.OVER_SCROLL_NEVER
|
||||||
|
setMixedContentAllowed(true)
|
||||||
|
this.loadDataWithBaseURL("http://telegra.ph", context.assets.open("viewer.html").bufferedReader().readText(), "text/html", "utf-8", null)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setArticleTitle(title: String) {
|
||||||
|
this.loadUrl("javascript:setTitle('$title');")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setAuthor(author: String?, url: String?) {
|
||||||
|
this.loadUrl("javascript:setAuthor('$author','$url');")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setViews(views: Int) {
|
||||||
|
this.loadUrl("javascript:setViews('$views');")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setDescription(description: String) {
|
||||||
|
this.loadUrl("javascript:setDescription('${description.replace("\n", "<br>")}');")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setContent(content: String?) {
|
||||||
|
this.loadUrl("javascript:setContent('${content?.replace("'", "\\'")}');")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
@ -8,14 +7,10 @@
|
||||||
<telegra.ph.Editor
|
<telegra.ph.Editor
|
||||||
android:id="@+id/editor"
|
android:id="@+id/editor"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"/>
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
<im.delight.android.webview.AdvancedWebView
|
<telegra.ph.Viewer
|
||||||
android:id="@+id/webView"
|
android:id="@+id/viewer"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent" />
|
||||||
android:layout_marginEnd="16dp"
|
|
||||||
android:layout_marginLeft="16dp"
|
|
||||||
android:layout_marginRight="16dp"
|
|
||||||
android:layout_marginStart="16dp"/>
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
Loading…
Reference in New Issue