Fix crash with bookmarks & make bookmarks impl better (still ugly, but ok)

This commit is contained in:
Jan-Lukas Else 2018-02-06 19:51:21 +01:00
parent 5d109b88b2
commit 0eb13a09ab
2 changed files with 24 additions and 11 deletions

View File

@ -247,9 +247,9 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener, FileChooserD
MaterialDialog.Builder(this)
.title(R.string.bookmarks)
.positiveText(android.R.string.ok)
.items(bookmarks().reversed().map { it.split("xxx;xxx")[1] })
.items(bookmarks().reversed().map { it.second })
.itemsCallback { _, _, i, _ ->
loadPage(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
loadPage(bookmarks().reversed().map { it.first }[i])
}
.itemsLongCallback { _, _, i, _ ->
MaterialDialog.Builder(this)
@ -258,7 +258,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener, FileChooserD
.positiveText(android.R.string.yes)
.negativeText(android.R.string.no)
.onPositive { _, _ ->
deleteBookmark(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
deleteBookmark(bookmarks().reversed().map { it.first }[i])
}
.show()
true
@ -285,7 +285,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener, FileChooserD
MaterialDialog.Builder(this)
.title(R.string.title_question)
.input(getString(R.string.title_hint), "", { _, input ->
addBookmark("${currentUrl.split("/").last()}xxx;xxx$input")
addBookmark(currentUrl.split("/").last(), input.toString())
})
.show()
true

View File

@ -3,17 +3,30 @@ 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")
const val listItemSeparator = "+++;+++"
const val itemSeparator = "xxx;xxx"
fun Context.addBookmark(entry: String) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("bookmarks", bookmarks().apply {
add(entry)
}.joinToString(separator = "+++;+++")).apply()
fun Context.bookmarks(): List<Pair<String, String>> {
val list = PreferenceManager.getDefaultSharedPreferences(this).getString("bookmarks", null)?.split(listItemSeparator)?.map {
val splitParts = it.split(itemSeparator)
if (splitParts.size == 2) splitParts[0] to splitParts[1]
else null
}?.filterNotNull()
return if (list != null && list.isNotEmpty()) list else listOf("api" to "API Documentation")
}
fun Context.addBookmark(path: String, title: String) {
saveBookmarks(bookmarks().plus(path to title))
}
fun Context.deleteBookmark(path: String) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("bookmarks", bookmarks().filter { !it.contains(path) }.joinToString(separator = "+++;+++")).apply()
saveBookmarks(bookmarks().filter { it.first != path })
}
fun Context.saveBookmarks(bookmarks: List<Pair<String, String>>) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("bookmarks",
bookmarks.joinToString(separator = listItemSeparator) { "${it.first}$itemSeparator${it.second}" }
).apply()
}
fun Context.accessToken(): String = PreferenceManager.getDefaultSharedPreferences(this).getString("accessToken", "")