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">