From f9db1543a9a1773dc1dab5776adb40e30f83a3ba Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Thu, 4 Mar 2021 13:53:07 +0100 Subject: [PATCH 1/2] Open comments in activity. Remove useless fragment --- .../com/h/pixeldroid/posts/PostActivity.kt | 171 +++++++++++++++-- .../com/h/pixeldroid/posts/PostFragment.kt | 52 ------ .../h/pixeldroid/posts/StatusViewHolder.kt | 172 +++--------------- .../h/pixeldroid/utils/api/objects/Status.kt | 3 +- app/src/main/res/layout/activity_post.xml | 66 +++++-- app/src/main/res/layout/post_fragment.xml | 46 +---- 6 files changed, 237 insertions(+), 273 deletions(-) delete mode 100644 app/src/main/java/com/h/pixeldroid/posts/PostFragment.kt diff --git a/app/src/main/java/com/h/pixeldroid/posts/PostActivity.kt b/app/src/main/java/com/h/pixeldroid/posts/PostActivity.kt index 95f67060..6331e4ae 100644 --- a/app/src/main/java/com/h/pixeldroid/posts/PostActivity.kt +++ b/app/src/main/java/com/h/pixeldroid/posts/PostActivity.kt @@ -1,26 +1,35 @@ package com.h.pixeldroid.posts +import android.content.Context import android.os.Bundle import android.util.Log +import android.view.LayoutInflater import android.view.View +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.DISCOVER_TAG -import com.h.pixeldroid.utils.api.objects.Status.Companion.DOMAIN_TAG 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.Companion.VIEW_COMMENTS_TAG +import com.h.pixeldroid.utils.displayDimensionsInPx import retrofit2.HttpException import java.io.IOException class PostActivity : BaseActivity() { - private lateinit var postFragment : PostFragment lateinit var domain : String private lateinit var accessToken : String private lateinit var binding: ActivityPostBinding + private lateinit var status: Status + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityPostBinding.inflate(layoutInflater) @@ -28,18 +37,27 @@ class PostActivity : BaseActivity() { supportActionBar?.setDisplayHomeAsUpEnabled(true) - val status = intent.getSerializableExtra(POST_TAG) as Status? + status = intent.getSerializableExtra(POST_TAG) as Status + val viewComments = intent.getSerializableExtra(VIEW_COMMENTS_TAG) as Boolean val user = db.userDao().getActiveUser() domain = user?.instance_uri.orEmpty() accessToken = user?.accessToken.orEmpty() - postFragment = PostFragment() - val arguments = Bundle() - arguments.putString(DOMAIN_TAG, domain) - initializeFragment(arguments, status) + supportActionBar?.title = getString(R.string.post_title).format(status.account?.getDisplayName()) + + val holder = StatusViewHolder(binding.postFragmentSingle) + + holder.bind(status, apiHolder.api!!, db, lifecycleScope, displayDimensionsInPx(), isActivity = true) + + val credential: String = "Bearer $accessToken" + activateCommenter(credential) + + if(viewComments){ + retrieveComments(apiHolder.api!!, credential) + } } override fun onSupportNavigateUp(): Boolean { @@ -47,13 +65,134 @@ class PostActivity : BaseActivity() { return true } - private fun initializeFragment(arguments: Bundle, status: Status?){ - supportActionBar?.title = getString(R.string.post_title).format(status!!.account?.getDisplayName()) - arguments.putSerializable(POST_TAG, status) - postFragment.arguments = arguments - supportFragmentManager.isStateSaved - supportFragmentManager.beginTransaction() - .add(R.id.postFragmentSingle, postFragment).commit() - binding.postFragmentSingle.visibility = View.VISIBLE + private fun activateCommenter(credential: String) { + //Toggle comment button + toggleCommentInput() + + //Activate commenterpostPicture + binding.submitComment.setOnClickListener { + val textIn = binding.editComment.text + //Open text input + if(textIn.isNullOrEmpty()) { + Toast.makeText( + binding.root.context, + binding.root.context.getString(R.string.empty_comment), + Toast.LENGTH_SHORT + ).show() + } else { + //Post the comment + lifecycleScope.launchWhenCreated { + apiHolder.api?.let { it1 -> postComment(it1, credential) } + } + } + } } + + 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, + credential: String) { + + + val itemBinding = CommentBinding.inflate( + LayoutInflater.from(context), commentContainer, true + ) + + itemBinding.user.text = commentUsername + itemBinding.commentText.text = parseHTMLText( + commentContent, + mentions, + apiHolder.api!!, + context, + credential, + lifecycleScope + ) + } + + private fun retrieveComments(api: PixelfedAPI, credential: String) { + lifecycleScope.launchWhenCreated { + status.id.let { + try { + val statuses = api.statusComments(it, credential).descendants + + binding.commentContainer.removeAllViews() + + //Create the new views for each comment + for (status in statuses) { + addComment(binding.root.context, binding.commentContainer, status.account!!.username!!, + status.content!!, status.mentions.orEmpty(), credential + ) + } + binding.commentContainer.visibility = View.VISIBLE + + } catch (exception: IOException) { + Log.e("COMMENT FETCH ERROR", exception.toString()) + } catch (exception: HttpException) { + Log.e("COMMENT ERROR", "${exception.code()} with body ${exception.response()?.errorBody()}") + } + } + } + } + + private suspend fun postComment( + api: PixelfedAPI, + credential: String, + ) { + val textIn = binding.editComment.text + val nonNullText = textIn.toString() + status.id.let { + try { + val response = api.postStatus(credential, nonNullText, it) + binding.commentIn.visibility = View.GONE + + //Add the comment to the comment section + addComment( + binding.root.context, binding.commentContainer, response.account!!.username!!, + response.content!!, response.mentions.orEmpty(), credential + ) + + Toast.makeText( + binding.root.context, + binding.root.context.getString(R.string.comment_posted).format(textIn), + Toast.LENGTH_SHORT + ).show() + } catch (exception: IOException) { + Log.e("COMMENT ERROR", exception.toString()) + Toast.makeText( + binding.root.context, binding.root.context.getString(R.string.comment_error), + Toast.LENGTH_SHORT + ).show() + } catch (exception: HttpException) { + Toast.makeText( + binding.root.context, binding.root.context.getString(R.string.comment_error), + Toast.LENGTH_SHORT + ).show() + Log.e("ERROR_CODE", exception.code().toString()) + } + } + } + } diff --git a/app/src/main/java/com/h/pixeldroid/posts/PostFragment.kt b/app/src/main/java/com/h/pixeldroid/posts/PostFragment.kt deleted file mode 100644 index c53969a4..00000000 --- a/app/src/main/java/com/h/pixeldroid/posts/PostFragment.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.h.pixeldroid.posts - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.lifecycle.lifecycleScope -import com.h.pixeldroid.databinding.PostFragmentBinding -import com.h.pixeldroid.utils.api.objects.Status -import com.h.pixeldroid.utils.api.objects.Status.Companion.DOMAIN_TAG -import com.h.pixeldroid.utils.api.objects.Status.Companion.POST_TAG -import com.h.pixeldroid.utils.BaseFragment -import com.h.pixeldroid.utils.bindingLifecycleAware -import com.h.pixeldroid.utils.displayDimensionsInPx - - -class PostFragment : BaseFragment() { - - private lateinit var statusDomain: String - private var currentStatus: Status? = null - - var binding: PostFragmentBinding by bindingLifecycleAware() - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - currentStatus = arguments?.getSerializable(POST_TAG) as Status? - statusDomain = arguments?.getString(DOMAIN_TAG)!! - } - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - binding = PostFragmentBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - val api = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db) - - val holder = StatusViewHolder(binding) - - - - holder.bind(currentStatus, api, db, lifecycleScope, requireContext().displayDimensionsInPx()) - } - -} diff --git a/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt b/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt index eb2c3902..78ae8132 100644 --- a/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt +++ b/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt @@ -19,13 +19,14 @@ import com.bumptech.glide.RequestBuilder import com.google.android.material.snackbar.Snackbar import com.h.pixeldroid.R import com.h.pixeldroid.databinding.AlbumImageViewBinding -import com.h.pixeldroid.databinding.CommentBinding import com.h.pixeldroid.databinding.PostFragmentBinding import com.h.pixeldroid.utils.BlurHashDecoder 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_TAG +import com.h.pixeldroid.utils.api.objects.Status.Companion.VIEW_COMMENTS_TAG import com.h.pixeldroid.utils.db.AppDatabase import com.karumi.dexter.Dexter import com.karumi.dexter.listener.PermissionDeniedResponse @@ -44,7 +45,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold private var status: Status? = null - fun bind(status: Status?, pixelfedAPI: PixelfedAPI, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope, displayDimensionsInPx: Pair) { + fun bind(status: Status?, pixelfedAPI: PixelfedAPI, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope, displayDimensionsInPx: Pair, isActivity: Boolean = false) { this.itemView.visibility = View.VISIBLE this.status = status @@ -72,9 +73,9 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold val user = db.userDao().getActiveUser()!! - setupPost(picRequest, user.instance_uri, false) + setupPost(picRequest, user.instance_uri, isActivity) - activateButtons(pixelfedAPI, db, lifecycleScope) + activateButtons(pixelfedAPI, db, lifecycleScope, isActivity) } @@ -130,10 +131,6 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold binding.postPager.visibility = View.GONE binding.postIndicator.visibility = View.GONE } - - //Set comment initial visibility - binding.commentIn.visibility = View.GONE - binding.commentContainer.visibility = View.GONE } private fun setupPostPics( @@ -201,7 +198,12 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold } } - private fun activateButtons(api: PixelfedAPI, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope){ + private fun activateButtons( + api: PixelfedAPI, + db: AppDatabase, + lifecycleScope: LifecycleCoroutineScope, + isActivity: Boolean + ){ val user = db.userDao().getActiveUser()!! val credential = "Bearer ${user.accessToken}" @@ -217,9 +219,8 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold api, credential, status?.reblogged ?: false, lifecycleScope ) - activateCommenter(api, credential, lifecycleScope) - showComments(api, credential, lifecycleScope) + showComments(api, credential, lifecycleScope, isActivity) activateMoreButton(api, db, lifecycleScope) } @@ -518,7 +519,8 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold private fun showComments( api: PixelfedAPI, credential: String, - lifecycleScope: LifecycleCoroutineScope + lifecycleScope: LifecycleCoroutineScope, + isActivity: Boolean ) { //Show all comments of a post if (status?.replies_count == 0) { @@ -529,143 +531,29 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold status?.replies_count ?: 0, status?.replies_count ?: 0 ) - setOnClickListener { - visibility = View.GONE - - lifecycleScope.launchWhenCreated { - //Retrieve the comments - retrieveComments(api, credential) + if(isActivity) { + setOnClickListener { + lifecycleScope.launchWhenCreated { + //Retrieve the comments + //TODO retrieveComments(api, credential) + } + } + } else { + setOnClickListener { + lifecycleScope.launchWhenCreated { + //Open status in activity + val intent = Intent(context, PostActivity::class.java) + intent.putExtra(POST_TAG, status) + intent.putExtra(VIEW_COMMENTS_TAG, true) + context.startActivity(intent) + } } } } } } - private fun activateCommenter( - api: PixelfedAPI, - credential: String, - lifecycleScope: LifecycleCoroutineScope - ) { - //Toggle comment button - toggleCommentInput() - //Activate commenterpostPicture - binding.submitComment.setOnClickListener { - val textIn = binding.editComment.text - //Open text input - if(textIn.isNullOrEmpty()) { - Toast.makeText( - binding.root.context, - binding.root.context.getString(R.string.empty_comment), - Toast.LENGTH_SHORT - ).show() - } else { - //Post the comment - lifecycleScope.launchWhenCreated { - postComment(api, credential) - } - } - } - } - - private fun toggleCommentInput() { - //Toggle comment button - binding.commenter.setOnClickListener { - when(binding.commentIn.visibility) { - View.VISIBLE -> { - binding.commentIn.visibility = View.GONE - ImageConverter.setImageFromDrawable( - binding.root, - binding.commenter, - R.drawable.ic_comment_empty - ) - } - View.GONE -> { - binding.commentIn.visibility = View.VISIBLE - ImageConverter.setImageFromDrawable( - binding.root, - binding.commenter, - R.drawable.ic_comment_blue - ) - } - } - } - } - - fun addComment(context: android.content.Context, commentContainer: LinearLayout, commentUsername: String, commentContent: String) { - - - val itemBinding = CommentBinding.inflate( - LayoutInflater.from(context), commentContainer, false - ) - - itemBinding.user.text = commentUsername - itemBinding.commentText.text = commentContent - } - - private suspend fun retrieveComments( - api: PixelfedAPI, - credential: String, - ) { - status?.id?.let { - try { - val statuses = api.statusComments(it, credential).descendants - - binding.commentContainer.removeAllViews() - - //Create the new views for each comment - for (status in statuses) { - addComment(binding.root.context, binding.commentContainer, status.account!!.username!!, - status.content!! - ) - } - binding.commentContainer.visibility = View.VISIBLE - - } catch (exception: IOException) { - Log.e("COMMENT FETCH ERROR", exception.toString()) - } catch (exception: HttpException) { - Log.e("COMMENT ERROR", "${exception.code()} with body ${exception.response()?.errorBody()}") - } - } - } - - private suspend fun postComment( - api: PixelfedAPI, - credential: String, - ) { - val textIn = binding.editComment.text - val nonNullText = textIn.toString() - status?.id?.let { - try { - val response = api.postStatus(credential, nonNullText, it) - binding.commentIn.visibility = View.GONE - - //Add the comment to the comment section - addComment( - binding.root.context, binding.commentContainer, response.account!!.username!!, - response.content!! - ) - - Toast.makeText( - binding.root.context, - binding.root.context.getString(R.string.comment_posted).format(textIn), - Toast.LENGTH_SHORT - ).show() - } catch (exception: IOException) { - Log.e("COMMENT ERROR", exception.toString()) - Toast.makeText( - binding.root.context, binding.root.context.getString(R.string.comment_error), - Toast.LENGTH_SHORT - ).show() - } catch (exception: HttpException) { - Toast.makeText( - binding.root.context, binding.root.context.getString(R.string.comment_error), - Toast.LENGTH_SHORT - ).show() - Log.e("ERROR_CODE", exception.code().toString()) - } - } - } companion object { diff --git a/app/src/main/java/com/h/pixeldroid/utils/api/objects/Status.kt b/app/src/main/java/com/h/pixeldroid/utils/api/objects/Status.kt index c9fb3a94..715d28bb 100644 --- a/app/src/main/java/com/h/pixeldroid/utils/api/objects/Status.kt +++ b/app/src/main/java/com/h/pixeldroid/utils/api/objects/Status.kt @@ -64,8 +64,7 @@ open class Status( { companion object { const val POST_TAG = "postTag" - const val DOMAIN_TAG = "domainTag" - const val DISCOVER_TAG = "discoverTag" + const val VIEW_COMMENTS_TAG = "view_comments_tag" } fun getPostUrl() : String? = media_attachments?.firstOrNull()?.url diff --git a/app/src/main/res/layout/activity_post.xml b/app/src/main/res/layout/activity_post.xml index 8f9084e5..26edcf48 100644 --- a/app/src/main/res/layout/activity_post.xml +++ b/app/src/main/res/layout/activity_post.xml @@ -1,26 +1,58 @@ - - - - - + android:layout_height="wrap_content"> - \ No newline at end of file + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/post_fragment.xml b/app/src/main/res/layout/post_fragment.xml index 78189b18..cc267614 100644 --- a/app/src/main/res/layout/post_fragment.xml +++ b/app/src/main/res/layout/post_fragment.xml @@ -6,8 +6,7 @@ android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" - xmlns:sparkbutton="http://schemas.android.com/apk/res-auto" - tools:context=".posts.PostFragment"> + xmlns:sparkbutton="http://schemas.android.com/apk/res-auto"> - - - - - - - - - - - - - - + tools:text="3 comments" /> From a90a42f033be85937761b3af7222970795881c46 Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Sun, 14 Mar 2021 17:21:56 +0100 Subject: [PATCH 2/2] Fix comments (still using linearlayout for now) --- .../com/h/pixeldroid/posts/PostActivity.kt | 64 ++++++++----------- .../h/pixeldroid/posts/StatusViewHolder.kt | 41 +++++++----- .../h/pixeldroid/utils/api/objects/Status.kt | 1 + .../main/res/drawable/selector_commenter.xml | 9 +++ app/src/main/res/layout/activity_post.xml | 3 +- app/src/main/res/layout/post_fragment.xml | 2 +- 6 files changed, 64 insertions(+), 56 deletions(-) create mode 100644 app/src/main/res/drawable/selector_commenter.xml diff --git a/app/src/main/java/com/h/pixeldroid/posts/PostActivity.kt b/app/src/main/java/com/h/pixeldroid/posts/PostActivity.kt index 6331e4ae..be453563 100644 --- a/app/src/main/java/com/h/pixeldroid/posts/PostActivity.kt +++ b/app/src/main/java/com/h/pixeldroid/posts/PostActivity.kt @@ -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, - credential: String) { + private fun addComment(context: Context, commentContainer: LinearLayout, + commentUsername: String, commentContent: String, mentions: List, + 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) { diff --git a/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt b/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt index 78ae8132..f25a8160 100644 --- a/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt +++ b/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt @@ -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 diff --git a/app/src/main/java/com/h/pixeldroid/utils/api/objects/Status.kt b/app/src/main/java/com/h/pixeldroid/utils/api/objects/Status.kt index 715d28bb..9b88e619 100644 --- a/app/src/main/java/com/h/pixeldroid/utils/api/objects/Status.kt +++ b/app/src/main/java/com/h/pixeldroid/utils/api/objects/Status.kt @@ -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 diff --git a/app/src/main/res/drawable/selector_commenter.xml b/app/src/main/res/drawable/selector_commenter.xml new file mode 100644 index 00000000..84248bac --- /dev/null +++ b/app/src/main/res/drawable/selector_commenter.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_post.xml b/app/src/main/res/layout/activity_post.xml index 26edcf48..9108e345 100644 --- a/app/src/main/res/layout/activity_post.xml +++ b/app/src/main/res/layout/activity_post.xml @@ -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">