Skip to content

Commit

Permalink
Added missing comment features, fixed theming
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed May 17, 2024
1 parent c106fb0 commit 64a92bd
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 107 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ private void openDetailFragmentFromCommentReplies(
@Nullable final CommentRepliesFragment repliesFragment =
(CommentRepliesFragment) fm.findFragmentByTag(CommentRepliesFragment.TAG);
@Nullable final CommentsInfoItem rootComment =
repliesFragment == null ? null : repliesFragment.commentsInfoItem;
repliesFragment == null ? null : repliesFragment.getCommentsInfoItem();

// sometimes this function pops the backstack, other times it's handled by the system
if (popBackStack) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package org.schabi.newpipe.fragments.list.comments

import android.content.res.Configuration
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -20,65 +23,109 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.fragment.app.FragmentActivity
import coil.compose.AsyncImage
import org.schabi.newpipe.R
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
import org.schabi.newpipe.extractor.stream.Description
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.util.image.ImageStrategy

@Composable
fun Comment(comment: CommentsInfoItem) {
val context = LocalContext.current

Row(modifier = Modifier.padding(all = 8.dp)) {
if (ImageStrategy.shouldLoadImages()) {
AsyncImage(
model = ImageStrategy.choosePreferredImage(comment.uploaderAvatars),
contentDescription = null,
placeholder = painterResource(R.drawable.placeholder_person),
error = painterResource(R.drawable.placeholder_person),
modifier = Modifier
.size(42.dp)
.clip(CircleShape)
.clickable {
NavigationHelper.openCommentAuthorIfPresent(context as FragmentActivity, comment)
}
)
}
Surface(color = MaterialTheme.colorScheme.background) {
Row(modifier = Modifier.padding(all = 8.dp)) {
if (ImageStrategy.shouldLoadImages()) {
AsyncImage(
model = ImageStrategy.choosePreferredImage(comment.uploaderAvatars),
contentDescription = null,
placeholder = painterResource(R.drawable.placeholder_person),
error = painterResource(R.drawable.placeholder_person),
modifier = Modifier
.size(42.dp)
.clip(CircleShape)
.clickable {
NavigationHelper.openCommentAuthorIfPresent(
context as FragmentActivity,
comment
)
}
)
}

Spacer(modifier = Modifier.width(8.dp))

var isExpanded by rememberSaveable { mutableStateOf(false) }

Spacer(modifier = Modifier.width(8.dp))
Column(
modifier = Modifier.clickable { isExpanded = !isExpanded },
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Text(
text = comment.uploaderName,
color = MaterialTheme.colorScheme.secondary
)

var isExpanded by rememberSaveable { mutableStateOf(false) }
Text(
text = comment.commentText.content,
// If the comment is expanded, we display all its content
// otherwise we only display the first two lines
maxLines = if (isExpanded) Int.MAX_VALUE else 2,
style = MaterialTheme.typography.bodyMedium
)

Column(modifier = Modifier.clickable { isExpanded = !isExpanded }) {
Text(
text = comment.uploaderName,
color = MaterialTheme.colors.secondary
)
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Image(
painter = painterResource(R.drawable.ic_thumb_up),
contentDescription = stringResource(R.string.detail_likes_img_view_description)
)
Text(text = comment.likeCount.toString())

Spacer(modifier = Modifier.height(4.dp))
if (comment.isHeartedByUploader) {
Image(
painter = painterResource(R.drawable.ic_heart),
contentDescription = stringResource(R.string.detail_heart_img_view_description)
)
}

Text(
text = comment.commentText.content,
// If the comment is expanded, we display all its content
// otherwise we only display the first line
maxLines = if (isExpanded) Int.MAX_VALUE else 1,
style = MaterialTheme.typography.body2
)
if (comment.isPinned) {
Image(
painter = painterResource(R.drawable.ic_pin),
contentDescription = stringResource(R.string.detail_pinned_comment_view_description)
)
}
}
}
}

// TODO: Add support for comment replies
}
}

@Preview
@Preview(
name = "Light mode",
uiMode = Configuration.UI_MODE_NIGHT_NO
)
@Preview(
name = "Dark mode",
uiMode = Configuration.UI_MODE_NIGHT_YES
)
@Composable
fun CommentPreview() {
val comment = CommentsInfoItem(1, "", "")
comment.commentText = Description("Hello world!", Description.PLAIN_TEXT)
comment.uploaderName = "Test"
comment.likeCount = 100
comment.isHeartedByUploader = true
comment.isPinned = true

Comment(comment)
AppTheme {
Comment(comment)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
import org.schabi.newpipe.fragments.list.BaseListInfoFragment
import org.schabi.newpipe.info_list.ItemViewMode
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.util.ExtractorHelper
import org.schabi.newpipe.util.Localization
import java.util.Queue
Expand Down Expand Up @@ -50,7 +51,9 @@ class CommentRepliesFragment() : BaseListInfoFragment<CommentsInfoItem, CommentR
return Supplier {
ComposeView(requireContext()).apply {
setContent {
CommentRepliesHeader(commentsInfoItem, disposables)
AppTheme {
CommentRepliesHeader(commentsInfoItem, disposables)
}
}
}
}
Expand Down
Loading

0 comments on commit 64a92bd

Please sign in to comment.