Fix comments (still using linearlayout for now)

This commit is contained in:
Matthieu 2021-03-14 17:21:56 +01:00
parent f9db1543a9
commit a90a42f033
6 changed files with 64 additions and 56 deletions

View File

@ -5,18 +5,19 @@ import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
import android.widget.LinearLayout
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.h.pixeldroid.R
import com.h.pixeldroid.databinding.ActivityPostBinding
import com.h.pixeldroid.databinding.CommentBinding
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.utils.api.objects.Status.Companion.POST_TAG
import com.h.pixeldroid.utils.BaseActivity
import com.h.pixeldroid.utils.ImageConverter
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.Mention
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.utils.api.objects.Status.Companion.POST_COMMENT_TAG
import com.h.pixeldroid.utils.api.objects.Status.Companion.POST_TAG
import com.h.pixeldroid.utils.api.objects.Status.Companion.VIEW_COMMENTS_TAG
import com.h.pixeldroid.utils.displayDimensionsInPx
import retrofit2.HttpException
@ -38,7 +39,8 @@ class PostActivity : BaseActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
status = intent.getSerializableExtra(POST_TAG) as Status
val viewComments = intent.getSerializableExtra(VIEW_COMMENTS_TAG) as Boolean
val viewComments: Boolean = (intent.getSerializableExtra(VIEW_COMMENTS_TAG) ?: false) as Boolean
val postComment: Boolean = (intent.getSerializableExtra(POST_COMMENT_TAG) ?: false) as Boolean
val user = db.userDao().getActiveUser()
@ -52,10 +54,23 @@ class PostActivity : BaseActivity() {
holder.bind(status, apiHolder.api!!, db, lifecycleScope, displayDimensionsInPx(), isActivity = true)
val credential: String = "Bearer $accessToken"
val credential = "Bearer $accessToken"
activateCommenter(credential)
if(viewComments){
if(viewComments || postComment){
//Scroll already down as much as possible (since comments are not loaded yet)
binding.scrollview.requestChildFocus(binding.editComment, binding.editComment)
//Open keyboard if we want to post a comment
if(postComment && binding.editComment.requestFocus()) {
window.setSoftInputMode(SOFT_INPUT_STATE_VISIBLE)
binding.editComment.requestFocus()
}
// also retrieve comments if we're not posting the comment
if(!postComment) retrieveComments(apiHolder.api!!, credential)
}
binding.postFragmentSingle.viewComments.setOnClickListener {
retrieveComments(apiHolder.api!!, credential)
}
}
@ -66,10 +81,7 @@ class PostActivity : BaseActivity() {
}
private fun activateCommenter(credential: String) {
//Toggle comment button
toggleCommentInput()
//Activate commenterpostPicture
//Activate commenter
binding.submitComment.setOnClickListener {
val textIn = binding.editComment.text
//Open text input
@ -88,33 +100,9 @@ class PostActivity : BaseActivity() {
}
}
private fun toggleCommentInput() {
//Toggle comment button
binding.postFragmentSingle.commenter.setOnClickListener {
when(binding.commentIn.visibility) {
View.VISIBLE -> {
binding.commentIn.visibility = View.GONE
ImageConverter.setImageFromDrawable(
binding.root,
binding.postFragmentSingle.commenter,
R.drawable.ic_comment_empty
)
}
View.GONE -> {
binding.commentIn.visibility = View.VISIBLE
ImageConverter.setImageFromDrawable(
binding.root,
binding.postFragmentSingle.commenter,
R.drawable.ic_comment_blue
)
}
}
}
}
fun addComment(context: Context, commentContainer: LinearLayout,
commentUsername: String, commentContent: String, mentions: List<Mention>,
credential: String) {
private fun addComment(context: Context, commentContainer: LinearLayout,
commentUsername: String, commentContent: String, mentions: List<Mention>,
credential: String) {
val itemBinding = CommentBinding.inflate(
@ -148,6 +136,8 @@ class PostActivity : BaseActivity() {
}
binding.commentContainer.visibility = View.VISIBLE
//Focus the comments
binding.scrollview.requestChildFocus(binding.commentContainer, binding.commentContainer)
} catch (exception: IOException) {
Log.e("COMMENT FETCH ERROR", exception.toString())
} catch (exception: HttpException) {

View File

@ -25,6 +25,7 @@ import com.h.pixeldroid.utils.ImageConverter
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.Attachment
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.utils.api.objects.Status.Companion.POST_COMMENT_TAG
import com.h.pixeldroid.utils.api.objects.Status.Companion.POST_TAG
import com.h.pixeldroid.utils.api.objects.Status.Companion.VIEW_COMMENTS_TAG
import com.h.pixeldroid.utils.db.AppDatabase
@ -68,8 +69,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
//Setup the post layout
val picRequest = Glide.with(itemView)
.asDrawable().fitCenter()
val picRequest = Glide.with(itemView).asDrawable().fitCenter()
val user = db.userDao().getActiveUser()!!
@ -197,7 +197,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
}
}
//region buttons
private fun activateButtons(
api: PixelfedAPI,
db: AppDatabase,
@ -220,7 +220,22 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
lifecycleScope
)
showComments(api, credential, lifecycleScope, isActivity)
if(isActivity){
binding.commenter.visibility = View.INVISIBLE
}
else {
binding.commenter.setOnClickListener {
lifecycleScope.launchWhenCreated {
//Open status in activity
val intent = Intent(it.context, PostActivity::class.java)
intent.putExtra(POST_TAG, status)
intent.putExtra(POST_COMMENT_TAG, true)
it.context.startActivity(intent)
}
}
}
showComments(lifecycleScope, isActivity)
activateMoreButton(api, db, lifecycleScope)
}
@ -515,14 +530,13 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
}
}
//endregion
private fun showComments(
api: PixelfedAPI,
credential: String,
lifecycleScope: LifecycleCoroutineScope,
isActivity: Boolean
lifecycleScope: LifecycleCoroutineScope,
isActivity: Boolean
) {
//Show all comments of a post
//Show number of comments on the post
if (status?.replies_count == 0) {
binding.viewComments.text = binding.root.context.getString(R.string.NoCommentsToShow)
} else {
@ -531,14 +545,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
status?.replies_count ?: 0,
status?.replies_count ?: 0
)
if(isActivity) {
setOnClickListener {
lifecycleScope.launchWhenCreated {
//Retrieve the comments
//TODO retrieveComments(api, credential)
}
}
} else {
if(!isActivity) {
setOnClickListener {
lifecycleScope.launchWhenCreated {
//Open status in activity

View File

@ -65,6 +65,7 @@ open class Status(
companion object {
const val POST_TAG = "postTag"
const val VIEW_COMMENTS_TAG = "view_comments_tag"
const val POST_COMMENT_TAG = "post_comment_tag"
}
fun getPostUrl() : String? = media_attachments?.firstOrNull()?.url

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_comment_empty"
android:state_pressed="false" />
<item
android:drawable="@drawable/ic_comment_blue"
android:state_pressed="true"/>
</selector>

View File

@ -4,7 +4,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".posts.PostActivity">
tools:context=".posts.PostActivity"
android:id="@+id/scrollview">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"

View File

@ -118,7 +118,7 @@
android:layout_width="30dp"
android:layout_height="30dp"
android:padding="4dp"
android:src="@drawable/ic_comment_empty"
android:src="@drawable/selector_commenter"
app:layout_constraintBottom_toBottomOf="@+id/liker"
app:layout_constraintEnd_toStartOf="@id/reblogger"
app:layout_constraintStart_toEndOf="@id/liker"