Skip to content

Commit

Permalink
Fix crash when minimizing the channel about tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Sep 1, 2024
1 parent 507b3b4 commit 45aa445
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 300 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ 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.ktx.parcelable
import org.schabi.newpipe.ui.components.channel.AboutChannelSection
import org.schabi.newpipe.ui.components.channel.ParcelableChannelInfo
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.util.KEY_INFO

Expand All @@ -22,15 +23,15 @@ class AboutChannelFragment : Fragment() {
) = content {
AppTheme {
Surface(color = MaterialTheme.colorScheme.background) {
AboutChannelSection(requireArguments().serializable(KEY_INFO)!!)
AboutChannelSection(requireArguments().parcelable(KEY_INFO)!!)
}
}
}

companion object {
@JvmStatic
fun getInstance(channelInfo: ChannelInfo) = AboutChannelFragment().apply {
arguments = bundleOf(KEY_INFO to channelInfo)
arguments = bundleOf(KEY_INFO to ParcelableChannelInfo(channelInfo))
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/org/schabi/newpipe/ktx/Bundle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import android.os.Parcelable
import androidx.core.os.BundleCompat
import java.io.Serializable

inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
return BundleCompat.getParcelable(this, key, T::class.java)
}

inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String?): ArrayList<T>? {
return BundleCompat.getParcelableArrayList(this, key, T::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ 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.extractor.Image
import org.schabi.newpipe.extractor.Image.ResolutionLevel
import org.schabi.newpipe.ui.components.metadata.ImageMetadataItem
import org.schabi.newpipe.ui.components.metadata.MetadataItem
import org.schabi.newpipe.ui.components.metadata.TagsSection
Expand All @@ -22,35 +23,35 @@ import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.NO_SERVICE_ID

@Composable
fun AboutChannelSection(channelInfo: ChannelInfo) {
fun AboutChannelSection(channelInfo: ParcelableChannelInfo) {
// 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()) {
val (serviceId, description, count, avatars, banners, tags) = channelInfo

if (description.isNotEmpty()) {
Text(text = description)
}

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

if (channelInfo.avatars.isNotEmpty()) {
ImageMetadataItem(R.string.metadata_avatars, channelInfo.avatars)
if (avatars.isNotEmpty()) {
ImageMetadataItem(R.string.metadata_avatars, avatars)
}

if (channelInfo.banners.isNotEmpty()) {
ImageMetadataItem(R.string.metadata_banners, channelInfo.banners)
if (banners.isNotEmpty()) {
ImageMetadataItem(R.string.metadata_banners, banners)
}

if (channelInfo.tags.isNotEmpty()) {
TagsSection(channelInfo.serviceId, channelInfo.tags)
if (tags.isNotEmpty()) {
TagsSection(serviceId, tags)
}
}
}
Expand All @@ -59,9 +60,18 @@ fun AboutChannelSection(channelInfo: ChannelInfo) {
@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
val images = listOf(
Image("https://example.com/image_low.png", 16, 16, ResolutionLevel.LOW),
Image("https://example.com/image_mid.png", 32, 32, ResolutionLevel.MEDIUM)
)
val info = ParcelableChannelInfo(
serviceId = NO_SERVICE_ID,
description = "This is an example description",
subscriberCount = 10,
avatars = images,
banners = images,
tags = listOf("Tag 1", "Tag 2")
)

AppTheme {
Surface(color = MaterialTheme.colorScheme.background) {
Expand Down
Loading

0 comments on commit 45aa445

Please sign in to comment.