Beautiful viewer *-* (Bootstrap :-* )

This commit is contained in:
Jan-Lk Else 2016-12-21 11:07:08 +01:00
parent ad7b1e235b
commit 6ce9e5b7ef
6 changed files with 72 additions and 24 deletions

View File

@ -0,0 +1,12 @@
package telegra.ph
import android.content.Context
import android.preference.PreferenceManager
fun Context.getHistory() = PreferenceManager.getDefaultSharedPreferences(this).getStringSet("history", mutableSetOf<String>())
fun Context.addToHistory(entry: String) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putStringSet("history", getHistory().apply {
add(entry)
}).apply()
}

View File

@ -6,43 +6,59 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import com.afollestad.materialdialogs.MaterialDialog
import im.delight.android.webview.AdvancedWebView
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
private val TELEGRAPH = "http://telegra.ph/"
private val htmlHead = "<!DOCTYPE html><html><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/3.3.7/css/bootstrap.min.css\"><style> * { max-width: 100%; height: auto; word-break: break-all; word-break: break-word; }</style></head><body>"
private val htmlEnd = "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></body></html>"
private val webView: AdvancedWebView? by lazy { findViewById(R.id.webView) as AdvancedWebView }
private var url = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView?.setListener(this, this)
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")) {
Api().getPage(intent.dataString.split("/").last()) { page ->
page?.let {
var html = "<h1>${it.title}</h1>"
if (!it.author_name.isNullOrEmpty() && !it.author_url.isNullOrBlank()) html += "<a href=\"${it.author_url}\">${it.author_name}</a><br>"
else if (!it.author_name.isNullOrEmpty()) html += "${it.author_name}<br>"
if (it.views != 0) html += "${it.views} times viewed<br><br>"
if (it.content.isNullOrBlank()) html += it.description.replace("\n", "<br>") else html += it.content
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
}
}
loadPage(intent.dataString.split("/").last())
} else {
webView?.loadUrl(TELEGRAPH)
}
}
private fun loadPage(path: String) {
Api().getPage(path) { page ->
page?.let {
var html = htmlHead
html += "<h1>${it.title}</h1>"
if (!it.author_name.isNullOrEmpty() && !it.author_url.isNullOrBlank()) html += "<a href=\"${it.author_url}\">${it.author_name}</a><br>"
else if (!it.author_name.isNullOrEmpty()) html += "${it.author_name}<br>"
if (it.views != 0) html += "${it.views} times viewed<br><br>"
if (it.content.isNullOrBlank()) html += it.description.replace("\n", "<br>") else html += it.content
html += htmlEnd
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
url = it.url
addToHistory("${it.path}xxx;xxx${it.title}")
}
}
}
override fun onPageFinished(url: String?) {
}
@ -92,12 +108,22 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.history -> {
MaterialDialog.Builder(this)
.title(R.string.history)
.items(getHistory().reversed().map { it.split("xxx;xxx")[1] })
.itemsCallback { materialDialog, view, i, charSequence ->
loadPage(getHistory().reversed().map { it.split("xxx;xxx")[0] }[i])
}
.show()
true
}
R.id.share -> {
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TITLE, webView?.title)
shareIntent.putExtra(Intent.EXTRA_TEXT, webView?.url)
shareIntent.putExtra(Intent.EXTRA_TEXT, if (webView?.url != "about:blank") webView?.url ?: url else url)
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)))
true
}

View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<im.delight.android.webview.AdvancedWebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
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"/>

View File

@ -1,12 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/share"
android:title="@string/share"
app:showAsAction="ifRoom"/>
android:id="@+id/history"
android:title="@string/history"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/help"
android:title="@string/help"
app:showAsAction="ifRoom"/>
android:id="@+id/share"
android:title="@string/share"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/help"
android:title="@string/help"
app:showAsAction="ifRoom"/>
</menu>

View File

@ -5,4 +5,5 @@
<string name="help_text">
<![CDATA[<h3>How to use text formatting?</h3>You can use formatting like <b>bold</b>, <i>italics</i> and so on at least on Lollipop and above by double tapping the text.<h3>The app only shows strange things!</h3>It\'s not verified, that the app will work on devices with KitKat or lower. In Lollipop Google introduced a Chrome based and updatable WebView with more features and fixes. I\'m sorry! But in December \'16 Telegram will publish docs for the Telegraph API, so I\'ll be able to create a native app instead of a wrapper. Be patient!]]>
</string>
<string name="history">History</string>
</resources>

View File

@ -1,9 +1,10 @@
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorBackground">@android:color/white</item>
</style>
</resources>