Skip to content

Commit d3a6991

Browse files
Use Fragment.content extension, improve comment composables
1 parent 3641698 commit d3a6991

4 files changed

Lines changed: 53 additions & 73 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ dependencies {
216216
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
217217
implementation 'androidx.core:core-ktx:1.12.0'
218218
implementation 'androidx.documentfile:documentfile:1.0.1'
219-
implementation 'androidx.fragment:fragment-ktx:1.6.2'
219+
implementation 'androidx.fragment:fragment-compose:1.8.2'
220220
implementation "androidx.lifecycle:lifecycle-livedata-ktx:${androidxLifecycleVersion}"
221221
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${androidxLifecycleVersion}"
222222
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'

app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,25 @@ package org.schabi.newpipe.fragments.list.comments
33
import android.os.Bundle
44
import android.view.LayoutInflater
55
import android.view.ViewGroup
6-
import androidx.compose.ui.platform.ComposeView
7-
import androidx.compose.ui.platform.ViewCompositionStrategy
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.material3.Surface
88
import androidx.core.os.bundleOf
99
import androidx.fragment.app.Fragment
10-
import androidx.lifecycle.viewmodel.compose.viewModel
10+
import androidx.fragment.compose.content
1111
import org.schabi.newpipe.ui.components.comment.CommentSection
1212
import org.schabi.newpipe.ui.theme.AppTheme
1313
import org.schabi.newpipe.util.KEY_SERVICE_ID
1414
import org.schabi.newpipe.util.KEY_URL
15-
import org.schabi.newpipe.viewmodels.CommentsViewModel
1615

1716
class CommentsFragment : Fragment() {
1817
override fun onCreateView(
1918
inflater: LayoutInflater,
2019
container: ViewGroup?,
2120
savedInstanceState: Bundle?
22-
) = ComposeView(requireContext()).apply {
23-
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
24-
setContent {
25-
val viewModel = viewModel<CommentsViewModel>()
26-
AppTheme {
27-
CommentSection(commentsFlow = viewModel.comments)
21+
) = content {
22+
AppTheme {
23+
Surface(color = MaterialTheme.colorScheme.background) {
24+
CommentSection()
2825
}
2926
}
3027
}

app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package org.schabi.newpipe.fragments.list.videos
22

33
import android.os.Bundle
44
import android.view.LayoutInflater
5-
import android.view.View
65
import android.view.ViewGroup
76
import androidx.compose.material3.MaterialTheme
87
import androidx.compose.material3.Surface
9-
import androidx.compose.ui.platform.ComposeView
10-
import androidx.compose.ui.platform.ViewCompositionStrategy
118
import androidx.core.os.bundleOf
129
import androidx.fragment.app.Fragment
10+
import androidx.fragment.compose.content
1311
import org.schabi.newpipe.extractor.stream.StreamInfo
1412
import org.schabi.newpipe.ktx.serializable
1513
import org.schabi.newpipe.ui.components.video.RelatedItems
@@ -21,15 +19,10 @@ class RelatedItemsFragment : Fragment() {
2119
inflater: LayoutInflater,
2220
container: ViewGroup?,
2321
savedInstanceState: Bundle?
24-
): View {
25-
return ComposeView(requireContext()).apply {
26-
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
27-
setContent {
28-
AppTheme {
29-
Surface(color = MaterialTheme.colorScheme.background) {
30-
RelatedItems(requireArguments().serializable<StreamInfo>(KEY_INFO)!!)
31-
}
32-
}
22+
) = content {
23+
AppTheme {
24+
Surface(color = MaterialTheme.colorScheme.background) {
25+
RelatedItems(requireArguments().serializable<StreamInfo>(KEY_INFO)!!)
3326
}
3427
}
3528
}
Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
package org.schabi.newpipe.ui.components.comment
22

33
import android.content.res.Configuration
4-
import androidx.compose.foundation.layout.Column
5-
import androidx.compose.foundation.layout.fillMaxWidth
64
import androidx.compose.foundation.layout.padding
7-
import androidx.compose.foundation.layout.wrapContentSize
85
import androidx.compose.foundation.lazy.LazyColumn
96
import androidx.compose.foundation.lazy.rememberLazyListState
107
import androidx.compose.material3.HorizontalDivider
118
import androidx.compose.material3.MaterialTheme
129
import androidx.compose.material3.Surface
13-
import androidx.compose.material3.Text
1410
import androidx.compose.runtime.Composable
1511
import androidx.compose.runtime.derivedStateOf
1612
import androidx.compose.runtime.getValue
1713
import androidx.compose.runtime.remember
18-
import androidx.compose.ui.Alignment
1914
import androidx.compose.ui.Modifier
2015
import androidx.compose.ui.input.nestedscroll.nestedScroll
2116
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
22-
import androidx.compose.ui.res.stringResource
2317
import androidx.compose.ui.tooling.preview.Preview
2418
import androidx.compose.ui.tooling.preview.PreviewParameter
2519
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
2620
import androidx.compose.ui.unit.dp
27-
import androidx.compose.ui.unit.sp
21+
import androidx.lifecycle.viewmodel.compose.viewModel
2822
import androidx.paging.LoadState
2923
import androidx.paging.LoadStates
3024
import androidx.paging.PagingData
@@ -37,66 +31,58 @@ import org.schabi.newpipe.extractor.comments.CommentsInfoItem
3731
import org.schabi.newpipe.extractor.stream.Description
3832
import org.schabi.newpipe.paging.CommentsDisabledException
3933
import org.schabi.newpipe.ui.components.common.LoadingIndicator
34+
import org.schabi.newpipe.ui.components.common.NoItemsMessage
4035
import org.schabi.newpipe.ui.theme.AppTheme
36+
import org.schabi.newpipe.viewmodels.CommentsViewModel
37+
38+
@Composable
39+
fun CommentSection(commentsViewModel: CommentsViewModel = viewModel()) {
40+
CommentSection(commentsFlow = commentsViewModel.comments)
41+
}
4142

4243
@Composable
4344
fun CommentSection(
4445
parentComment: CommentsInfoItem? = null,
4546
commentsFlow: Flow<PagingData<CommentsInfoItem>>
4647
) {
47-
Surface(color = MaterialTheme.colorScheme.background) {
48-
val comments = commentsFlow.collectAsLazyPagingItems()
49-
val itemCount by remember { derivedStateOf { comments.itemCount } }
50-
val nestedScrollInterop = rememberNestedScrollInteropConnection()
51-
val state = rememberLazyListState()
48+
val comments = commentsFlow.collectAsLazyPagingItems()
49+
val itemCount by remember { derivedStateOf { comments.itemCount } }
50+
val nestedScrollInterop = rememberNestedScrollInteropConnection()
51+
val state = rememberLazyListState()
5252

53-
LazyColumnScrollbar(state = state) {
54-
LazyColumn(modifier = Modifier.nestedScroll(nestedScrollInterop), state = state) {
55-
if (parentComment != null) {
56-
item {
57-
CommentRepliesHeader(comment = parentComment)
58-
HorizontalDivider(thickness = 1.dp)
59-
}
53+
LazyColumnScrollbar(state = state) {
54+
LazyColumn(modifier = Modifier.nestedScroll(nestedScrollInterop), state = state) {
55+
if (parentComment != null) {
56+
item {
57+
CommentRepliesHeader(comment = parentComment)
58+
HorizontalDivider(thickness = 1.dp)
6059
}
60+
}
6161

62-
if (itemCount == 0) {
63-
item {
64-
val refresh = comments.loadState.refresh
65-
if (refresh is LoadState.Loading) {
66-
LoadingIndicator(modifier = Modifier.padding(top = 8.dp))
62+
if (itemCount == 0) {
63+
item {
64+
val refresh = comments.loadState.refresh
65+
if (refresh is LoadState.Loading) {
66+
LoadingIndicator(modifier = Modifier.padding(top = 8.dp))
67+
} else {
68+
val error = (refresh as? LoadState.Error)?.error
69+
val message = if (error is CommentsDisabledException) {
70+
R.string.comments_are_disabled
6771
} else {
68-
NoCommentsMessage((refresh as? LoadState.Error)?.error)
72+
R.string.no_comments
6973
}
70-
}
71-
} else {
72-
items(itemCount) {
73-
Comment(comment = comments[it]!!)
74+
NoItemsMessage(message)
7475
}
7576
}
77+
} else {
78+
items(itemCount) {
79+
Comment(comment = comments[it]!!)
80+
}
7681
}
7782
}
7883
}
7984
}
8085

81-
@Composable
82-
private fun NoCommentsMessage(error: Throwable?) {
83-
val message = if (error is CommentsDisabledException) {
84-
R.string.comments_are_disabled
85-
} else {
86-
R.string.no_comments
87-
}
88-
89-
Column(
90-
modifier = Modifier
91-
.fillMaxWidth()
92-
.wrapContentSize(Alignment.Center),
93-
horizontalAlignment = Alignment.CenterHorizontally
94-
) {
95-
Text(text = "(╯°-°)╯", fontSize = 35.sp)
96-
Text(text = stringResource(id = message), fontSize = 24.sp)
97-
}
98-
}
99-
10086
private class CommentDataProvider : PreviewParameterProvider<PagingData<CommentsInfoItem>> {
10187
private val notLoading = LoadState.NotLoading(true)
10288

@@ -130,7 +116,9 @@ private fun CommentSectionPreview(
130116
@PreviewParameter(CommentDataProvider::class) pagingData: PagingData<CommentsInfoItem>
131117
) {
132118
AppTheme {
133-
CommentSection(commentsFlow = flowOf(pagingData))
119+
Surface(color = MaterialTheme.colorScheme.background) {
120+
CommentSection(commentsFlow = flowOf(pagingData))
121+
}
134122
}
135123
}
136124

@@ -154,6 +142,8 @@ private fun CommentRepliesPreview() {
154142
val flow = flowOf(PagingData.from(replies))
155143

156144
AppTheme {
157-
CommentSection(parentComment = comment, commentsFlow = flow)
145+
Surface(color = MaterialTheme.colorScheme.background) {
146+
CommentSection(parentComment = comment, commentsFlow = flow)
147+
}
158148
}
159149
}

0 commit comments

Comments
 (0)