Skip to content

Commit 2653787

Browse files
Migrate about channel fragment to Jetpack Compose
1 parent de6285b commit 2653787

File tree

7 files changed

+133
-121
lines changed

7 files changed

+133
-121
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.schabi.newpipe.fragments.list.channel
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.ViewGroup
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.material3.Surface
8+
import androidx.core.os.bundleOf
9+
import androidx.fragment.app.Fragment
10+
import androidx.fragment.compose.content
11+
import org.schabi.newpipe.extractor.channel.ChannelInfo
12+
import org.schabi.newpipe.ktx.serializable
13+
import org.schabi.newpipe.ui.components.channel.AboutChannelSection
14+
import org.schabi.newpipe.ui.theme.AppTheme
15+
import org.schabi.newpipe.util.KEY_INFO
16+
17+
class AboutChannelFragment : Fragment() {
18+
override fun onCreateView(
19+
inflater: LayoutInflater,
20+
container: ViewGroup?,
21+
savedInstanceState: Bundle?
22+
) = content {
23+
AppTheme {
24+
Surface(color = MaterialTheme.colorScheme.background) {
25+
AboutChannelSection(requireArguments().serializable(KEY_INFO)!!)
26+
}
27+
}
28+
}
29+
30+
companion object {
31+
@JvmStatic
32+
fun getInstance(channelInfo: ChannelInfo) = AboutChannelFragment().apply {
33+
arguments = bundleOf(KEY_INFO to channelInfo)
34+
}
35+
}
36+
}

app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ private void updateTabs() {
481481
if (ChannelTabHelper.showChannelTab(
482482
context, preferences, R.string.show_channel_tabs_about)) {
483483
tabAdapter.addFragment(
484-
new ChannelAboutFragment(currentInfo),
484+
AboutChannelFragment.getInstance(currentInfo),
485485
context.getString(R.string.channel_tab_about));
486486
}
487487
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.schabi.newpipe.ui.components.channel
2+
3+
import android.content.res.Configuration
4+
import androidx.compose.foundation.layout.Arrangement
5+
import androidx.compose.foundation.layout.Column
6+
import androidx.compose.foundation.layout.padding
7+
import androidx.compose.material3.MaterialTheme
8+
import androidx.compose.material3.Surface
9+
import androidx.compose.material3.Text
10+
import androidx.compose.runtime.Composable
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.platform.LocalContext
13+
import androidx.compose.ui.tooling.preview.Preview
14+
import androidx.compose.ui.unit.dp
15+
import org.schabi.newpipe.R
16+
import org.schabi.newpipe.extractor.channel.ChannelInfo
17+
import org.schabi.newpipe.ui.components.metadata.ImageMetadataItem
18+
import org.schabi.newpipe.ui.components.metadata.MetadataItem
19+
import org.schabi.newpipe.ui.theme.AppTheme
20+
import org.schabi.newpipe.util.Localization
21+
import org.schabi.newpipe.util.NO_SERVICE_ID
22+
import org.schabi.newpipe.util.image.ImageStrategy
23+
24+
@Composable
25+
fun AboutChannelSection(channelInfo: ChannelInfo) {
26+
// This tab currently holds little information, so a lazy column isn't needed here.
27+
Column(
28+
modifier = Modifier.padding(12.dp),
29+
verticalArrangement = Arrangement.spacedBy(4.dp)
30+
) {
31+
val description = channelInfo.description
32+
if (!description.isNullOrEmpty()) {
33+
Text(text = description)
34+
}
35+
36+
val count = channelInfo.subscriberCount
37+
if (count != -1L) {
38+
MetadataItem(
39+
title = R.string.metadata_subscribers,
40+
value = Localization.shortCount(LocalContext.current, count)
41+
)
42+
}
43+
44+
ImageStrategy.choosePreferredImage(channelInfo.avatars)?.let {
45+
ImageMetadataItem(R.string.metadata_avatars, channelInfo.avatars, it)
46+
}
47+
48+
ImageStrategy.choosePreferredImage(channelInfo.banners)?.let {
49+
ImageMetadataItem(R.string.metadata_banners, channelInfo.banners, it)
50+
}
51+
52+
if (channelInfo.tags.isNotEmpty()) {
53+
TagsSection(channelInfo.serviceId, channelInfo.tags)
54+
}
55+
}
56+
}
57+
58+
@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO)
59+
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
60+
@Composable
61+
private fun AboutChannelSectionPreview() {
62+
val info = ChannelInfo(NO_SERVICE_ID, "", "", "", "")
63+
info.description = "This is an example description"
64+
info.subscriberCount = 10
65+
66+
AppTheme {
67+
Surface(color = MaterialTheme.colorScheme.background) {
68+
AboutChannelSection(info)
69+
}
70+
}
71+
}

app/src/main/java/org/schabi/newpipe/ui/components/metadata/ImageMetadataItem.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.schabi.newpipe.ui.components.metadata
33
import android.content.Context
44
import android.content.res.Configuration
55
import androidx.annotation.StringRes
6-
import androidx.compose.foundation.lazy.LazyListScope
76
import androidx.compose.material3.MaterialTheme
87
import androidx.compose.material3.Surface
98
import androidx.compose.runtime.Composable
@@ -38,14 +37,6 @@ fun ImageMetadataItem(
3837
MetadataItem(title = title, value = imageLinks)
3938
}
4039

