diff --git a/app/src/main/java/telegra/ph/History.kt b/app/src/main/java/telegra/ph/History.kt new file mode 100644 index 0000000..b5474bb --- /dev/null +++ b/app/src/main/java/telegra/ph/History.kt @@ -0,0 +1,12 @@ +package telegra.ph + +import android.content.Context +import android.preference.PreferenceManager + +fun Context.getHistory() = PreferenceManager.getDefaultSharedPreferences(this).getStringSet("history", mutableSetOf()) + +fun Context.addToHistory(entry: String) { + PreferenceManager.getDefaultSharedPreferences(this).edit().putStringSet("history", getHistory().apply { + add(entry) + }).apply() +} \ No newline at end of file diff --git a/app/src/main/java/telegra/ph/MainActivity.kt b/app/src/main/java/telegra/ph/MainActivity.kt index ff955bd..1b250cf 100644 --- a/app/src/main/java/telegra/ph/MainActivity.kt +++ b/app/src/main/java/telegra/ph/MainActivity.kt @@ -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 = "" + private val htmlEnd = "" 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 = "

${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

" - if (it.content.isNullOrBlank()) html += it.description.replace("\n", "
") 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 += "

${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

" + if (it.content.isNullOrBlank()) html += it.description.replace("\n", "
") 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 } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c66fd09..56bce08 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,10 @@ \ No newline at end of file + 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"/> \ No newline at end of file diff --git a/app/src/main/res/menu/activity_main.xml b/app/src/main/res/menu/activity_main.xml index 622e26a..fc2f41a 100644 --- a/app/src/main/res/menu/activity_main.xml +++ b/app/src/main/res/menu/activity_main.xml @@ -1,12 +1,16 @@ + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:id="@+id/history" + android:title="@string/history" + app:showAsAction="ifRoom"/> + android:id="@+id/share" + android:title="@string/share" + app:showAsAction="ifRoom"/> + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 40da6a7..f47e7df 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,4 +5,5 @@ How to use text formatting?You can use formatting like bold, italics and so on at least on Lollipop and above by double tapping the text.

The app only shows strange things!

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!]]>
+ History diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index a00f57d..5604826 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,9 +1,10 @@ -