2016-12-21 11:26:31 +00:00
|
|
|
package telegra.ph
|
|
|
|
|
|
|
|
import android.content.Context
|
2020-10-31 21:16:59 +00:00
|
|
|
import androidx.preference.PreferenceManager
|
2016-12-21 11:26:31 +00:00
|
|
|
|
2018-02-06 18:51:21 +00:00
|
|
|
const val listItemSeparator = "+++;+++"
|
|
|
|
const val itemSeparator = "xxx;xxx"
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2016-12-21 11:26:31 +00:00
|
|
|
|
2018-02-06 18:51:21 +00:00
|
|
|
fun Context.addBookmark(path: String, title: String) {
|
|
|
|
saveBookmarks(bookmarks().plus(path to title))
|
2016-12-21 11:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun Context.deleteBookmark(path: String) {
|
2018-02-06 18:51:21 +00:00
|
|
|
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()
|
2016-12-21 19:57:36 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 17:13:55 +00:00
|
|
|
var Context.accessToken: String
|
2019-02-10 09:53:48 +00:00
|
|
|
get() = PreferenceManager.getDefaultSharedPreferences(this).getString("accessToken", "") as String
|
2018-02-14 17:13:55 +00:00
|
|
|
set(value) {
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("accessToken", value).apply()
|
|
|
|
}
|
|
|
|
|
|
|
|
var Context.authorName: String?
|
|
|
|
get() = PreferenceManager.getDefaultSharedPreferences(this).getString("authorName", null)
|
|
|
|
set(value) {
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("authorName", value).apply()
|
|
|
|
}
|