Working comment improvements

This commit is contained in:
Matthieu 2022-08-02 19:07:06 +02:00
parent f490735113
commit 696228517d
7 changed files with 317 additions and 121 deletions

View File

@ -45,6 +45,10 @@ android {
testBuildType "staging"
buildTypes {
debug {
applicationIdSuffix '.debug'
versionNameSuffix "-debug"
}
staging {
initWith debug
testCoverageEnabled true
@ -67,10 +71,6 @@ android {
buildConfigField "String", "CLIENT_ID", System.getenv("CLIENT_ID") ?: localProperties['CLIENT_ID'] ?: '""'
buildConfigField "String", "CLIENT_SECRET", System.getenv("CLIENT_SECRET") ?: localProperties['CLIENT_SECRET'] ?: '""'
}
debug {
applicationIdSuffix '.debug'
versionNameSuffix "-debug"
}
release {
minifyEnabled true
shrinkResources true

View File

@ -1,25 +1,36 @@
package org.pixeldroid.app.posts
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
import android.widget.LinearLayout
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.paging.ExperimentalPagingApi
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Job
import org.pixeldroid.app.R
import org.pixeldroid.app.databinding.ActivityPostBinding
import org.pixeldroid.app.databinding.CommentBinding
import org.pixeldroid.app.posts.feeds.initAdapter
import org.pixeldroid.app.posts.feeds.launch
import org.pixeldroid.app.posts.feeds.uncachedFeeds.FeedViewModel
import org.pixeldroid.app.posts.feeds.uncachedFeeds.comments.CommentContentRepository
import org.pixeldroid.app.profile.ProfileViewModelFactory
import org.pixeldroid.app.utils.BaseThemedWithBarActivity
import org.pixeldroid.app.utils.api.PixelfedAPI
import org.pixeldroid.app.utils.api.objects.Mention
import org.pixeldroid.app.utils.api.objects.Status
import org.pixeldroid.app.utils.api.objects.Status.Companion.POST_COMMENT_TAG
import org.pixeldroid.app.utils.api.objects.Status.Companion.POST_TAG
import org.pixeldroid.app.utils.api.objects.Status.Companion.VIEW_COMMENTS_TAG
import org.pixeldroid.app.utils.displayDimensionsInPx
import org.pixeldroid.app.utils.setProfileImageFromURL
import retrofit2.HttpException
import java.io.IOException
@ -27,6 +38,10 @@ class PostActivity : BaseThemedWithBarActivity() {
lateinit var domain : String
private lateinit var binding: ActivityPostBinding
private lateinit var profileAdapter: PagingDataAdapter<Status, RecyclerView.ViewHolder>
private lateinit var viewModel: FeedViewModel<Status>
private var job: Job? = null
private lateinit var status: Status
@ -45,7 +60,6 @@ class PostActivity : BaseThemedWithBarActivity() {
domain = user?.instance_uri.orEmpty()
supportActionBar?.title = getString(R.string.post_title).format(status.account?.getDisplayName())
val holder = StatusViewHolder(binding.postFragmentSingle)
@ -53,6 +67,7 @@ class PostActivity : BaseThemedWithBarActivity() {
holder.bind(status, apiHolder, db, lifecycleScope, displayDimensionsInPx(), isActivity = true)
activateCommenter()
retrieveComments()
if(viewComments || postComment){
//Scroll already down as much as possible (since comments are not loaded yet)
@ -63,12 +78,6 @@ class PostActivity : BaseThemedWithBarActivity() {
window.setSoftInputMode(SOFT_INPUT_STATE_VISIBLE)
binding.editComment.requestFocus()
}
// also retrieve comments if we're not posting the comment
if(!postComment) retrieveComments(apiHolder.api!!)
}
binding.postFragmentSingle.viewComments.setOnClickListener {
retrieveComments(apiHolder.api!!)
}
}
@ -92,53 +101,24 @@ class PostActivity : BaseThemedWithBarActivity() {
}
}
private fun addComment(context: Context, commentContainer: LinearLayout,
commentUsername: String, commentContent: String, mentions: List<Mention>) {
@OptIn(ExperimentalPagingApi::class)
private fun retrieveComments() {
// get the view model
@Suppress("UNCHECKED_CAST")
viewModel = ViewModelProvider(this@PostActivity, ProfileViewModelFactory(
CommentContentRepository(
apiHolder.setToCurrentUser(),
status.id
)
)
)[FeedViewModel::class.java] as FeedViewModel<Status>
profileAdapter = CommentAdapter()
initAdapter(binding.postCommentsProgressBar, binding.postRefreshLayout,
binding.commentRecyclerView, binding.motionLayout, binding.errorLayout,
profileAdapter)
val itemBinding = CommentBinding.inflate(
LayoutInflater.from(context), commentContainer, true
)
itemBinding.user.text = commentUsername
itemBinding.commentText.text = parseHTMLText(
commentContent,
mentions,
apiHolder,
context,
lifecycleScope
)
}
private fun retrieveComments(api: PixelfedAPI) {
lifecycleScope.launchWhenCreated {
status.id.let {
try {
val statuses = api.statusComments(it).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()
)
}
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) {
Log.e("COMMENT ERROR", "${exception.code()} with body ${exception.response()?.errorBody()}")
}
}
}
job = launch(job, lifecycleScope, viewModel, profileAdapter)
}
private suspend fun postComment(
@ -152,10 +132,7 @@ class PostActivity : BaseThemedWithBarActivity() {
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()
)
profileAdapter.refresh()
Toast.makeText(
binding.root.context,
@ -178,4 +155,79 @@ class PostActivity : BaseThemedWithBarActivity() {
}
}
inner class CommentViewHolder(val binding: CommentBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(comment: Status) {
setProfileImageFromURL(binding.profilePic,
comment.account!!.anyAvatar(),
binding.profilePic
)
binding.user.text = comment.account.username
binding.commentText.text = parseHTMLText(
comment.content!!,
comment.mentions.orEmpty(),
apiHolder,
this@PostActivity,
lifecycleScope
)
if(comment.replies_count == 0 || comment.replies_count == null){
binding.replies.visibility = View.GONE
} else {
binding.replies.visibility = View.VISIBLE
binding.replies.text = resources.getQuantityString(
R.plurals.replies_count,
comment.replies_count,
comment.replies_count
)
}
binding.comment.setOnClickListener{ openComment(comment) }
binding.profilePic.setOnClickListener{ comment.account.openProfile(itemView.context) }
binding.user.setOnClickListener { comment.account.openProfile(itemView.context) }
}
private fun openComment(comment: Status) {
val intent = Intent(itemView.context, PostActivity::class.java).apply {
putExtra(POST_TAG, comment)
}
itemView.context.startActivity(intent)
}
}
inner class CommentAdapter : PagingDataAdapter<Status, RecyclerView.ViewHolder>(
UIMODEL_COMPARATOR
) {
fun create(parent: ViewGroup): CommentViewHolder {
val itemBinding = CommentBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
return CommentViewHolder(itemBinding)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return create(parent)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val post = getItem(position)
post?.let {
(holder as CommentViewHolder).bind(it)
}
}
}
private val UIMODEL_COMPARATOR = object : DiffUtil.ItemCallback<Status>() {
override fun areItemsTheSame(oldItem: Status, newItem: Status): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Status, newItem: Status): Boolean =
oldItem.content == newItem.content
}
}

View File

@ -0,0 +1,33 @@
package org.pixeldroid.app.posts.feeds.uncachedFeeds.comments
import androidx.paging.ExperimentalPagingApi
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow
import org.pixeldroid.app.posts.feeds.uncachedFeeds.UncachedContentRepository
import org.pixeldroid.app.posts.feeds.uncachedFeeds.profile.ProfilePagingSource
import org.pixeldroid.app.utils.api.PixelfedAPI
import org.pixeldroid.app.utils.api.objects.Status
import javax.inject.Inject
class CommentContentRepository @ExperimentalPagingApi
@Inject constructor(
private val api: PixelfedAPI,
private val statusId: String
) : UncachedContentRepository<Status> {
override fun getStream(): Flow<PagingData<Status>> {
return Pager(
config = PagingConfig(
initialLoadSize = NETWORK_PAGE_SIZE,
pageSize = NETWORK_PAGE_SIZE),
pagingSourceFactory = {
CommentPagingSource(api, statusId)
}
).flow
}
companion object {
private const val NETWORK_PAGE_SIZE = 20
}
}

View File

@ -0,0 +1,34 @@
package org.pixeldroid.app.posts.feeds.uncachedFeeds.comments
import androidx.paging.PagingSource
import androidx.paging.PagingState
import org.pixeldroid.app.utils.api.PixelfedAPI
import org.pixeldroid.app.utils.api.objects.Status
import retrofit2.HttpException
import java.io.IOException
class CommentPagingSource(
private val api: PixelfedAPI,
private val statusId: String
) : PagingSource<String, Status>() {
override suspend fun load(params: LoadParams<String>): LoadResult<String, Status> {
//val position = params.key
return try {
val comments = api.statusComments(statusId).descendants
// TODO use pagination to have many comments also work
// For now, don't paginate (nextKey and prevKey null)
LoadResult.Page(
data = comments,
prevKey = null,
nextKey = null
)
} catch (exception: HttpException) {
LoadResult.Error(exception)
} catch (exception: IOException) {
LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<String, Status>): String? = null
}

View File

@ -1,59 +1,112 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".posts.PostActivity"
android:id="@+id/scrollview">
tools:context=".posts.PostActivity">
<androidx.constraintlayout.widget.ConstraintLayout
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/postRefreshLayout"
android:layout_width="0dp"
android:layout_height="0dp" />
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<include layout="@layout/post_fragment"
android:id="@+id/postFragmentSingle"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
<include
android:id="@+id/postFragmentSingle"
layout="@layout/post_fragment" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/commentIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/postFragmentSingle"
tools:layout_editor_absoluteX="10dp">
<com.google.android.material.textfield.TextInputLayout
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/submitComment"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="@+id/editComment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
android:layout_height="wrap_content"
android:hint="@string/comment"
android:importantForAutofill="no"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
<EditText
android:id="@+id/editComment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/comment"
android:inputType="text"
android:importantForAutofill="no" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/submitComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:contentDescription="@string/submit_comment"
android:text="@string/comment"
app:layout_constraintBottom_toBottomOf="@+id/textInputLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textInputLayout2"
app:layout_constraintTop_toTopOf="@+id/textInputLayout2" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageButton
android:id="@+id/submitComment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_weight="1"
android:contentDescription="@string/submit_comment"
android:src="@drawable/ic_send_blue" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/commentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/commentIn">
</com.google.android.material.appbar.CollapsingToolbarLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
</ScrollView>
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:visibility="visible"
app:layoutDescription="@xml/error_layout_xml_error_scene"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include
android:id="@+id/errorLayout"
layout="@layout/error_layout"
tools:layout_editor_absoluteX="50dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/commentRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layoutManager="LinearLayoutManager"
app:layout_constraintTop_toBottomOf="@id/errorLayout"
tools:listitem="@layout/comment" />
<ProgressBar
android:id="@+id/postCommentsProgressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/errorLayout" />
</androidx.constraintlayout.motion.widget.MotionLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -1,39 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
android:layout_height="wrap_content"
android:layout_margin="4dp">
<androidx.cardview.widget.CardView
android:id="@+id/comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<ImageView
android:id="@+id/profilePic"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:contentDescription="@string/profile_picture"
tools:src="@drawable/ic_default_user"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8"
android:layout_marginStart="10dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/profilePic"
app:layout_constraintTop_toTopOf="parent"
tools:text="username" />
<TextView
android:id="@+id/commentText"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
tools:text="This is a comment on this awesome post" />
</LinearLayout>
</androidx.cardview.widget.CardView>
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/user"
app:layout_constraintTop_toBottomOf="@+id/user"
tools:text="This is a comment on this awesome post. Very long comment! \nMaybe with multiple lines" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/replies"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="@id/commentText"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="@id/commentText"
app:layout_constraintTop_toBottomOf="@id/commentText"
tools:text="3 replies" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

View File

@ -291,4 +291,8 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
<string name="add_images_error">Error while adding images</string>
<string name="notification_thumbnail">"Thumbnail of image in this notification's post"</string>
<string name="post_preview">Preview of a post</string>
<plurals name="replies_count">
<item quantity="one">%d reply</item>
<item quantity="other">%d replies</item>
</plurals>
</resources>