mirror of https://github.com/jlelse/teleposter
Post list
This commit is contained in:
parent
51fade7db5
commit
dcf355895e
|
@ -11,7 +11,7 @@ class Api {
|
||||||
fun getPage(path: String?, accessToken: String?, callback: (success: Boolean, page: Page?) -> Unit) {
|
fun getPage(path: String?, accessToken: String?, callback: (success: Boolean, page: Page?) -> Unit) {
|
||||||
Bridge.get("${ApiBase}getPage/$path?access_token=%s&return_content=true", accessToken).asString { response, s, bridgeException ->
|
Bridge.get("${ApiBase}getPage/$path?access_token=%s&return_content=true", accessToken).asString { response, s, bridgeException ->
|
||||||
if (!s.isNullOrBlank() && bridgeException == null) try {
|
if (!s.isNullOrBlank() && bridgeException == null) try {
|
||||||
callback(true, JSONObject(s).parsePage())
|
callback(true, JSONObject(s).parsePageResponse())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
callback(false, null)
|
callback(false, null)
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ class Api {
|
||||||
fun createPage(accessToken: String?, content: String?, title: String?, callback: (success: Boolean, Page?) -> Unit) {
|
fun createPage(accessToken: String?, content: String?, title: String?, callback: (success: Boolean, Page?) -> Unit) {
|
||||||
Bridge.get("${ApiBase}createPage?access_token=%s&title=%s&content=%s&return_content=true", accessToken, title, content).asString { response, s, bridgeException ->
|
Bridge.get("${ApiBase}createPage?access_token=%s&title=%s&content=%s&return_content=true", accessToken, title, content).asString { response, s, bridgeException ->
|
||||||
if (!s.isNullOrBlank() && bridgeException == null) try {
|
if (!s.isNullOrBlank() && bridgeException == null) try {
|
||||||
callback(true, JSONObject(s).parsePage())
|
callback(true, JSONObject(s).parsePageResponse())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
callback(false, null)
|
callback(false, null)
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ class Api {
|
||||||
fun editPage(accessToken: String?, path: String?, content: String?, title: String?, callback: (success: Boolean, Page?) -> Unit) {
|
fun editPage(accessToken: String?, path: String?, content: String?, title: String?, callback: (success: Boolean, Page?) -> Unit) {
|
||||||
Bridge.get("${ApiBase}editPage/$path?access_token=%s&title=%s&content=%s&return_content=true", accessToken, title, content).asString { response, s, bridgeException ->
|
Bridge.get("${ApiBase}editPage/$path?access_token=%s&title=%s&content=%s&return_content=true", accessToken, title, content).asString { response, s, bridgeException ->
|
||||||
if (!s.isNullOrBlank() && bridgeException == null) try {
|
if (!s.isNullOrBlank() && bridgeException == null) try {
|
||||||
callback(true, JSONObject(s).parsePage())
|
callback(true, JSONObject(s).parsePageResponse())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
callback(false, null)
|
callback(false, null)
|
||||||
}
|
}
|
||||||
|
@ -42,28 +42,64 @@ class Api {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createAccount(callback: (accessToken: String?) -> Unit) {
|
fun createAccount(callback: (accessToken: String?) -> Unit) {
|
||||||
Bridge.get("${ApiBase}createAccount?short_name=teleposter").asJsonObject { response, jsonObject, bridgeException ->
|
Bridge.get("${ApiBase}createAccount?short_name=teleposter").asString { response, s, bridgeException ->
|
||||||
if (jsonObject != null) callback(jsonObject.optJSONObject("result")?.optString("access_token"))
|
if (!s.isNullOrBlank() && bridgeException == null) try {
|
||||||
|
callback(JSONObject(s).optJSONObject("result")?.optString("access_token"))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
callback(null)
|
||||||
|
}
|
||||||
else callback(null)
|
else callback(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getPageList(accessToken: String?, offset: Int = 0, callback: (success: Boolean, MutableList<Page>?) -> Unit) {
|
||||||
|
Bridge.get("${ApiBase}getPageList?access_token=%s&limit=200&offset=$offset", accessToken).asString { response, s, bridgeException ->
|
||||||
|
if (!s.isNullOrBlank() && bridgeException == null) try {
|
||||||
|
JSONObject(s).optJSONObject("result")?.let {
|
||||||
|
val totalCount = it.optInt("total_count")
|
||||||
|
var currentCount = 200 + offset
|
||||||
|
val result = mutableListOf<Page>()
|
||||||
|
it.optJSONArray("pages")?.let {
|
||||||
|
for (i in 0 until it.length()) {
|
||||||
|
val page = it.optJSONObject(i)?.parsePage()
|
||||||
|
if (page != null) result.add(page)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentCount < totalCount) {
|
||||||
|
getPageList(accessToken, currentCount) { success, pages ->
|
||||||
|
if (success && pages != null) {
|
||||||
|
result.addAll(pages)
|
||||||
|
callback(true, result)
|
||||||
|
}
|
||||||
|
if (!success) callback(false, null)
|
||||||
|
}
|
||||||
|
currentCount += 200
|
||||||
|
} else callback(true, result)
|
||||||
|
} ?: callback(false, null)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
callback(false, null)
|
||||||
|
}
|
||||||
|
else callback(false, null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun JSONObject.parsePageResponse(): Page? {
|
||||||
|
if (optBoolean("ok", false)) optJSONObject("result")?.let { return it.parsePage() }
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
private fun JSONObject.parsePage(): Page? {
|
private fun JSONObject.parsePage(): Page? {
|
||||||
val result: Page = Page()
|
val result: Page = Page()
|
||||||
if (optBoolean("ok", false)) {
|
result.path = optString("path", "")
|
||||||
optJSONObject("result")?.let {
|
result.url = optString("url", "")
|
||||||
result.path = it.optString("path", "")
|
result.title = optString("title", "")
|
||||||
result.url = it.optString("url", "")
|
result.description = optString("description", "")
|
||||||
result.title = it.optString("title", "")
|
result.author_name = optString("author_name", "")
|
||||||
result.description = it.optString("description", "")
|
result.author_url = optString("author_url", "")
|
||||||
result.author_name = it.optString("author_name", "")
|
result.image_url = optString("image_url", "")
|
||||||
result.author_url = it.optString("author_url", "")
|
optJSONArray("content")?.parseContent(result)
|
||||||
result.image_url = it.optString("image_url", "")
|
result.views = optInt("views", 0)
|
||||||
it.optJSONArray("content")?.parseContent(result)
|
result.can_edit = optBoolean("can_edit", false)
|
||||||
result.views = it.optInt("views", 0)
|
|
||||||
result.can_edit = it.optBoolean("can_edit", false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -198,6 +198,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
R.id.bookmarks -> {
|
R.id.bookmarks -> {
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
.title(R.string.bookmarks)
|
.title(R.string.bookmarks)
|
||||||
|
.positiveText(android.R.string.ok)
|
||||||
.items(bookmarks().reversed().map { it.split("xxx;xxx")[1] })
|
.items(bookmarks().reversed().map { it.split("xxx;xxx")[1] })
|
||||||
.itemsCallback { materialDialog, view, i, charSequence ->
|
.itemsCallback { materialDialog, view, i, charSequence ->
|
||||||
loadPage(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
|
loadPage(bookmarks().reversed().map { it.split("xxx;xxx")[0] }[i])
|
||||||
|
@ -217,6 +218,22 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
.show()
|
.show()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
R.id.published -> {
|
||||||
|
Api().getPageList(accessToken()) { success, result ->
|
||||||
|
if (!success || result == null || result.isEmpty()) showError()
|
||||||
|
else {
|
||||||
|
MaterialDialog.Builder(this)
|
||||||
|
.title(R.string.published)
|
||||||
|
.positiveText(android.R.string.ok)
|
||||||
|
.items(result.map(Page::title))
|
||||||
|
.itemsCallback { materialDialog, view, i, charSequence ->
|
||||||
|
loadPage(result.map(Page::path)[i])
|
||||||
|
}
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
R.id.bookmark -> {
|
R.id.bookmark -> {
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
.title(R.string.title_question)
|
.title(R.string.title_question)
|
||||||
|
|
|
@ -17,13 +17,17 @@
|
||||||
android:id="@+id/try_edit"
|
android:id="@+id/try_edit"
|
||||||
android:title="@string/try_edit"
|
android:title="@string/try_edit"
|
||||||
app:showAsAction="ifRoom"/>
|
app:showAsAction="ifRoom"/>
|
||||||
|
<item
|
||||||
|
android:id="@+id/bookmark"
|
||||||
|
android:title="@string/bookmark_this"
|
||||||
|
app:showAsAction="ifRoom"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/bookmarks"
|
android:id="@+id/bookmarks"
|
||||||
android:title="@string/bookmarks"
|
android:title="@string/bookmarks"
|
||||||
app:showAsAction="ifRoom"/>
|
app:showAsAction="ifRoom"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/bookmark"
|
android:id="@+id/published"
|
||||||
android:title="@string/bookmark_this"
|
android:title="@string/published"
|
||||||
app:showAsAction="ifRoom"/>
|
app:showAsAction="ifRoom"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/share"
|
android:id="@+id/share"
|
||||||
|
|
|
@ -22,4 +22,5 @@
|
||||||
<string name="viewer_html_end">
|
<string name="viewer_html_end">
|
||||||
<![CDATA[<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js\"></script><script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></body></html>]]>
|
<![CDATA[<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js\"></script><script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></body></html>]]>
|
||||||
</string>
|
</string>
|
||||||
|
<string name="published">Published posts</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in New Issue