|
| 1 | +package org.schabi.newpipe.ui.components.video |
| 2 | + |
| 3 | +import android.content.res.Configuration |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.layout.Row |
| 6 | +import androidx.compose.foundation.layout.Spacer |
| 7 | +import androidx.compose.foundation.layout.padding |
| 8 | +import androidx.compose.foundation.layout.size |
| 9 | +import androidx.compose.material3.Icon |
| 10 | +import androidx.compose.material3.MaterialTheme |
| 11 | +import androidx.compose.material3.Surface |
| 12 | +import androidx.compose.material3.Text |
| 13 | +import androidx.compose.runtime.Composable |
| 14 | +import androidx.compose.ui.Alignment |
| 15 | +import androidx.compose.ui.Modifier |
| 16 | +import androidx.compose.ui.platform.LocalContext |
| 17 | +import androidx.compose.ui.res.painterResource |
| 18 | +import androidx.compose.ui.res.stringResource |
| 19 | +import androidx.compose.ui.text.TextStyle |
| 20 | +import androidx.compose.ui.text.font.FontWeight |
| 21 | +import androidx.compose.ui.text.style.LineHeightStyle |
| 22 | +import androidx.compose.ui.tooling.preview.Preview |
| 23 | +import androidx.compose.ui.unit.dp |
| 24 | +import org.schabi.newpipe.R |
| 25 | +import org.schabi.newpipe.extractor.stream.StreamInfo |
| 26 | +import org.schabi.newpipe.extractor.stream.StreamType |
| 27 | +import org.schabi.newpipe.ui.theme.AppTheme |
| 28 | +import org.schabi.newpipe.util.Localization |
| 29 | +import org.schabi.newpipe.util.NO_SERVICE_ID |
| 30 | + |
| 31 | +@Composable |
| 32 | +fun ViewAndThumbs(info: StreamInfo) { |
| 33 | + Column( |
| 34 | + modifier = Modifier.padding(start = 6.dp) |
| 35 | + ) { |
| 36 | + if (info.viewCount >= 0) { |
| 37 | + val viewCount = when (info.streamType) { |
| 38 | + StreamType.AUDIO_LIVE_STREAM -> { |
| 39 | + Localization.listeningCount(LocalContext.current, info.viewCount) |
| 40 | + } |
| 41 | + |
| 42 | + StreamType.LIVE_STREAM -> { |
| 43 | + Localization.localizeWatchingCount(LocalContext.current, info.viewCount) |
| 44 | + } |
| 45 | + |
| 46 | + else -> { |
| 47 | + Localization.localizeViewCount(LocalContext.current, info.viewCount) |
| 48 | + } |
| 49 | + } |
| 50 | + // View count |
| 51 | + Text( |
| 52 | + text = viewCount, |
| 53 | + style = MaterialTheme.typography.labelLarge, |
| 54 | + maxLines = 1, |
| 55 | + modifier = Modifier |
| 56 | + .padding(top = 5.dp, bottom = 4.dp) |
| 57 | + .align(Alignment.CenterHorizontally) |
| 58 | + |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + Row { |
| 63 | + val showLikes = info.likeCount >= 0 |
| 64 | + val showDislikes = info.dislikeCount >= 0 |
| 65 | + val showDisabled = !showLikes && !showDislikes |
| 66 | + |
| 67 | + // Like icon |
| 68 | + if (showLikes || showDisabled) { |
| 69 | + Icon( |
| 70 | + painter = painterResource(R.drawable.ic_thumb_up), |
| 71 | + contentDescription = stringResource(R.string.detail_likes_img_view_description), |
| 72 | + modifier = Modifier |
| 73 | + .align(Alignment.CenterVertically) |
| 74 | + .size(16.dp) |
| 75 | + |
| 76 | + ) |
| 77 | + } |
| 78 | + if (showLikes) { |
| 79 | + Text( |
| 80 | + text = Localization.shortCount(LocalContext.current, info.likeCount), |
| 81 | + style = MaterialTheme.typography.bodyMedium.merge( |
| 82 | + TextStyle( |
| 83 | + lineHeightStyle = LineHeightStyle( |
| 84 | + alignment = LineHeightStyle.Alignment.Proportional, |
| 85 | + trim = LineHeightStyle.Trim.LastLineBottom |
| 86 | + ) |
| 87 | + ) |
| 88 | + ), |
| 89 | + fontWeight = FontWeight.Light, |
| 90 | + maxLines = 1, |
| 91 | + modifier = Modifier.padding(start = 6.dp) |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + if ((showLikes && showDislikes) || showDisabled) { |
| 96 | + Spacer(Modifier.size(8.dp)) |
| 97 | + } |
| 98 | + |
| 99 | + // Dislike icon |
| 100 | + if (showDislikes || showDisabled) { |
| 101 | + Icon( |
| 102 | + painter = painterResource(R.drawable.ic_thumb_down), |
| 103 | + contentDescription = stringResource(R.string.detail_dislikes_img_view_description), |
| 104 | + modifier = Modifier |
| 105 | + .align(Alignment.CenterVertically) |
| 106 | + .size(16.dp) |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + if (showDislikes) { |
| 111 | + Text( |
| 112 | + text = Localization.shortCount(LocalContext.current, info.dislikeCount), |
| 113 | + style = MaterialTheme.typography.bodyMedium.merge( |
| 114 | + TextStyle( |
| 115 | + lineHeightStyle = LineHeightStyle( |
| 116 | + alignment = LineHeightStyle.Alignment.Proportional, |
| 117 | + trim = LineHeightStyle.Trim.LastLineBottom |
| 118 | + ) |
| 119 | + ) |
| 120 | + ), |
| 121 | + fontWeight = FontWeight.Light, |
| 122 | + maxLines = 1, |
| 123 | + modifier = Modifier.padding(start = 6.dp) |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + if (showDisabled) { |
| 128 | + Text( |
| 129 | + text = "Disabled", |
| 130 | + style = MaterialTheme.typography.titleMedium, |
| 131 | + fontWeight = FontWeight.Bold, |
| 132 | + modifier = Modifier.padding(start = 8.dp) |
| 133 | + ) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +@Composable |
| 140 | +fun Preview(info: StreamInfo) { |
| 141 | + AppTheme { |
| 142 | + Surface { |
| 143 | + ViewAndThumbs(info) |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO) |
| 149 | +@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 150 | +@Composable |
| 151 | +fun PreviewNormal() { |
| 152 | + Preview( |
| 153 | + StreamInfo(NO_SERVICE_ID, "", "", StreamType.VIDEO_STREAM, "", "", 0).apply { |
| 154 | + viewCount = 1247 |
| 155 | + likeCount = 1290 |
| 156 | + dislikeCount = 327 |
| 157 | + } |
| 158 | + ) |
| 159 | +} |
| 160 | + |
| 161 | +@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO) |
| 162 | +@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 163 | +@Composable |
| 164 | +fun PreviewLikes() { |
| 165 | + Preview( |
| 166 | + StreamInfo(NO_SERVICE_ID, "", "", StreamType.VIDEO_STREAM, "", "", 0).apply { |
| 167 | + viewCount = 1247 |
| 168 | + likeCount = 1290 |
| 169 | + dislikeCount = -1 |
| 170 | + } |
| 171 | + ) |
| 172 | +} |
| 173 | + |
| 174 | +@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO) |
| 175 | +@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 176 | +@Composable |
| 177 | +fun PreviewDislikes() { |
| 178 | + Preview( |
| 179 | + StreamInfo(NO_SERVICE_ID, "", "", StreamType.VIDEO_STREAM, "", "", 0).apply { |
| 180 | + viewCount = 1247 |
| 181 | + likeCount = -1 |
| 182 | + dislikeCount = 327 |
| 183 | + } |
| 184 | + ) |
| 185 | +} |
| 186 | + |
| 187 | +@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO) |
| 188 | +@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 189 | +@Composable |
| 190 | +fun PreviewDisabled() { |
| 191 | + Preview( |
| 192 | + StreamInfo(NO_SERVICE_ID, "", "", StreamType.VIDEO_STREAM, "", "", 0).apply { |
| 193 | + viewCount = -1 |
| 194 | + likeCount = -1 |
| 195 | + dislikeCount = -1 |
| 196 | + } |
| 197 | + ) |
| 198 | +} |
0 commit comments