Replaced History with Bookmarks

This commit is contained in:
Jan-Lk Else 2016-12-21 12:26:31 +01:00
parent 6ce9e5b7ef
commit 90d134dc70
6 changed files with 79 additions and 31 deletions

View File

@ -1,18 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="telegra.ph">
package="telegra.ph">
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="false"
android:icon="@drawable/telegraph"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:allowBackup="false"
android:icon="@drawable/telegraph"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@ -20,8 +22,10 @@
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
<data android:scheme="https"/>
<data android:host="telegra.ph"/>

View File

@ -1,12 +0,0 @@
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

@ -35,11 +35,13 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
overScrollMode = View.OVER_SCROLL_NEVER
}
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) {
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph"))
loadPage(intent.dataString.split("/").last())
} else {
webView?.loadUrl(TELEGRAPH)
}
else loadEditor()
}
private fun loadEditor() {
webView?.loadUrl(TELEGRAPH)
}
private fun loadPage(path: String) {
@ -54,7 +56,6 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
html += htmlEnd
webView?.loadDataWithBaseURL(it.url, html, "text/html; charset=UTF-8", null, null)
url = it.url
addToHistory("${it.path}xxx;xxx${it.title}")
}
}
}
@ -108,13 +109,38 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.history -> {
R.id.create -> {
loadEditor()
true
}
R.id.bookmarks -> {
MaterialDialog.Builder(this)
.title(R.string.history)
.items(getHistory().reversed().map { it.split("xxx;xxx")[1] })
.title(R.string.bookmarks)
.items(bookmarks().reversed().map { it.split("xxx;xxx")[1] })
.itemsCallback { materialDialog, view, i, charSequence ->
loadPage(getHistory().reversed().map { it.split("xxx;xxx")[0] }[i])
loadPage(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
}
.itemsLongCallback { materialDialog, view, i, charSequence ->
MaterialDialog.Builder(this)
.title(R.string.delete)
.content(R.string.delete_question)
.positiveText(android.R.string.yes)
.negativeText(android.R.string.no)
.onPositive { materialDialog, dialogAction ->
deleteBookmark(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
}
.show()
true
}
.show()
true
}
R.id.bookmark -> {
MaterialDialog.Builder(this)
.title(R.string.title_question)
.input(getString(R.string.title_hint), "", { dialog, input ->
addBookmark("${(if (webView?.url != "about:blank") webView?.url ?: url else url).split("/").last()}xxx;xxx$input")
})
.show()
true
}

View File

@ -0,0 +1,16 @@
package telegra.ph
import android.content.Context
import android.preference.PreferenceManager
fun Context.bookmarks(): MutableList<String> = PreferenceManager.getDefaultSharedPreferences(this).getString("bookmarks", null)?.split("+++;+++")?.toMutableList() ?: mutableListOf("apixxx;xxxAPI Documentation")
fun Context.addBookmark(entry: String) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("bookmarks", bookmarks().apply {
add(entry)
}.joinToString(separator = "+++;+++")).apply()
}
fun Context.deleteBookmark(path: String) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("bookmarks", bookmarks().filter { !it.contains(path) }.joinToString(separator = "+++;+++")).apply()
}

View File

@ -2,8 +2,16 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/history"
android:title="@string/history"
android:id="@+id/create"
android:title="@string/create"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/bookmarks"
android:title="@string/bookmarks"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/bookmark"
android:title="@string/bookmark_this"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/share"

View File

@ -5,5 +5,11 @@
<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>
<string name="bookmarks">Bookmarks</string>
<string name="bookmark_this">Bookmark this</string>
<string name="create">Create</string>
<string name="title_question">Title?</string>
<string name="title_hint">Awesome Post #1</string>
<string name="delete">Delete</string>
<string name="delete_question">Do you really want to delete this?</string>
</resources>