mirror of https://github.com/jlelse/teleposter
Fix crash with bookmarks & make bookmarks impl better (still ugly, but ok)
This commit is contained in:
parent
5d109b88b2
commit
0eb13a09ab
|
@ -247,9 +247,9 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener, FileChooserD
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
.title(R.string.bookmarks)
|
.title(R.string.bookmarks)
|
||||||
.positiveText(android.R.string.ok)
|
.positiveText(android.R.string.ok)
|
||||||
.items(bookmarks().reversed().map { it.split("xxx;xxx")[1] })
|
.items(bookmarks().reversed().map { it.second })
|
||||||
.itemsCallback { _, _, i, _ ->
|
.itemsCallback { _, _, i, _ ->
|
||||||
loadPage(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
|
loadPage(bookmarks().reversed().map { it.first }[i])
|
||||||
}
|
}
|
||||||
.itemsLongCallback { _, _, i, _ ->
|
.itemsLongCallback { _, _, i, _ ->
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
|
@ -258,7 +258,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener, FileChooserD
|
||||||
.positiveText(android.R.string.yes)
|
.positiveText(android.R.string.yes)
|
||||||
.negativeText(android.R.string.no)
|
.negativeText(android.R.string.no)
|
||||||
.onPositive { _, _ ->
|
.onPositive { _, _ ->
|
||||||
deleteBookmark(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
|
deleteBookmark(bookmarks().reversed().map { it.first }[i])
|
||||||
}
|
}
|
||||||
.show()
|
.show()
|
||||||
true
|
true
|
||||||
|
@ -285,7 +285,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener, FileChooserD
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
.title(R.string.title_question)
|
.title(R.string.title_question)
|
||||||
.input(getString(R.string.title_hint), "", { _, input ->
|
.input(getString(R.string.title_hint), "", { _, input ->
|
||||||
addBookmark("${currentUrl.split("/").last()}xxx;xxx$input")
|
addBookmark(currentUrl.split("/").last(), input.toString())
|
||||||
})
|
})
|
||||||
.show()
|
.show()
|
||||||
true
|
true
|
||||||
|
|
|
@ -3,17 +3,30 @@ package telegra.ph
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
|
|
||||||
fun Context.bookmarks(): MutableList<String> = PreferenceManager.getDefaultSharedPreferences(this).getString("bookmarks", null)?.split("+++;+++")?.toMutableList()
|
const val listItemSeparator = "+++;+++"
|
||||||
?: mutableListOf("apixxx;xxxAPI Documentation")
|
const val itemSeparator = "xxx;xxx"
|
||||||
|
|
||||||
fun Context.addBookmark(entry: String) {
|
fun Context.bookmarks(): List<Pair<String, String>> {
|
||||||
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("bookmarks", bookmarks().apply {
|
val list = PreferenceManager.getDefaultSharedPreferences(this).getString("bookmarks", null)?.split(listItemSeparator)?.map {
|
||||||
add(entry)
|
val splitParts = it.split(itemSeparator)
|
||||||
}.joinToString(separator = "+++;+++")).apply()
|
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) {
|
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", "")
|
fun Context.accessToken(): String = PreferenceManager.getDefaultSharedPreferences(this).getString("accessToken", "")
|
||||||
|
|
Loading…
Reference in New Issue