p2play-app-android/app/src/main/java/org/libre/agosto/p2play/ajax/Comments.kt

74 lines
2.2 KiB
Kotlin
Raw Normal View History

2019-01-25 18:38:37 +01:00
package org.libre.agosto.p2play.ajax
import android.util.JsonReader
import android.util.Log
import org.libre.agosto.p2play.models.CommentaryModel
import java.io.InputStreamReader
2024-04-06 22:38:04 +02:00
class Comments : Client() {
2019-01-25 18:38:37 +01:00
private fun parseCommentaries(data: JsonReader): ArrayList<CommentaryModel> {
2019-02-18 00:29:52 +01:00
val commentaries = arrayListOf<CommentaryModel>()
2019-01-25 18:38:37 +01:00
data.beginObject()
2024-04-06 22:38:04 +02:00
while (data.hasNext()) {
when (data.nextName()) {
2019-01-25 18:38:37 +01:00
"data" -> {
data.beginArray()
while (data.hasNext()) {
val comment = CommentaryModel()
comment.parseCommentary(data)
2019-01-25 18:38:37 +01:00
commentaries.add(comment)
}
data.endArray()
}
else -> data.skipValue()
}
}
data.endObject()
return commentaries
}
fun getCommentaries(videoId: Int): ArrayList<CommentaryModel> {
var commentaries = arrayListOf<CommentaryModel>()
val con = this._newCon("videos/$videoId/comment-threads", "GET")
try {
if (con.responseCode == 200) {
2019-02-18 00:29:52 +01:00
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
2019-01-25 18:38:37 +01:00
commentaries = parseCommentaries(data)
data.close()
2019-01-25 18:38:37 +01:00
}
2024-04-06 22:38:04 +02:00
} catch (err: Exception) {
2019-02-18 00:29:52 +01:00
err.printStackTrace()
2019-01-25 18:38:37 +01:00
}
con.disconnect()
2019-01-25 18:38:37 +01:00
return commentaries
}
2019-01-25 19:30:48 +01:00
fun makeCommentary(token: String, videoId: Int, text: String): Boolean {
val con = this._newCon("videos/$videoId/comment-threads", "POST", token)
2019-02-18 00:29:52 +01:00
val params = "text=$text"
2019-01-25 19:30:48 +01:00
con.outputStream.write(params.toByteArray())
var response: Boolean
2019-01-25 19:30:48 +01:00
try {
if (con.responseCode == 200) {
con.disconnect()
response = true
2024-04-06 22:38:04 +02:00
} else {
2019-01-25 19:30:48 +01:00
Log.d("Status", con.responseMessage)
response = false
2019-01-25 19:30:48 +01:00
}
2024-04-06 22:38:04 +02:00
} catch (err: Exception) {
2019-01-25 19:30:48 +01:00
err.printStackTrace()
response = false
2019-01-25 19:30:48 +01:00
}
con.disconnect()
return response
2019-01-25 19:30:48 +01:00
}
2024-04-06 22:38:04 +02:00
}