41-
fun LazyListScope.imageMetadataItem(@StringRes title: Int, images: List<Image>) {
42-
ImageStrategy.choosePreferredImage(images)?.let {
43-
item {
44-
ImageMetadataItem(title, images, it)
45-
}
46-
}
47-
}
48-
4940
private fun convertImagesToLinks(
5041
context: Context,
5142
images: List<Image>,

app/src/main/java/org/schabi/newpipe/ui/components/metadata/MetadataItem.kt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import androidx.compose.foundation.layout.Arrangement
66
import androidx.compose.foundation.layout.Column
77
import androidx.compose.foundation.layout.Row
88
import androidx.compose.foundation.layout.fillMaxWidth
9-
import androidx.compose.foundation.lazy.LazyListScope
9+
import androidx.compose.foundation.layout.width
1010
import androidx.compose.material3.MaterialTheme
1111
import androidx.compose.material3.Surface
1212
import androidx.compose.material3.Text
@@ -35,24 +35,13 @@ fun MetadataItem(@StringRes title: Int, value: AnnotatedString) {
3535
verticalAlignment = Alignment.CenterVertically
3636
) {
3737
Text(
38-
modifier = Modifier.weight(0.3f),
38+
modifier = Modifier.width(96.dp),
3939
textAlign = TextAlign.End,
40-
text = stringResource(title).uppercase(),
40+
text = stringResource(title),
4141
fontWeight = FontWeight.Bold
4242
)
4343

44-
Text(
45-
modifier = Modifier.weight(0.7f),
46-
text = value
47-
)
48-
}
49-
}
50-
51-
fun LazyListScope.metadataItem(@StringRes title: Int, value: String) {
52-
if (value.isNotEmpty()) {
53-
item {
54-
MetadataItem(title, value)
55-
}
44+
Text(text = value)
5645
}
5746
}
5847

app/src/main/java/org/schabi/newpipe/ui/components/video/VideoDescriptionSection.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.ui.components.video
22

33
import android.content.res.Configuration
4+
import androidx.annotation.StringRes
45
import androidx.compose.animation.AnimatedVisibility
56
import androidx.compose.animation.expandVertically
67
import androidx.compose.animation.fadeIn
@@ -15,6 +16,7 @@ import androidx.compose.foundation.layout.Row
1516
import androidx.compose.foundation.layout.fillMaxWidth
1617
import androidx.compose.foundation.layout.padding
1718
import androidx.compose.foundation.lazy.LazyColumn
19+
import androidx.compose.foundation.lazy.LazyListScope
1820
import androidx.compose.foundation.lazy.rememberLazyListState
1921
import androidx.compose.foundation.text.selection.SelectionContainer
2022
import androidx.compose.material3.ExperimentalMaterial3Api
@@ -43,19 +45,20 @@ import androidx.compose.ui.tooling.preview.Preview
4345
import androidx.compose.ui.unit.dp
4446
import my.nanihadesuka.compose.LazyColumnScrollbar
4547
import org.schabi.newpipe.R
48+
import org.schabi.newpipe.extractor.Image
4649
import org.schabi.newpipe.extractor.localization.DateWrapper
4750
import org.schabi.newpipe.extractor.stream.Description
4851
import org.schabi.newpipe.extractor.stream.StreamExtractor
4952
import org.schabi.newpipe.extractor.stream.StreamInfo
5053
import org.schabi.newpipe.extractor.stream.StreamType
5154
import org.schabi.newpipe.ui.components.common.DescriptionText
55+
import org.schabi.newpipe.ui.components.metadata.ImageMetadataItem
5256
import org.schabi.newpipe.ui.components.metadata.MetadataItem
5357
import org.schabi.newpipe.ui.components.metadata.TagsSection
54-
import org.schabi.newpipe.ui.components.metadata.imageMetadataItem
55-
import org.schabi.newpipe.ui.components.metadata.metadataItem
5658
import org.schabi.newpipe.ui.theme.AppTheme
5759
import org.schabi.newpipe.util.Localization
5860
import org.schabi.newpipe.util.NO_SERVICE_ID
61+
import org.schabi.newpipe.util.image.ImageStrategy
5962
import java.time.OffsetDateTime
6063

6164
@OptIn(ExperimentalMaterial3Api::class)
@@ -202,6 +205,22 @@ fun VideoDescriptionSection(streamInfo: StreamInfo) {
202205
}
203206
}
204207

208+
private fun LazyListScope.metadataItem(@StringRes title: Int, value: String) {
209+
if (value.isNotEmpty()) {
210+
item {
211+
MetadataItem(title, value)
212+
}
213+
}
214+
}
215+
216+
private fun LazyListScope.imageMetadataItem(@StringRes title: Int, images: List<Image>) {
217+
ImageStrategy.choosePreferredImage(images)?.let {
218+
item {
219+
ImageMetadataItem(title, images, it)
220+
}
221+
}
222+
}
223+
205224
@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO)
206225
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
207226
@Composable

0 commit comments

Comments
 (0)