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>
|
||||
</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.util.AttributeSet
|
||||
import android.webkit.JavascriptInterface
|
||||
import android.webkit.WebSettings
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import im.delight.android.webview.AdvancedWebView
|
||||
|
||||
class Editor : AdvancedWebView {
|
||||
|
@ -27,7 +24,6 @@ class Editor : AdvancedWebView {
|
|||
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
|
||||
private fun init() {
|
||||
this.settings.javaScriptEnabled = true
|
||||
this.settings.cacheMode = WebSettings.LOAD_NO_CACHE
|
||||
this.addJavascriptInterface(MyJavaScriptInterface(), "android")
|
||||
this.settings.loadWithOverviewMode = true
|
||||
this.settings.useWideViewPort = true
|
||||
|
@ -42,21 +38,11 @@ class Editor : AdvancedWebView {
|
|||
}
|
||||
|
||||
fun reset() {
|
||||
this.loadUrl("javascript:$('#summernote').summernote('reset');")
|
||||
this.loadUrl("javascript:reset();")
|
||||
}
|
||||
|
||||
fun setText(html: String) {
|
||||
webViewClient = object : WebViewClient() {
|
||||
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 setContent(content: String?) {
|
||||
this.loadUrl("javascript:setContent('${content?.replace("'", "\\'")}');")
|
||||
}
|
||||
|
||||
fun getText(callback: (json: String?) -> Unit) {
|
||||
|
|
|
@ -11,16 +11,9 @@ import com.afollestad.materialdialogs.MaterialDialog
|
|||
import im.delight.android.webview.AdvancedWebView
|
||||
|
||||
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||
private val webView: AdvancedWebView? by lazy {
|
||||
findViewById<AdvancedWebView?>(R.id.webView)?.apply {
|
||||
private val viewer: Viewer? by lazy {
|
||||
findViewById<Viewer?>(R.id.viewer)?.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 {
|
||||
|
@ -29,7 +22,6 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
}
|
||||
}
|
||||
|
||||
private var currentUrl = ""
|
||||
private var currentPage: TelegraphApi.Page? = null
|
||||
private var editorMode = true
|
||||
private var canEdit = false
|
||||
|
@ -56,14 +48,14 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
isEdit = false
|
||||
invalidateOptionsMenu()
|
||||
editor?.visibility = View.VISIBLE
|
||||
webView?.visibility = View.GONE
|
||||
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?.setText(page.content ?: "")
|
||||
editor?.setContent(page.content)
|
||||
} else {
|
||||
showError(error)
|
||||
}
|
||||
|
@ -79,7 +71,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
editorMode = false
|
||||
canEdit = false
|
||||
invalidateOptionsMenu()
|
||||
webView?.visibility = View.VISIBLE
|
||||
viewer?.visibility = View.VISIBLE
|
||||
editor?.visibility = View.GONE
|
||||
currentPage = null
|
||||
// Load
|
||||
|
@ -95,21 +87,17 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
editorMode = false
|
||||
canEdit = page?.canEdit ?: false
|
||||
invalidateOptionsMenu()
|
||||
webView?.visibility = View.VISIBLE
|
||||
viewer?.visibility = View.VISIBLE
|
||||
editor?.visibility = View.GONE
|
||||
currentPage = page
|
||||
webView?.clearHistory()
|
||||
viewer?.clearHistory()
|
||||
// Show
|
||||
page?.let {
|
||||
var html = getString(R.string.viewer_html_head)
|
||||
html += "<h1>${it.title}</h1>"
|
||||
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>"
|
||||
if (it.views != 0) html += "${it.views} times viewed<br><br>"
|
||||
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
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -140,29 +128,13 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
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)
|
||||
editor?.onActivityResult(requestCode, resultCode, data)
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
if (webView?.onBackPressed() == false) return
|
||||
if (viewer?.onBackPressed() == false) return
|
||||
else super.onBackPressed()
|
||||
}
|
||||
|
||||
|
@ -264,7 +236,8 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
MaterialDialog.Builder(this)
|
||||
.title(R.string.title_question)
|
||||
.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()
|
||||
true
|
||||
|
@ -273,8 +246,8 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
|||
val shareIntent = Intent()
|
||||
shareIntent.action = Intent.ACTION_SEND
|
||||
shareIntent.type = "text/plain"
|
||||
shareIntent.putExtra(Intent.EXTRA_TITLE, webView?.title)
|
||||
shareIntent.putExtra(Intent.EXTRA_TEXT, currentUrl)
|
||||
shareIntent.putExtra(Intent.EXTRA_TITLE, currentPage?.title)
|
||||
shareIntent.putExtra(Intent.EXTRA_TEXT, currentPage?.url)
|
||||
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)))
|
||||
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"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
@ -8,14 +7,10 @@
|
|||
<telegra.ph.Editor
|
||||
android:id="@+id/editor"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<im.delight.android.webview.AdvancedWebView
|
||||
android:id="@+id/webView"
|
||||
<telegra.ph.Viewer
|
||||
android:id="@+id/viewer"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:layout_marginStart="16dp"/>
|
||||
android:layout_height="match_parent" />
|
||||
</LinearLayout>
|
Loading…
Reference in New Issue