Skip to content

Commit

Permalink
Migrate about channel fragment to Jetpack Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Sep 1, 2024
1 parent de6285b commit 2653787
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.schabi.newpipe.fragments.list.channel

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.compose.content
import org.schabi.newpipe.extractor.channel.ChannelInfo
import org.schabi.newpipe.ktx.serializable
import org.schabi.newpipe.ui.components.channel.AboutChannelSection
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.util.KEY_INFO

class AboutChannelFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = content {
AppTheme {
Surface(color = MaterialTheme.colorScheme.background) {
AboutChannelSection(requireArguments().serializable(KEY_INFO)!!)
}
}
}

companion object {
@JvmStatic
fun getInstance(channelInfo: ChannelInfo) = AboutChannelFragment().apply {
arguments = bundleOf(KEY_INFO to channelInfo)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ private void updateTabs() {
if (ChannelTabHelper.showChannelTab(
context, preferences, R.string.show_channel_tabs_about)) {
tabAdapter.addFragment(
new ChannelAboutFragment(currentInfo),
AboutChannelFragment.getInstance(currentInfo),
context.getString(R.string.channel_tab_about));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.schabi.newpipe.ui.components.channel

import android.content.res.Configuration
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.schabi.newpipe.R
import org.schabi.newpipe.extractor.channel.ChannelInfo
import org.schabi.newpipe.ui.components.metadata.ImageMetadataItem
import org.schabi.newpipe.ui.components.metadata.MetadataItem
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.NO_SERVICE_ID
import org.schabi.newpipe.util.image.ImageStrategy

@Composable
fun AboutChannelSection(channelInfo: ChannelInfo) {
// This tab currently holds little information, so a lazy column isn't needed here.
Column(
modifier = Modifier.padding(12.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
val description = channelInfo.description
if (!description.isNullOrEmpty()) {
Text(text = description)
}

val count = channelInfo.subscriberCount
if (count != -1L) {
MetadataItem(
title = R.string.metadata_subscribers,
value = Localization.shortCount(LocalContext.current, count)
)
}

ImageStrategy.choosePreferredImage(channelInfo.avatars)?.let {
ImageMetadataItem(R.string.metadata_avatars, channelInfo.avatars, it)
}

ImageStrategy.choosePreferredImage(channelInfo.banners)?.let {
ImageMetadataItem(R.string.metadata_banners, channelInfo.banners, it)
}

if (channelInfo.tags.isNotEmpty()) {
TagsSection(channelInfo.serviceId, channelInfo.tags)
}
}
}

@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun AboutChannelSectionPreview() {
val info = ChannelInfo(NO_SERVICE_ID, "", "", "", "")
info.description = "This is an example description"
info.subscriberCount = 10

AppTheme {
Surface(color = MaterialTheme.colorScheme.background) {
AboutChannelSection(info)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.schabi.newpipe.ui.components.metadata
import android.content.Context
import android.content.res.Configuration
import androidx.annotation.StringRes
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -38,14 +37,6 @@ fun ImageMetadataItem(
MetadataItem(title = title, value = imageLinks)
}

fun LazyListScope.imageMetadataItem(@StringRes title: Int, images: List<Image>) {
ImageStrategy.choosePreferredImage(images)?.let {
item {
ImageMetadataItem(title, images, it)
}
}
}

private fun convertImagesToLinks(
context: Context,
images: List<Image>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
Expand Down Expand Up @@ -35,24 +35,13 @@ fun MetadataItem(@StringRes title: Int, value: AnnotatedString) {
verticalAlignment = Alignment.CenterVertically
) {
Text(
modifier = Modifier.weight(0.3f),
modifier = Modifier.width(96.dp),
textAlign = TextAlign.End,
text = stringResource(title).uppercase(),
text = stringResource(title),
fontWeight = FontWeight.Bold
)

Text(
modifier = Modifier.weight(0.7f),
text = value
)
}
}

fun LazyListScope.metadataItem(@StringRes title: Int, value: String) {
if (value.isNotEmpty()) {
item {
MetadataItem(title, value)
}
Text(text = value)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.schabi.newpipe.ui.components.video

import android.content.res.Configuration
import androidx.annotation.StringRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
Expand All @@ -15,6 +16,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.ExperimentalMaterial3Api
Expand Down Expand Up @@ -43,19 +45,20 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import my.nanihadesuka.compose.LazyColumnScrollbar
import org.schabi.newpipe.R
import org.schabi.newpipe.extractor.Image
import org.schabi.newpipe.extractor.localization.DateWrapper
import org.schabi.newpipe.extractor.stream.Description
import org.schabi.newpipe.extractor.stream.StreamExtractor
import org.schabi.newpipe.extractor.stream.StreamInfo
import org.schabi.newpipe.extractor.stream.StreamType
import org.schabi.newpipe.ui.components.common.DescriptionText
import org.schabi.newpipe.ui.components.metadata.ImageMetadataItem
import org.schabi.newpipe.ui.components.metadata.MetadataItem
import org.schabi.newpipe.ui.components.metadata.TagsSection
import org.schabi.newpipe.ui.components.metadata.imageMetadataItem
import org.schabi.newpipe.ui.components.metadata.metadataItem
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.NO_SERVICE_ID
import org.schabi.newpipe.util.image.ImageStrategy
import java.time.OffsetDateTime

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -202,6 +205,22 @@ fun VideoDescriptionSection(streamInfo: StreamInfo) {
}
}

private fun LazyListScope.metadataItem(@StringRes title: Int, value: String) {
if (value.isNotEmpty()) {
item {
MetadataItem(title, value)
}
}
}

private fun LazyListScope.imageMetadataItem(@StringRes title: Int, images: List<Image>) {
ImageStrategy.choosePreferredImage(images)?.let {
item {
ImageMetadataItem(title, images, it)
}
}
}

@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
Expand Down

0 comments on commit 2653787

Please sign in to comment.