package com.h.pixeldroid.utils
import android.graphics.ColorMatrix
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import com.h.pixeldroid.R
import com.h.pixeldroid.api.PixelfedAPI
import com.h.pixeldroid.fragments.feeds.postFeeds.PostViewHolder
import com.h.pixeldroid.objects.Context
import com.h.pixeldroid.objects.Status
import com.h.pixeldroid.utils.ImageConverter.Companion.setImageFromDrawable
import kotlinx.android.synthetic.main.comment.view.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
abstract class PostUtils {
companion object {
fun toggleCommentInput(
holder : PostViewHolder
) {
//Toggle comment button
holder.commenter.setOnClickListener {
when(holder.commentIn.visibility) {
View.VISIBLE -> {
holder.commentIn.visibility = View.GONE
setImageFromDrawable(holder.postView, holder.commenter, R.drawable.ic_comment_empty)
}
View.GONE -> {
holder.commentIn.visibility = View.VISIBLE
setImageFromDrawable(holder.postView, holder.commenter, R.drawable.ic_comment_blue)
fun reblogPost(
holder : PostViewHolder,
api: PixelfedAPI,
credential: String,
post : Status
//Call the api function
api.reblogStatus(credential, post.id!!).enqueue(object : Callback<Status> {
override fun onFailure(call: Call<Status>, t: Throwable) {
Log.e("REBLOG ERROR", t.toString())
holder.reblogger.isChecked = false
override fun onResponse(call: Call<Status>, response: Response<Status>) {
if(response.code() == 200) {
val resp = response.body()!!
//Update shown share count
holder.nshares.text = resp.getNShares(holder.context)
holder.reblogger.isChecked = resp.reblogged!!
} else {
Log.e("RESPONSE_CODE", response.code().toString())
})
fun undoReblogPost(
api.undoReblogStatus(credential, post.id!!).enqueue(object : Callback<Status> {
holder.reblogger.isChecked = true
fun likePostCall(
api.likePost(credential, post.id!!).enqueue(object : Callback<Status> {
Log.e("LIKE ERROR", t.toString())
holder.liker.isChecked = false
//Update shown like count and internal like toggle
holder.nlikes.text = resp.getNLikes(holder.context)
holder.liker.isChecked = resp.favourited ?: false
fun unLikePostCall(
api.unlikePost(credential, post.id!!).enqueue(object : Callback<Status> {
Log.e("UNLIKE ERROR", t.toString())
holder.liker.isChecked = true
fun postComment(
val textIn = holder.comment.text
val nonNullText = textIn.toString()
api.postStatus(credential, nonNullText, post.id).enqueue(object :
Callback<Status> {
Log.e("COMMENT ERROR", t.toString())
Toast.makeText(holder.context, holder.context.getString(R.string.comment_error),
Toast.LENGTH_SHORT).show()
//Check that the received response code is valid
//Add the comment to the comment section
addComment(holder.context, holder.commentCont, resp.account!!.username,
resp.content!!
)
Toast.makeText(holder.context,
holder.context.getString(R.string.comment_posted).format(textIn),
Log.e("COMMENT SUCCESS", "posted: $textIn")
Log.e("ERROR_CODE", response.code().toString())
fun addComment(context: android.content.Context, commentContainer: LinearLayout, commentUsername: String, commentContent: String) {
val view = LayoutInflater.from(context)
.inflate(R.layout.comment, commentContainer, true)
view.user.text = commentUsername
view.commentText.text = commentContent
fun retrieveComments(
api.statusComments(post.id!!, credential).enqueue(object :
Callback<Context> {
override fun onFailure(call: Call<Context>, t: Throwable) {
Log.e("COMMENT FETCH ERROR", t.toString())
override fun onResponse(
call: Call<Context>,
response: Response<Context>
val statuses = response.body()!!.descendants
//Create the new views for each comment
for (status in statuses) {
addComment(holder.context, holder.commentCont, status.account!!.username,
status.content!!
Log.e("COMMENT ERROR", "${response.code()} with body ${response.errorBody()}")
fun censorColorMatrix(): ColorMatrix {
val array: FloatArray = floatArrayOf( 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f )
return ColorMatrix(array)
fun uncensorColorMatrix(): ColorMatrix {
return ColorMatrix()