From 369a46f8fe09b7a7e8b19d01784fb63a6bdbea9d Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 28 Jun 2024 07:57:25 +0530 Subject: [PATCH] Improve code organization --- .../schabi/newpipe/{ui => compose}/Toolbar.kt | 6 ++--- .../comments => compose/comment}/Comment.kt | 23 ++++--------------- .../comment}/CommentRepliesHeader.kt | 7 +++--- .../comment}/CommentSection.kt | 7 +++--- .../newpipe/{ui => compose}/theme/Color.kt | 2 +- .../{ui => compose}/theme/SizeTokens.kt | 2 +- .../newpipe/{ui => compose}/theme/Theme.kt | 2 +- .../newpipe/compose/util/ParseDescription.kt | 20 ++++++++++++++++ .../list/comments/CommentsFragment.kt | 4 +++- .../comments => paging}/CommentsSource.kt | 2 +- 10 files changed, 43 insertions(+), 32 deletions(-) rename app/src/main/java/org/schabi/newpipe/{ui => compose}/Toolbar.kt (97%) rename app/src/main/java/org/schabi/newpipe/{fragments/list/comments => compose/comment}/Comment.kt (92%) rename app/src/main/java/org/schabi/newpipe/{fragments/list/comments => compose/comment}/CommentRepliesHeader.kt (95%) rename app/src/main/java/org/schabi/newpipe/{fragments/list/comments => compose/comment}/CommentSection.kt (96%) rename app/src/main/java/org/schabi/newpipe/{ui => compose}/theme/Color.kt (98%) rename app/src/main/java/org/schabi/newpipe/{ui => compose}/theme/SizeTokens.kt (88%) rename app/src/main/java/org/schabi/newpipe/{ui => compose}/theme/Theme.kt (98%) create mode 100644 app/src/main/java/org/schabi/newpipe/compose/util/ParseDescription.kt rename app/src/main/java/org/schabi/newpipe/{fragments/list/comments => paging}/CommentsSource.kt (96%) diff --git a/app/src/main/java/org/schabi/newpipe/ui/Toolbar.kt b/app/src/main/java/org/schabi/newpipe/compose/Toolbar.kt similarity index 97% rename from app/src/main/java/org/schabi/newpipe/ui/Toolbar.kt rename to app/src/main/java/org/schabi/newpipe/compose/Toolbar.kt index b788932a2..469d88ec0 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/Toolbar.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/Toolbar.kt @@ -1,4 +1,4 @@ -package org.schabi.newpipe.ui +package org.schabi.newpipe.compose import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -27,8 +27,8 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import org.schabi.newpipe.R -import org.schabi.newpipe.ui.theme.AppTheme -import org.schabi.newpipe.ui.theme.SizeTokens +import org.schabi.newpipe.compose.theme.AppTheme +import org.schabi.newpipe.compose.theme.SizeTokens @Composable fun TextAction(text: String, modifier: Modifier = Modifier) { diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/Comment.kt b/app/src/main/java/org/schabi/newpipe/compose/comment/Comment.kt similarity index 92% rename from app/src/main/java/org/schabi/newpipe/fragments/list/comments/Comment.kt rename to app/src/main/java/org/schabi/newpipe/compose/comment/Comment.kt index abc8bfafe..b63845ae1 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/Comment.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/comment/Comment.kt @@ -1,4 +1,4 @@ -package org.schabi.newpipe.fragments.list.comments +package org.schabi.newpipe.compose.comment import android.content.res.Configuration import androidx.compose.foundation.Image @@ -29,9 +29,6 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.AnnotatedString -import androidx.compose.ui.text.ParagraphStyle -import androidx.compose.ui.text.fromHtml import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter @@ -42,26 +39,16 @@ import androidx.paging.Pager import androidx.paging.PagingConfig import coil.compose.AsyncImage import org.schabi.newpipe.R +import org.schabi.newpipe.compose.theme.AppTheme +import org.schabi.newpipe.compose.util.rememberParsedDescription import org.schabi.newpipe.extractor.Page 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.paging.CommentsSource import org.schabi.newpipe.util.Localization import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.image.ImageStrategy -@Composable -fun rememberParsedText(commentText: Description): AnnotatedString { - // TODO: Handle links and hashtags, Markdown. - return remember(commentText) { - if (commentText.type == Description.HTML) { - AnnotatedString.fromHtml(commentText.content) - } else { - AnnotatedString(commentText.content, ParagraphStyle()) - } - } -} - @OptIn(ExperimentalMaterial3Api::class) @Composable fun Comment(comment: CommentsInfoItem) { @@ -112,7 +99,7 @@ fun Comment(comment: CommentsInfoItem) { } Text( - text = rememberParsedText(comment.commentText), + text = rememberParsedDescription(comment.commentText), // 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, diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesHeader.kt b/app/src/main/java/org/schabi/newpipe/compose/comment/CommentRepliesHeader.kt similarity index 95% rename from app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesHeader.kt rename to app/src/main/java/org/schabi/newpipe/compose/comment/CommentRepliesHeader.kt index 4fe69b9cc..eeb56d998 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesHeader.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/comment/CommentRepliesHeader.kt @@ -1,4 +1,4 @@ -package org.schabi.newpipe.fragments.list.comments +package org.schabi.newpipe.compose.comment import android.content.res.Configuration import androidx.compose.foundation.Image @@ -25,9 +25,10 @@ import androidx.compose.ui.unit.dp import androidx.fragment.app.FragmentActivity import coil.compose.AsyncImage import org.schabi.newpipe.R +import org.schabi.newpipe.compose.theme.AppTheme +import org.schabi.newpipe.compose.util.rememberParsedDescription 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.Localization import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.image.ImageStrategy @@ -106,7 +107,7 @@ fun CommentRepliesHeader(comment: CommentsInfoItem) { } Text( - text = rememberParsedText(comment.commentText), + text = rememberParsedDescription(comment.commentText), style = MaterialTheme.typography.bodyMedium ) } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentSection.kt b/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt similarity index 96% rename from app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentSection.kt rename to app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt index 900392c09..72e401d9a 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentSection.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt @@ -1,4 +1,4 @@ -package org.schabi.newpipe.fragments.list.comments +package org.schabi.newpipe.compose.comment import android.content.res.Configuration import androidx.compose.foundation.layout.Column @@ -28,14 +28,15 @@ import kotlinx.coroutines.flow.flowOf import my.nanihadesuka.compose.LazyColumnScrollbar import my.nanihadesuka.compose.ScrollbarSettings import org.schabi.newpipe.R +import org.schabi.newpipe.compose.theme.AppTheme 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.paging.CommentsDisabledException @Composable fun CommentSection( - flow: Flow>, parentComment: CommentsInfoItem? = null, + flow: Flow> ) { val replies = flow.collectAsLazyPagingItems() val itemCount by remember { derivedStateOf { replies.itemCount } } diff --git a/app/src/main/java/org/schabi/newpipe/ui/theme/Color.kt b/app/src/main/java/org/schabi/newpipe/compose/theme/Color.kt similarity index 98% rename from app/src/main/java/org/schabi/newpipe/ui/theme/Color.kt rename to app/src/main/java/org/schabi/newpipe/compose/theme/Color.kt index b61906ebe..0aa330390 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/theme/Color.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/theme/Color.kt @@ -1,4 +1,4 @@ -package org.schabi.newpipe.ui.theme +package org.schabi.newpipe.compose.theme import androidx.compose.ui.graphics.Color diff --git a/app/src/main/java/org/schabi/newpipe/ui/theme/SizeTokens.kt b/app/src/main/java/org/schabi/newpipe/compose/theme/SizeTokens.kt similarity index 88% rename from app/src/main/java/org/schabi/newpipe/ui/theme/SizeTokens.kt rename to app/src/main/java/org/schabi/newpipe/compose/theme/SizeTokens.kt index d8104d7ae..274fa9d43 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/theme/SizeTokens.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/theme/SizeTokens.kt @@ -1,4 +1,4 @@ -package org.schabi.newpipe.ui.theme +package org.schabi.newpipe.compose.theme import androidx.compose.ui.unit.dp diff --git a/app/src/main/java/org/schabi/newpipe/ui/theme/Theme.kt b/app/src/main/java/org/schabi/newpipe/compose/theme/Theme.kt similarity index 98% rename from app/src/main/java/org/schabi/newpipe/ui/theme/Theme.kt rename to app/src/main/java/org/schabi/newpipe/compose/theme/Theme.kt index 846794d72..1c9325f96 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/theme/Theme.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/theme/Theme.kt @@ -1,4 +1,4 @@ -package org.schabi.newpipe.ui.theme +package org.schabi.newpipe.compose.theme import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.material3.MaterialTheme diff --git a/app/src/main/java/org/schabi/newpipe/compose/util/ParseDescription.kt b/app/src/main/java/org/schabi/newpipe/compose/util/ParseDescription.kt new file mode 100644 index 000000000..567a106ed --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/compose/util/ParseDescription.kt @@ -0,0 +1,20 @@ +package org.schabi.newpipe.compose.util + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.text.ParagraphStyle +import androidx.compose.ui.text.fromHtml +import org.schabi.newpipe.extractor.stream.Description + +@Composable +fun rememberParsedDescription(description: Description): AnnotatedString { + // TODO: Handle links and hashtags, Markdown. + return remember(description) { + if (description.type == Description.HTML) { + AnnotatedString.fromHtml(description.content) + } else { + AnnotatedString(description.content, ParagraphStyle()) + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt index decd9391c..76eb0041e 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt @@ -11,7 +11,9 @@ import androidx.core.os.bundleOf import androidx.fragment.app.Fragment import androidx.paging.Pager import androidx.paging.PagingConfig -import org.schabi.newpipe.ui.theme.AppTheme +import org.schabi.newpipe.compose.comment.CommentSection +import org.schabi.newpipe.compose.theme.AppTheme +import org.schabi.newpipe.paging.CommentsSource import org.schabi.newpipe.util.NO_SERVICE_ID class CommentsFragment : Fragment() { diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsSource.kt b/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt similarity index 96% rename from app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsSource.kt rename to app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt index 9e46a9df1..d78a7a83d 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsSource.kt +++ b/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt @@ -1,4 +1,4 @@ -package org.schabi.newpipe.fragments.list.comments +package org.schabi.newpipe.paging import androidx.paging.PagingState import androidx.paging.rxjava3.RxPagingSource